A chi-square goodness-of-fit test compares the counts you observed to the counts you expected.
A big chi-square statistic (the total mismatch) means observed and expected differ a lot. (e.g. stat = 25)
Example: rolling a die 60 times, expecting 10 of each face. (e.g. obs 18 sixes vs 10)
A small p-value (low chance it is just luck) suggests the data do not match the expected pattern. (e.g. p = 0.02)
Degrees of freedom (the count of categories free to vary) equal the number of categories minus one. (e.g. 6 faces, df = 5)
Definition
χ2=i∑Ei(Oi−Ei)2
Oi
the observed count in each cell
Ei
the expected count if the guess is true
χ2
the test statistic (bigger = worse fit)
In plain words
You roll a die 100 times expecting each face about the same. If one face shows up way too often, this test notices the die might be loaded.
Where you'd use it
You roll a 4-sided die 100 times expecting 25 of each face but get 22, 28, 30, 20. chisq.test finds the gaps small, giving a high p-value (around 0.5), so the die looks fair.
A candy maker claims colours come 1/4 each. In a bag of 200 you count 40, 70, 50, 40; a chi-square test with a p-value below 0.05 would be evidence the real mix differs from the claim.
You predicted survey answers split evenly across 4 options but observed lopsided counts. Feeding the observed and expected counts into chisq.test tells you whether the mismatch is real or just chance.
Worked example 14
Expected 25, 25, 25, 25 but you rolled 22, 28, 30, 20. The gaps are small, so it looks fair (high p-value).
Frequently asked questions
What does chisq.test test?
Whether two categorical variables are associated (independence).
What if the cell counts are small?
The χ² approximation is unreliable when expected counts drop below ~5 — use Fisher’s exact test.