Jugami Statistics

Jugami › Statistics and Probability › Chi-Square Tests

Statistics · Unit 8

Chi-Square Tests

How to tell whether counts in categories fit a model, and whether two categorical variables are related, using one number: χ².

Some data come as numbers, like heights or test scores. Much data comes as categories: which face a die shows, which lunch a student picks, which party a voter supports. With categories, all you can do is count. This unit shows how to compare the counts you observed with the counts a model says you should expect, and how to decide whether the gap is bigger than chance would make. It uses the hypothesis tests and p-values from Unit 5 and the two-way tables from Unit 2.

Six bars showing how often each face of a die came up, with a dashed gold line at the expected count and the chi-square formula beside them. Observed vs expected 123456 χ² = Σ (O − E)² E O: observed E: expected

Weldon's dice, 1900

In 1894 the zoologist W. F. Raphael Weldon set out to test whether his dice were fair. He and his helpers threw 12 dice at a time, 26,306 times, and each time counted how many dice showed a 5 or a 6. If the dice were fair, a 5 or 6 has probability 1/3 on each die, so the counts should follow a binomial distribution with n = 12 and p = 1/3. Weldon sent his table to Francis Galton in a letter dated 2 February 1894.

The counts looked close to the binomial, but not exactly. Some differences were positive, some negative. Was the gap just chance, or were the dice off? Nobody had a good way to judge a whole table of differences at once.

In 1900 Karl Pearson published one. In the Philosophical Magazine he combined all the differences into a single number, which he called χ² (chi-square, said “kye-square”), and worked out how big it tends to be by chance alone. Weldon's dice were his first example. Pearson's answer: a gap as large as Weldon's would turn up by chance only about once in 62,500 tries. The dice favored 5s and 6s.

Weldon's 26,306 throws of 12 dice: observed counts vs a fair-dice model
Dice showing 5 or 6012345678910+
Observed1851,1493,2655,4756,1145,1943,0671,33140310518
Expected (fair)2031,2163,3455,5766,2735,0182,9271,2553928714

Look at the pattern: too few throws with 0–4 fives and sixes, too many with 5 or more. Done the modern way, with the last three columns grouped so every expected count is at least 5, the data give χ² ≈ 35.5 on 10 degrees of freedom, a p-value of about 0.0001. Pearson blamed the pips: on ordinary dice they are hollowed out, so the six face is a little lighter than the one face opposite it. (A modern repeat of the experiment, below in the history, suggests the real cause may be dice that are not perfect cubes.)

Pearson's χ² is still one of the most used tests in science. By the end of this unit you'll be able to run it yourself.

Categorical data and counts

A categorical variable puts each individual into a group: eye color, favorite sport, yes or no. You can't average categories, but you can count them. Those counts are the data for every test in this unit.

Counts, not percents. The χ² formula needs counts. If you're given percents, turn them back into counts first. 30% of 50 people and 30% of 5,000 people carry very different amounts of evidence.

Observed and expected counts

The observed counts (O) are what you actually saw. The expected counts (E) are what the null hypothesis says you'd see on average:

expected count = n × (claimed proportion)

Roll a fair die 60 times and you expect 60 × 1/6 = 10 of each face. You won't get exactly 10 of each; chance always moves the counts around. The question a χ² test answers is whether the observed counts are too far from the expected ones to blame on chance.

Expected counts don't have to be whole numbers. If 1/3 of 50 throws should show a 5 or 6, the expected count is 16.67. That's an average over many repeats, not a prediction for one. Don't round it.

The chi-square statistic

For each category, find how far the observed count is from the expected count, square it, and divide by the expected count. Then add up over all the categories:

χ² = Σ (O − E)² ÷ E

Each term (O − E)² ÷ E is that category's contribution. The largest contributions show you where the data and the model disagree most.

Move the counts, watch χ²

Drag the top of any bar, or pick a category and use the buttons. The dashed lines are the expected counts. Watch how each category's contribution, the total χ² and the p-value change.

Things to try:

Degrees of freedom and p-values

If the null hypothesis is true, χ² follows (approximately) a chi-square distribution. There is a different curve for each number of degrees of freedom (df). All of them start at 0 and are skewed right; the mean of each curve equals its df.

Chi-square curves for 2, 4 and 8 degrees of freedom Three right-skewed curves. With 2 degrees of freedom the curve starts high at zero and falls. With 4 it peaks near 2, and with 8 it peaks near 6 and is lower and wider. Each curve is centered at its degrees of freedom. 0 4 8 12 16 Value of χ² df = 2 df = 4 df = 8 Chi-square distributions
The more categories (degrees of freedom), the farther right the curve sits. Its mean equals its degrees of freedom.

For a goodness-of-fit test, df = (number of categories) − 1. Why minus 1? The counts must add up to n, so once you know all but one of them, the last is fixed. Only k − 1 counts are free to vary.

The p-value is the area under the curve to the right of your χ²: the chance of a fit this bad or worse if H₀ is true. It is always one-sided, because any kind of misfit makes χ² larger.

Critical values of χ² at the 5% level (reject H₀ if χ² is larger)
df123456
χ² critical value3.845.997.819.4911.0712.59

As a rough guide, a χ² near its df is unremarkable. A χ² far above its df is suspicious.

The goodness-of-fit test

A goodness-of-fit test asks whether one categorical variable follows a claimed distribution.

Worked example: the cafeteria claim

