Back to the Introduction to Statistics outline The Course Maker
Introduction to Statistics outline
Week 16 · Chapter

Chapter 16 — Chi-Square Tests for Categorical Data

Introduction to Statistics Generic evergreen edition

Course: Introduction to Statistics (18-week generic edition)
The module's primary reading · ~30–40 minutes · Objective 9 · Read this before (or alongside) the week's lecture — everything else in the module builds on it. This chapter carries the week's chi-square critical-value mini table; keep it handy for every problem you work.


Why you already care about this

Open a bag of candy whose label promises a mix — say, equal parts of four colors — and count. The counts will not match the label. They never do. Twenty-six of one color, twenty-two of another, and a suspicious shortage of your favorite. Here is the question that separates a shrug from a discovery: is that mismatch just the ordinary jitter of random sampling, or is the label wrong?

You have spent five weeks building tests for claims about means (Weeks 11 and 14) and proportions (Weeks 12 and 15) — one number at a time. But so much of the real world arrives as a whole table of category counts at once: colors in a mix, customers by weekday, T-shirt orders by size, pets by housing type. This week you get the one machine that judges an entire table in a single move: the chi-square test (χ², pronounced "kai-square" — the Greek letter chi, not "x").

By the end of this chapter you will be able to: build a table of expected counts from a claim (or from independence); compute the chi-square statistic and its degrees of freedom; run a complete goodness-of-fit test by hand and read a test of independence with technology's help; check the conditions that make either test trustworthy; and — the professional's move — read the contributions to χ² to say where a table went off-script, not just that it did. The memory hook for the whole week: one number for the whole table's drift.

Where we are

The pieces are already on your bench. Week 4 taught you to describe a two-way table — marginal and conditional distributions, association by eye. Week 13 taught you what a hypothesis test means: a null hypothesis, a statistic measuring drift from it, and a verdict stated in careful words. Weeks 14 and 15 ran that logic on means and proportions. This week the Week 4 table and the Week 13 logic finally meet — the describing table gets its own test. Next week, regression completes the toolkit.

1. Observed versus expected: the two tables that argue

Every chi-square test is an argument between two tables.

  • The observed counts (O) are what you actually tallied — real data, one count per category.
  • The expected counts (E) are what the claim being tested predicts you would count, on average, in a sample your size.

For this section's setting — one categorical variable against a claimed distribution — the recipe is a single multiplication:

Expected count for a category = n × the claimed proportion for that category.

Two habits to install immediately. First, expected counts are long-run averages, so decimals are fine — an expected count of 22.5 is not an error, and you never round expected counts to force them to look like data. Second, chi-square runs on raw counts, never on percentages. Thirty percent of 40 items and thirty percent of 4,000 items are wildly different amounts of evidence; converting to percents amputates the sample size, which is the very thing that powers the test.

Worked example (the candy bag). A candy company claims its fruit-drop mix is 25% each of red, orange, green, and purple. A quality check counts a 100-candy sample and finds 30 red, 25 orange, 25 green, 20 purple. The claim predicts 100 × 0.25 = 25 of each color. So the argument is: observed 30/25/25/20 versus expected 25/25/25/25. Two categories sit exactly on target; two drifted by 5. Is that drift meaningful? To answer, we need to compress the whole disagreement into one number.

2. The chi-square statistic: measuring a whole table's drift

Here is the number, built so that every design choice has a reason:

χ² = Σ (O − E)² ⁄ E — for every category: take the gap between observed and expected, square it, divide by the expected count — then add up all the terms.

  • The gap (O − E): positive and negative gaps both matter — but across a table the gaps always sum to zero (the counts and the expected counts both total n), so adding raw gaps tells you nothing.
  • Squared: kills the canceling, and — just like the standard deviation back in Week 3 — makes large gaps count extra.
  • Divided by E: scales the surprise. Being 5 off when you expected 10 is shocking; being 5 off when you expected 1,000 is rounding noise.
  • Summed: one number for the whole table. A χ² of 0 means a perfect match; bigger means more total drift. And because every term is a square divided by a positive number, χ² can never be negative — if yours is, you made an arithmetic slip, full stop.

