package com.imsl.test.example.stat; import java.text.*; import com.imsl.stat.*; import com.imsl.math.*; /** *

* Calculates a number of statistics associated with a contingency table.

*

* The following example uses the same distance vision data as in * {@link ContingencyTableEx1}. A number of different test statistics are shown. * *

* * @see Code * @see Output */ 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()); } } }