StatisticsChi-Square Tests for Categorical Data1 of 3
Chi-Square Tests for Categorical Data · 1
Chi-Square Test for Goodness of Fit
We know how to test a claim about a single proportion, and now we need a way to test whether the observed counts across all categories of one categorical variable fit a claimed model at once.
1
Measuring Mismatch with the Chi-Square Statistic
The Chi-Square statistic (χ2) is a single number that measures the total mismatch between the counts we observe in a sample and the counts we expect from a claimed model.
Suppose a video game claims its mystery loot boxes drop items across four rarity tiers according to this model: 50% Common, 30% Uncommon, 15% Rare, and 5% Legendary. To test that claim, you open 100 boxes and observe 58 Common, 24 Uncommon, 15 Rare, and 3 Legendary.
Measuring Mismatch with the Chi-Square Statistic
To combine all the category misses into one mismatch score without positive and negative gaps canceling out, and while keeping small and large categories in fair proportion, we compute the Chi-Square statistic:
χ2=∑E(observed countO−expected countE)2
The 2 in χ2 is part of the statistic's name (because each gap is squared), so you never take a square root at the end.
Text solution
Subtract E from O and square each category's gap Squaring each gap gets rid of the negative signs, so extra items in one category cannot cancel out shortages in another.
Divide each squared gap by the expected count E Dividing by E compares each squared miss to the size of its category, so missing 2 out of 5 expected Legendaries counts nearly as much as missing 8 out of 50 expected Commons.
Add the category contributions to get χ2 Adding the pieces across all four tiers gives the overall mismatch score for the sample.
χ2=1.28+1.20+0+0.80=3.28
Because the total is fixed at 100 boxes, extra boxes in one rarity tier always come out of the other tiers so the signed gaps O−E add up to zero. Shifting just a few boxes in a small category like Legendary (E=5) makes χ2 jump much more than shifting the same number of boxes in Common (E=50).
With 100 loot boxes total, adding boxes to one category takes them from another so the raw gaps always cancel out, while moving the same number of boxes jumps chi-squared much more in a small expected category than in a big one.
2
Degrees of Freedom and Finding the P-Value
Now that we have a mismatch score of χ2=3.28, we need to know whether 3.28 is just ordinary random luck or strong evidence against the developer's model. To judge the score, we use the degrees of freedom (df), which equals the average χ2 score we expect from random chance when the model is true, and the p-value, which is the chance of getting a χ2 score at least as large as ours by random luck alone.
Degrees of Freedom and Finding the P-Value
Once the sample total is fixed, knowing the counts in all but one category fixes the final category's count:
df=average χ2 when H0 is trueCategories−1
Because squared gaps can never be negative, the χ2 distribution starts at 0 and is skewed to the right. Both higher-than-expected and lower-than-expected counts make (O−E)2 larger, so any mismatch with the model pushes χ2 to the right and the p-value is always the area in the right tail.
Text solution
State the null and alternative hypotheses We test the claim that every category's true proportion matches the model against the alternative that the model is off in at least one category.
H0Ha:The claimed rarity modelis correct (pCommon=0.50,pUncommon=0.30,pRare=0.15,pLegendary=0.05).:The claimed model is notcorrect (at least onecategory’s true proportiondiffers from the claim).
Subtract 1 from the number of categories to find df With 4 rarity categories and a fixed total of 100 boxes, 3 categories are free to vary.
df=4−1=3
Find the right-tail p-value from the χ2 table or calculator On the df=3 row of the χ2 table, 3.28 is smaller than the first cutoff on the left (4.11 for tail probability 0.25), and χ2cdf gives the exact right-tail area.
Compare the p-value to α=0.05 to conclude in context Because a mismatch score of 3.28 or higher happens about 35% of the time by random chance when the model is true, we fail to reject H0 and do not have convincing evidence against the developer's claimed drop rates.
p-value=0.35>0.05⟹Fail to reject H0
As a larger mismatch pushes χ2 further to the right along the df=3 curve, the shaded right-tail p-value shrinks past the cutoffs on the df=3 row of the χ2 table (4.11 for 0.25,6.25 for 0.10,7.81 for 0.05, and 11.34 for 0.01).
A bigger chi-square value moves you further right on the curve, leaving a smaller tail area and passing fixed cutoffs in the table.
3
Running a Goodness of Fit Test When n Isn't 100
In a sample of 100 boxes, the expected counts matched the claimed percentages directly. For any other sample size n, we find each category's expected count by multiplying n by that category's claimed proportion pi.
After a game update, players open n=150 loot boxes to test the same claimed model (50% Common, 30% Uncommon, 15% Rare, 5% Legendary) and observe 90 Common, 45 Uncommon, 12 Rare, and 3 Legendary.
Running a Goodness of Fit Test When n Isn't 100
For each category, the expected count is the average count we would get in the long run across many samples of size n:
E=sample sizen⋅claimed proportionpi
Text solution
Multiply n=150 by each claimed proportion pi without rounding Keeping the decimals gives the exact expected counts so all four still add up to 150.
Compute each category's contribution E(O−E)2 and add them to find χ2 Writing out each category's piece shows that the shortage of Rare items (12 observed vs. 22.5 expected) makes up 4.9 of the 10.6 total mismatch.
Find the right-tail p-value for df=4−1=3 and compare to α=0.05 On the df=3 row of the table, 10.6 lands between 9.84 (p=0.02, or 9.35 for p=0.025 if the table leaves out 0.02) and 11.34 (p=0.01), giving convincing evidence that the drop rates after the update do not fit the developer's model.
On a test, either the table interval or the calculator p-value gets full credit as long as you write down df=3 with your work.
4
Conditions for a Chi-Square Goodness of Fit Test
Before we can trust the smooth χ2 curve to give an accurate p-value, we need to check that the data are raw counts from a random, independent sample and that every category's expected count is large enough for the curve to work well.
Conditions for a Chi-Square Goodness of Fit Test
Use actual counts, never percentages. Percentages hide the sample size by treating any sample as if n=100. When a model is truly off by 8% on Commons, plugging in raw counts makes χ2 grow with a larger sample size so we can tell the model is wrong:
For n=100:50(58−50)2For n=10,000:5,000(5,800−5,000)2=5082=1.28=5,0008002=128
Large Counts (E≥5 in every category). Dividing by an expected count under 5 makes a miss of just one or two items give a huge χ2 piece, so the smooth curve no longer gives accurate probabilities:
E=n⋅pi≥5for every category
Both loot box tests passed because their smallest expected counts were E=5 and E=7.5, even though both samples had an observed count of O=3 in Legendary.
Random and Independent (10% condition). The data must come from a random sample or randomized experiment where observations are independent. When sampling without replacement, the sample size n can be at most 10% of the population.
Key concepts
Use a Chi-Square Goodness of Fit test when you have one categorical variable from a single sample and want to test whether the observed counts across all categories fit a claimed model of proportions (such as testing all 4 loot box rarity tiers at once). If a question only asks about a single category's proportion (such as testing only whether Legendary items drop 5% of the time), use a one-sample z-test for a proportion instead.
Before computing the statistic, check that the data are raw counts from a random, independent sample (n≤10% of the population if sampled without replacement) and that every category's expected count is at least 5:
E=n⋅pi≥5(for n=150 and pLegendary=0.05,E=7.5≥5 passes even when O=3)
Keep expected counts unrounded, divide each category's squared gap by E, and add the pieces across all categories:
χ2=∑E(O−E)2,df=Categories−1
When H0 is true, random variation produces an average score of χ2≈df (like χ2=3.28 with df=3, which gives p-value≈0.35>0.05 and fails to reject H0). Only mismatches that push χ2 far above df into the right tail (like χ2=10.6 with df=3, where χ2cdf(10.6,1000,3)≈0.014<0.05) give convincing evidence to reject H0.
5
Practice
1
A candy company claims its bags contain 40% red, 30% blue, 20% green, and 10% yellow candies. From a random sample of n=85 candies, you observe 39 red, 24 blue, 17 green, and 5 yellow. Find the expected count for each color, then compare red and yellow: which color has the larger raw gap ∣O−E∣, and which contributes more to the χ2 statistic?
After finding E=85⋅pi for each color, compare what you divide each squared gap (O−E)2 by.
Multiply n=85 by each claimed proportion to get the unrounded expected counts:
Red has the larger raw gap (5 vs. 3.5), but yellow contributes nearly twice as much to χ2 because its squared gap is divided by a much smaller expected count (8.5 vs. 34): 0.74 for red and 1.44 for yellow.
2
A six-sided die is claimed to land on each face (1 through 6) with probability 61. In 120 rolls, the observed counts for faces 1 through 5 are 18,23,19,21, and 16. Find the observed count for face 6, state the degrees of freedom, and explain how comparing the χ2 statistic to the degrees of freedom tells you right away whether the die looks fair.
The 6 observed counts must add up to n=120. Once you compute χ2, what is the average χ2 score for a fair die with df=6−1?
Subtract the first five faces from the total of 120 rolls to find the remaining count for face 6:
Face 6 observed count=120−(18+23+19+21+16)=120−97=23
Because only 5 of the 6 categories are free to vary once the total is fixed at 120:
df=6−1=5
(Watch out: using the sample size to get df=120−1=119 confuses the Chi-Square test with a one-sample t-test, and leaving Face 6 out of the sum gives χ2=2031=1.55.)
When the fair-die model is true, random chance produces an average χ2 equal to df=5, so a score of 2.00 is completely ordinary and gives no evidence against the die being fair: χ2=2.00.
3
A geneticist models pea plant offspring as 56.25% purple-smooth, 18.75% purple-wrinkled, 18.75% white-smooth, and 6.25% white-wrinkled. A random sample of 160 plants gives 75 purple-smooth, 39 purple-wrinkled, 30 white-smooth, and 16 white-wrinkled. State the hypotheses, compute χ2 and df, find the p-value both as an interval from the df=3 row of the χ2 table (cutoffs: 4.11 for 0.25,6.25 for 0.10,7.81 for 0.05,9.35 for 0.025,9.84 for 0.02,11.34 for 0.01) and using a calculator command, and state your conclusion at α=0.05.
Watch out: dividing the squared gaps by the observed counts 75,39,30,16 instead of the expected counts 90,30,30,10 gives χ2≈7.33, which falls below the 7.81 cutoff and flips the conclusion.
Convert each percentage to a decimal before multiplying by n=160, then find where your χ2 total sits along the df=3 cutoffs.
Hypotheses
H0Ha:The offspring proportionsmatch the claimed model (56.25%,18.75%,18.75%,6.25%).:The claimed model is notcorrect (at least onecategory’s true proportiondiffers from the claim).
(Watch out: dividing the squared gaps by the observed counts 75,39,30,16 instead of the expected counts 90,30,30,10 gives χ2≈7.33, which falls below the 7.81 cutoff and flips the conclusion.)
P-value and conclusion On the df=3 row, 8.80 falls between 7.81 (p=0.05) and 9.35 (p=0.025):
Because p-value≈0.032<0.05, we reject H0: there is convincing evidence that the pea plant offspring types do not fit the claimed model at p-value≈0.032.
4
A transit agency claims that commuter arrivals across five station gates follow the model: Gate A 35%, Gate B 25%, Gate C 20%, Gate D 12%, and Gate E 8%. An inspector takes a random sample of 50 of the station's 12,000 daily commuters and observes 19 at Gate A, 14 at Gate B, 11 at Gate C, 4 at Gate D, and 2 at Gate E. The inspector says the Chi-Square Goodness of Fit test cannot be used because Gates D and E have observed counts under 5. Check all conditions for the test to see if the inspector is right.
Watch out: rejecting Gate D because OD=4<5 checks the wrong column, while stopping at ED=6≥5 misses that Gate E still fails.
Which column, Observed O or Expected E=50⋅pi, does the Large Counts condition actually check?
Random and Independent (10% condition) The data are raw counts from a random sample of 50 commuters, and 50≤10% of 12,000 (1,200), so both conditions pass.
Large Counts (E≥5) The inspector is checking the wrong column. Large Counts requires every Expected count, not every Observed count, to be at least 5. Multiplying n=50 by each claimed proportion gives:
(Watch out: rejecting Gate D because OD=4<5 checks the wrong column, while stopping at ED=6≥5 misses that Gate E still fails.)
Gate D passes (ED=6≥5 even though OD=4), but the test still cannot be run because Gate E has an expected count of EE=4<5.
5
A music app claims its shuffle plays 50% songs from the 2010s, 30% from the 2000s, and 20% from the 1990s. A listener observes 55% from the 2010s, 27% from the 2000s, and 18% from the 1990s, and plugs those percentages directly into the formula: χ2=50(55−50)2+30(27−30)2+20(18−20)2=1.00. Explain what rule this breaks, compute the true χ2 statistic if those percentages came from a random sample of n=200 songs versus n=1,000 songs, and explain why the conclusion changes.
Watch out: converting observed percentages to counts 110,54,36 while leaving expected values as percentages 50,30,20 gives χ2=104.0 instead of 2.00.
Convert the percentages into actual counts of songs for n=200 and for n=1,000 before computing χ2.
Plugging percentages into the χ2 formula breaks the raw counts condition by treating the sample as if n=100.
For n=200 songs (df=3−1=2) Observed counts are 110,54,36 and expected counts are 100,60,40:
(Watch out: converting observed percentages to counts 110,54,36 while leaving expected values as percentages 50,30,20 gives χ2=104.0 instead of 2.00.) With df=2,χ2cdf(2.00,1000,2)≈0.37>0.05, so we fail to reject H0.
For n=1,000 songs (df=3−1=2) Observed counts are 550,270,180 and expected counts are 500,300,200:
With df=2,χ2cdf(10.00,1000,2)≈0.0067<0.05, so we reject H0.
A 5% gap across 200 songs is easily explained by random variation (χ2=2.00,p-value≈0.37), whereas the same percentage gap across 1,000 songs gives convincing evidence against the claimed model (χ2=10.00,p-value≈0.0067).