14. Inference for Contingency Tables

a. The Chi-Square Statistic
Math 115

Yurk

Where We’ve Been — and Where We’re Going

We’ve been building inference for categorical data in stages:

Topic What we tested Statistic Example
6 One categorical variable \hat{p} Payday loans
10 Two groups × a binary outcome (2×2) \hat{p}_1 - \hat{p}_2 CPR survival
14 (today) Two categorical variables (any r×c) X^2 Party × spending

Today continues the thread from topics 6 and 10. Once a variable has more than two categories, the table grows past 2×2 and a single difference in proportions can’t summarize it — so we score the whole table with one number, X^2.

Party × Military Spending

Is opinion on military spending associated with political party? General Social Survey, 2016 — random sample of 149 U.S. adults.

  • Explanatory: party (Dem / Ind / Rep) — 3 categories
  • Response: “Do we spend too little / about right / too much on the military?” — 3 categories

“No relationship” between two categorical variables is called independence:

  • H_0: party and military-spending opinion are independent (no association)
  • H_A: party and military-spending opinion are associated (not independent)

The Observed Table

Party (3 rows) × opinion (3 columns) — a 3×3 table, bigger than topic 10’s 2×2 in both directions.

Party Too Little About Right Too Much Row total
Dem 17 14 12 43
Ind 20 28 24 72
Rep 24 8 2 34
Col total 61 50 38 149

Looking row by row, Republicans look noticeably more likely to say “Too Little” and noticeably less likely to say “Too Much” than Democrats or Independents — in this sample. Real association, or sampling noise?

To answer that, we need to compare these counts to what we’d expect if party and opinion were truly independent.

Expected Count for One Cell

Each cell has an expected count — the value we’d see if H_0 (independence) held:

Party Too Little About Right Too Much Row total
Dem 17 14 12 43
Ind 20 28 24 72
Rep 24 8 2 34
Col total 61 50 38 149

Focus on one cell — Democrats who said “Too Little” (observed 17). If H_0 held, what count would we expect here?

Overall, \dfrac{61}{149} \approx 41\% said “Too Little.” Under H_0, that same share appears in every row.

So expect that share of the 43 Democrats: E_{\text{Dem, TL}} = \underbrace{43}_{\text{Democrats}} \times \underbrace{\tfrac{61}{149}}_{H_0\text{ proportion}} \approx 17.6.

Expected 17.6 vs observed 17 — almost exactly what independence predicts.

The Expected-Count Formula

We just built the Democrat cell as row total \times H_0 proportion:

E_{\text{Dem, TL}} = \underbrace{43}_{\text{row total}} \times \underbrace{\tfrac{61}{149}}_{H_0\text{ proportion}} \approx 17.6

Every cell works the same way — just swap in that cell’s own row total and column:

\text{Expected} = (\text{row total}) \times \underbrace{\frac{\text{column total}}{\text{grand total}}}_{H_0\text{ proportion}} = \frac{(\text{row total}) \times (\text{column total})}{\text{grand total}}

Compute One Expected Count

Republicans saying “Too Little” — if H_0 (independence) held, what count would we expect? Read the totals you need off the table, then use \text{Expected} = \dfrac{\text{row} \times \text{column}}{\text{grand total}}.

Party Too Little About Right Too Much Row total
Dem 17 14 12 43
Ind 20 28 24 72
Rep 24 8 2 34
Col total 61 50 38 149

E_{\text{Rep, TL}} = \dfrac{34 \times 61}{149} = \dfrac{2074}{149} \approx 13.92. Observed was 24 — far more than H_0 predicts. This cell is where the association starts to show.

Observed vs Expected

Observed (Expected) for every cell:

Party Too Little About Right Too Much
Dem 17 (17.6) 14 (14.43) 12 (10.97)
Ind 20 (29.48) 28 (24.16) 24 (18.36)
Rep 24 (13.92) 8 (11.41) 2 (8.67)

Democrats: observed ≈ expected across all three — 17 vs 17.6, 14 vs 14.4, 12 vs 11.

Independents: fewer “Too Little” than expected, more “Too Much” — they lean toward “Too Much.”

Republicans: far more “Too Little” than expected, far fewer “Too Much” — they lean toward “Too Little.”

Independents and Republicans pull in opposite directions. We need one number that combines all nine gaps.

The Chi-Square Statistic

Score each cell, then sum across the whole table:

X^2 = \sum_{\text{all cells}} \frac{(\text{Observed} - \text{Expected})^2}{\text{Expected}}

Two design choices in the formula:

  • Squaring (\text{O} - \text{E}) — gaps become positive, so they can’t cancel across the table.
  • Dividing by Expected — a gap of 5 matters more when the cell expects 10 than 100.

Bigger X^2 = more total deviation from independence = stronger evidence against H_0.

Computing X^2 for Military

For each cell, compute \dfrac{(O - E)^2}{E}:

Party Too Little About Right Too Much
Dem \frac{(17 - 17.6)^2}{17.6} \approx 0.02 \frac{(14 - 14.43)^2}{14.43} \approx 0.01 \frac{(12 - 10.97)^2}{10.97} \approx 0.1
Ind \frac{(20 - 29.48)^2}{29.48} \approx 3.05 \frac{(28 - 24.16)^2}{24.16} \approx 0.61 \frac{(24 - 18.36)^2}{18.36} \approx 1.73
Rep \frac{(24 - 13.92)^2}{13.92} \approx 7.3 \frac{(8 - 11.41)^2}{11.41} \approx 1.02 \frac{(2 - 8.67)^2}{8.67} \approx 5.13

X^2 \approx 0.02 + 0.01 + 0.1 + 3.05 + 0.61 + 1.73 + 7.3 + 1.02 + 5.13 \approx 18.97

Where Does X^2 Come From?

The full sum is X^2 \approx 18.97. Each cell’s contribution sits in the table above. Which two cells push X^2 up the most — and what’s the story those cells tell?

  1. Identify the two largest cell contributions. Republicans, “Too Little” (\approx 7.3) and Republicans, “Too Much” (\approx 5.13). Together they account for about 12.4 out of 18.97 — roughly two-thirds of the total.

  2. What do those two cells say about where the association is? Republicans were more likely than expected to say “Too Little” (24 observed vs 13.9 expected) and less likely than expected to say “Too Much” (2 observed vs 8.7 expected). X^2 summarizes how different the table is from “no association”; the cell-by-cell contributions tell us where the difference is.

What X^2 Does — and Doesn’t — Tell Us

  • X^2 = 0 means observed = expected in every cell — perfect independence.
  • X^2 = 18.97 for the military table — but is that “big”?
  • X^2 scores total deviation from independence; it doesn’t say which groups differ or which direction.
  • To turn X^2 into a yes/no decision, we need a reference distribution — what X^2 values would look like under H_0

Next time (14b): the chi-square distribution, conditions, the p-value, and the decision.

References