STAT 200 · Statistics for Economics
Chi-Square Tests for Categories
Chi-square tests compare counts in categories with what you would expect, to check a pattern or whether two traits are linked.
Key takeaways
- A chi-square test compares observed counts with expected counts, where the expected counts come from the null hypothesis.
- The statistic adds up (observed − expected)² ÷ expected across all categories. Big gaps make a big total.
- A goodness-of-fit test checks one categorical variable against a claimed pattern. A test of independence checks whether two categorical variables are linked.
- Degrees of freedom decide which chi-square curve gives the cutoff. A test of independence has (rows − 1) × (columns − 1).
- Expected counts should not be tiny, and a significant result shows a link, not a cause.
Picture Kai, who runs a small survey for the town. He asks 200 residents two questions. Do you rent or own your home? And how do you usually get to work, by car or by public transit? He lays the answers in a little grid. Owners seem to drive more. Renters seem to ride more. But is that a real link, or did this particular group of 200 just happen to line up that way?
Averages and t tests cannot help here, because the answers are not numbers. They are categories: rent or own, car or transit. For counts in categories, statisticians use the chi-square test (said "kai-square," and written with the Greek letter χ). It asks one question, again and again: do the counts we saw sit close to the counts we would expect if nothing special were going on?
The idea: expected versus observed
Suppose a store owner believes her customers come evenly across Monday through Thursday. She counts 100 shoppers over four days. If her belief is right, she would expect 25 each day. What she actually sees is 18, 30, 22 and 30. Close to 25? Sort of. Is it close enough that luck explains the wobble?
The chi-square statistic gives a single score for the total mismatch. For each category, take the gap between observed (O) and expected (E), square it so that gaps in either direction add up instead of canceling, and divide by E so that a gap of 5 matters more when you only expected 10 than when you expected 1,000. Then add the results:
Zero means a perfect match. The bigger the total, the worse the match.
The score is never negative. If the null is true, the score tends to be small. A big score is evidence against the null.
Goodness of fit: one variable
A goodness-of-fit test asks whether the counts in one set of categories match a claimed pattern. The null says the pattern holds. Here is the store owner's test, step by step.
Day Observed Expected (O − E)² ÷ E
Monday 18 25 (−7)² ÷ 25 = 49 ÷ 25 = 1.96
Tuesday 30 25 (5)² ÷ 25 = 25 ÷ 25 = 1.00
Wednesday 22 25 (−3)² ÷ 25 = 9 ÷ 25 = 0.36
Thursday 30 25 (5)² ÷ 25 = 25 ÷ 25 = 1.00
Total 100 100 χ² = 4.32 The degrees of freedom for a goodness-of-fit test are the number of categories minus 1. Here that is 4 − 1 = 3. Why minus 1? Once you know three of the four counts and the total, the fourth is fixed, just like the n − 1 idea in the t test lesson.
Now we need a cutoff. The chi-square curve is not symmetric. It starts at zero, rises quickly, then trails off to the right. Only the right tail counts as "too big." For a 5% test, standard table cutoffs are:
| Degrees of freedom | Cutoff at 5% |
|---|---|
| 1 | 3.841 |
| 2 | 5.991 |
| 3 | 7.815 |
| 4 | 9.488 |
| 5 | 11.070 |
With 3 df the cutoff is 7.815. Our χ² of 4.32 is below it, so we fail to reject. The day-to-day wobble is the kind of thing luck alone could easily make. As always, that is not proof of evenness. It only says 100 shoppers are not enough to call it uneven.
Independence: two variables
Back to Kai's survey. A test of independence asks whether two categorical variables are linked. The null says they are independent: knowing one tells you nothing about the other.
Kai's 200 answers look like this:
| Drives | Transit | Row total | |
|---|---|---|---|
| Owns | 70 | 30 | 100 |
| Rents | 40 | 60 | 100 |
| Column total | 110 | 90 | 200 |
If housing and commuting were unrelated, then each group would split the same way as everyone. Overall, 110 out of 200 drive, which is 55%. So of the 100 owners we would expect 55 to drive and 45 to ride. The same goes for 100 renters. The rule for each cell is:
expected count = (row total × column total) ÷ grand total
Owns, drives: 100 × 110 ÷ 200 = 55
Owns, transit: 100 × 90 ÷ 200 = 45
Rents, drives: 100 × 110 ÷ 200 = 55
Rents, transit: 100 × 90 ÷ 200 = 45 Now add up the mismatch across the four cells:
Owns, drives: (70 − 55)² ÷ 55 = 225 ÷ 55 ≈ 4.09
Owns, transit: (30 − 45)² ÷ 45 = 225 ÷ 45 = 5.00
Rents, drives: (40 − 55)² ÷ 55 = 225 ÷ 55 ≈ 4.09
Rents, transit: (60 − 45)² ÷ 45 = 225 ÷ 45 = 5.00
Total χ² ≈ 18.18 The degrees of freedom are (rows − 1) × (columns − 1) = (2 − 1) × (2 − 1) = 1. The 5% cutoff for 1 df is 3.841. Our 18.18 is far past it. (It even beats the cutoff for 0.1%, which is about 10.83.) We reject independence. In Kai's town, housing and commuting are linked.
Out of 100 owners and 100 renters. If housing and commuting were unrelated, each group would have 55 drivers. Hypothetical example: the numbers are made up to show the method, not a quote or an average.
What the test does and does not say
The chi-square test says the link in this sample is bigger than luck would usually make. It does not say which way the link points, or why it exists. You must look back at the table. Owners drive more than expected, and renters less. Why? Maybe owners live in suburbs with few buses. Maybe renters live near stations. Maybe income plays a role. The test is silent on the reasons.
It also does not say how strong the link is. A huge sample can make a small, boring link "significant," the same trap we saw in the lesson on p-values. After a significant result, always look at the percentages. Here, 70% of owners drive versus 40% of renters, a gap of 30 points, which is large as well as significant.
Where to look after a significant result
A big χ² tells you something is off, but not where. The fix is to scan the cells and see which ones contribute the most. In Kai's table, all four cells added between 4 and 5 points to the total. That is the signature of a clean two-way pattern: owners lean to cars and renters lean to transit, equally strongly in both directions.
In a larger table the picture can be lumpier. Suppose a bigger version of Kai's survey had three kinds of homes (own, rent, live with family) and three ways of commuting. One cell might be responsible for most of the total, such as "live with family, walk," and the other eight cells might be near their expected counts. Then the honest summary is about that one cell, not the whole table. Economists doing this by hand compute each cell's share of the total, and software prints the same thing as "standardized residuals."
A simple habit helps: after the test, write the result as two or three percentages a neighbor could understand. "Seven in ten owners drive, compared with four in ten renters." The test shows the gap is not luck. The sentence shows what the gap is.
Where economists use it
Chi-square tests turn up wherever the data are counts. A labor economist might ask whether the share of workers in each of four job types is the same across two cities. A researcher on household finance might ask whether the number of people who hold a savings account depends on which of five income bands they belong to. A policy analyst might check whether a program's applicants, sorted into approved and denied, differ by neighborhood in ways luck cannot explain. A teacher of economics might even check whether the answers to a multiple-choice question are spread evenly across A, B, C and D, as a guessing check.
The same two-step habit applies each time. Build the table of observed counts. Work out what you would expect if the traits were unrelated. Then ask whether the mismatch is too large to blame on luck. For numeric outcomes, such as earnings or wait times, different tools fit, like the t tests you have already seen, and the ANOVA lesson that comes next.
One caution applies to every such use. If a significant link between, say, neighborhood and approval rates appears, the test does not by itself show unfair treatment. Many other factors, like income or the type of application, may differ across neighborhoods. A careful study asks whether the link survives once such factors are held fixed, and that takes methods beyond this lesson. Report the link, name the possible explanations, and avoid leaping to blame.
A common mistake: small expected counts and sloppy data
The chi-square formula is an approximation. It works well when each expected count is at least about 5. If a cell expects only 1 or 2, the cutoff table can mislead, and you should merge categories or use an exact method that software provides.
Another slip is to feed the test percentages instead of counts. If Kai said "70% of owners drive" and ran the formula on 70 and 30 without noting that 100 people stood behind them, the answer would be wrong, because the formula needs the true number of people. Finally, each person must appear once. If the same household answers twice, the counts overstate how much evidence you have. As the lesson on sampling bias warns, no test can repair a poorly gathered sample.
Words to know
Chi-square statistic (χ²) a score that adds up (observed − expected)² ÷ expected across all categories.
Observed count the number of cases you actually found in a category or cell.
Expected count the number you would find in a category if the null hypothesis were true.
Goodness-of-fit test a chi-square test of whether one variable's counts match a claimed pattern.
Test of independence a chi-square test of whether two categorical variables are linked.
Contingency table a grid of counts that cross-sorts people by two categories.
Degrees of freedom the number that picks the right chi-square curve; (rows − 1) × (columns − 1) for a table.
Check yourself
- Why do we square the gaps (O − E) in the chi-square formula?
- A die is rolled 60 times. If it is fair, how many of each face are expected? The counts were 8, 12, 10, 9, 11, 10. Compute χ².
- Using the 5% cutoff of 11.070 for 5 df, does the die in question 2 look unfair?
- A table has 3 rows and 4 columns. How many degrees of freedom does the test of independence have?
- In a 2×2 table, a group has row total 80, the column total is 50 and the grand total is 200. What is the expected count for that cell?
- A chi-square test of independence is significant. Does it prove that one trait causes the other? Explain.
Show the answers
- Squaring stops positive and negative gaps from canceling each other out, and it makes big gaps count much more than small ones.
- Each face is expected 60 ÷ 6 = 10 times; the gaps squared are 4, 4, 0, 1, 1, 0, which total 10, and divided by 10 gives χ² = 1.0.
- No: 1.0 is far below 11.070, so we fail to reject, and the die looks fair on these 60 rolls.
- Degrees of freedom = (3 − 1) × (4 − 1) = 2 × 3 = 6.
- Expected = 80 × 50 ÷ 200 = 20.
- No: it shows the two traits are linked beyond luck, but the cause could run either way or come from a third factor.