Chi-Square Test of Independence

A clinic records two things about every patient: whether they were vaccinated, and whether they later fell sick. The counts land in a tidy two-way table. Now the real question: are these two things linked — does vaccination relate to getting sick — or is the table just noise, the counts scattered the way pure chance would scatter them?

The chi-square test of independence answers exactly this. It takes a contingency table of two categorical variables and asks whether they are associated (knowing one tells you something about the other) or independent (they drift apart with no connection). It reuses the very same statistic you met in the goodness-of-fit test\chi^2 = \sum \dfrac{(O - E)^2}{E} — but with a clever new way of building the expected counts E.

The null hypothesis: independence

We start by assuming there is no link. That is the null hypothesis:

If two variables really are independent, probability has a rule for how the counts should split. The chance of landing in a given row and a given column is just the row's share times the column's share. Multiply that probability by the grand total N and you get the expected count for that cell:

E = N \cdot \frac{\text{row total}}{N} \cdot \frac{\text{column total}}{N} = \frac{\text{row total} \times \text{column total}}{\text{grand total}}.

Every cell gets its own E from its own row and column totals. The observed counts O are what actually happened. If the two variables truly are independent, each O should sit close to its E; big gaps are the fingerprint of an association.

Worked example 1: vaccination and illness (a 2×2 table)

Two hundred people, split by vaccination and by whether they got sick. First the observed counts, with all the margins:

SickWellRow total
Vaccinated1090100
Unvaccinated3070100
Column total40160200

Now the expected count in each cell, using E = \dfrac{\text{row} \times \text{col}}{200}:

Notice the expected counts keep the same margins (each row still totals 100, each column 40 and 160) — they just spread the totals as if there were no link. Here is the observed count over its expected count, cell by cell:

Sick (O / E)Well (O / E)
Vaccinated10 / 2090 / 80
Unvaccinated30 / 2070 / 80

Add up \dfrac{(O - E)^2}{E} across all four cells:

\chi^2 = \frac{(10-20)^2}{20} + \frac{(90-80)^2}{80} + \frac{(30-20)^2}{20} + \frac{(70-80)^2}{80} = 5 + 1.25 + 5 + 1.25 = 12.5.

The degrees of freedom for an r \times c table are (r-1)(c-1). This table is 2 \times 2, so df = (2-1)(2-1) = 1, and the 5% critical value is 3.84. Since 12.5 > 3.84, we reject independence: there is an association between vaccination and illness in this data.

Seeing it: observed against expected

Put the observed count (solid) beside the expected count (pale) for each of the four cells. Where the two bars in a pair pull apart, that cell pumps up \chi^2; where they match, it contributes almost nothing.

The two Sick cells swing hardest — vaccinated people were much less sick than independence would predict (10 vs 20), unvaccinated much more (30 vs 20). Those two gaps, squared and scaled, are what push the total to 12.5.

Worked example 2: a drug that doesn't beat the placebo

Not every table hides an association. A trial gives 60 people a drug and 60 a placebo, then records who recovered:

RecoveredNotRow total
Drug451560
Placebo402060
Column total8535120

Expected counts, again \dfrac{\text{row} \times \text{col}}{120}:

\chi^2 = \frac{(45-42.5)^2}{42.5} + \frac{(15-17.5)^2}{17.5} + \frac{(40-42.5)^2}{42.5} + \frac{(20-17.5)^2}{17.5} \approx 0.15 + 0.36 + 0.15 + 0.36 = 1.01.

Still df = 1, still comparing to 3.84. But now 1.01 < 3.84, so we fail to reject H_0. The recovery difference (45 vs 40) is small enough to be ordinary chance — this data gives no evidence that the drug and recovery are associated.

Reading the verdict carefully

A significant result (example 1) says the two variables move together — and that is all it says. Three warnings live inside that one sentence:

Goodness-of-fit asks "does one variable match a claimed distribution?"; independence asks "do two variables move together?". They feel different, yet both reduce to counts we saw versus counts a model predicts — and \sum (O-E)^2/E measures that mismatch either way. The only thing that changes is where the expected counts come from: a stated distribution for goodness-of-fit, or the row-and-column margins for independence. Same ruler, different thing being measured.