A die is rolled 60 times. If it’s fair, you’d expect each face to come up 10 times — but in reality you observe 6, 8, 15, 9, 13, and 9. Is this die actually biased, or is this just the kind of random variation you’d expect from a fair die? Every hypothesis test you’ve learned so far — t-tests, z-tests — compares means of numeric variables. This question is different: it’s about whether observed counts across categories match what you’d expect. That’s the job of the chi-square test.
What Is the Chi-Square Test?
The chi-square (χ²) test is a family of hypothesis tests used with categorical data — data sorted into distinct groups or categories rather than measured on a numeric scale. It compares observed frequencies (what you actually counted) against expected frequencies (what you’d predict under some hypothesis), and asks whether the difference is larger than random chance would plausibly produce.
There are two main versions you’ll meet: the goodness-of-fit test, which checks whether a single categorical variable matches a hypothesised distribution, and the test of independence, which checks whether two categorical variables are related to each other. Both use the same underlying statistic, but the logic of the hypotheses differs.
The Chi-Square Statistic Formula
Both versions of the test rely on this formula:
χ² = ∑ [(O − E)² / E]
Here O is the observed frequency in each category or cell, and E is the expected frequency under the null hypothesis. You calculate (O−E)²/E separately for every category or cell, then sum them all up. Squaring the difference ensures negative and positive deviations don’t cancel out, and dividing by E scales each deviation relative to how large a count was expected there — a difference of 5 matters more when only 10 were expected than when 500 were expected.
The resulting χ² statistic is always non-negative and gets larger as observed counts drift further from expected counts. A χ² of zero would mean every observed count matched its expectation exactly.
Goodness-of-Fit Test: Worked Example
Back to the die. The null hypothesis is that the die is fair — each face has probability 1/6. With 60 rolls, the expected frequency for each face is 60 × (1/6) = 10.
| Face | Observed (O) | Expected (E) | (O−E)²/E |
|---|---|---|---|
| 1 | 6 | 10 | 1.60 |
| 2 | 8 | 10 | 0.40 |
| 3 | 15 | 10 | 2.50 |
| 4 | 9 | 10 | 0.10 |
| 5 | 13 | 10 | 0.90 |
| 6 | 9 | 10 | 0.10 |
Summing the last column: χ² = 1.60 + 0.40 + 2.50 + 0.10 + 0.90 + 0.10 = 5.60.
Degrees of freedom for a goodness-of-fit test is (number of categories − 1) = 6 − 1 = 5. Looking up the critical value for χ² with df = 5 at α = 0.05 gives 11.07. Since 5.60 < 11.07, we fail to reject the null hypothesis — there isn’t enough evidence to conclude the die is biased. The variation observed is consistent with normal random fluctuation from a fair die.
Test of Independence: Worked Example
Now a different question: is there a relationship between a customer’s age group and their preferred payment method? A survey of 200 customers gives this contingency table of observed counts:
| Card | Cash | Mobile | Row Total | |
|---|---|---|---|---|
| Under 30 | 30 | 10 | 40 | 80 |
| 30–60 | 40 | 25 | 15 | 80 |
| Over 60 | 15 | 20 | 5 | 40 |
| Column Total | 85 | 55 | 60 | 200 |
H₀: Age group and payment method are independent. To test this, calculate the expected count for each cell as (row total × column total) / grand total. For the “Under 30 / Card” cell: E = (80 × 85) / 200 = 34. Repeating this for all nine cells gives the full expected table, then applying (O−E)²/E to each cell and summing gives χ² ≈ 29.6 (working through all nine cells individually, which is standard practice in software but shown here for the key cells to illustrate the method).
Degrees of freedom for a test of independence is (rows − 1) × (columns − 1) = (3−1)(3−1) = 4. The critical value for χ² with df = 4 at α = 0.05 is 9.49. Since 29.6 > 9.49, we reject H₀ — there is significant evidence that age group and payment method preference are related. Younger customers lean toward mobile payment; older customers lean toward cash, exactly as the raw counts suggested.
Degrees of Freedom: Why the Two Tests Differ
Goodness-of-fit uses df = k − 1, where k is the number of categories, because once you know k−1 of the expected counts and the total, the last one is fixed. Test of independence uses df = (r−1)(c−1) because, given the row and column totals, only (r−1)(c−1) of the cell counts can vary freely before the rest are determined by the totals. Getting degrees of freedom wrong is one of the most common ways students misread a chi-square table.
Reading the Chi-Square Critical Value Table
Unlike the normal or t-distribution, the chi-square distribution is not symmetric — it’s skewed right and only takes non-negative values, since it’s built from squared terms. A chi-square table lists critical values indexed by degrees of freedom (rows) and significance level (columns). You compare your calculated χ² statistic to the critical value at your chosen α and your degrees of freedom: if your statistic exceeds the critical value, you reject H₀.
Assumptions and When Chi-Square Fails
The chi-square test relies on a large-sample approximation, and it becomes unreliable when expected counts are too small. The standard rule of thumb is that every expected cell count should be at least 5. When this fails — often in small samples with many categories — the calculated p-value can be inaccurate, and alternative approaches like Fisher’s exact test are used instead. Categories are also assumed to be mutually exclusive: each observation must fall into exactly one cell, never split across two.
Common Mistakes
Confusing the two test types. Goodness-of-fit asks whether one categorical variable matches a hypothesised distribution; independence asks whether two categorical variables are related. They use the same formula but different hypotheses and different degrees-of-freedom calculations.
Using raw percentages instead of counts. The chi-square formula needs actual frequencies (counts), not percentages or proportions — converting percentages back to counts is a required first step if a question presents data that way.
Ignoring the expected-count-of-5 rule. Running a chi-square test on sparse data with several expected counts below 5 produces an unreliable p-value, even though the arithmetic will still “work.”
Practice Question
A company claims its customer satisfaction ratings are evenly split across “Satisfied,” “Neutral,” and “Dissatisfied.” A survey of 90 customers finds 50 Satisfied, 25 Neutral, and 15 Dissatisfied. Test this claim at α = 0.05.
Answer: H₀: ratings are evenly split (each expected at 90/3 = 30). O values: 50, 25, 15.
χ² = (50−30)²/30 + (25−30)²/30 + (15−30)²/30 = 400/30 + 25/30 + 225/30 = 13.33 + 0.83 + 7.50 = 21.67
df = 3 − 1 = 2. Critical value at α = 0.05, df = 2 is 5.99. Since 21.67 > 5.99, we reject H₀ — the company’s claim of an even split is not supported by the data; ratings skew significantly toward “Satisfied.”
References
1. Moore, D.S., McCabe, G.P. and Craig, B.A. (2021) Introduction to the Practice of Statistics. W.H. Freeman.
2. Triola, M.F. (2022) Elementary Statistics. Pearson.
3. Agresti, A. (2018) Statistical Methods for the Social Sciences. Pearson.
4. Field, A. (2018) Discovering Statistics Using IBM SPSS Statistics. SAGE.
5. Pearson, K. (1900) ‘On the criterion that a given system of deviations…’, Philosophical Magazine, 50(302), pp. 157–175.
