The Chi-Squared Contingency Test

A biologist crosses a population of snails and records two traits at once: shell banding pattern and habitat (grassland vs. woodland). Are these traits independent? Or does banding pattern track habitat because of natural selection against conspicuous morphs? This is not a question about a single categorical variable's distribution. The goodness-of-fit framework from The Chi-Squared Goodness-of-Fit Test cannot answer it. It is a question about the relationship between two categorical variables measured on the same organisms. It calls for the chi-squared test of independence, built on a contingency table. Contingency tables are everywhere in biology: genotype by phenotype, treatment by survival outcome, species by habitat, disease status by exposure. Whenever an investigator classifies each individual along two categorical axes simultaneously, the natural question is whether the two classifications are associated or independent. The chi-squared contingency test (also called the chi-squared test of association or of independence) gives a rigorous, general-purpose answer. It does so without requiring the investigator to specify in advance what the "expected" proportions should be. The expected counts are derived directly from the data's own row and column totals. This is done under the null hypothesis of no association. The big picture is this: association between categorical variables is detected in a specific way. It is found by comparing the observed joint distribution of counts across cells. This is compared to the joint distribution expected if the two variables were statistically independent. Independence has a precise meaning. The probability of falling into a given row category is the same regardless of column category. The reverse is also true. This generates a specific, calculable set of expected cell counts. The discrepancy between observed and expected counts is summed appropriately across all cells. It is then compared to a chi-squared distribution to yield a p-value. This article builds the contingency chi-squared test from first principles. It covers how expected counts are computed from marginal totals. It also explains why the degrees of freedom differ from the goodness-of-fit case. It covers the classic experiments and case-control studies that made this test a cornerstone of biology and medicine. It also covers the pitfalls that trip up even careful researchers. These include small expected counts, confusing statistical with biological significance, and misreading 2×2 tables. It sets up a later, crucial distinction. That is the difference between a significant association and one that actually matters. This is the theme of Statistical vs Biological Importance.

Key takeaways
  • The chi-squared contingency test asks whether two categorical variables, scored on the same subjects, are statistically independent.
  • Expected counts per cell are calculated from row and column marginal totals: E = (row total × column total) / grand total.
  • The test statistic is χ² = Σ (O − E)² / E, summed over all cells of the table.
  • Degrees of freedom = (rows − 1) × (columns − 1), reflecting how many cell counts are free to vary given the margins.
  • Small expected counts (rule of thumb: fewer than 5 in more than 20% of cells) invalidate the chi-squared approximation; Fisher's exact test is the remedy for small samples, especially 2×2 tables.
  • A significant association tells you the variables are not independent — it does not tell you the association is large, causal, or biologically important.
The core insight

The chi-squared contingency test compares observed cross-classified counts to the counts expected under independence — computed purely from the table's own row and column totals — and a large discrepancy signals association between the two categorical variables.

The core idea

A contingency table cross-classifies each of n independently sampled individuals by two categorical variables at once: rows might be genotype classes, columns might be disease status, and each cell holds the count of individuals falling into that specific row-column combination. The table also carries marginal totals — the sum across each row (giving the total for that row category, ignoring column) and the sum down each column (giving the total for that column category, ignoring row) — and a grand total, n, the sum of everything. The null hypothesis of the contingency test is that the two classifying variables are statistically independent: knowing an individual's row category gives no information about its column category. Independence, in probability terms, means P(row i and column j) = P(row i) × P(column j) for every cell. Because the marginal probabilities P(row i) and P(column j) are themselves estimated from the data (as row total / n and column total / n), the expected count for cell (i, j) under independence is estimated as:

E₍ᵢⱼ₎ = (row total₍ᵢ₎ × column total₍ⱼ₎) / n This is the single most important formula in the test — it converts a qualitative hypothesis ("no association") into a quantitative prediction for every cell, using nothing but the observed margins. The alternative hypothesis is simply that the variables are not independent — some rows are disproportionately paired with some columns. The test does not specify which cells drive an association or the direction of any 2-category effect (that requires inspecting residuals or, for 2×2 tables, an odds ratio or relative risk) — it only asks whether independence, overall, is tenable. The test statistic sums standardized squared discrepancies over every cell in the table, exactly as in the goodness-of-fit test, but now the "expected" values come from the data's own margins rather than from an externally specified ratio (like 3:1 in a genetic cross):

