Instant PDF download, name-and-email checkout. Complete bundle: every Economics and Statistics book
Statistics guide, Unit 3, step 16 of 22Previous stepNext step

Step 16: Chi-Square Tests for Categorical Data

SyllabusAP Stats 3.14–3.15
In 30 seconds
  • Chi-square tests work with counts in categories. You work out the counts you would expect if a hypothesis were true, then measure how far the observed counts are from them.
  • The statistic is \( \chi^2 = \sum (O – E)^2 / E \). It is never negative, and the test is always right-tailed: only a large \( \chi^2 \) is evidence against \( H_0 \).
  • Goodness of fit (one variable): \( E = n \times p_i \), df = categories − 1. Test of independence (two-way table): \( E = \text{row total} \times \text{column total} \div \text{grand total} \), df = (rows − 1)(columns − 1).
  • Exam point: every expected count must be at least 5, always use raw counts (not percentages), and a significant result shows association, not causation.
Video lesson coming soonA short animated lesson for this step will appear here.

Comparing what you saw with what you expected

Picture a small shop that records 250 sales over five weekdays: Monday 42, Tuesday 38, Wednesday 45, Thursday 52, Friday 73. If customers were equally likely to come on any day, you would expect 50 sales each day. Friday’s 73 looks high. But a few days are always a bit above or below 50 just by chance. Is Friday’s surplus real, or is it noise?

Every test so far was about means or proportions. Chi-square (pronounced “kai-square”, written \( \chi^2 \)) tests work directly with counts: how many observations fall into each category. That makes them the natural tool for categorical data with more than two categories.

The idea is simple:

  1. Work out the counts you would expect if a hypothesis were true.
  2. Compare them with the counts you actually observed.
  3. Measure the total gap. Close means the hypothesis survives; far apart means it does not.
Key termExpected count (E): the number of observations you would expect in a category or cell if the null hypothesis were exactly true.

The chi-square statistic

Measuring the “total gap” needs care. If you just add up the raw differences \( O – E \), they always sum to zero, because the observed and expected counts have the same total. Surpluses cancel shortfalls. Also, a gap of 10 matters much more when you expected 20 than when you expected 1,000.

The chi-square statistic fixes both problems. Square each difference so nothing cancels, then divide by the expected count to put it on the right scale:

\[ \chi^2 = \sum \frac{(O – E)^2}{E} \]

Here \( O \) is the observed count, \( E \) the expected count, and you add up over every cell. Three properties follow straight away:

  • \( \chi^2 \) is never negative, because each term is a square divided by a positive number.
  • It equals zero only when every observed count matches its expected count exactly.
  • Larger values mean worse agreement, so the test is always right-tailed. A small \( \chi^2 \) supports \( H_0 \), so there is nothing to reject on the left.

Critical values depend on the degrees of freedom. At \( \alpha = 0.05 \): df 1 → 3.841, df 2 → 5.991, df 3 → 7.815, df 4 → 9.488, df 5 → 11.070. Reject \( H_0 \) when your statistic is bigger than the tabled value.

The goodness-of-fit test

The goodness-of-fit test looks at one categorical variable and asks whether its counts match a claimed distribution. Are sales spread evenly across weekdays? Is a die fair? Does this year’s breakdown match last year’s percentages?

The expected counts come straight from \( H_0 \): multiply the total sample size by the proportion the null hypothesis claims for each category.

\[ E = n \times p_i \qquad \text{df} = (\text{number of categories}) – 1 \]
Worked example

The shop’s 250 sales are Mon 42, Tue 38, Wed 45, Thu 52, Fri 73. Test at \( \alpha = 0.05 \) whether sales are spread evenly across the five days.

Step 1\( H_0 \): each day has \( p = 0.20 \). \( H_a \): the days are not equally likely. Expected count \( E = 250 \times 0.20 = 50 \) for every day.
Step 2Contributions \( (O-E)^2/E \): Mon \( 64/50 = 1.28 \), Tue \( 144/50 = 2.88 \), Wed \( 25/50 = 0.50 \), Thu \( 4/50 = 0.08 \), Fri \( 529/50 = 10.58 \).
Step 3\( \chi^2 = 1.28 + 2.88 + 0.50 + 0.08 + 10.58 = 15.32 \), with df = 5 − 1 = 4.
Step 4Critical value 9.488. Since \( 15.32 \gt 9.488 \), reject \( H_0 \) (p ≈ 0.004).

There is convincing evidence that sales are not spread evenly across the week. Friday alone contributes 10.58 of the 15.32, more than two-thirds, and its \( O – E \) is positive. So the result is driven by Friday being busier than expected.

