Prob and StatsInference

Chi-Square Tests

Test goodness of fit and association in two-way tables.

Not started Jump to practice

Some questions are about counts in categories: Is a die fair? Is preferring cats or dogs related to age? The chi-square statistic measures how far observed counts are from the counts a model expects. It extends the logic of significance tests to whole tables, including the two-way tables you already know.

Key idea

$$\chi^2 = \sum \dfrac{(O - E)^2}{E},$$ with observed counts $O$ and expected counts $E$ (always counts, never percents). Goodness of fit: $E = n \times$ (model proportion), and $df = k - 1$ for $k$ categories. Independence in a two-way table: $df = (r - 1)(c - 1)$ and $$E = \dfrac{\text{row total} \times \text{column total}}{\text{grand total}}.$$ The p-value is the area to the right of $\chi^2$ under the chi-square curve. Expected counts should all be at least 5.

Why it works

Each term compares a gap with the size expected: a gap of 10 is huge if 20 were expected and tiny if 1,000 were. Under $H_0$ a count varies with standard deviation about $\sqrt{E}$, so $(O - E)/\sqrt{E}$ acts like a z-score and each term is roughly a squared z-score. A sum of squared z-scores follows a chi-square distribution, which is skewed right; its degrees of freedom count how many cells are free to vary once the totals are fixed. For independence, the expected count gives each row the same column percents as the whole table, which is exactly what independence means.
The chi-square curve with 5 df. For the die example, χ² = 4.2 sits in the middle of the curve: a large p-value.

Worked example: Is the die fair?

A die is rolled 60 times: the faces 1 to 6 come up 8, 12, 9, 14, 6 and 11 times. Test whether it is fair.
  1. $H_0$: each face has probability $\frac16$. Expected count for each face: $60 \times \frac16 = 10$ (all at least 5).
  2. $$\chi^2 = \dfrac{4 + 4 + 1 + 16 + 16 + 1}{10} = 4.2$$ with $df = 6 - 1 = 5$.
  3. The p-value (right tail of the chi-square curve with 5 df) is about $0.52$.
  4. Fail to reject: these counts are typical for a fair die.

Answer: $\chi^2 = 4.2$, p ≈ 0.52: no evidence the die is unfair.

Observed counts with expected counts in parentheses.
Took reviewDid notTotal
Passed45 (33.75)90 (101.25)135
Did not pass5 (16.25)60 (48.75)65
Total50150200

Worked example: A test of independence

Use the table to test whether passing is independent of taking the review class.
  1. Expected for "passed and took review": $\dfrac{135 \times 50}{200} = 33.75$; the others follow the same way (shown in parentheses).
  2. Each gap is 11.25 in size: $$\chi^2 = \dfrac{11.25^2}{33.75} + \dfrac{11.25^2}{101.25} + \dfrac{11.25^2}{16.25} + \dfrac{11.25^2}{48.75} \approx 3.75 + 1.25 + 7.79 + 2.60 = 15.38$$
  3. $df = (2 - 1)(2 - 1) = 1$. The p-value is about $0.0001$.
  4. Reject independence: passing is associated with the review class (an association; the students chose whether to take it).

Answer: $\chi^2 \approx 15.4$, df 1, p ≈ 0.0001.

Common mistake

Using percentages instead of counts. The same percents are much stronger evidence from 1,000 observations than from 100, and $\chi^2$ grows with the counts to reflect that. Converting to percents hides the sample size. Check that every O and E is a count of individuals.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.

Loading practice…

Builds on

Leads to

This is one of the top rungs of Probability and Statistics.

Sources and further reading

  • OpenStax, Introductory Statistics 2e, section 11.2 Goodness-of-Fit Test
  • OpenStax, Introductory Statistics 2e, section 11.3 Test of Independence
@teddyrookbook GitHub