χ² = Σᵢⱼ (Oᵢⱼ − Eᵢⱼ)² / Eᵢⱼ Under the null, this statistic approximately follows a chi-squared distribution — but with degrees of freedom (r − 1)(c − 1), not (number of cells − 1). This is the key structural difference from goodness-of-fit, and it arises because fitting the row and column marginal proportions themselves "uses up" degrees of freedom, as explained mechanistically below.

Mechanism — how the test actually works, step by step

Step 1 — Tabulate. Classify every sampled individual into exactly one row category and exactly one column category, producing an r × c table of observed counts Oᵢⱼ. Each individual contributes to exactly one cell; the total across all cells is n.

Step 2 — Compute margins. Sum each row to get row totals Rᵢ, sum each column to get column totals Cⱼ, and confirm ΣRᵢ = ΣCⱼ = n.

Step 3 — Compute expected counts under independence. For each cell, Eᵢⱼ = (Rᵢ × Cⱼ) / n. Intuitively: if row category i makes up a fraction Rᵢ/n of the sample, and the variables are independent, that same fraction of column category j's total (Cⱼ individuals) should also fall in row i, giving Eᵢⱼ = (Rᵢ/n) × Cⱼ.

Step 4 — Compute the statistic. For every cell, calculate (Oᵢⱼ − Eᵢⱼ)²/Eᵢⱼ, and sum over all r × c cells to get the test statistic χ².

Step 5 — Determine degrees of freedom. df = (r − 1)(c − 1). The reasoning: once you fix the row and column marginal totals (which are treated as given, not tested), only (r − 1)(c − 1) cell counts can be chosen freely — every other cell count is then forced, because each row must sum to its row total and each column to its column total. A 2×2 table therefore has only 1 degree of freedom: fix one cell, and the other three follow arithmetically from the margins.

Step 6 — Compare to the reference distribution. Locate χ² on the chi-squared distribution with the computed df, and obtain the p-value: the probability of observing a discrepancy this large or larger, under independence, by sampling variation alone.

Step 7 — Decide and interpret. If p falls below the pre-chosen significance threshold (conventionally α = 0.05), reject independence: the variables are associated in this sample. If not, the data are consistent with independence — which is not proof of independence, only an absence of detected evidence against it.

Why (r − 1)(c − 1) and not rc − 1: In goodness-of-fit, the expected proportions are fixed by theory (e.g., Mendelian 3:1), so only the total n is estimated from data, leaving (number of categories − 1) degrees of freedom. In the contingency test, the expected counts themselves depend on estimated row and column proportions — (r − 1) independent row proportions and (c − 1) independent column proportions are estimated from the data before any expected count can be computed. This "spends" (r − 1) + (c − 1) degrees of freedom relative to the naive rc − 1, leaving exactly (r − 1)(c − 1) free cells — the geometric fact that a table's interior, once the margins are fixed, has only that many independently variable entries.

Figure
Constructing expected counts from row and column margins in a 2×3 contingency table
Observed table (habitat × shell colour) Yellow Pink Brown Row total Grass 42 18 10 70 Woodland 8 22 30 60 70 Col total 50 40 40 n=130 E(Grass,Yellow) = (70 × 50) / 130 = 26.9 E(Woodland,Brown) = (60 × 40) / 130 = 18.5 df = (2−1)(3−1) = 2 → compare Σ(O−E)²/E to χ²₂ Grassland snails are enriched for yellow shells; woodland snails for brown — suggests association
Figure
Why 2×2 tables have exactly 1 degree of freedom
Fixing margins constrains the table a R1 − a C1 − a n−R1−C1+a Column totals: C1, C2 Row totals: R1, R2 — grand total n fixed Choose a freely → other 3 cells are forced by the margins. df = (2−1)(2−1) = 1

The evidence

