A contingency table is a table of frequencies that classifies observations according to two or more categorical variables. Each cell records the number of observations belonging to a particular combination of categories. In statistics, contingency tables are used to summarize data, compare conditional distributions, and investigate whether variables are associated. A two-variable table is usually displayed as rows and columns; tables involving additional variables can be represented as collections of two-way tables or as multidimensional arrays.
Structure and notation
For two variables with row categories and column categories, an contingency table has observed cell counts , where and .
Its row totals, column totals, and overall total are
The plus sign indicates summation over the corresponding index. Row and column totals are called marginal totals, while is the grand total.
For example, a survey might classify 100 respondents by their work arrangement and preferred meeting format:
| Work arrangement | Online preferred | In-person preferred | Total |
|---|---|---|---|
| Remote | 30 | 20 | 50 |
| On-site | 20 | 30 | 50 |
| Total | 50 | 50 | 100 |
The cell count 30 represents respondents who both work remotely and prefer online meetings. The marginal totals describe each variable separately, without retaining information about how the two variables are related.
In an ordinary count table, categories for each variable are mutually exclusive, and each observation contributes to exactly one cell. Multiple-response questions, in which respondents may select several categories, require a different interpretation: the entries may count responses rather than distinct individuals.
Counts, proportions, and conditional distributions
Dividing each count by the grand total produces relative frequencies,
which form an empirical joint probability distribution for the two variables.
Three percentage displays answer different questions:
- Overall percentages: , the percentage of all observations in a cell.
- Row percentages: , the distribution of column categories within a row.
- Column percentages: , the distribution of row categories within a column.
Row and column percentages estimate different conditional probabilities. Their denominators must therefore be stated explicitly.
In the example, 60% of remote workers prefer online meetings, compared with 40% of on-site workers. Conversely, 60% of those who prefer online meetings work remotely. These two percentages happen to be equal because of the table’s particular margins; they need not be equal in general.
When group sizes differ, raw counts alone can be misleading. A larger group may contain more observations in every outcome category even when its within-group proportion for a particular outcome is smaller.
Independence and expected frequencies
Two variables and exhibit statistical independence when
for every category pair. Equivalently, the conditional distribution of either variable is unchanged across categories of the other, wherever the conditioning probabilities are positive.
Under the fitted independence model, the expected cell counts are
These are fitted expectations, not additional observations. They need not be integers.
In the example, every expected count is . The observed counts depart from this pattern: the remote–online and on-site–in-person cells are more frequent than the independence model predicts.
Sample proportions rarely satisfy independence exactly, even when the population variables are independent. Statistical inference assesses whether the observed departures are compatible with sampling variation.
Statistical tests
Pearson’s chi-squared test
The standard Pearson chi-squared test of independence uses the test statistic
Under the null hypothesis of independence and suitable sampling conditions, its large-sample distribution is approximately a chi-squared distribution with
degrees of freedom. This formula assumes an ordinary table without structural restrictions and with positive population marginal probabilities.
For the example,
The resulting p-value describes how unusual a statistic at least this large would be under the null model. It is not the probability that independence is true, and it does not measure the strength or practical importance of an association.
The same calculation can test homogeneity: whether several independently sampled populations share the same categorical distribution. The numerical procedure is the same, but the sampling design and interpretation differ from those of a test of independence in a single sampled population.
The approximation depends on expected counts and table structure, not simply on total sample size. Very small expected counts can make it inaccurate. Familiar thresholds, such as an expected count of five, are rules of thumb rather than universal validity conditions.
Exact and simulation-based tests
Fisher’s exact test is widely used for tables, particularly with small or sparse counts. Under independence, conditioning on both margins gives a hypergeometric distribution for one cell count, which determines the remaining cells.
Exact procedures also exist for larger tables, although computation can become demanding. Conditional Monte Carlo methods can approximate test probabilities when enumerating all possible tables is impractical. For discrete distributions, different definitions of a two-sided exact test can produce different p-values.
Likelihood-ratio test
An alternative statistic is the likelihood-ratio deviance,
where a term with is defined to contribute zero. Under standard regularity conditions, has the same asymptotic chi-squared reference distribution as Pearson’s statistic.
This statistic also has an information-theoretic interpretation:
where is the empirical mutual information calculated with natural logarithms.
Measuring association
A significance test addresses compatibility with a null model; an effect size describes the magnitude or pattern of association.
Two-by-two tables
For a table written as
| Outcome present | Outcome absent | |
|---|---|---|
| Group 1 | ||
| Group 2 |
the odds ratio is
when the relevant denominators are nonzero. It compares the odds of the outcome in Group 1 with those in Group 2.
Two other comparisons are the difference in outcome proportions,
and their ratio,
Which quantities have a meaningful population interpretation depends on how observations were sampled. For example, when sampling fixes the numbers of outcome-positive and outcome-negative observations, observed outcome proportions generally do not estimate population outcome probabilities directly.
For the illustrative table, taking “online preferred” as the outcome and remote workers as Group 1 gives an odds ratio of , a proportion difference of , and a proportion ratio of . These describe different aspects of the same association.
Larger tables
A common standardized association measure is Cramér’s :
It ranges from zero to one for a nondegenerate table. In a table it equals the absolute value of the phi coefficient. It does not indicate a direction of association, and conventional labels such as “small” or “large” depend on context.
For ordered categories, methods that use the ordering may be more informative than nominal-category measures. Treating categories as unordered discards information about their sequence.
Sampling models and assumptions
The same observed table can arise under different sampling schemes:
- Multinomial sampling: the grand total is fixed, and observations are classified into cells.
- Product-multinomial sampling: one set of marginal totals is fixed, as when predetermined numbers are sampled from several groups.
- Poisson sampling: cell counts are modeled as independent Poisson variables, with the grand total random.
- Fixed-margin sampling or conditional inference: both margins are fixed by design or conditioned upon in the analysis.
These distinctions affect likelihoods, parameter interpretation, and exact inference, even though several large-sample procedures coincide.
Ordinary chi-squared procedures assume independent observational units. Repeated measurements on the same person, matched pairs, household clusters, and complex survey designs generally require methods that account for dependence or sampling design. A cross-tabulation remains a valid descriptive display, but its appearance alone does not establish that the usual test is appropriate.
Multiway tables and statistical models
A three-way table adds a third classification, often displayed as a series of two-way tables, one for each category of the third variable. Such tables can distinguish marginal association from conditional independence.
Associations may change after stratification. In Simpson’s paradox, an association in an aggregated table can reverse when the data are separated into groups. Differences in group composition can create such patterns. Whether a variable should be conditioned on is a question of study design and causal structure, not something the table alone determines.
Log-linear models express expected cell counts through main effects and interaction terms. For a two-way independence model,
with constraints imposed for identifiability. Additional interaction terms represent departures from independence. Multiway versions can describe more complex patterns of conditional association.
When one categorical variable is designated as the response, logistic regression and related models provide another way to analyze the data, particularly when explanatory variables include continuous measurements.
Applications and limitations
Contingency tables appear in surveys, quality control, population studies, and many other settings. A classification model’s confusion matrix is a specialized contingency table comparing predicted categories with observed categories.
Important limitations include:
- Association is not causation. An association can arise through confounding, selection processes, or other features of the data-generating process.
- Zero counts have different meanings. A sampling zero is an unobserved but possible combination; a structural zero is an impossible combination. Structural zeros alter the model and may change degrees of freedom.
- Sparse tables can be difficult to analyze. Adding categories or variables rapidly increases the number of cells, often leaving little information in individual cells.
- Grouping loses information. Converting continuous measurements into categories makes a table possible but discards within-category variation and can make results sensitive to cut points.
- Missing observations affect interpretation. Excluding incomplete records or treating missingness as a category changes what the table describes and may introduce bias.
- Overall tests do not identify specific departures. A significant result establishes evidence against the overall independence model, not that every row or cell differs. Examining many cells or contrasts raises multiple-testing issues.
- Precision matters. Effect estimates can be unstable in small or sparse tables; suitable confidence intervals convey uncertainty that counts and p-values alone do not.