Statistics Reference
Chi-Square Test Explained
Every chi-square test is the same three moves: write down the counts a hypothesis expects, measure how far the observed counts stray, and ask the χ² distribution whether that gap is chance-sized. This guide works both classic versions in full — a fair-die goodness-of-fit test and a 2×2 independence test — including every expected count.
One Statistic, Two Questions
χ² = Σ (O − E)² / E over every cell
Goodness-of-fit
One categorical variable vs a claimed distribution. “Do these 60 die rolls fit a fair die?” Expected counts come from the hypothesized probabilities; df = k − 1.
Independence
Two categorical variables in a contingency table. “Does answer depend on group?” Expected counts assume no relationship; df = (r − 1)(c − 1).
Large deviations from expectation inflate χ²; the chi-square table (or an exact p-value) says how large is too large for the given degrees of freedom.
Worked Example 1: Is This Die Fair? (Goodness-of-Fit)
A die is rolled 60 times. A fair die expects 60 × 1/6 = 10 of each face.
| Face | Observed | Expected | (O−E)²/E |
|---|---|---|---|
| 1 | 5 | 10 | 2.5 |
| 2 | 8 | 10 | 0.4 |
| 3 | 9 | 10 | 0.1 |
| 4 | 8 | 10 | 0.4 |
| 5 | 10 | 10 | 0 |
| 6 | 20 | 10 | 10 |
| Total | 60 | 60 | 13.4 |
Decision. df = 6 − 1 = 5; the α = 0.05 critical value is 11.070. Since 13.4 > 11.070, reject fairness (exact p = 0.020). Note where the evidence lives: the face-6 cell alone contributes 10 of the 13.4 — the per-cell contributions are the diagnostic, not just the total.
Worked Example 2: Does Preference Depend on Group? (Independence)
200 customers — 100 men, 100 women — answer yes/no to a feature: men 30 yes / 70 no, women 50 yes / 50 no. Column totals: 80 yes, 120 no.
- Expected counts from (row × column) / total: men-yes = 100 × 80 / 200 = 40, men-no = 60, women-yes = 40, women-no = 60.
- Cell contributions: (30−40)²/40 = 2.5; (70−60)²/60 ≈ 1.67; (50−40)²/40 = 2.5; (50−60)²/60 ≈ 1.67.
- Statistic: χ² = 2.5 + 1.67 + 2.5 + 1.67 ≈ 8.33, with df = (2−1)(2−1) = 1.
- Decision: the df = 1 critical value at α = 0.05 is 3.841. Since 8.33 > 3.841, preference and group are associated (exact p = 0.004).
Strength is a separate question: φ = √(χ²/N) = √(8.33/200) ≈ 0.20 — a real but modest association. The chi-square calculator runs both test types from your observed counts and reports the exact p-value.
Validity Rules and Pitfalls
- Expected counts ≥ 5 in (nearly) every cell — otherwise the χ² approximation frays; use exact tests for small tables.
- Counts, not percentages. The test needs raw frequencies; feeding it percentages silently changes N and the conclusion.
- Independent observations — each subject contributes one count. Repeated measures need McNemar's test instead.
- Association ≠ causation: a significant independence test inherits every caveat in the correlation-vs-causation guide.
Try the Chi-Square Calculator
Goodness-of-fit and independence tests from your observed counts, with expected values and the exact p-value shown.
Frequently Asked Questions
What is the chi-square test used for, in plain terms?
It compares counts you observed against counts a hypothesis predicts. The goodness-of-fit version asks whether one categorical variable follows a claimed distribution (is this die fair?); the independence version asks whether two categorical variables are related (does preference depend on gender?). Both reduce to the same statistic: the sum of (observed - expected)^2 / expected across all cells.
How are expected counts calculated?
For goodness-of-fit, multiply the total count by each category's hypothesized probability: 60 rolls of a fair die expect 60 x 1/6 = 10 per face. For independence, each cell expects (row total x column total) / grand total - the count that would occur if the two variables were unrelated. The test then measures how far reality deviates from those blueprints.
What are the degrees of freedom for each chi-square test?
Goodness-of-fit: k - 1 for k categories (minus one more for each parameter estimated from the data). Independence: (rows - 1) x (columns - 1); a 2x2 table has df = 1. Degrees of freedom set which row of the chi-square table supplies the critical value.
Why does the chi-square statistic divide by the expected count?
A deviation of 5 means something different when 10 were expected than when 1,000 were. Dividing each squared gap by its expected count scales every cell's contribution to its own baseline, making cells comparable and giving the statistic its chi-square distribution under the null hypothesis.
What sample size does a chi-square test need?
The classic rule: all expected counts of at least 5 (some sources allow 20% of cells between 1 and 5). Small expected counts make the chi-square approximation unreliable - the statistic is discrete but the reference distribution is continuous. For small 2x2 tables, Fisher's exact test is the standard alternative.
Does a significant chi-square mean the relationship is strong?
No - like every p-value, it mixes effect and sample size. A trivial association reaches significance in a large enough sample. Measure strength separately: Cramer's V (or the phi coefficient for 2x2 tables) rescales chi-square to a 0-1 range. The worked independence example has phi = sqrt(8.33/200) = 0.20, a modest association despite p = 0.004.