That last step matters. A significant \( \chi^2 \) only says the pattern differs from \( H_0 \) somewhere. To find out where, scan the contributions for the biggest terms, and look at the sign of \( O – E \) to see the direction. A good check on your arithmetic: the \( O – E \) column must add up to zero.

Not every test rejects. If a company claims its customers split 50% / 30% / 20% across three plans, and a sample of 120 gives 60, 35 and 25, the expected counts are 60, 36 and 24. The contributions are 0, 0.028 and 0.042, so \( \chi^2 = 0.07 \) with df = 2. That is far below 5.991, so you fail to reject. This does not prove the claim; it simply finds no evidence against it.

The test of independence

The second test handles two categorical variables recorded on the same people, arranged in a two-way table. The question is whether the variables are related.

  • \( H_0 \): the two variables are independent. Knowing one tells you nothing about the other.
  • \( H_a \): the variables are associated.

If the variables really were unrelated, each row would split in the same proportions as the whole table. That gives the expected counts:

\[ E = \frac{\text{row total} \times \text{column total}}{\text{grand total}} \qquad \text{df} = (\text{rows} – 1)(\text{columns} – 1) \]
Key termTest of independence: a chi-square test on a two-way table that asks whether two categorical variables are associated or unrelated.
Worked example

A company asks 500 customers in three regions which of two products they prefer.

RegionProduct AProduct BRow total
North12080200
South90110200
East4060100
Column total250250500
Step 1\( H_0 \): preference is independent of region. \( H_a \): they are associated.
Step 2Expected counts: North and South each \( (200 \times 250)/500 = 100 \) per product; East \( (100 \times 250)/500 = 50 \) per product. The smallest is 50, well above 5.
Step 3Contributions: North \( 400/100 + 400/100 = 4 + 4 \); South \( 100/100 + 100/100 = 1 + 1 \); East \( 100/50 + 100/50 = 2 + 2 \). So \( \chi^2 = 14.00 \).
Step 4df = (3 − 1)(2 − 1) = 2, critical value 5.991. \( 14.00 \gt 5.991 \), so reject \( H_0 \) (p ≈ 0.0009).

There is convincing evidence that product preference is associated with region. North contributes 8 of the 14 and chose A more often than expected (120 against 100), while East leans towards B.

Conditions, cautions and choosing the test

Chi-square tests need two conditions:

  1. A random sample with independent observations: each person counted once, in exactly one cell.
  2. All expected counts at least 5. This applies to expected counts, not observed ones. An observed count of 0 is fine if its expected count is 5 or more.

If some expected counts fall below 5, combine categories that are genuinely similar. This raises the counts and reduces df. Choose the merge for sensible reasons, never by trying merges until one gives a significant result.

Common mistakeNever calculate \( \chi^2 \) from percentages or proportions. That is like pretending the sample size is 100. The same 60/40 split is weak evidence with 50 people and very strong evidence with 5,000. If you are given percentages, convert them back to counts using the sample size first.

A second warning: a significant test of independence shows only that the variables are associated. It does not show that region causes preference; a third factor such as income could drive both. And because \( \chi^2 \) is always positive, it gives no direction. Use the cell contributions and the row percentages for that.

Telling the two tests apart is just a matter of counting variables:

Goodness of fitTest of independence
VariablesOneTwo
Data layoutA single row of countsA two-way table
QuestionDo counts match a claimed distribution?Are the two variables related?
Expected counts\( n \times p_i \)row × column ÷ total
dfcategories − 1(rows − 1)(columns − 1)

One last link. A 2×2 table compares two groups on a yes/no outcome, which is the two-proportion problem from Step 12. The two tests always agree, because for a 2×2 table \( \chi^2 = z^2 \). For example, 90 of 200 men and 70 of 200 women gave \( z = 2.04 \); as a chi-square test the same data gives \( \chi^2 = 4.17 \approx 2.04^2 \), with the same p-value of about 0.041. For a 2×2 table, prefer the two-proportion method, because it gives a direction and a confidence interval. Chi-square earns its place in larger tables.

Exam tipLay out a contribution table with columns O, E, O − E and \( (O-E)^2/E \). It earns method marks and catches slips (O − E must sum to 0). State df and the critical value, check every expected count is at least 5, and finish with a sentence in context that names the cell driving the result, without claiming causation.

Practise: 6 questions

Try each one on paper first, then check the worked answer.

