Statistics · Unit 3 · Week 9 · Lesson 30 · 11 minute reading

Two Categories Walk into a Contingency Table

Association between categorical variables, expected cell counts, and the independence test.

By the end of this lesson, you should be able to

  • Compute expected counts in a contingency table.
  • Interpret Pearson's test of independence.
  • Avoid mistaking an association for a causal relationship.

Eighty fictional commuters report whether they drive or use a train, and whether they frequently arrive late.

Mode Late Not late Total
Car 18 22 40
Train 10 30 40
Total 28 52 80

The observed late rates are 45% for drivers and 25% for train riders. Are these variables associated?

What independence predicts

Under a model in which the row and column classifications are independent, the expected count in row ii, column jj is

Eij=(row totali)(column totalj)n.E_{ij}=\frac{(\text{row total}_i)(\text{column total}_j)}{n}.

For the car-and-late cell, the expected count is 40(28)/80=1440(28)/80=14. The other expected counts are 26, 14, and 26.

Pearson’s chi-square statistic sums squared observed-minus-expected differences divided by the corresponding expected counts. Here:

X2=16/14+16/26+16/14+16/26≈3.516.X^2=16/14+16/26+16/14+16/26\approx3.516.

For a 2×22\times2 table, the usual reference degrees of freedom are (2−1)(2−1)=1(2-1)(2-1)=1 under appropriate conditions.

Interpretation before ritual

If the test indicates an association, that does not establish that switching a commuter from car to train would reduce lateness. Route distance, schedules, weather, job flexibility, and other factors might differ between the groups.

If the same commuter appears repeatedly, the assumed independent observational units may also be wrong.

When expected counts are sparse, a different procedure, such as Fisher’s exact test for a suitable 2×22\times2 reference model, can be preferable.

Ask R the same question

tab <- matrix(c(18,10,22,30), nrow=2,
  dimnames=list(Mode=c("Car","Train"),
                Late=c("Yes","No")))
chisq.test(tab)
prop.table(tab,1)

The row proportions are usually much easier to interpret than a bare p-value.

Check your understanding

  1. What is the expected car-and-late count under independence?
  2. What is the usual degrees-of-freedom formula for an r×cr\times c table?
  3. Why doesn’t an association imply that one classification caused the other?
Worked responses
  1. (r−1)(c−1)(r-1)(c-1).

  2. Confounding and selection, among other causes, may create or distort the association.

Derive the expected table and its constraints

Under independence, P(R=i,C=j)=P(R=i)P(C=j)P(R=i,C=j)=P(R=i)P(C=j). Estimating the marginal probabilities by row and column proportions gives Eij=RiCj/nE_{ij}=R_iC_j/n. These expected counts preserve the observed row and column totals. For an rr by cc table with positive relevant margins, fitting the margins leaves (r−1)(c−1)(r-1)(c-1) independent directions of discrepancy, the usual asymptotic degrees of freedom.

For the synthetic table [[18,22],[10,30]][[18,22],[10,30]], row totals are 40 each, column totals 28 and 52, and expected counts are [[14,26],[14,26]][[14,26],[14,26]]. Pearson’s statistic is

X2=2(1614+1626)≈3.51648.X^2=2\left(\frac{16}{14}+\frac{16}{26}\right)\approx3.51648.

The uncorrected one-degree-of-freedom p-value is approximately 0.060760.06076. R’s default for a two-by-two table may apply Yates’s continuity correction, so specify correct=FALSE to reproduce this exact uncorrected calculation. Neither answer licenses hiding the correction choice.

A measure of association is Cramér’s V=X2/[nmin⁡(r−1,c−1)]V=\sqrt{X^2/[n\min(r-1,c-1)]}. Here V≈0.20966V\approx0.20966. The first-row event proportion is 18/40=0.4518/40=0.45 and the second is 10/40=0.2510/40=0.25, a risk difference of 0.200.20. Those directional, interpretable rates convey information that a nonnegative global statistic does not.

A contingency table of independent people is not interchangeable with paired measurements on the same people or clustered respondents. For sparse two-by-two tables, Fisher’s exact test conditions on margins and tests a corresponding odds-ratio null under its model. “Exact” refers to the finite reference calculation; it does not guarantee the data-collection design is appropriate.

Additional practice

  1. If a three-by-four table meets the test assumptions, how many degrees of freedom does the independence statistic use?
  2. Show that expected counts for each row sum back to its observed row total.
  3. Why would repeated visits by the same person undermine the ordinary independent-count reference?
Worked additional solutions
  1. (3−1)(4−1)=6(3-1)(4-1)=6.
  2. ∑jRiCj/n=Ri(∑jCj)/n=Ri\sum_jR_iC_j/n=R_i(\sum_jC_j)/n=R_i.
  3. Visits share person-level characteristics and are not independent units. The reference variance and effective information may differ from the naive count.
Your study progress · Optional

Keep your place

Mark this lesson complete after you've read it, attempted its exercises, and explained its central idea in your own words.

Progress is not saved unless you choose to mark the lesson complete.

Saved only in this browser's local storage. No required account, server transmission, or analytics. You can reset your saved progress on the course page. Clearing browser data also clears the record.