14. Inference for Contingency Tables

Math 115

Yurk and Martin

Extending Our Framework

Previously, we compared two groups with a binary outcome (2×2 table).

  • Used difference in proportions: \(\hat{p}_1 - \hat{p}_2\)
  • Used Z-statistic for hypothesis testing

But what if we have:

  • More than 2 groups? (e.g., Dem, Ind, Rep)
  • Non-binary outcomes? (e.g., Spending: Too Little, About Right, Too Much)

We need a new approach!

Two Ways to Generalize

Generalization 1: More groups

  • Explanatory variable has 3+ categories
  • Example: Democrat, Independent, Republican

Generalization 2: Non-binary response

  • Response variable has 3+ categories
  • Example (Government Spending): “Too Little”, “About Right”, “Too Much”

With either generalization, we can’t use a single difference in proportions!

The Hypothesis Framework

Same concept as before, different statistic:

  • \(H_0\): No association between the variables (independence)
  • \(H_A\): There is an association (variables are not independent)

New statistic: Chi-square, \(X^2\), measures deviation from independence.

Government Spending

Research question: Is political party associated with opinions on military spending?

  • Explanatory: Party (Dem, Ind, Rep) — 3 categories
  • Response: Opinion on military spending (“Too Little”, “About Right”, “Too Much”) — 3 categories
  • Data: General Social Survey, 2016 (n = 149)

This creates a 3×3 contingency table.

The Contingency Table

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

Beyond simple \(2\times 2\) tables, we can’t compute just one difference in proportions.

We need to compare all observed counts to what we’d expect under \(H_0\).

What Would We Expect Under \(H_0\)?

If \(H_0\) is true (no association), each party should have the same distribution of opinions.

Expected count for any cell:

\[\text{Expected} = \frac{\text{row total} \times \text{column total}}{\text{grand total}}\]

This distributes the overall opinion proportions equally across all parties.

Expected Counts

Example: Democrats who say “Too Little”

\[\text{Expected} = \frac{43 \times 61}{149} = 17.6\]

Party Too Little About Right Too Much
Dem 17 (17.6) 14 (14.4) 12 (11)
Ind 20 (29.5) 28 (24.2) 24 (18.4)
Rep 24 (13.9) 8 (11.4) 2 (8.7)

Observed (Expected) — How different are they?

The Chi-Square Statistic

Measures total deviation of observed from expected:

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

Why this formula?

  • Squaring: Makes all contributions positive
  • Dividing by Expected: Standardizes (a difference of 5 matters more when Expected is 10 than when Expected is 100)

Computing \(X^2\)

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

Party Too Little About Right Too Much
Dem \(\frac{(17 - 17.6)^2}{17.6} = 0.02\) \(\frac{(14 - 14.4)^2}{14.4} = 0.01\) \(\frac{(12 - 11)^2}{11} = 0.10\)
Ind \(\frac{(20 - 29.5)^2}{29.5} = 3.05\) \(\frac{(28 - 24.2)^2}{24.2} = 0.61\) \(\frac{(24 - 18.4)^2}{18.4} = 1.73\)
Rep \(\frac{(24 - 13.9)^2}{13.9} = 7.30\) \(\frac{(8 - 11.4)^2}{11.4} = 1.02\) \(\frac{(2 - 8.7)^2}{8.7} = 5.13\)

\[X^2 = 0.02 + 0.01 + 0.10 + 3.05 + 0.61 + 1.73 + 7.30 + 1.02 + 5.13 = 18.97\]

What Does \(X^2\) Tell Us?

  • \(X^2 = 0\) means observed = expected exactly (perfect independence)
  • Larger \(X^2\) means greater deviation from independence
  • Larger \(X^2\) → stronger evidence against \(H_0\)

But how large is “large enough” to reject \(H_0\)?

We need a reference distribution → chi-square distribution

Degrees of Freedom

For contingency tables:

\[df = (\text{rows} - 1) \times (\text{columns} - 1)\]

For our 3×3 table:

\[df = (3-1) \times (3-1) = 2 \times 2 = 4\]

Intuition: After fixing row and column totals, only \(df\) cells can vary freely.

The Chi-Square Distribution

  • Always right-skewed, starts at 0
  • As df increases, distribution shifts right

The Null Distribution

When conditions are met, we can use the chi-square distribution as our reference:

  • Use the chi-square distribution with \(df = (r-1) \times (c-1)\)
  • This distribution describes what \(X^2\) values we’d expect if \(H_0\) is true

Computing the p-value:

  • P-value = area to the right of observed \(X^2\) under the chi-square curve
  • Larger \(X^2\) values are more extreme → stronger evidence against \(H_0\)

Conditions for Chi-Square Test

Independence:

  • Random sample ✓
  • Observations independent of each other ✓

Expected Counts (under \(H_0\)):

  • At least 5 expected in each cell ✓

Check expected counts, not observed counts!

Finding the P-value

Chi-squared distribution with \(df=4\)

P-value < 0.001

Conclusion: Military Spending

Results:

  • \(X^2 = 18.97\)
  • df = 4
  • P-value < 0.001

Decision: Reject \(H_0\)

Conclusion: There is a statistically significant association between political party and opinions on military spending. The parties differ in their views.

Note: The chi-square test tells us there IS an association, but not the direction or which groups differ.

Randomization Test for Independence

  • We can randomly permute the response (opinion) between the three explanatory groups (party)
    • To simulate the null hypothesis being true
  • For each permuted sample, we calculate value of the \(X^2\) statistic
  • In this course we won’t cover randomization for contingency tables
    • But you should know that you can use randomization for these problems

Randomized \(X^2\) Null Distribution

  • Here is a null distribution and p-value for the military spending question

Second Example: Space Exploration

Same parties, same opinion categories, but asking about space exploration spending.

Party Too Little About Right Too Much Total
Dem 8 22 13 43
Ind 13 37 22 72
Rep 9 17 8 34

\(X^2 = 1.33\), df = 4, P-value = 0.857

Comparing the Two Results

Issue \(X^2\) P-value Conclusion
Military 18.97 < 0.001 Strong association
Space 1.33 0.857 No evidence of association

Same groups, different topics, very different conclusions!

  • Political party is associated with military spending views
  • No evidence that political party is associated with space exploration views

Summary

Chi-square test extends two-proportion test to larger tables:

Component Formula/Value
Expected count \(\frac{\text{row total} \times \text{column total}}{\text{grand total}}\)
Chi-square statistic \(X^2 = \sum \frac{(O - E)^2}{E}\)
Degrees of freedom \(df = (r-1) \times (c-1)\)
P-value Right tail of chi-square distribution

Conditions: Independence + Expected counts ≥ 5 in each cell

Connection to Topic 10

Two Proportions Chi-Square Test
Table size 2×2 only Any r×c
Test statistic Z \(X^2\)
Null distribution Normal Chi-square
What we test \(p_1 - p_2 = 0\) Independence
Direction Can be one-sided Always two-sided

Chi-square generalizes the two-proportion test to larger tables!

(For 2×2 tables, both methods give equivalent results)

References