A school cafeteria says students choose pizza 40% of the time, the salad bar 30%, sandwiches 20% and soup 10%. On a random day, 200 students chose: pizza 92, salad 50, sandwiches 44, soup 14. Do the data fit the claim?

  1. Hypotheses. H₀: the proportions are 0.40, 0.30, 0.20, 0.10. Hₐ: at least one proportion is different.
  2. Expected counts. 200 × 0.40 = 80, 200 × 0.30 = 60, 200 × 0.20 = 40, 200 × 0.10 = 20. All are at least 5.
  3. Statistic. χ² = (92 − 80)²/80 + (50 − 60)²/60 + (44 − 40)²/40 + (14 − 20)²/20 = 1.80 + 1.67 + 0.40 + 1.80 = 5.67.
  4. df and p-value. 4 categories, so df = 3. The p-value is P(χ² ≥ 5.67) ≈ 0.13. (5.67 is below the critical value 7.81.)
  5. Conclusion. At α = 0.05, fail to reject H₀. These data are consistent with the cafeteria's claim. They don't prove it is right, only that the differences are the size chance often produces.

“We accept H₀, so the claim is true.” A χ² test can show a model fits badly; it can't prove a model is right. Say “fail to reject” and “consistent with”.

Two-way tables and expected counts

Now two variables. A survey of 200 randomly chosen students at a high school recorded each student's grade and how they prefer to study:

Observed counts: grade by preferred way to study
GradeAloneWith friendsOnline groupTotal
9th305020100
12th453520100
Total758540200

If grade and study style were independent, both grades would split the same way as the whole school: 75 of 200 study alone, so 75/200 of the 100 ninth graders, 37.5, would be expected to. In general:

expected = (row total × column total) ÷ grand total
Expected counts if grade and study style are independent
GradeAloneWith friendsOnline group
9th100 × 75 ÷ 200 = 37.5100 × 85 ÷ 200 = 42.5100 × 40 ÷ 200 = 20
12th37.542.520

The expected table has the same row and column totals as the observed one. That's a good check on your arithmetic.

The test of independence

A chi-square test of independence uses one random sample, classified by two variables, and asks whether the variables are associated.

Worked example: grade and study style

  1. Hypotheses. H₀: grade and study style are independent (no association). Hₐ: they are associated.
  2. Statistic. Add (O − E)²/E over all six cells: 1.50 + 1.32 + 0 + 1.50 + 1.32 + 0 = 5.65.
  3. Degrees of freedom. For a table with r rows and c columns, df = (r − 1)(c − 1). Here (2 − 1)(3 − 1) = 2.
  4. p-value. P(χ² ≥ 5.65) with df = 2 is about 0.059. Just above 0.05: 5.65 falls short of the critical value 5.99.
  5. Conclusion. At α = 0.05, fail to reject H₀. There is some evidence that ninth and twelfth graders study differently (ninth graders lean toward studying with friends), but not quite enough at the 5% level.

Why (r − 1)(c − 1)? Once the row and column totals are fixed, you can fill in only r − 1 rows and c − 1 columns freely; the rest is forced by the totals. R. A. Fisher pointed this out in 1922, correcting the count Pearson had used for tables.

Independence or homogeneity?

The same arithmetic answers a slightly different question. A test of homogeneity takes separate random samples from two or more populations (say, 100 ninth graders and 100 twelfth graders, sampled separately) and asks whether the distribution of one variable is the same in each. The χ², df and p-value are found exactly the same way. What differs is the design and the wording of the hypotheses:

Choosing a chi-square test
TestDataQuestiondf
Goodness of fitOne sample, one variableDo the counts fit a claimed distribution?k − 1
IndependenceOne sample, two variablesAre the two variables associated?(r − 1)(c − 1)
HomogeneitySeveral samples, one variableIs the distribution the same in each population?(r − 1)(c − 1)

Association isn't causation. A significant test of independence says the variables are related in the population. Unless the groups came from random assignment, it doesn't say one causes the other.

Conditions

The χ² curve is only an approximation to how the statistic really behaves. It works well when:

If an expected count is too small, you can combine neighboring categories, as was done with Weldon's 10, 11 and 12, or collect more data.

Checking the observed counts for “at least 5”. An observed count of 2 is fine if its expected count is 8. The condition is about what the model expects.

Common mistakes

Using percents or proportions instead of counts. The formula only works on counts.

Dividing by O instead of E. It's (O − E)² ÷ E, the expected count.

Getting the degrees of freedom wrong. Goodness of fit: categories − 1, not n − 1. Two-way table: (rows − 1)(columns − 1), not counting the total row and column.

Doubling the p-value. A χ² test is always right-tailed. There's no “two-sided” version.

Saying the data prove H₀. A large p-value means the data are consistent with H₀, not that H₀ is true.

Rounding expected counts to whole numbers. Keep the decimals until the end.

Reading cause into a test of independence. Association in an observational study doesn't show cause and effect. Splitting a two-way table by a third variable can even reverse an association: see Simpson's paradox in Misleading Statistics.

Practice

History

Quiz

Ten questions. Pick an answer for each, then check.

1. A die is rolled 120 times to test whether it is fair. What is the expected count for each face?

2. A goodness-of-fit test compares counts in 5 categories. How many degrees of freedom does it have?

3. A category has observed count 18 and expected count 12. What is its contribution to χ²?

4. In a two-way table, a cell's row total is 60, its column total is 80, and the grand total is 200. Its expected count under independence is

5. A chi-square test of independence uses a table with 3 rows and 4 columns. What is df?

6. Which condition must be checked before trusting a chi-square p-value?

7. A test gives χ² = 2.1 with 4 degrees of freedom. Which statement is best?

8. One random sample of 400 teens records both their favorite sport and whether they have a part-time job. Which test asks whether these are related?

9. Why is a chi-square test always right-tailed?

10. In 1900, Karl Pearson's first example of his new χ² test used

Talk about it

Sources