AP Statistics · Inference for categorical data: chi-square
Goodness of fit
1The chi-square statistic
A chi-square goodness-of-fit test checks whether the distribution of a single categorical variable matches a claimed distribution.
- $H_0$: the claimed distribution is correct (e.g. $p_1 = 0.5$, $p_2 = 0.3$, $p_3 = 0.2$).
- $H_a$: at least one of the proportions differs from the claim.
Compare observed counts $O$ with expected counts $E = np_i$ under $H_0$:
$$\chi^2 = \sum \frac{(O - E)^2}{E}$$
A large $\chi^2$ means the observed counts are far from what $H_0$ predicts.
Check yourself
Expected counts in a goodness-of-fit test are found by:
Show the answer
n × the claimed proportion for each category
Under H₀.
2Chi-square distributions
- Take only non-negative values and are skewed right.
- Each has degrees of freedom: for goodness of fit, $df = \text{number of categories} - 1$.
- The mean of a $\chi^2$ distribution equals its $df$; as $df$ grows, the curve shifts right and becomes more symmetric.
- The P-value is always the upper tail area: $P(\chi^2 \ge \text{observed})$.
Chi-square curves: the P-value for the die test is the area to the right of 4.5
Check yourself
The P-value for a chi-square test is the area:
Show the answer
To the right of the observed χ²
Large values are evidence against H₀.
3Conditions
- Random: a random sample or randomised experiment.
- 10%: $n \le 10\%$ of the population.
- Large Counts: all expected counts are at least 5.
The Large Counts condition is about expected, not observed, counts.
Check yourself
The Large Counts condition for chi-square tests requires:
Show the answer
All expected counts at least 5
Expected counts.
4Carrying out the test
is a die fair? It is rolled 120 times. Observed: 15, 22, 18, 25, 16, 24 for faces 1 to 6.
- $H_0$: each face has probability $\frac{1}{6}$; $H_a$: at least one face has a different probability.
- Expected: $120 \times \frac{1}{6} = 20$ each (all ≥ 5); rolls are independent.
- $\chi^2 = \frac{25 + 4 + 4 + 25 + 16 + 16}{20} = \frac{90}{20} = 4.5$.
- $df = 5$; P-value $= P(\chi^2_5 \ge 4.5) \approx 0.48$.
- $0.48 > 0.05$: fail to reject $H_0$. No convincing evidence the die is unfair.
Follow-up: if $H_0$ is rejected, look at the components $\frac{(O - E)^2}{E}$; the largest show which categories contribute most to the difference.
Check yourself
For 4 categories, df =
Show the answer
3
Categories − 1.
5Going deeper: expected counts, the test in full and follow-up analysis
A chi-square goodness-of-fit test asks whether the distribution of one categorical variable in a population matches a claimed distribution.
Hypotheses
- H₀: the stated distribution is correct (list the claimed proportions).
- Hₐ: the stated distribution is not correct (at least one proportion differs).
The alternative is always this general "something differs" statement; it is not one-sided.
Expected counts: expected = n × (claimed proportion). They need not be whole numbers; do not round them.
Test statistic
$$\chi^2 = \sum\frac{(\text{observed} - \text{expected})^2}{\text{expected}}$$
- Use counts, never percentages.
- df = number of categories − 1.
- Large values of χ² are evidence against H₀; the P-value is always the area to the right.
Conditions
- Random: a random sample (or randomized experiment).
- 10%: n ≤ 10% of the population when sampling without replacement.
- Large counts: all expected counts are at least 5. (Check the expected counts, not the observed.)
is a die fair? It is rolled 120 times: the faces 1 to 6 come up 15, 25, 18, 22, 16 and 24 times. Test at α = 0.05.
- State: H₀: each face has probability 1 ÷ 6; Hₐ: at least one face's probability is not 1 ÷ 6.
- Plan: chi-square goodness-of-fit test. Random: the rolls are random. Large counts: every expected count is 120 ÷ 6 = 20, at least 5.
- Do: differences (observed − expected): −5, +5, −2, +2, −4, +4. Squares: 25, 25, 4, 4, 16, 16. χ² = (25 + 25 + 4 + 4 + 16 + 16) ÷ 20 = 90 ÷ 20 = 4.5. df = 5. P-value ≈ 0.48.
- Conclude: since 0.48 > 0.05, fail to reject H₀. There is not convincing evidence that the die is unfair.
Worked example (unequal expected proportions): a company says its sweets are 30% red, 20% green, 20% yellow and 30% blue. A random sample of 200 has 48 red, 50 green, 46 yellow and 56 blue.
- Expected: 60, 40, 40, 60.
- Contributions: (48 − 60)² ÷ 60 = 2.40; (50 − 40)² ÷ 40 = 2.50; (46 − 40)² ÷ 40 = 0.90; (56 − 60)² ÷ 60 = 0.27.
- χ² = 6.07; df = 3. P-value ≈ 0.11.
- At α = 0.05, fail to reject H₀: the data are consistent with the company's claim.
The chi-square distributions
- Take only non-negative values.
- Skewed right; less skewed as df increases.
- Mean equal to df. So a χ² value near df is unremarkable; a value far above df is unusual.
Follow-up analysis: when the result is significant, look at the individual contributions $\frac{(O - E)^2}{E}$ to see which categories differ most from what was expected, and in which direction (observed above or below expected).
a test on birthdays by season gives χ² = 11.2 with df = 3 (P ≈ 0.011). The largest contribution, 7.9, comes from summer, where 68 births were observed against 50 expected. Conclusion: the distribution of births differs from uniform, mainly because there were more summer births than expected.
Why (O − E)² ÷ E? Squaring stops positive and negative differences cancelling; dividing by E puts each difference in proportion: being 10 away from an expected 20 matters far more than 10 away from an expected 1000.
If an expected count is below 5: combine sensible neighboring categories, or collect more data.
Which chi-square test?
- One variable, one sample, compared with a claimed distribution: goodness of fit.
- One variable, two or more populations or treatments: homogeneity.
- Two variables, one sample: independence.
Common mistakes
- Using percentages or proportions in the formula.
- Checking the large counts condition with observed counts.
- Rounding expected counts to whole numbers.
- df = number of categories (forgetting to subtract 1), or df = n − 1.
- Writing a one-sided alternative.
- Doubling the P-value.
- Concluding that the claimed distribution "is correct" after failing to reject.
Check yourself
A goodness-of-fit test compares observed counts in 5 categories with a claimed distribution. How many degrees of freedom are there?
Show the answer
4
Degrees of freedom equal the number of categories minus one.
6For top marks
- State hypotheses in words in context, including the claimed proportions.
- Show expected counts and check they are all ≥ 5.
- Give $\chi^2$, $df$ and the P-value; conclude in context.
- After rejecting $H_0$, identify the largest component and say whether that category was over- or under-represented.
"The largest contribution is from the 'blue' category (component 4.2), where there were more blue sweets than expected."
Check yourself
A large χ² value suggests:
Show the answer
Observed counts differ substantially from expected
Large discrepancies.
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