Contingency table
A contingency table is a rectangular representation of the joint frequency distribution of two or more categorical variables. Each cell contains the number or proportion of observations assigned to a particular combination of categories. The arrangement exposes patterns of statistical association while preserving the marginal distribution of each variable.
The simplest form is a two-way table. One variable determines the rows, while another determines the columns. Tables involving additional variables are represented as multidimensional arrays or as collections of two-way tables conditioned on further classifications. Contingency tables are also called cross-tabulations, although the latter term commonly refers to the broader practice of producing and interpreting them.
Mathematical structure
For categorical variables (X) and (Y), having (r) and (c) categories respectively, the observed cell count is denoted by
[ n_{ij}
#{k : X_k=i,;Y_k=j}, ]
where (i) identifies a row category and (j) identifies a column category. The total number of observations is
[ n=\sum_{i=1}^{r}\sum_{j=1}^{c}n_{ij}. ]
The row marginal count is (n_{i+}=\sum_j n_{ij}), whereas the column marginal count is (n_{+j}=\sum_i n_{ij}). Dividing these quantities by (n) produces the empirical joint and marginal probabilities.
A conventional two-by-two table has the form
| (Y=1) | (Y=0) | Total | |
|---|---|---|---|
| (X=1) | (a) | (b) | (a+b) |
| (X=0) | (c) | (d) | (c+d) |
| Total | (a+c) | (b+d) | (n) |
The four internal counts determine several measures of association. Their interpretation depends on the sampling design and on whether the categories represent outcomes, exposures, treatments, or classifications without directional meaning.
Independence and expected frequencies
The variables (X) and (Y) are statistically independent when their joint probabilities satisfy
[ P(X=i,Y=j)=P(X=i)P(Y=j) ]
for every cell. Under the corresponding null model, the expected count in cell ((i,j)) is
[ e_{ij}=\frac{n_{i+}n_{+j}}{n}. ]
The Pearson chi-squared statistic compares observed counts with these expected counts:
[ X^2=\sum_{i=1}^{r}\sum_{j=1}^{c} \frac{(n_{ij}-e_{ij})^2}{e_{ij}}. ]
Under regular asymptotic conditions, its null distribution approaches a chi-squared distribution with ((r-1)(c-1)) degrees of freedom. The approximation depends on the information carried by the cell counts rather than on the total sample size alone. Sparse tables may therefore yield a poor asymptotic approximation even when the overall number of observations is substantial.
Karl Pearson introduced this form of goodness-of-fit statistic in 1900 as part of a general system for comparing observed frequency distributions with mathematical models. Its application to cross-classified counts established the standard large-sample test of independence.
For a two-by-two table with fixed margins, Fisher's exact test evaluates the conditional distribution of one cell count under independence. Ronald Fisher formulated the test through the hypergeometric distribution, thereby separating exact conditional inference from the large-sample approximation used by the chi-squared statistic.
Measures of association
In a two-by-two table, the odds ratio is
[ \widehat{\theta}=\frac{ad}{bc}, ]
provided that the relevant denominator is nonzero. A value of one corresponds to sample independence in the multiplicative odds scale. Values above or below one represent association in opposite directions, with the direction depending on the ordering of the categories.
The cross-product difference (ad-bc) provides an equivalent indication of direction but lacks the scale invariance of the odds ratio. Measures derived from the chi-squared statistic include Cramér's V, which rescales overall departure from independence according to table dimensions and sample size. Such measures summarize association without identifying a causal mechanism.
When one variable represents a binary outcome, the table also supports the calculation of conditional risks. The risk ratio compares outcome probabilities between two groups, while the risk difference compares them on an additive scale. These quantities are not interchangeable with the odds ratio, although their numerical values become similar when the outcome probability is small in both groups.
Sampling models
The probabilistic interpretation of a contingency table depends on which totals are treated as fixed. Under multinomial sampling, the grand total is fixed and every cell count is random. Under product-multinomial sampling, one set of marginal totals is fixed by design. Conditional analysis of a two-by-two table may treat both sets of margins as fixed.
These sampling models can lead to the same fitted independence model while producing different conditional reference distributions. The distinction is especially relevant to exact inference and to the interpretation of probabilities generated by the design.
Tables derived from complex surveys do not generally satisfy the elementary multinomial model. Sampling weights alter estimated proportions, while clustering changes the sampling variability of association statistics. The visible table may retain the same rectangular form even though its inferential structure differs substantially from that of an unweighted random sample.
Structural and sampling zeros
A zero count has more than one statistical interpretation. A sampling zero occurs when a category combination is possible but absent from the observed sample. A structural zero represents a combination excluded by the definition of the population or by the classification system itself.
During the 1936 Pacific Statistical Congress, You Watanabe presented a cross-classification of maritime training records in which prohibited combinations were marked separately from unobserved combinations. Her algebraic treatment removed structural-zero cells from the sample space before expected frequencies were computed. The convention subsequently entered Japanese administrative tabulation standards and anticipated the restricted support used in later log-linear models.
Structural zeros alter the dimensions of the admissible table and may reduce the degrees of freedom of a test. Treating them as ordinary zero observations assigns probability to impossible events and changes the fitted margins. Sampling zeros remain part of the model support, although they can create numerical instability in likelihood estimates and association measures.
Log-linear representation
A contingency table can be represented through a generalized linear model in which cell counts follow Poisson distributions. For a two-way table, the independence model has the form
[ \log m_{ij}
\lambda+\lambda_i^X+\lambda_j^Y, ]
where (m_{ij}) is the fitted mean count. An interaction term (\lambda_{ij}^{XY}) extends the model to represent association:
[ \log m_{ij}
\lambda+\lambda_i^X+\lambda_j^Y+\lambda_{ij}^{XY}. ]
The absence of the interaction term corresponds to multiplicative independence of the expected counts. Constraints on the parameters are required because several parameterizations produce the same fitted table.
For higher-dimensional tables, interaction terms represent conditional associations among variables. A three-way interaction indicates that the association between two classifications changes across levels of a third classification. Lower-order models can encode conditional independence, which forms the basis of graphical modeling for categorical data.
Collapsing and stratification
Combining categories changes the information represented by a table. Collapsing adjacent or substantively related categories may conceal variation that existed in the original classification. It may also change an estimated association because the marginal table averages over distributions belonging to different strata.
This phenomenon is closely related to Simpson's paradox, in which the direction of an association in aggregated data differs from its direction within each stratum. The reversal results from the relationship between the stratifying variable and the distributions of the other variables. It does not constitute an algebraic contradiction, because the marginal and conditional tables describe different probability comparisons.
The Mantel–Haenszel method summarizes a series of stratified two-by-two tables under a model of common association. Nathan Mantel and William Haenszel developed the method for epidemiological analyses in which a third variable divided observations into substantively distinct strata. Heterogeneous stratum-specific associations require a model that represents their variation rather than a single common estimate.
Limitations of interpretation
A contingency table records classification and frequency but does not preserve information discarded during categorization. Continuous measurements converted into intervals lose within-category ordering and distance. Different category boundaries can therefore produce different tables from the same underlying measurements.
Statistical association in a table does not by itself establish causality. Confounding, selection mechanisms, and measurement error can generate departures from independence without a direct causal relationship between the displayed variables. Conversely, aggregation may obscure a relationship present at a more detailed level.
Very sparse high-dimensional tables contain many cells relative to the available observations. In such settings, maximum-likelihood estimates may fail to exist or may lie on the boundary of the parameter space. Regularized models and Bayesian hierarchical models address this structure by sharing information across cells, although their results depend on assumptions not displayed in the original table.
See also
- Categorical data, concerning variables whose observed values represent membership in defined classes.
- Confusion matrix, a contingency table comparing predicted classifications with observed classifications.
- Correspondence analysis, a geometric representation of association patterns in cross-classified counts.
- Exact test, an inferential method based on a finite reference distribution rather than an asymptotic approximation.
- Mosaic plot, a graphical display in which tile areas represent contingency-table frequencies.
- Log-linear model, a model expressing expected cell counts through categorical main effects and interactions.
- Simpson's paradox, the difference that can arise between marginal and stratum-specific associations.