Chi-Square Tests
Imagine you roll a die 120 times and get a 6 only 12 times when you expected about 20. Is the die loaded, or is this just the ordinary luck of the draw? Or suppose a survey shows that men and women seem to prefer different phone brands — is that a real difference in the population, or noise in your sample? Chi-square tests are the standard tool for answering exactly this kind of question: they measure whether the counts you actually observed differ from the counts you would expect under some hypothesis by more than random chance can comfortably explain.
The beauty of the chi-square () family is that it works with categorical data — colors, brands, blood types, yes/no answers — where you cannot compute a meaningful average. Instead of comparing means, you compare frequencies, and a single elegant formula turns the total mismatch into one number you can look up in a table. This page teaches you the two most important versions and, just as importantly, why they work.
Learning Objectives
- Distinguish the chi-square goodness-of-fit test from the test of independence.
- Compute expected counts from a hypothesis or from a contingency table's margins.
- Calculate the Pearson statistic and interpret it against degrees of freedom.
- Build and read a contingency table and state precise hypotheses.
- Decide whether to reject the null hypothesis using a critical value or p-value.
- Recognize the assumptions and common pitfalls that invalidate the test.
Quick Answer
A chi-square test compares observed frequencies () to expected frequencies () using the statistic A large value means the observed data are far from what the null hypothesis predicts. The goodness-of-fit test checks whether one categorical variable follows a claimed distribution; degrees of freedom are (categories minus one). The test of independence checks whether two categorical variables in a contingency table are related; degrees of freedom are . You compare your computed to a critical value from the chi-square distribution (or find a p-value); if exceeds the critical value, you reject the null. The test assumes independent observations and expected counts of at least 5 in most cells.
Where It Came From
Before 1900, statisticians had no rigorous way to judge whether a set of counts agreed with a theory. People eyeballed tables and argued. The breakthrough came from Karl Pearson, working at University College London, who in 1900 published the paper "On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling." The clumsy title hides a revolutionary idea.
Pearson's motivating problem was concrete. Scientists of his era — including gamblers, biologists studying inheritance, and astronomers checking measurement errors — kept asking the same question in different clothing: my data fell into these categories in these proportions; is that consistent with the distribution my theory predicts, or is the discrepancy too large to be chance? Pearson wanted a single, general number that summarized the total deviation and whose behavior under pure randomness was known mathematically, so that "surprising" could be defined precisely rather than felt intuitively.
His insight was that each cell's squared deviation, scaled by its expected size, could be summed, and that this sum follows a predictable distribution — the chi-square distribution — when the null hypothesis is true. Dividing by matters: a deviation of 10 is enormous where you expected 5 but trivial where you expected 5000. Scaling puts every category on a fair footing. Later, R. A. Fisher refined the crucial detail of degrees of freedom in the 1920s, correcting Pearson's original count when parameters are estimated from the data. Together their work gave us the first widely used hypothesis test for categorical data, and it remains a workhorse of science more than a century later.
Observed vs Expected Counts and the Statistic
Every chi-square test rests on two lists of numbers:
- Observed counts () — what your data actually show.
- Expected counts () — what the null hypothesis predicts on average for a sample of your size.
The Pearson statistic aggregates the mismatch:
If observed and expected agree perfectly, every term is 0 and . The worse the fit, the larger grows. Because we square the deviations, over- and under-counts do not cancel, and large individual gaps are penalized heavily.
Worked example (goodness-of-fit). A candy company claims its bags are red, blue, green, and yellow. You open a bag of 200 candies and count: red 50, blue 55, green 45, yellow 50. Does the bag match the claim?
Expected counts for : red , blue , green , yellow .
| Color | |||
|---|---|---|---|
| Red | 50 | 60 | |
| Blue | 55 | 60 | |
| Green | 45 | 40 | |
| Yellow | 50 | 40 |
Summing: .
Degrees of freedom . The critical value at significance level is . Since , we fail to reject the null: the data are consistent with the company's claimed color distribution.
The Goodness-of-Fit Test
Use goodness-of-fit when you have one categorical variable and a hypothesized distribution for it. The steps are always the same:
- State (the data follow the claimed distribution) and (they do not).
- Compute expected counts: , where is the hypothesized probability of category .
- Compute .
- Find degrees of freedom: (subtract one more for each parameter you estimated from the data).
- Compare to the critical value, or compute a p-value, and conclude.
Worked example (uniform die). You suspect a die is fair and roll it 60 times, getting: 1→8, 2→13, 3→9, 4→7, 5→11, 6→12. Under fairness each face is expected times.
Numerators: , so . With , the critical value is . Because , there is no evidence the die is unfair.
Contingency Tables and the Test of Independence
When you have two categorical variables measured on the same subjects, you cross-tabulate them into a contingency table (rows = categories of one variable, columns = categories of the other). The test of independence asks: are the two variables related, or independent?
The clever part is computing expected counts. Under independence, the expected count of a cell is:
This follows directly from the multiplication rule for independent events: if row and column classifications are independent, the probability of landing in a given cell is the product of the marginal probabilities, and multiplying by gives the expected frequency.
Worked example. A researcher surveys 200 people about whether they exercise regularly and whether they sleep well:
| Sleeps well | Sleeps poorly | Row total | |
|---|---|---|---|
| Exercises | 60 | 20 | 80 |
| Does not | 50 | 70 | 120 |
| Col total | 110 | 90 | 200 |
Expected counts:
- Exercises & sleeps well:
- Exercises & sleeps poorly:
- Does not & sleeps well:
- Does not & sleeps poorly:
Now the statistic:
Term by term: ; ; ; . Sum: .
Degrees of freedom . The critical value is . Since , we reject independence: exercise and sleep quality are significantly associated in this sample.
Real-World Applications
- Genetics. Testing whether offspring ratios match Mendelian predictions (e.g. a 9:3:3:1 dihybrid cross) is a classic goodness-of-fit application.
- Medicine and epidemiology. Contingency tables compare treatment vs placebo against recovered vs not, testing whether outcome depends on treatment.
- Marketing. Firms test whether product preference is independent of age group or region before targeting campaigns.
- Quality control. Manufacturers check whether defect counts across shifts or machines match an expected uniform rate.
- Social science and polling. Researchers test associations between demographic categories and opinions, such as voting preference by education level.
Common Mistakes
-
Using percentages or proportions instead of raw counts. Why wrong: the formula is calibrated for actual frequencies; feeding it percentages destroys the dependence on sample size and inflates or deflates the statistic arbitrarily. Correction: always compute from raw counts, never from rates.
-
Ignoring the expected-count rule. Why wrong: the chi-square distribution is only an approximation to the true distribution of the statistic, and it breaks down when expected cell counts are small (a common rule: no cell below 5, and all above 1). Correction: combine sparse categories or use Fisher's exact test for small samples.
-
Miscounting degrees of freedom. Why wrong: using instead of , or forgetting to subtract for estimated parameters, points you to the wrong critical value and can flip your conclusion. Correction: goodness-of-fit uses ; independence uses .
Comparison and Connections
Chi-square tests are often confused with each other and with other tests. The table clarifies:
| Test | Data | Question | Degrees of freedom |
|---|---|---|---|
| Goodness-of-fit | One categorical variable | Does it match a claimed distribution? | |
| Test of independence | Two categorical variables, one sample | Are the variables associated? | |
| Test of homogeneity | Same table structure, several populations sampled | Do groups share the same distribution? |
Independence and homogeneity use identical arithmetic but differ in study design (one sample cross-classified vs several fixed-size samples). Chi-square differs from the t-test and ANOVA, which compare means of numerical data; chi-square compares frequencies of categories. The distribution itself is also the sum of squared standard normal variables, which is why it appears throughout statistics, including in variance estimation.
Practice Questions
Recall
State the Pearson chi-square formula and explain what and stand for. Answer: , where is the observed count in a category and is the expected count under the null hypothesis.
Understanding
Why do we divide each squared deviation by rather than just summing ? Answer: Dividing by scales each deviation relative to how large it was expected to be, so a gap of 10 counts as major where 5 were expected but minor where 5000 were expected. It puts categories of different sizes on a comparable footing.
Application
A geneticist expects a 3:1 ratio of tall to short plants. In 160 plants she observes 108 tall and 52 short. Compute and test at (, critical value ). Answer: Expected: tall , short . . Since , reject the 3:1 hypothesis.
Analysis
Your contingency test gives with (critical value at ). A colleague says the result is "clearly significant." How should you respond? Answer: The result barely exceeds the critical value, so it is significant at but only marginally. The p-value is just under , meaning the evidence is weak; one should report the p-value, check expected-count assumptions, and avoid overstating the strength of the association.
FAQ
Is the chi-square test one-tailed or two-tailed? The chi-square test is inherently one-tailed on the right. Any deviation — in either direction — makes the squared terms positive and pushes up, so we only ever reject in the upper tail.
What if my expected counts are less than 5? The approximation becomes unreliable. Combine adjacent categories, collect more data, or use Fisher's exact test (especially for 2×2 tables), which computes exact probabilities.
Does a significant result mean the variables cause each other? No. Chi-square detects association, not causation. A significant test of independence tells you the variables are statistically related, but a confounding factor could drive both.
What is the difference between the test of independence and homogeneity? The math is identical. Independence starts from one sample cross-classified by two variables; homogeneity starts from several separate samples compared on one variable. The interpretation and sampling design differ, not the computation.
How large does have to be to reject the null? There is no universal threshold — it depends on the degrees of freedom. Look up the critical value for your and chosen , or compute the p-value. More degrees of freedom require a larger to reach significance.
Quick Revision
- Statistic: ; always uses raw counts.
- Goodness-of-fit: ; .
- Independence: ; .
- Reject when critical value (right tail only).
- Assumptions: independent observations, expected counts in (nearly) all cells.
- Detects association, not causation; introduced by Karl Pearson in 1900.
Related Topics
Prerequisites
Related Topics
- Hypothesis Testing (significance levels, p-values, and critical values)
- Probability Distributions (the chi-square distribution)
Next Topics
- Analysis of Variance (ANOVA) for comparing means across groups
- Correlation and regression for numerical associations