Statistics 🔬 Confidence & Inference

Chi-Square Tests

Turning a two-way table's association into a hypothesis test — goodness-of-fit, independence, and homogeneity, all from one statistic.

Intro StatisticsCollege intro statistics level
Chi-Square Tests — illustration
Illustrative image (AI-generated).
💡
The big idea: The two-way tables lesson showed how to describe association by comparing conditional distributions. Chi-square tests turn that comparison into inference: compare observed counts to the counts expected under a null hypothesis, and ask whether the mismatch is bigger than chance alone would produce. Three different designs — goodness-of-fit, independence, and homogeneity — all boil down to the exact same statistic and the exact same logic; only the sampling design and the conclusion differ.
🎯 By the end, you'll be able to
  • Compute the chi-square statistic from observed and expected counts
  • State the correct degrees of freedom for goodness-of-fit versus independence and homogeneity
  • Explain why the chi-square test is upper-tailed
  • Distinguish a test of independence from a test of homogeneity by design, not by arithmetic
  • Check the expected-count condition and know what to do when it fails
📎 You should already know
  • Two-way tables: marginal, joint, and conditional distributions
  • Hypothesis testing logic: H0, Ha, and the p-value
  • Reading a two-way table's row and column totals

From association to inference

The two-way tables lesson compared conditional distributions to describe association, with no hypothesis test attached. A chi-square test asks the inferential version of the same question: if the null hypothesis were true, would data like this be a surprising mismatch, or an ordinary one?

The same statistic supports three designs:

  • Goodness-of-fit: one sample, one categorical variable, compared against a hypothesized distribution across its categories.
  • Independence: one sample, cross-classified on two categorical variables, asking whether they are associated.
  • Homogeneity: several separate samples (group sizes fixed by the researcher), compared on one categorical variable, asking whether the groups share the same distribution.
\[ \chi^2 = \sum \frac{(O - E)^2}{E} \]
The chi-square statistic: for every cell, take observed minus expected, square it, divide by expected, then add up over all cells.
\[ E_{ij} = \frac{(\text{row}_i)(\text{col}_j)}{n} \]
In a two-way table, the expected count for a cell is its row total times its column total, divided by the grand total n — the same rule from the two-way tables lesson.
\[ \text{df}_{GOF} = k - 1 \qquad \text{df}_{indep,\ homog} = (r-1)(c-1) \]
Degrees of freedom: goodness-of-fit uses k minus 1, where k is the number of categories. Independence and homogeneity on an r-by-c table use (r minus 1) times (c minus 1).
⚠️ Two different degrees-of-freedom rules
WRONG: writing the goodness-of-fit df as (r − 1)(c − 1), or the independence df as k − 1. RIGHT: goodness-of-fit: df = k − 1. Independence and homogeneity on an r × c table: df = (r − 1)(c − 1). They are different formulas for different table shapes — do not swap them.
⚠️ Independence vs. homogeneity: design, not arithmetic
WRONG: "Tests of independence and homogeneity are interchangeable names." RIGHT: independence means one sample cross-classified on two variables — "are these two variables associated?" Homogeneity means several separate samples, with group sizes fixed by the researcher, compared on one variable — "do these groups share the same distribution?" The arithmetic is identical; the design and the conclusion differ.
⚠️ The condition is on expected counts
WRONG: "All observed counts must be at least 5." RIGHT: the condition is on the expected counts: every expected count must be at least 5. Observed counts may be 0. If some expected counts fall below 5, combine categories, collect more data, or use an exact test.
🎮 Interactive: expected counts, contributions, and the chi-square statistic LIVE
Edit the four cell counts a, b, c, d of a 2x2 table and watch the expected counts, each cell's contribution (observed minus expected, squared, divided by expected), the chi-square statistic, and the p-value update live. This simulation is limited to 2x2 tables, so degrees of freedom is always 1. An indicator shows whether the expected-count condition — every expected count at least 5 — is met.
⚠️ Chi-square is upper-tailed
WRONG: "Because the alternative is two-sided, split alpha between two tails." RIGHT: the chi-square test is inherently upper-tailed. Chi-square is never negative, and any departure from the expected counts — in either direction, for any cell — makes it larger. The whole alpha goes in the upper tail: p = P(chi-square with df degrees of freedom ≥ the observed statistic).
📝 Worked example: A survey cross-classifies 100 residents by region and opinion: North 30 favor / 20 oppose; South 15 favor / 35 oppose (row totals 50 and 50; column totals 45 favor and 55 oppose). Test H0: region and opinion are independent, against Ha: they are associated.
  1. Expected counts (row total times column total, over n = 100): North-favor = 22.5, North-oppose = 27.5, South-favor = 22.5, South-oppose = 27.5.
  2. Contribution, North-favor: \( (30 - 22.5)^2 / 22.5 = 7.5^2/22.5 = 2.5 \).
  3. Contribution, North-oppose: \( (20 - 27.5)^2 / 27.5 = 7.5^2/27.5 \approx 2.045 \).
  4. Contribution, South-favor: \( (15 - 22.5)^2 / 22.5 = 7.5^2/22.5 = 2.5 \).
  5. Contribution, South-oppose: \( (35 - 27.5)^2 / 27.5 = 7.5^2/27.5 \approx 2.045 \).
  6. Chi-square statistic: \( 2.5 + 2.045 + 2.5 + 2.045 \approx 9.091 \).
  7. Degrees of freedom: \( (2-1)(2-1) = 1 \). This gives a p-value of about 0.0026.