Worked example, continued. For the candy bag:

  • Red: (30 − 25)² ⁄ 25 = 25 ⁄ 25 = 1.0
  • Orange: (25 − 25)² ⁄ 25 = 0
  • Green: (25 − 25)² ⁄ 25 = 0
  • Purple: (20 − 25)² ⁄ 25 = 25 ⁄ 25 = 1.0

χ² = 1.0 + 0 + 0 + 1.0 = 2.0. The bag's entire disagreement with the label measures 2.0. Big or small? On its own, unanswerable — a statistic means nothing until you know what luck alone typically produces. That ruler is next.

3. Degrees of freedom, critical values, and the verdict

Even when a claim is exactly true, random sampling makes χ² bounce above zero — some drift is guaranteed. The chi-square distribution describes that luck-only bounce, and its shape depends on one knob: the degrees of freedom (df). For a goodness-of-fit test,

df = k − 1, where k is the number of categories.

(Why minus one? Once the sample size is fixed, the last category's count isn't free — fill in all but one count and the final one is dictated.)

To turn χ² into a verdict, compare it with a critical value: the point that luck alone exceeds only 5% (or 1%) of the time. This course runs on a four-row mini table — embedded here, in the tutorial, and in the assignment coach; anything beyond it, technology supplies.

The table below gives the chi-square critical values used all week: reject H₀ when χ² exceeds the entry.

df Critical value, 5% level Critical value, 1% level
1 3.841 6.635
2 5.991 9.210
3 7.815 11.345
4 9.488 13.277