EasyQ1. A sample of 300 has claimed proportions 0.5, 0.25 and 0.25. Find the expected counts and the df for a goodness-of-fit test. Show worked answer
\( E = 300 \times 0.5 = 150 \), \( 300 \times 0.25 = 75 \), \( 300 \times 0.25 = 75 \).
Check: 150 + 75 + 75 = 300.
df = 3 − 1 = 2.
EasyQ2. In a two-way table, a cell’s row total is 150, its column total is 200 and the grand total is 600. Find its expected count. What is df for a table with 3 rows and 4 columns? Show worked answer
\( E = (150 \times 200) \div 600 = 30000 \div 600 = 50 \).
df = (3 − 1)(4 − 1) = 2 × 3 = 6.
MediumQ3. A die is rolled 120 times, giving 15, 22, 18, 25, 17, 23 for faces 1 to 6. Test at \( \alpha = 0.05 \) whether the die is fair. Show worked answer
\( H_0 \): each face has \( p = 1/6 \). \( H_a \): the die is not fair.
\( E = 120 \div 6 = 20 \) for each face.
Contributions: \( 25/20 = 1.25 \), \( 4/20 = 0.20 \), \( 4/20 = 0.20 \), \( 25/20 = 1.25 \), \( 9/20 = 0.45 \), \( 9/20 = 0.45 \).
\( \chi^2 = 3.80 \), df = 5, critical value 11.070.
\( 3.80 \lt 11.070 \), so fail to reject \( H_0 \) (p ≈ 0.58). There is no evidence that the die is unfair. This does not prove it is fair.
MediumQ4. A restaurant records 200 orders across four dishes: 65, 40, 55, 40. Test at \( \alpha = 0.05 \) whether all dishes are equally popular (critical value 7.815). Show worked answer
\( E = 200 \div 4 = 50 \) each, df = 3.
Contributions: \( 225/50 = 4.50 \), \( 100/50 = 2.00 \), \( 25/50 = 0.50 \), \( 100/50 = 2.00 \).
\( \chi^2 = 9.00 \gt 7.815 \), so reject \( H_0 \): the dishes are not equally popular.
Dish 1 contributes most (4.50) and was ordered more than expected (65 against 50).
MediumQ5. A goodness-of-fit test with claimed proportions 40%, 30%, 20%, 7%, 3% is planned on a sample of 60. Can it go ahead as set up? If not, what should you do? Show worked answer
Expected counts: \( 60 \times 0.40 = 24 \), \( 18 \), \( 12 \), \( 60 \times 0.07 = 4.2 \), \( 60 \times 0.03 = 1.8 \).
Two expected counts are below 5, so the test is not valid as set up.
Fix: merge the sparse categories with a similar one. Combining the last three gives a claimed proportion of 0.30 and an expected count of 18. Now the expected counts are 24, 18, 18, all above 5, and df falls from 4 to 2.
ExamQ6. A university classifies 400 students by faculty and weekly library use. Arts: 120 users, 80 non-users. Science: 80 users, 120 non-users. (a) State the hypotheses. (b) Compute the expected counts. (c) Compute \( \chi^2 \) and df. (d) Using a critical value of 3.841, conclude in context and say whether faculty causes library use. [7 marks] Show worked answer
(a) \( H_0 \): library use is independent of faculty; \( H_a \): they are associated [1].
(b) Row totals 200 and 200; column totals 200 users and 200 non-users; N = 400. Every cell: \( (200 \times 200) \div 400 = 100 \) [1]. All above 5, so the condition holds.
(c) Each cell contributes \( (\pm 20)^2/100 = 4 \), so \( \chi^2 = 16.00 \) [2]; df = (2 − 1)(2 − 1) = 1 [1].
(d) \( 16.00 \gt 3.841 \), so reject \( H_0 \): there is strong evidence that weekly library use is associated with faculty, with Arts students using it more than expected [1]. This shows association only; course requirements or timetables could explain it, so no causal claim is justified [1].

Check yourself

1. Why is the chi-square test always right-tailed?

A small \( \chi^2 \) means observed and expected agree, which supports \( H_0 \).

2. You want to test whether gender is related to subject choice. Which test?

Two categorical variables in a two-way table call for the test of independence.

3. The expected-count condition says…

The condition is about expected counts; an observed count of 0 is allowed.

Your score: 0 out of 3

The short version

  • Chi-square compares observed and expected counts: \( \chi^2 = \sum (O-E)^2/E \). It is never negative and always right-tailed.
  • Goodness of fit: one variable, \( E = n \times p_i \), df = categories − 1.
  • Test of independence: two variables, \( E = \) row × column ÷ total, df = (rows − 1)(columns − 1).
  • Use raw counts, check every expected count is at least 5 (merge similar categories if not), and inspect the contributions to see what drove the result.
  • Association is not causation. The full chapter in Statistics Made Simple has 56 practice questions with a complete answer key.
Want more questions like these, with worked answers?
The practice book has hundreds more, graded from easy to exam level.
Next: step 17 of 22One-Sample t Procedures and Paired Data