✓ All expected counts are at least 5, so the test is appropriate. With chi-square ≈ 9.09, df = 1, p ≈ 0.0026, which is far below alpha = 0.05: reject H0. There is evidence of an association between region and opinion. Because this is a survey, not an experiment with random assignment, this evidence supports association only — not that region causes the opinion.
⚠️ Reading the omnibus result correctly
WRONG (1): "A significant chi-square tells us which cell caused it." RIGHT: the omnibus test only signals that some departure from expected exists somewhere in the table. To see where, inspect the standardized residuals, (O − E) / √E, cell by cell. WRONG (2): "Chi-square proves the variables are dependent," or "failing to reject proves independence." RIGHT: a significant result is evidence of association; failing to reject means insufficient evidence of association — not proof that the variables are independent.
📝 Worked example: A die is rolled 60 times to test H0: the die is fair (each face equally likely), so the expected count is 10 rolls per face. The observed counts are 8, 9, 12, 8, 13, 10 for faces 1 through 6. Test H0 at alpha = 0.05.
  1. Expected count per face: 60/6 = 10 for each of the k = 6 categories.
  2. Contributions: \( (8-10)^2/10 = 0.4 \), \( (9-10)^2/10 = 0.1 \), \( (12-10)^2/10 = 0.4 \), \( (8-10)^2/10 = 0.4 \), \( (13-10)^2/10 = 0.9 \), \( (10-10)^2/10 = 0 \).
  3. Chi-square statistic: \( 0.4 + 0.1 + 0.4 + 0.4 + 0.9 + 0 = 2.2 \).
  4. Degrees of freedom: \( k - 1 = 6 - 1 = 5 \).
✓ All expected counts (10 each) are at least 5, so the test is appropriate. Chi-square = 2.2 with df = 5 is far below the critical value of about 11.07 at alpha = 0.05, so we fail to reject H0. This is insufficient evidence that the die is unfair — not proof that it is exactly fair.

Check your understanding

1. A goodness-of-fit test compares observed counts across k = 4 categories to a hypothesized distribution. What are the degrees of freedom?
Goodness-of-fit uses df = k - 1 = 4 - 1 = 3. The (r-1)(c-1) formula is for independence/homogeneity on a two-way table, not for goodness-of-fit.
2. A researcher fixes sample sizes of 50 patients per treatment group in advance, then records each patient's outcome (recovered / not recovered), comparing outcome distributions across groups. Which test is this?
Fixing the group sizes in advance and comparing one variable's distribution across those separate samples is the design for a homogeneity test, even though the arithmetic matches a test of independence exactly.
3. In a 2x2 table, three of the four expected counts are 6, 7, and 8, and the fourth expected count is 3. What should be done?
The expected-count condition requires every expected count to be at least 5. One cell at 3 violates it, regardless of the observed counts or the other three cells.
4. A chi-square test yields a large statistic and a small p-value. What is the most correct conclusion?
A significant chi-square is evidence of association, not proof of dependence, and it says nothing about causation without a randomized design. The chi-square test is upper-tailed, so alpha is not split between tails, and the p-value is P(data | H0), not P(H0).
✅ Key takeaways
  • Chi-square tests compare observed counts to expected counts under a null hypothesis: chi-square = sum of (observed - expected)^2 / expected.
  • Goodness-of-fit uses df = k - 1. Independence and homogeneity on an r x c table use df = (r - 1)(c - 1) — do not mix these up.
  • The expected-count condition (every expected count at least 5) applies to expected counts, never to observed counts.
  • The chi-square test is upper-tailed: any departure from expected, in any direction, pushes the statistic higher, so the whole alpha sits in the upper tail.
  • Independence (one sample, two variables) and homogeneity (several fixed-size samples, one variable) share identical arithmetic but differ in design and conclusion.
  • A significant chi-square is evidence of association, located by inspecting residuals cell by cell — never proof of causation, and a failure to reject is not proof of independence.