AP Statistics · Inference for categorical data: chi-square
Tests for homogeneity and independence
1Two-way tables and expected counts
Both tests use a two-way table of counts. Under $H_0$, the expected count in each cell is
$$E = \frac{\text{row total} \times \text{column total}}{\text{grand total}}$$
The test statistic is the same as before: $\chi^2 = \sum \frac{(O - E)^2}{E}$, with
$$df = (\text{rows} - 1)(\text{columns} - 1)$$
Check yourself
A table has 3 rows and 4 columns. df =
Show the answer
6
(3 − 1)(4 − 1).
2Homogeneity or independence?
The calculation is identical; the design and hypotheses differ.
- Homogeneity: separate random samples from two or more populations (or groups in an experiment), with one categorical variable. $H_0$: the distribution of the variable is the same in every population.
- Independence: one random sample, classified by two categorical variables. $H_0$: there is no association between the two variables in the population.
(a) random samples of 100 voters from each of three towns are asked if they support a new park → homogeneity. (b) one random sample of 500 adults is classified by age group and preferred news source → independence.
Check yourself
A researcher takes one random sample of 400 students and records year group and favourite sport. The test is:
Show the answer
Chi-square test for independence
One sample, two variables.
3Worked example: independence
A random sample of 190 students is classified by study method and exam result.
- Flashcards: 40 passed, 20 failed (60).
- Rereading: 30 passed, 30 failed (60).
- Practice tests: 55 passed, 15 failed (70).
- Totals: 125 passed, 65 failed.
- $H_0$: study method and result are independent among all students; $H_a$: they are associated.
- Expected passes: Flashcards $\frac{125 \times 60}{190} \approx 39.47$, Rereading $39.47$, Practice tests $46.05$. Expected fails: $20.53$, $20.53$, $23.95$. All ≥ 5.
- $\chi^2 \approx 11.75$, $df = (2 - 1)(3 - 1) = 2$.
- P-value $\approx 0.003 < 0.05$: reject $H_0$. Convincing evidence of an association between study method and result.
- Largest components: Rereading-failed ($\frac{(30 - 20.53)^2}{20.53} \approx 4.37$) and Practice-failed ($3.35$). More rereaders and fewer practice testers failed than expected.
Pass rates by study method compared with the overall rate expected under H₀
Check yourself
Expected count for row total 80, column total 50, grand total 200 is:
Show the answer
20
80 × 50/200.
4Conditions and conclusions
- Random: random samples (homogeneity) or one random sample (independence), or random assignment.
- 10%: each sample ≤ 10% of its population.
- Large Counts: all expected counts ≥ 5.
Association is not causation unless the data come from a randomised experiment. In the study-method example, students chose their own methods, so stronger students might prefer practice tests.
Check yourself
Rejecting H₀ in a test for independence shows:
Show the answer
Evidence of an association between the variables
Not causation.
5Going deeper: expected counts in a two-way table, and telling the two tests apart
Both tests use a two-way table of counts and the same calculations. They differ in how the data were collected and so in the hypotheses.
Chi-square test for homogeneity
- Data: independent random samples from two or more populations, or two or more treatment groups in an experiment; one categorical variable recorded for each individual.
- H₀: there is no difference in the distribution of the variable among the populations (or treatments).
- Hₐ: there is a difference.
Chi-square test for independence
- Data: one random sample from one population; two categorical variables recorded for each individual.
- H₀: there is no association between the two variables in the population (they are independent).
- Hₐ: there is an association.
How to tell which: how many samples (or groups) were taken? Several: homogeneity. One, classified two ways: independence.
Expected counts (under H₀)
$$\text{expected} = \frac{\text{row total} \times \text{column total}}{\text{table total}}$$
Test statistic: $\chi^2 = \sum\frac{(O - E)^2}{E}$ over all cells.
Degrees of freedom: (rows − 1)(columns − 1), not counting the totals.
Conditions: Random (random samples or random assignment); 10% where sampling without replacement; all expected counts at least 5.
random samples of 100 students from each of two schools are asked to rate their cafeteria as low, medium or high.
- School A: 30 low, 50 medium, 20 high.
- School B: 20 low, 40 medium, 40 high.
- Column totals: 50, 90, 60. Table total 200.
- State: H₀: the distribution of cafeteria ratings is the same at the two schools. Hₐ: the distributions differ. (Two samples, one variable: homogeneity.)
- Plan: chi-square test for homogeneity. Random: independent random samples. Expected counts: each row total is 100, so for both schools: low 100 × 50 ÷ 200 = 25; medium 45; high 30. All at least 5.
- Do: for School A: (30 − 25)² ÷ 25 = 1.00; (50 − 45)² ÷ 45 = 0.56; (20 − 30)² ÷ 30 = 3.33. School B gives the same three values. χ² = 2(1.00 + 0.56 + 3.33) = 9.78. df = (2 − 1)(3 − 1) = 2. P-value ≈ 0.0075.
- Conclude: since 0.0075 < 0.05, reject H₀. There is convincing evidence that the distribution of cafeteria ratings differs between the two schools.
- Follow-up: the largest contributions come from the "high" column: School B has more high ratings than expected (40 against 30), School A fewer (20 against 30).
Worked example (independence): one random sample of 300 adults is classified by exercise habit (regular or not) and by sleep quality (good, fair, poor).
- One sample, two variables: test for independence.
- H₀: there is no association between exercise habit and sleep quality among adults in the population. Hₐ: there is an association.
- df = (2 − 1)(3 − 1) = 2.
- If the regular-exercise row total is 120 and the "good sleep" column total is 150: expected count for that cell = 120 × 150 ÷ 300 = 60.
- If H₀ is rejected: there is convincing evidence of an association. This does not show that exercise causes better sleep: the data are observational.
Why that formula for expected counts? If H₀ is true, the best estimate of the proportion in a column is the overall proportion (column total ÷ table total). Applying it to a row: row total × (column total ÷ table total).
Comparing two proportions: for a 2 × 2 table, the chi-square test gives the same P-value as a two-sided two-proportion z test (and χ² = z²). For a one-sided question, use the z test.
Why not several z tests? Comparing three or more groups pair by pair inflates the chance of a Type I error. The chi-square test compares them all at once.
After a significant result: examine the cells with the largest contributions and say how the observed counts differ from the expected.
Conditional distributions: support the analysis with percentages. Here 20% of School A's sample and 40% of School B's gave a high rating.
Common mistakes
- Stating independence hypotheses for a homogeneity design, or the reverse.
- Writing hypotheses about the sample ("in this table") in place of the population.
- Including the totals row and column when counting df.
- Using percentages in the formula.
- Checking observed counts for the large counts condition.
- Concluding cause and effect from an observational study.
- Saying "the variables are independent" after failing to reject; say "there is not convincing evidence of an association."
Check yourself
A researcher takes separate random samples of 200 teenagers and 200 adults and records each person's favorite type of music from five categories. Which test is appropriate, and with how many degrees of freedom?
Show the answer
Chi-square test for homogeneity, with 4 degrees of freedom
Two independent samples and one variable; df = (2 − 1)(5 − 1).
6For top marks
- Identify the test from the design: how many samples, how many variables.
- Write $H_0$ in context: "There is no association between X and Y among all…" or "The distribution of Y is the same for all…".
- Show at least one expected count calculation and confirm all are ≥ 5.
- Follow up a significant result with the largest components, saying which cells are above or below expected.
"The distribution of opinions on the park differs between towns: Town A had more supporters than expected (component 3.80)."
Check yourself
Which is the null hypothesis for homogeneity?
Show the answer
The distribution of the variable is the same in each population
Same distribution.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera