Example 2: Contingency Table

The following example, which illustrates the use of Kappa and McNemar tests, uses the same distance vision data as in Example 1.


import java.text.*;
import com.imsl.stat.*;
import com.imsl.math.*;

public class ContingencyTableEx2 {

    public static void main(String args[]) {
        double[][] table = {
            {821.0, 112.0, 85.0, 35.0},
            {116.0, 494.0, 145.0, 27.0},
            {72.0, 151.0, 583.0, 87.0},
            {43.0, 34.0, 106.0, 331.0}
        };
        String[] rlabels = {
            "Gamma", "Tau B", "Tau C", "D-Row", "D-Column", "Correlation",
            "Spearman", "GK tau rows", "GK tau cols.", "U - sym.",
            "U - rows", "U - cols.", "Lambda-sym.", "Lambda-row", "Lambda-col.",
            "l-star-rows", "l-star-col.", "Lin. trend", "Kruskal row",
            "Kruskal col.", "Kappa", "McNemar", "McNemar df=1"
        };
        ContingencyTable ct = new ContingencyTable(table);
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumFractionDigits(4);

        System.out.println("Pearson chi-squared statistic = "
                + nf.format(ct.getChiSquared()));
        System.out.println("p-value for Pearson chi-squared = "
                + nf.format(ct.getP()));
        System.out.println("degrees of freedom = " + ct.getDegreesOfFreedom());
        System.out.println("G-squared statistic = "
                + nf.format(ct.getGSquared()));
        System.out.println("p-value for G-squared = "
                + nf.format(ct.getGSquaredP()));
        System.out.println("degrees of freedom = " + ct.getDegreesOfFreedom());

        nf.setMaximumFractionDigits(2);
        nf.setMinimumFractionDigits(2);
        PrintMatrix pm = new PrintMatrix("\n* * * Table Values * * *");
        PrintMatrixFormat pmf = new PrintMatrixFormat();
        pmf.setNumberFormat(nf);
        pm.print(pmf, table);

        pm.setTitle("* * * Expected Values * * *");
        pm.print(pmf, ct.getExpectedValues());

        nf.setMinimumFractionDigits(4);
        pmf.setNumberFormat(nf);
        pm.setTitle("* * * Contributions to Chi-squared* * *");
        pm.print(pmf, ct.getContributions());

        nf.setMinimumFractionDigits(4);
        System.out.println("* * * Chi-square Statistics * * *");
        System.out.println("Exact mean = " + nf.format(ct.getExactMean()));
        System.out.println("Exact standard deviation = "
                + nf.format(ct.getExactStdev()));
        System.out.println("Phi = " + nf.format(ct.getPhi()));
        System.out.println("P = " + nf.format(ct.getContingencyCoef()));
        System.out.println("Cramer's V = " + nf.format(ct.getCramersV()));

        System.out.println("\n             stat.      std. err.   "
                + "std. err.(Ho) t-value(Ho)  p-value");
        double[][] stat = ct.getStatistics();
        for (int i = 0; i < stat.length; i++) {
            StringBuffer sb = new StringBuffer(rlabels[i]);

            int len = sb.length();
            for (int j = 0; j < (13 - len); j++) {
                sb.append(' ');
            }
            sb.append(nf.format(stat[i][0]));

            len = sb.length();
            for (int j = 0; j < (24 - len); j++) {
                sb.append(' ');
            }
            sb.append(nf.format(stat[i][1]));

            len = sb.length();
            for (int j = 0; j < (36 - len); j++) {
                sb.append(' ');
            }
            sb.append(nf.format(stat[i][2]));

            len = sb.length();
            for (int j = 0; j < (50 - len); j++) {
                sb.append(' ');
            }
            sb.append(nf.format(stat[i][3]));

            len = sb.length();
            for (int j = 0; j < (63 - len); j++) {
                sb.append(' ');
            }
            sb.append(nf.format(stat[i][4]));

            System.out.println(sb.toString());
        }
    }
}

Output

Pearson chi-squared statistic = 3,304.3684
p-value for Pearson chi-squared = 0.0000
degrees of freedom = 9
G-squared statistic = 2,781.0190
p-value for G-squared = 0.0000
degrees of freedom = 9
     
* * * Table Values * * *
     0       1       2       3     
0  821.00  112.00   85.00   35.00  
1  116.00  494.00  145.00   27.00  
2   72.00  151.00  583.00   87.00  
3   43.00   34.00  106.00  331.00  

          * * * Expected Values * * *
      0        1       2       3        4      
0    341.69  256.92  298.49  155.90  1,053.00  
1    253.75  190.80  221.67  115.78    782.00  
2    289.77  217.88  253.14  132.21    893.00  
3    166.79  125.41  145.70   76.10    514.00  
4  1,052.00  791.00  919.00  480.00  3,242.00  

          * * * Contributions to Chi-squared* * *
       0          1         2          3           4       
0    672.3626   81.7416  152.6959     93.7612  1,000.5613  
1     74.7802  481.8351   26.5189     68.0768    651.2109  
2    163.6605   20.5287  429.8489     15.4625    629.5006  
3     91.8743   66.6263   10.8183    853.7768  1,023.0957  
4  1,002.6776  650.7317  619.8819  1,031.0772  3,304.3684  

* * * Chi-square Statistics * * *
Exact mean = 9.0028
Exact standard deviation = 4.2402
Phi = 1.0096
P = 0.7105
Cramer's V = 0.5829

             stat.      std. err.   std. err.(Ho) t-value(Ho)  p-value
Gamma        0.7757     0.0123      0.0149        52.1897      0.0000
Tau B        0.6429     0.0122      0.0123        52.1897      0.0000
Tau C        0.6293     0.0121      ?             52.1897      0.0000
D-Row        0.6418     0.0122      0.0123        52.1897      0.0000
D-Column     0.6439     0.0122      0.0123        52.1897      0.0000
Correlation  0.6926     0.0128      0.0172        40.2669      0.0000
Spearman     0.6939     0.0127      0.0127        54.6614      0.0000
GK tau rows  0.3420     0.0123      ?             ?            ?
GK tau cols. 0.3430     0.0122      ?             ?            ?
U - sym.     0.3171     0.0110      ?             ?            ?
U - rows     0.3178     0.0110      ?             ?            ?
U - cols.    0.3164     0.0110      ?             ?            ?
Lambda-sym.  0.5373     0.0124      ?             ?            ?
Lambda-row   0.5374     0.0126      ?             ?            ?
Lambda-col.  0.5372     0.0126      ?             ?            ?
l-star-rows  0.5506     0.0136      ?             ?            ?
l-star-col.  0.5636     0.0127      ?             ?            ?
Lin. trend   ?          ?           ?             ?            ?
Kruskal row  1,561.4859 3.0000      ?             ?            0.0000
Kruskal col. 1,563.0303 3.0000      ?             ?            0.0000
Kappa        0.5744     0.0111      0.0106        54.3583      0.0000
McNemar      4.7625     6.0000      ?             ?            0.5746
McNemar df=1 0.9487     1.0000      ?             0.3459       0.3301
Link to Java source.