The chi-squared test of independence was developed by Karl Pearson in his landmark 1900 paper, which introduced the general chi-squared goodness-of-fit statistic and, in the same intellectual stroke, showed how it could test association in cross-classified data. Pearson's original motivation was largely biometric — his work at University College London (with Walter Weldon and Francis Galton) concerned quantifying correlation and association in biological measurements, including whether traits like eye colour and hair colour in humans, or physical characters across categories of organisms, were inherited independently or jointly. The test's biological coming-of-age, however, is inseparable from R. A. Fisher's rigorous derivation of the correct degrees of freedom. Pearson initially argued, incorrectly, that an r × c contingency table should be tested with df = rc − 1, treating it as if fitting a single multi-category distribution. Fisher, in a series of papers in the 1920s (building on his 1922 clarification of degrees of freedom for the goodness-of-fit test), demonstrated that because the expected counts in a contingency table are computed from estimated row and column marginal probabilities, two separate sets of parameters are being fitted from the same data — and the correct degrees of freedom must be (r − 1)(c − 1), not rc − 1. This was not a minor technical quibble: using Pearson's df systematically distorted p-values, particularly badly for larger tables, and the Pearson–Fisher dispute (playing out publicly in the 1920s) is one of the founding controversies of modern statistics. A defining biological application that cemented the test's importance in genetics and epidemiology came from case-control association studies — the type of design where a contingency test answers, definitively, whether an exposure or genotype is associated with a disease outcome. A canonical example: smoking and lung cancer. In the early 1950s, Richard Doll and A. Bradford Hill compared smoking histories among lung cancer patients and matched controls in London hospitals (published in the British Medical Journal, 1950). Structuring the data as a 2×2 contingency table — smoker/non-smoker by cancer case/control — and applying a chi-squared test (and the closely related methods that followed, given some small expected counts), they found an association so overwhelming (p far below any conventional threshold) that it could not plausibly be attributed to chance sampling alone. Combined with a clear dose-response relationship (heavier smokers showed higher relative risk) established in their subsequent prospective cohort study of British doctors, this evidence became a central pillar — alongside animal carcinogenesis experiments — in establishing smoking as a cause of lung cancer, a conclusion the 1964 U.S. Surgeon General's report formally endorsed. A cleaner, purely genetic illustration of the contingency chi-squared test in action is testing for linkage between two loci: if alleles at locus A and locus B assort independently (unlinked, on different chromosomes or far apart on the same one), then genotype at A and genotype at B, scored across many offspring, should form an independent contingency table. Departures from independence — an excess of parental-type combinations relative to what marginal allele frequencies predict — signal linkage, and this exact logic (a contingency chi-squared test on cross-classified genotype data) is still the workhorse for detecting linkage disequilibrium in modern genome-wide association studies, though those now use much larger samples and stricter multiple-testing corrections.

Worked examples

Example 1 — A plant ecology case: soil type and flower colour. A field botanist samples wild Linaria plants growing on two soil types and records flower colour (purple wild-type or a rarer white variant). The observed 2×2 table:

PurpleWhiteRow total
Limestone soil84690
Acidic soil543690
Column total13842n = 180

Under independence, expected counts are computed from margins: E(Limestone, Purple) = (90 × 138)/180 = 69 E(Limestone, White) = (90 × 42)/180 = 21 E(Acidic, Purple) = (90 × 138)/180 = 69 E(Acidic, White) = (90 × 42)/180 = 21 χ² = (84−69)²/69 + (6−21)²/21 + (54−69)²/69 + (36−21)²/21 χ² = 225/69 + 225/21 + 225/69 + 225/21 χ² = 3.26 + 10.71 + 3.26 + 10.71 = 27.94 df = (2−1)(2−1) = 1. The critical value for α = 0.05 with 1 df is 3.84 — the observed χ² of 27.94 vastly exceeds it (p < 0.0001). Soil type and flower colour are strongly associated in this sample: white-flowered plants are disproportionately common on acidic soil, consistent with a hypothesis that pH-dependent pigment expression, or selection linked to pollinator visitation on different soils, differs by habitat. Note what this test does not establish: it does not identify why the association exists, nor rule out a confound (e.g., a third variable like light exposure correlated with both soil type and where samples were taken) — only a controlled experiment or additional covariates can address causation.

Example 2 — A clinical scenario: vaccine and infection status, unbalanced groups. A field trial of an oral vaccine for a livestock pathogen enrolls unevenly sized groups (as often happens in observational veterinary studies): 240 vaccinated animals, only 60 unvaccinated animals available as an opportunistic comparison group. After the exposure season:

InfectedNot infectedRow total
Vaccinated18222240
Unvaccinated154560
Column total33267n = 300

Expected counts under independence: E(Vacc, Infected) = (240 × 33)/300 = 26.4 E(Vacc, Not) = (240 × 267)/300 = 213.6 E(Unvacc, Infected) = (60 × 33)/300 = 6.6 E(Unvacc, Not) = (60 × 267)/300 = 53.4 χ² = (18−26.4)²/26.4 + (222−213.6)²/213.6 + (15−6.6)²/6.6 + (45−53.4)²/53.4 = 70.56/26.4 + 70.56/213.6 + 70.56/6.6 + 70.56/53.4 = 2.67 + 0.33 + 10.69 + 1.32 = 15.01 df = 1; critical value at α = 0.05 is 3.84, so this is significant (p ≈ 0.0001). Infection rate is 7.5% among vaccinated animals but 25% among unvaccinated ones — vaccination is associated with substantially lower infection. All expected counts here exceed 5, so the chi-squared approximation is defensible; if the unvaccinated group had been even smaller (say 20 animals, pushing some expected counts below 5), Fisher's exact test would be the more defensible choice, since the chi-squared approximation degrades when expected counts are small — a concern flagged below.

