IMSL C# Numerical Library

ContingencyTable Class

Performs a chi-squared analysis of a two-way contingency table.

For a list of all members of this type, see ContingencyTable Members.

System.Object
   Imsl.Stat.ContingencyTable

public class ContingencyTable

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

Remarks

Class ContingencyTable computes statistics associated with an r \times c contingency table. The function computes the chi-squared test of independence, expected values, contributions to chi-squared, row and column marginal totals, some measures of association, correlation, prediction, uncertainty, the McNemar test for symmetry, a test for linear trend, the odds and the log odds ratio, and the kappa statistic (if the appropriate optional arguments are selected).

Notation

Let x_{ij} denote the observed cell frequency in the ij cell of the table and n denote the total count in the table. Let p_{ij} = p_{i\bullet} p_{j\bullet} denote the predicted cell probabilities under the null hypothesis of independence, where p_{i\bullet} and p_{j\bullet} are the row and column marginal relative frequencies. Next, compute the expected cell counts as e_{ij} = np_{ij}.

Also required in the following are a_{uv} and b_{uv} for u, v = 1, \ldots, n. Let (r_s, c_s) denote the row and column response of observation s. Then, a_{uv} = 1, 0, or -1, depending on whether r_u \lt r_v, r_u = r_v, or r_u \gt r_v, respectively. The b_{uv} are similarly defined in terms of the c_s variables.

Chi-squared Statistic

For each cell in the table, the contribution to \chi ^2 is given as (x_{ij} - e_{ij})^2/e_{ij}. The Pearson chi-squared statistic (denoted \chi ^2) is computed as the sum of the cell contributions to chi-squared. It has (r - 1) (c - 1) degrees of freedom and tests the null hypothesis of independence, i.e., H_0:p_{ij} = p_{i\bullet}p_{j\bullet}. The null hypothesis is rejected if the computed value of \chi ^2 is too large.

The maximum likelihood equivalent of \chi ^2, G^2 is computed as follows:

G^2 = - 2\sum\limits_{i,j} {x_{ij} } \ln \left( 
            {x_{ij} /np_{ij} } \right)

G^2 is asymptotically equivalent to \chi ^2 and tests the same hypothesis with the same degrees of freedom.

