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.
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.
Two categorical columns.
χ² statistic, df = (r − 1)(c − 1), p-value, expected-count table, Cramér's V (and φ for 2×2).
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.