The goodness-of-fit test, assembled. ① State hypotheses — H₀: the claimed distribution is correct; Hₐ: it is not (at least one category's true proportion differs). ② Build the expected counts. ③ Check the conditions (Section 4). ④ Compute χ². ⑤ Find df = k − 1. ⑥ Compare with the critical value — or read technology's p-value. ⑦ Conclude in context, in careful words.

Worked example, finished. The candy bag: df = 4 − 1 = 3, so the 5% critical value is 7.815. Our χ² = 2.0 < 7.815fail to reject H₀. A 30/25/25/20 split is squarely inside ordinary luck for a true 25/25/25/25 mix. (Technology agrees: the p-value is about 0.57 — more than half of all honest 100-candy samples drift at least this much.)

Now the sentence that earns or loses the point. Say: "the data are consistent with the claimed mix." Do not say: "we proved the mix is 25/25/25/25." Week 13's law still governs: fail to reject ≠ accept. Many nearby mixes — 26/25/25/24, say — would survive the same test. A not-guilty verdict is not a certificate of innocence.

Misconception check. "My χ² is 2.0 — that sounds small, so we're probably fine," or "6 sounds big." Chi-square has no universal scale. A χ² of 6 rejects at df = 1 (critical value 3.841) and fails to reject at df = 3 (7.815). The df-matched table entry — or a p-value — is the only legitimate referee.

4. The conditions (check before you trust any verdict)

Three requirements, checked before the test:

  1. Random (or representative) data. Week 1 never stops applying — a biased sample produces a beautifully computed test of the wrong thing.
  2. Counts. Raw category counts only — never percentages, never averages.
  3. Every expected count is at least 5. Small expected counts make the luck-only distribution misbehave, so the table's verdicts stop being trustworthy. Cures: collect a bigger sample, or merge sparse categories.

Read condition 3 again, carefully: it polices expected counts, not observed ones. An observed count of 0 is perfectly legal data (this week's data lab hands you four observed zeros); an expected count of 3 breaks the rule. Quick example: a claimed 60% milk / 30% dark / 10% white chocolate mix, checked with a bag of 30, gives expected counts 18, 9, and 3 — the white-chocolate cell fails. The smallest sample that fixes it: n = 50, making the smallest expected count 50 × 0.10 = 5 exactly.

Misconception check. "Some of my observed counts are tiny — even zero — so the test is invalid." No: the ≥ 5 condition is about expected counts. Observed zeros are information; expected counts are the requirement.

5. Reading the contributions: where did the table go off-script?

When the alarm does ring, χ² can also tell you where to look — if you read its parts. Each category's term, (O − E)² ⁄ E, is that category's contribution to chi-square. The statistic is their sum, so the largest contributions mark the categories doing the most to set off the alarm. Reading them turns a verdict into a diagnosis.

Worked example (a week of store traffic). A hardware store owner claims weekday customer traffic is even across the five weekdays (Mon–Fri) and staffs the register identically each day. A random sample of 200 receipts: Mon 28, Tue 34, Wed 38, Thu 44, Fri 56. Expected: 200 ÷ 5 = 40 per day (random receipts ✓, counts ✓, all E = 40 ≥ 5 ✓).

  • Mon: (28 − 40)² ⁄ 40 = 144 ⁄ 40 = 3.6
  • Tue: (34 − 40)² ⁄ 40 = 36 ⁄ 40 = 0.9
  • Wed: (38 − 40)² ⁄ 40 = 4 ⁄ 40 = 0.1
  • Thu: (44 − 40)² ⁄ 40 = 16 ⁄ 40 = 0.4
  • Fri: (56 − 40)² ⁄ 40 = 256 ⁄ 40 = 6.4

χ² = 3.6 + 0.9 + 0.1 + 0.4 + 6.4 = 11.4, df = 5 − 1 = 4. Since 11.4 > 9.488, reject H₀ at the 5% level — the even-traffic claim doesn't survive. Notice the nuance: 11.4 < 13.277, so at the strict 1% level this evidence would not clear the bar (technology's p ≈ 0.022). Significance is a dial, not a switch — the Week 13 conversation about α, still alive.

Now the diagnosis: Fri (6.4) and Mon (3.6) contribute 10.0 of the 11.4. Fri runs hot, Mon runs quiet, and midweek is almost exactly on script. The sensible action isn't "distrust every day" — it's "re-staff Fri and Mon."

Misconception check. "Fri had the biggest count, so of course it's the biggest problem." Contributions rank surprise, not size. A huge count sitting near its expected value contributes almost nothing, while a modest count far from expectation can top the list. Rank categories by (O − E)² ⁄ E — never by O. (In this example the biggest count and biggest contribution happen to coincide; the quiz will hand you a table where they don't.)

6. The test of independence: two variables, one table

Week 4's two-way tables return, all grown up. There, you described association between two categorical variables. The test of independence asks the inference version: H₀: the two variables are independent (no association) versus Hₐ: they are associated.

With no claimed mix to lean on, where do expected counts come from? From independence itself. If the variables were unrelated, each cell should hold its row's share of its column:

Expected cell count = (row total × column total) ⁄ grand total

Worked example (pets × housing). A city survey of 200 residents records each household's pet type and housing type.

The two-way table below shows the observed counts, with row and column totals.

Observed House Apartment Total
Dog 72 48 120
Cat 28 52 80
Total 100 100 200

Expected count for dog–house: (120 × 100) ⁄ 200 = 60. The same machinery fills the rest: dog–apartment 60, cat–house 40, cat–apartment 40 — all comfortably ≥ 5. (This course's division of labor, per the syllabus: for independence tests, software usually builds the expected table; your job is to know where any one cell comes from and to read the output.)

Contributions: dog–house (72 − 60)² ⁄ 60 = 144 ⁄ 60 = 2.4; dog–apartment 2.4; cat–house (28 − 40)² ⁄ 40 = 144 ⁄ 40 = 3.6; cat–apartment 3.6.

χ² = 2.4 + 2.4 + 3.6 + 3.6 = 12.0, and for an r × c table, df = (r − 1)(c − 1) = (2 − 1)(2 − 1) = 1. (Why? With the margins fixed, only that many cells are free — fill them and the totals dictate the rest. A 3 × 3 table has df 4, not 8.) Since 12.0 > 3.841 — and even > 6.635, the 1% bar — reject H₀ emphatically (p ≈ 0.0005). Pet type and housing type are associated: dog owners tilt toward houses, cat owners toward apartments.

And now the guardrail, said louder than the result: associated, not caused. This is a survey — nobody randomly assigned pets or housing. Yard space, building rules, household size: any of them could drive both choices. A chi-square test on observational data inherits every Week 1 warning. Memory hook: "χ² can convict the table, never the cause."

Misconception check. "The association is significant, so housing type causes pet choice (or the reverse)." Significance says the pattern is too strong to be luck — it says nothing about why the pattern exists. Only a randomized experiment could support a causal arrow, and you can't randomly assign people cats.

7. Which chi-square is it? (And is it chi-square at all?)

Both tests share one engine — expected counts, Σ (O − E)² ⁄ E, a df, a critical value. They differ only in the question:

  • One categorical variable versus a claimed distributiongoodness-of-fit, df = k − 1. "Does the candy match the label?" "Is traffic even across weekdays?"
  • Two categorical variables, one sample, "are they linked?" → independence, df = (r − 1)(c − 1). "Is pet type associated with housing type?"

And the wider ladder, now three rungs tall: a question about a mean → t procedures (Weeks 11, 14). A proportion, one or two → z procedures (Weeks 12, 15). Counts across categories → chi-square (this week). Not everything is chi-square: "is the mean delivery time really 30 minutes?" is a t-test — if the data are averages, the counts machine doesn't apply.

In practice (your tools). In Google Sheets or Excel: =COUNTIFS(A:A,"Dog",B:B,"House") builds one cell of an observed table straight from raw category columns (four formulas build the whole 2 × 2; a pivot table does the same job). Expected counts come from =(row total × column total)/grand total with cell references. Then =CHISQ.TEST(observed_range, expected_range) returns the p-value in one cell — ≈ 0.0005 for the pets table. Two more: =CHISQ.DIST.RT(11.4, 4) → ≈ 0.022 (a right-tail p-value on demand) and =CHISQ.INV.RT(0.05, 4) → 9.488 (the software regenerating our critical-value table). One warning from the AI side of your toolkit: chatbots asked for df or critical values from memory routinely answer df = 8 for a 3 × 3 table (it's 4) or mis-quote table entries — which is exactly why this course embeds its table. The tool drafts; you judge.

Key terms

  • Observed count (O) — the actual tally in a category.
  • Expected count (E) — the count a claim (or independence) predicts on average for a sample your size; for goodness-of-fit, n × claimed proportion; for independence, (row total × column total) ⁄ grand total. Decimals allowed.
  • Chi-square statistic (χ²) — Σ (O − E)² ⁄ E; one non-negative number measuring a whole table's drift from expected.
  • Contribution to chi-square — one category's term (O − E)² ⁄ E; the biggest contributions mark where the table went off-script.
  • Goodness-of-fit test — tests one categorical variable's counts against a claimed distribution; df = k − 1.
  • Test of independence — tests whether two categorical variables are associated; df = (r − 1)(c − 1).
  • Degrees of freedom (df) — the knob that sets the chi-square distribution's scale; determined by the table's shape, not the sample size.
  • Critical value — the cutoff luck alone exceeds only 5% (or 1%) of the time; reject H₀ when χ² exceeds it.
  • Expected-count condition — every expected count at least 5 (with random data, as counts); expected, never observed.

Summary

  • A chi-square test is two tables arguing: observed (what you counted) versus expected (what the claim predicts for n like yours). Expected counts may be decimals; the test eats raw counts, never percents.
  • χ² = Σ (O − E)² ⁄ E — gap, squared, scaled by expectation, summed. Zero = perfect match; never negative; "big" is defined only by the df-matched critical value or a p-value.
  • Goodness-of-fit (one variable vs. a claim): df = k − 1. Independence (two variables, linked?): expected = row × column ⁄ total, df = (r − 1)(c − 1).
  • Conditions before trust: random data · counts · every expected count ≥ 5 (observed zeros are legal).
  • Reject = the drift is too big to be luck. Fail to reject = consistent with the claim — never "proven." And a significant association is never a cause: χ² can convict the table, never the cause.
  • When the alarm rings, read the contributions to find where — rank by (O − E)² ⁄ E, not by the biggest count.

Check your understanding

Work these before looking at the answers — they're ungraded, and the answers (with reasoning) are at the bottom of the page.

  1. A snack brand claims the four colors in its chocolate-gem mix are equally common. A class combines its bags into a sample of 120 gems: 24 / 36 / 30 / 30 across the four colors. Run the full goodness-of-fit test at the 5% level: expected counts, χ², df, critical value, decision — and state the conclusion in careful words.
  2. A friend proudly reports χ² = −3.1 from their own candy count. Without seeing any of their work, how do you know a mistake happened? And what would χ² = 0 mean?
  3. A clothing retailer cross-tabulates a sample of 300 orders by T-shirt size (S / M / L / XL) and store location (downtown / airport). (a) What are the df for the test of independence? (b) The S row totals 60 orders and the downtown column totals 150. If size and location were independent, what count would you expect in the S–downtown cell?
  4. An online retailer finds a significant association (χ² test, p < 0.01) between when an order is placed (weekday vs. weekend) and whether it is returned. A manager concludes: "placing orders on weekends causes returns — we should disable weekend ordering." Give two distinct statistical objections.

Answers (worked reasoning — read after attempting)

  1. Expected: 120 ⁄ 4 = 30 per color (all ≥ 5 ✓). Contributions: (24 − 30)² ⁄ 30 = 36 ⁄ 30 = 1.2; (36 − 30)² ⁄ 30 = 1.2; (30 − 30)² ⁄ 30 = 0; 0. χ² = 2.4, df = 4 − 1 = 3, critical value 7.815. Since 2.4 < 7.815, fail to reject H₀: these counts are consistent with an equal mix. Careful words matter — we did not prove the colors are equally common; we found no convincing evidence against it.
  2. Impossible on its face: every term of χ² is a squared gap divided by a positive expected count, so every term is ≥ 0 and the sum can never be negative — somewhere an arithmetic slip happened (usually a lost square or a subtraction typo). χ² = 0 would mean every observed count exactly equals its expected count — a perfect match to the claim, which almost never happens in real samples.
  3. (a) df = (r − 1)(c − 1) = (4 − 1)(2 − 1) = 3. (Not 8 − 1 = 7 — cells minus one is the classic wrong turn.) (b) Expected = (row total × column total) ⁄ grand total = (60 × 150) ⁄ 300 = 30.
  4. First: association is not causation — this is observational data; nobody randomly assigned order timing, and a confounder could drive both (for example, weekend orders may skew toward gifts, and gifts get returned more — the gift-ness, not the calendar, may be doing the work). Second: the test convicts the table, not a mechanism or a remedy — "significant" says the pattern isn't luck, and says nothing about what disabling weekend ordering would change. (Bonus objection: with a large sample, even a tiny, practically trivial association becomes "significant" — Week 13's statistical-vs-practical distinction.)

Next up in this module: the slide deck if you want the lecture's visual walk-through, then the Readings & Resources page for optional deeper dives — and when you're ready to practice, Lecture Tutorial 16 turns your chatbot into your personal chi-square tutor (mini table included).