Measures Related to Chi-squared (Phi, Contingency Coefficient, and Cramer's V)

There are three measures related to chi-squared that do not depend on sample size:

{\rm{phi,}} \,\, \phi \, {\rm{=}} \, \sqrt {\chi ^2 
            {\rm{/}}n}

{\rm{contingency \,\,coefficient, \,\,}} 
            P{\rm{  =  }}\sqrt {\chi ^2 /\left( {n + \chi ^2 } \right)}

{\rm{Cramer's }}\,V{\rm{, }}\,\,V = \sqrt 
            {\chi ^2 /\left( {n\min \left( {r,c} \right)} \right)}

Since these statistics do not depend on sample size and are large when the hypothesis of independence is rejected, they can be thought of as measures of association and can be compared across tables with different sized samples. While both P and V have a range between 0.0 and 1.0, the upper bound of P is actually somewhat less than 1.0 for any given table (see Kendall and Stuart 1979, p. 587). The significance of all three statistics is the same as that of the \chi ^2 statistic, which is contained in the ChiSquared property.

The distribution of the \chi ^2 statistic in finite samples approximates a chi-squared distribution. To compute the exact mean and standard deviation of the \chi ^2 statistic, Haldane (1939) uses the multinomial distribution with fixed table marginals. The exact mean and standard deviation generally differ little from the mean and standard deviation of the associated chi-squared distribution.

Standard Errors and p-values for Some Measures of Association

In Columns 1 through 4 of statistics, estimated standard errors and asymptotic p-values are reported. Estimates of the standard errors are computed in two ways. The first estimate, in Column 1 of the return matrix from the Statistics property, is asymptotically valid for any value of the statistic. The second estimate, in Column 2 of the array, is only correct under the null hypothesis of no association. The z-scores in Column 3 of statistics are computed using this second estimate of the standard errors. The p-values in Column 4 are computed from this z-score. See Brown and Benedetti (1977) for a discussion and formulas for the standard errors in Column 2.

Measures of Association for Ranked Rows and Columns

The measures of association, \phi, P, and V, do not require any ordering of the row and column categories. Class ContingencyTable also computes several measures of association for tables in which the rows and column categories correspond to ranked observations. Two of these tests, the product-moment correlation and the Spearman correlation, are correlation coefficients computed using assigned scores for the row and column categories. The cell indices are used for the product-moment correlation, while the average of the tied ranks of the row and column marginals is used for the Spearman rank correlation. Other scores are possible.

Gamma, Kendall's \tau_b, Stuart's \tau_c, and Somers' D are measures of association that are computed like a correlation coefficient in the numerator. In all these measures, the numerator is computed as the "covariance" between the a_{uv} variables and b_{uv} variables defined above, i.e., as follows:

\sum\limits_u {\sum\limits_v {a_{uv} } } 
            b_{uv}

Recall that a_{uv} and b_{uv} can take values -1, 0, or 1. Since the product a_{uv}b_{uv} = 1 only if a_{uv} and b_{uv} are both 1 or are both -1, it is easy to show that this "covariance" is twice the total number of agreements minus the number of disagreements, where a disagreement occurs when a_{uv}b_{uv} = -1.

Kendall's \tau_b is computed as the correlation between the a_{uv} variables and the b_{uv} variables (see Kendall and Stuart 1979, p. 593). In a rectangular table (r \neq c), Kendall's \tau_b cannot be 1.0 (if all marginal totals are positive). For this reason, Stuart suggested a modification to the denominator of \tau in which the denominator becomes the largest possible value of the "covariance." This maximizing value is approximately n^2m/(m - 1), where m = min (r, c). Stuart's \tau_c uses this approximate value in its denominator. For large n, \tau_c \approx m\tau_b/(m - 1).

Gamma can be motivated in a slightly different manner. Because the "covariance" of the a_{uv} variables and the b_{uv} variables can be thought of as twice the number of agreements minus the disagreements, 2(A - D), where A is the number of agreements and D is the number of disagreements, Gamma is motivated as the probability of agreement minus the probability of disagreement, given that either agreement or disagreement occurred. This is shown as \gamma = (A - D)/(A + D).

Two definitions of Somers' D are possible, one for rows and a second for columns. Somers' D for rows can be thought of as the regression coefficient for predicting a_{uv} from b_{uv}. Moreover, Somer's D for rows is the probability of agreement minus the probability of disagreement, given that the column variable, b_{uv}, is not 0. Somers' D for columns is defined in a similar manner.

A discussion of all of the measures of association in this section can be found in Kendall and Stuart (1979, p. 592).

Measures of Prediction and Uncertainty

Optimal Prediction Coefficients: The measures in this section do not require any ordering of the row or column variables. They are based entirely upon probabilities. Most are discussed in Bishop et al. (1975, p. 385).

Consider predicting (or classifying) the column for a given row in the table. Under the null hypothesis of independence, choose the column with the highest column marginal probability for all rows. In this case, the probability of misclassification for any row is 1 minus this marginal probability. If independence is not assumed within each row, choose the column with the highest row conditional probability. The probability of misclassification for the row becomes 1 minus this conditional probability.

Define the optimal prediction coefficient \lambda _{c|r} for predicting columns from rows as the proportion of the probability of misclassification that is eliminated because the random variables are not independent. It is estimated by

\lambda _{c\,|\,r}  = \frac{{\left( {1 - p_{ 
            \bullet m} } \right) - (1 - \sum\limits_i {p_{im} } )}}{{1 - p_{ 
            \bullet m} }}

where m is the index of the maximum estimated probability in the row (p_{im}) or row margin (p_{\bullet m}). A similar coefficient is defined for predicting the rows from the columns. The symmetric version of the optimal prediction \lambda is obtained by summing the numerators and denominators of \lambda _{r|c} and \lambda _{c|r} then dividing. Standard errors for these coefficients are given in Bishop et al. (1975, p. 388).

A problem with the optimal prediction coefficients \lambda is that they vary with the marginal probabilities. One way to correct this is to use row conditional probabilities. The optimal prediction \lambda* coefficients are defined as the corresponding \lambda coefficients in which first the row (or column) marginals are adjusted to the same number of observations. This yields

\lambda _{c\,|\,r}^ * = \frac{{\sum\limits_i 
            {\max _j p_{j\,|\,i}  - \max _j (\sum\limits_i {p_{j\,|\,i} } )} }}{{R - 
            \max _j (\sum\limits_i {p_{j\,|\,i} )} }}

where i indexes the rows, j indexes the columns, and p_{j|i} is the (estimated) probability of column j given row i.

\lambda _{r\,|\,c}^ *

is similarly defined.

Goodman and Kruskal \tau: A second kind of prediction measure attempts to explain the proportion of the explained variation of the row (column) measure given the column (row) measure. Define the total variation in the rows as follows:

n/2 - (\sum\limits_i {x_{i \bullet }^2 } 
            )/\left( {2n} \right)

Note that this is 1/(2n) times the sums of squares of the a_{uv} variables.

With this definition of variation, the Goodman and Kruskal \tau coefficient for rows is computed as the reduction of the total variation for rows accounted for by the columns, divided by the total variation for the rows. To compute the reduction in the total variation of the rows accounted for by the columns, note that the total variation for the rows within column j is defined as follows:

q_j  = x_{ \bullet j} /2 - (\sum\limits_i 
            {x_{ij}^2 } )/\left( {2x_{i \bullet } } \right)

The total variation for rows within columns is the sum of the q_j variables. Consistent with the usual methods in the analysis of variance, the reduction in the total variation is given as the difference between the total variation for rows and the total variation for rows within the columns.

Goodman and Kruskal's \tau for columns is similarly defined. See Bishop et al. (1975, p. 391) for the standard errors.

Uncertainty Coefficients: The uncertainty coefficient for rows is the increase in the log-likelihood that is achieved by the most general model over the independence model, divided by the marginal log-likelihood for the rows. This is given by the following equation:

U_{r|c}  = \frac{{\sum\limits_{i,j} {x_{ij} 
            \log \left( {x_{i \bullet } x_{ \bullet j} /nx_{ij} } \right)} 
            }}{{\sum\limits_i {x_{i \bullet } \log \left( {x_{i \bullet } /n} \right)} 
            }}

The uncertainty coefficient for columns is similarly defined. The symmetric uncertainty coefficient contains the same numerator as U_{r|c} and U_{c|r}but averages the denominators of these two statistics. Standard errors for U are given in Brown (1983).

Kruskal-Wallis: The Kruskal-Wallis statistic for rows is a one-way analysis-of-variance-type test that assumes the column variable is monotonically ordered. It tests the null hypothesis that no row populations are identical, using average ranks for the column variable. The Kruskal-Wallis statistic for columns is similarly defined. Conover (1980) discusses the Kruskal-Wallis test.

Test for Linear Trend: When there are two rows, it is possible to test for a linear trend in the row probabilities if it is assumed that the column variable is monotonically ordered. In this test, the probabilities for row 1 are predicted by the column index using weighted simple linear regression. This slope is given by

\hat \beta  = \frac{{\sum\limits_j {x_{ 
            \bullet j} \left( {x_{1j} /x_{ \bullet j}  - x_{1 \bullet } /n} 
            \right)\left( {j - \bar j} \right)} }}{{\sum\limits_j {x_{ \bullet j} 
            \left( {j - \bar j} \right)^2 } }}

where

\bar j = \sum\limits_j {x_{ \bullet j} } 
            j/n

is the average column index. An asymptotic test that the slope is 0 may then be obtained (in large samples) as the usual regression test of zero slope.

In two-column data, a similar test for a linear trend in the column probabilities is computed. This test assumes that the rows are monotonically ordered.

Kappa: Kappa is a measure of agreement computed on square tables only. In the kappa statistic, the rows and columns correspond to the responses of two judges. The judges agree along the diagonal and disagree off the diagonal. Let

p_0  = \sum\limits_i {x_{ii} } /n

denote the probability that the two judges agree, and let

p_c  = \sum\limits_i {e_{ii} } /n

denote the expected probability of agreement under the independence model. Kappa is then given by (p_0 - p_c)/(1 - p_c).

McNemar Tests: The McNemar test is a test of symmetry in a square contingency table. In other words, it is a test of the null hypothesis H_0:\theta_{ij} = \theta_{ji}. The multiple degrees-of-freedom version of the McNemar test with r (r - 1)/2 degrees of freedom is computed as follows:

\sum\limits_{i \lt j} {\frac{{\left( {x_{ij} 
            - x_{ji} } \right)^2 }}{{\left( {x_{ij}  + x_{ji} } \right)}}}

The single degree-of-freedom test assumes that the differences, x_{ij} - x_{ji}, are all in one direction. The single degree-of-freedom test will be more powerful than the multiple degrees-of-freedom test when this is the case. The test statistic is given as follows:

\frac{{\left( {\sum\limits_{i \lt j} {\left( 
            {x_{ij}  - x_{ji} } \right)} } \right)^2 }} {{\sum\limits_{i \lt j} {\left( 
            {x_{ij}  + x_{ji} } \right)} }}

The exact probability can be computed by the binomial distribution.

Requirements

Namespace: Imsl.Stat

Assembly: ImslCS (in ImslCS.dll)

See Also

ContingencyTable Members | Imsl.Stat Namespace | Example 1 | Example 2