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 , column is
For the car-and-late cell, the expected count is . 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:
For a table, the usual reference degrees of freedom are 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 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
- What is the expected car-and-late count under independence?
- What is the usual degrees-of-freedom formula for an table?
- Why doesn’t an association imply that one classification caused the other?
Worked responses
-
-
.
-
Confounding and selection, among other causes, may create or distort the association.
Derive the expected table and its constraints
Under independence, . Estimating the marginal probabilities by row and column proportions gives . These expected counts preserve the observed row and column totals. For an by table with positive relevant margins, fitting the margins leaves independent directions of discrepancy, the usual asymptotic degrees of freedom.
For the synthetic table , row totals are 40 each, column totals 28 and 52, and expected counts are . Pearson’s statistic is
The uncorrected one-degree-of-freedom p-value is approximately . 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 . Here . The first-row event proportion is and the second is , a risk difference of . 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
- If a three-by-four table meets the test assumptions, how many degrees of freedom does the independence statistic use?
- Show that expected counts for each row sum back to its observed row total.
- Why would repeated visits by the same person undermine the ordinary independent-count reference?
Worked additional solutions
- .
- .
- Visits share person-level characteristics and are not independent units. The reference variance and effective information may differ from the naive count.
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.