Categorical › Contingency / proportions

Chi-square test

Pearson's chi-square test of independence asks whether two categorical variables are related, comparing observed cell counts against the counts expected if they were independent.

What is Chi-square test?

The Pearson chi-square is the classical test for whether two categorical variables are associated. For each cell, compute the expected count under independence (row total × col total / grand total), then sum (O − E)² / E across cells. Under H₀ the result is approximately χ² with (rows − 1)(cols − 1) df.

The asymptotic approximation breaks down when expected counts are small — the historical rule is that no cell should have E < 5 (Cochran's rule). For 2×2 tables this matters most; for larger tables a few sparse cells are usually OK. When the rule is violated, switch to Fisher's exact test, which computes the exact distribution and needs no large-sample approximation.

Chi-square is direction-agnostic — it tells you the variables are associated but not how. For ordered categories, follow up with Cochran-Armitage (monotonic trend); for 2×2 tables, report the odds ratio with CI alongside the test; for general associations, report Cramér's V as a size measure.

When should I use Chi-square test?

  • Two categorical variables and you want a quick global yes/no on independence.
  • Cross-tabulations of nominal data (sex × treatment, region × diagnosis).
  • Default test for any table where all expected counts are reasonable (≥ 5).

What data does it need?

Two categorical columns.

What does it report?

χ² statistic, df = (r − 1)(c − 1), p-value, expected-count table, Cramér's V (and φ for 2×2).

What does it assume?

  • Independent observations (one row per subject).
  • Each subject contributes to exactly one cell.
  • Expected count ≥ 5 in ≥ 80% of cells — otherwise switch to Fisher's exact.

Formula

χ² = Σ (O_ij − E_ij)² / E_ij, E_ij = (row total × col total) / grand total

How do I interpret the result?

Cramér's V runs from 0 (independence) to 1 (perfect association). V = 0.1 small, 0.3 medium, 0.5 large for df > 1.

For ordered exposure × binary outcome, prefer Cochran-Armitage — it tests specifically for monotonic trend, which chi-square misses entirely.

When all expected cells are ≥ 5, the asymptotic chi-square p is essentially identical to Fisher's exact p — pick chi-square for speed and Fisher when you have any small cells.

See also

References

  • Pearson (1900). On the criterion that a given system of deviations from the probable is such that it can be reasonably supposed to have arisen from random sampling. Philos. Mag. Series 5, 50(302).