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.

using System;
using Imsl.Stat;
using 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 = new String[]{"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);

		Console.Out.WriteLine("Pearson chi-squared statistic = " + 
			ct.ChiSquared.ToString("0.0000"));
		Console.Out.WriteLine("p-value for Pearson chi-squared = " + 
			ct.P.ToString("0.0000"));
		Console.Out.WriteLine("degrees of freedom = " + 
			ct.DegreesOfFreedom);
		Console.Out.WriteLine("G-squared statistic = " + 
			ct.GSquared.ToString("0.0000"));
		Console.Out.WriteLine("p-value for G-squared = " + 
			ct.GSquaredP.ToString("0.0000"));
		Console.Out.WriteLine("degrees of freedom = " + 
			ct.DegreesOfFreedom);
		

		PrintMatrix pm = new PrintMatrix("\n* * * Table Values * * *");
		PrintMatrixFormat pmf = new PrintMatrixFormat();
		pmf.NumberFormat = "0.00";
		pm.Print(pmf, table);
		
		pm.SetTitle("* * * Expected Values * * *");
		pm.Print(pmf, ct.GetExpectedValues());
		
		pmf.NumberFormat = "0.0000";
		pm.SetTitle("* * * Contributions to Chi-squared* * *");
		pm.Print(pmf, ct.GetContributions());

		Console.Out.WriteLine("* * * Chi-square Statistics * * *");
		Console.Out.WriteLine
			("Exact mean = " + ct.ExactMean.ToString("0.0000"));
		Console.Out.WriteLine("Exact standard deviation = " + 
			ct.ExactStdev.ToString("0.0000"));
		Console.Out.WriteLine("Phi = " + ct.Phi.ToString("0.0000"));
		Console.Out.WriteLine
			("P = " + ct.ContingencyCoef.ToString("0.0000"));
		Console.Out.WriteLine
			("Cramer's V = " + ct.CramersV.ToString("0.0000"));
		
		Console.Out.WriteLine("\n             stat.      std. err.   " 
			+ "std. err.(Ho) t-value(Ho)  p-value");
		double[,] stat = ct.GetStatistics();
		for (int i = 0; i < stat.GetLength(0); i++)
		{
			System.Text.StringBuilder sb = 
				new System.Text.StringBuilder(rlabels[i]);
			
			int len = sb.Length;
			for (int j = 0; j < (13 - len); j++)
				sb.Append(' ');
			sb.Append(stat[i,0].ToString("0.0000"));
			
			len = sb.Length;
			for (int j = 0; j < (24 - len); j++)
				sb.Append(' ');
			sb.Append(stat[i,1].ToString("0.0000"));
			
			len = sb.Length;
			for (int j = 0; j < (36 - len); j++)
				sb.Append(' ');
			sb.Append(stat[i,2].ToString("0.0000"));
			
			len = sb.Length;
			for (int j = 0; j < (50 - len); j++)
				sb.Append(' ');
			sb.Append(stat[i,3].ToString("0.0000"));
			
			len = sb.Length;
			for (int j = 0; j < (63 - len); j++)
				sb.Append(' ');
			sb.Append(stat[i,4].ToString("0.0000"));
			
			Console.Out.WriteLine(sb.ToString());
		}
	}
}

Output

Pearson chi-squared statistic = 3304.3684
p-value for Pearson chi-squared = 0.0000
degrees of freedom = 9
G-squared statistic = 2781.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  1053.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  1052.00  791.00  919.00  480.00  3242.00  

        * * * Contributions to Chi-squared* * *
       0         1         2          3          4      
0  672.3626   81.7416   152.6959  93.7612    1000.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   1023.0957  
4  1002.6776  650.7317  619.8819  1031.0772  3304.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      NaN           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      NaN           NaN          NaN
GK tau cols. 0.3430     0.0122      NaN           NaN          NaN
U - sym.     0.3171     0.0110      NaN           NaN          NaN
U - rows     0.3178     0.0110      NaN           NaN          NaN
U - cols.    0.3164     0.0110      NaN           NaN          NaN
Lambda-sym.  0.5373     0.0124      NaN           NaN          NaN
Lambda-row   0.5374     0.0126      NaN           NaN          NaN
Lambda-col.  0.5372     0.0126      NaN           NaN          NaN
l-star-rows  0.5506     0.0136      NaN           NaN          NaN
l-star-col.  0.5636     0.0127      NaN           NaN          NaN
Lin. trend   NaN        NaN         NaN           NaN          NaN
Kruskal row  1561.4859  3.0000      NaN           NaN          0.0000
Kruskal col. 1563.0303  3.0000      NaN           NaN          0.0000
Kappa        0.5744     0.0111      0.0106        54.3583      0.0000
McNemar      4.7625     6.0000      NaN           NaN          0.5746
McNemar df=1 0.9487     1.0000      NaN           0.3459       0.3301

Link to C# source.