Common misconceptions & traps

Misconception / TrapWhy it's wrong
"A significant chi-squared result tells us which cells are driving the association."It only tests overall independence; identifying the responsible cells requires inspecting standardized residuals or partitioning the table.
"Any 2×2 table can use the standard chi-squared formula."With small expected counts (rule of thumb: any Eᵢⱼ < 5, or >20% of cells below 5), the chi-squared approximation is unreliable; use Fisher's exact test instead.
"Degrees of freedom equal the number of cells minus one, just like goodness-of-fit."For contingency tables, df = (r−1)(c−1), because both row and column marginal proportions are estimated from the data.
"A larger χ² always means a stronger, more important association."χ² scales with sample size; a trivial association can produce a huge χ² if n is large enough. Effect size (e.g., Cramér's V, odds ratio) must be reported separately.
"Independence in the sample proves independence in the population."Failing to reject the null is not evidence of true independence — it may just reflect low power or small sample size.
"The test tells us one variable causes the other."Chi-squared tests association only; causal direction requires experimental design, temporal ordering, or additional causal inference tools.

The scientist behind it

Origins · 1 minKarl Pearson and how this was discovered
KP
Karl Pearson
1857–1936 · Biometrics and the founding of mathematical statistics, University College London

Pearson trained originally in mathematics and law before turning to biometry, driven by collaboration with the zoologist Walter Weldon, who needed rigorous tools to quantify variation and correlation in Weldon's studies of crabs and other organisms as tests of Darwinian natural selection. Alongside Francis Galton, Pearson co-founded the journal Biometrika in 1901 and built much of the statistical machinery — correlation coefficients, the method of moments, and the chi-squared test — that transformed biology from a largely descriptive science into a quantitative, hypothesis-testing discipline. His 1900 paper introducing the chi-squared statistic is among the most cited papers in the history of statistics. Pearson was also a committed eugenicist, and his statistical innovations were entangled with eugenic research programs of his era — a historical fact that responsible teaching of his scientific legacy should not omit. His later, protracted dispute with R. A. Fisher over degrees of freedom in contingency tables — in which Pearson was, on the mathematics, simply incorrect — is a reminder that even towering founders of a field can resist correction from a rising generation.

History
- 1900 — Karl Pearson introduces the chi-squared statistic for goodness-of-fit and contingency tables.
- 1901 — Pearson, Weldon, and Galton found Biometrika to formalize biometric statistics.
- 1922 — R. A. Fisher clarifies degrees of freedom for goodness-of-fit tests, sharpening the Pearson-era formulas.
- 1920s — Fisher demonstrates that contingency table df must be (r−1)(c−1), correcting Pearson's rc−1.
- 1950 — Doll and Hill publish the case-control smoking–lung cancer study using contingency-table analysis.

Where this matters today

Clinical epidemiology and case-control studies rely on the contingency chi-squared test (or its exact and stratified relatives) as the default first pass for testing whether an exposure — a drug, a toxin, a behavior — is associated with a disease outcome, exactly as in the Doll-Hill smoking studies; modern versions extend this with logistic regression and Mantel–Haenszel stratification to adjust for confounders, but the core cross-tabulation logic is unchanged.

Genome-wide association studies (GWAS) test millions of genetic markers for association with disease or trait status by constructing a contingency table (genotype × case/control) at every locus and computing a chi-squared or closely related statistic, then applying stringent multiple-testing correction (often a genome-wide significance threshold near p < 5 × 10⁻⁸) because so many tests are run simultaneously.

Conservation genetics uses contingency tests to check whether genotype frequencies at a marker locus differ between habitat patches, subspecies, or before-and-after a population bottleneck, informing management decisions about gene flow and population structure.

Agricultural and veterinary trials, as in the vaccine example above, use contingency tables to test whether a treatment (vaccine, feed additive, breeding line) is associated with an outcome (infection, mortality, yield category), guiding decisions before larger, costlier trials are commissioned.

Ecology and behavioral biology use the test constantly — habitat preference (species by microhabitat), mating assortment (male phenotype by female choice), and predator-prey interactions (prey type by predator species) are all naturally structured as contingency tables, making this one of the most frequently applied inferential tests across the biological sciences.

Ask the tutor: How do I decide, from real data, whether a statistically significant chi-squared association is actually biologically meaningful?

How is the contingency chi-squared test different from the goodness-of-fit test?

Goodness-of-fit tests whether a single categorical variable's observed distribution matches a theoretically specified distribution (like Mendelian 3:1 ratios), with expected counts derived from that external theory. The contingency test instead examines two categorical variables measured jointly on the same subjects, asking whether they are statistically independent; its expected counts come entirely from the data's own row and column marginal totals, not from an outside theoretical ratio. The formulas for the test statistic look identical — Σ(O−E)²/E — but the source of E, and the degrees of freedom formula, differ: (categories−1) for goodness-of-fit versus (r−1)(c−1) for contingency tables.

What sample size rule of thumb should I use before trusting the chi-squared approximation?

A commonly cited guideline (from Cochran's work in the mid-20th century) is that all expected cell counts should be at least 5, or at minimum no more than 20% of cells should fall below 5, and no expected count should be below 1. When this is violated — common in small 2×2 tables — the chi-squared distribution poorly approximates the true sampling distribution of the test statistic, inflating the false-positive rate. In that situation, Fisher's exact test, which computes an exact p-value from the hypergeometric distribution rather than relying on a large-sample approximation, is the appropriate substitute.

Can the chi-squared contingency test be extended beyond 2×2 tables?

Yes — the formula E = (row total × column total)/n and the statistic χ² = Σ(O−E)²/E generalize directly to any r × c table, with df = (r−1)(c−1). Larger tables allow testing association among variables with more than two categories each, such as blood type (4 categories) by geographic region (5 categories). The trade-off is that larger tables demand larger sample sizes to keep expected counts above the reliability threshold in every cell, and a significant overall result in a large table often needs follow-up analysis (residuals, or partitioning into sub-tables) to identify exactly which category combinations drive the association.

Does a non-significant chi-squared result prove the two variables are independent?

No. Failing to reject the null hypothesis means the data are consistent with independence, not that independence has been proven true. A modest sample size, weak true association, or unlucky sampling can all produce a non-significant result even when a real, biologically meaningful association exists in the underlying population. Statistical tests can provide evidence against a null hypothesis but never conclusively prove a null hypothesis is exactly true; researchers should report confidence intervals for the effect size and consider statistical power before concluding "no association."

What is the difference between testing "independence" and testing "homogeneity" with a chi-squared contingency table?

Mathematically, the calculations are identical, but the sampling design and the biological question differ. A test of independence draws one sample and cross-classifies each individual on two variables simultaneously (e.g., snails classified by both habitat and shell colour). A test of homogeneity instead draws separate, fixed-size samples from several distinct populations (e.g., separately sampling 90 grassland snails and 90 woodland snails) and asks whether the distribution of a single categorical variable — shell colour — is the same across those populations. Despite the different framing, both scenarios produce the same table structure, the same expected-count formula, and the same (r−1)(c−1) degrees of freedom.

How does the chi-squared contingency test relate to odds ratios and relative risk?

The chi-squared test answers a yes/no question — is there statistically detectable association? — but it does not, by itself, quantify how strong that association is in a directly interpretable way. For 2×2 tables specifically, the odds ratio (odds of outcome in exposed group divided by odds in unexposed group) and relative risk (ratio of probabilities) are the standard effect-size measures, each with its own confidence interval. Best practice in biology and medicine is to report the chi-squared p-value (or exact p-value) alongside an odds ratio or relative risk with a confidence interval, so readers can judge both statistical significance and the practical magnitude of the association.

Why does Karl Pearson's original degrees-of-freedom formula for contingency tables matter historically?

Pearson initially applied df = rc − 1 to contingency tables, treating them as a single multi-category goodness-of-fit problem, which systematically overstated the degrees of freedom and thus miscalculated p-values, particularly for larger tables. R. A. Fisher's correction to (r−1)(c−1) in the 1920s was a foundational moment in statistics: it clarified that estimating marginal proportions from the same data used to build expected counts "costs" additional degrees of freedom, a principle (parameters estimated from data reduce the effective degrees of freedom available for testing) that recurs throughout regression, ANOVA, and modern statistical modeling far beyond contingency tables.

Before this

Where this leads