Chi-square tests explained
When your outcome is a category rather than a measurement (yes or no, which product, which diagnosis), you can’t compare means. Instead you compare counts. Chi-square (χ²) tests do this by asking how far the counts you observed are from the counts you’d expect if nothing interesting were going on.
The chi-square statistic
Both chi-square tests use the same formula, summed over every category or cell:
χ² = Σ (Observed − Expected)² / Expected
If the observed counts are close to the expected ones, χ² is small. Large gaps make it large. The p-value comes from the chi-square distribution with the right degrees of freedom.
Test 1: goodness of fit
Question: does one categorical variable follow a particular set of proportions?
- Are the four blood types present in the proportions found in the general population?
- Do customers choose equally among five flavours?
- Does a die land on each face equally often?
Example: a 1974 survey of 592 statistics students at the University of Delaware recorded hair colour: 108 black, 286 brown, 71 red and 127 blond. If all four colours were equally common, we’d expect 148 of each. The test gives χ²(3) = 182.53, p < .001: the colours are clearly not equally common (no surprise, with brown far ahead).
Degrees of freedom = number of categories − 1.
Test 2: test of independence
Question: are two categorical variables related, or independent of each other?
- Is smoking status related to region?
- Does treatment group affect whether patients recover (yes/no)?
- Is hair colour associated with eye colour?
The data go in a contingency table (crosstab), with one variable as rows and the other as columns. Expected counts come from the row and column totals. For each cell, expected = row total × column total / grand total.
Example: in the same 592 students, hair colour and eye colour are strongly related, χ²(9) = 138.29, p < .001, Cramér’s V = .28. Blond hair goes with blue eyes much more often than independence would predict. The full lesson works through this example, including how to find which cells drive the result.
Degrees of freedom = (rows − 1) × (columns − 1).
Which one do you need?
| Goodness of fit | Test of independence | |
|---|---|---|
| Variables | One categorical | Two categorical |
| H₀ | The proportions match the expected ones | The two variables are independent |
| Expected counts from | Proportions you specify (equal, or from theory or past data) | The table’s own row and column totals |
| df | k − 1 | (r − 1)(c − 1) |
| Effect size | Cohen’s w | Cramér’s V (phi for 2×2) |
Assumptions (both tests)
- Counts, not percentages or means. Each case is counted once.
- Independent observations. Each person appears in exactly one cell. For the same people measured twice (yes/no before and after), use McNemar’s test.
- Large enough expected counts. The usual rule is at least 5 expected per cell. If that fails, use Fisher’s exact test, which calculates the p-value exactly instead of relying on the chi-square approximation. Fisher’s test works for larger tables too, not just 2×2.
The rule is about expected counts, not observed ones. A cell can have an observed count of 0 and still be fine, if its expected count is high enough.
Common mistakes
- Running chi-square on percentages. The test needs raw counts. 60% of 10 people is very different evidence from 60% of 1,000.
- Splitting a continuous variable into categories just to use chi-square. You lose information and power. Use a t-test, ANOVA or logistic regression instead.
- Ignoring small expected counts.
- Stopping at “significant” without saying which categories differ, or how strong the association is.
Analytika checks expected cell counts before it chooses a test. If they’re too small, it switches to Fisher’s exact test, and it reports Cramér’s V and the cells that stand out. Our free chi-square calculator runs both tests on a table you type in.
References
- Snee, R. D. (1974). Graphical display of two-way contingency tables. The American Statistician, 28(1), 9–12.
More in Categorical data
Try it with the free tools
Run this on your own data
Upload a dataset and ask your question in plain English. Analytika checks the assumptions, picks the test, runs it, and writes the results up in APA format.