Skip to content

The chi-square test of independence

5 min read

Short answer

Build a table of observed counts, compute the expected count for each cell E=row total×column totalgrand totalE=\dfrac{\text{row total}\times\text{column total}}{\text{grand total}}, and form χ2=∑(O−E)2E\chi^2=\sum\dfrac{(O-E)^2}{E}. Compare with the chi-square distribution with (r−1)(c−1)(r-1)(c-1) degrees of freedom. A large χ2\chi^2 means the variables are probably not independent.

Bar chartA–yesA–noB–yesB–noObserved counts; under independence each would be 25.
The test measures how far observed counts are from the independence benchmark.

The idea

If the variables were independent, each cell's share would equal its row share times its column share. The expected counts encode that. χ2\chi^2 adds up the squared gaps, scaled by the size of the expected count.

Conditions

Observations must be independent, and every expected count should be at least about 55. If not, merge categories or use an exact test.

Worked example

Two groups answer yes/no: group A has 3030 yes and 2020 no; group B has 2020 yes and 3030 no. Test independence at 5%.

Totals: 5050 per row, 5050 yes, 5050 no, 100100 overall, so every expected count is 2525. χ2=4⋅(30−25)225=4\chi^2=4\cdot\dfrac{(30-25)^2}{25}=4. With (2−1)(2−1)=1(2-1)(2-1)=1 degree of freedom the 5% critical value is 3.8413.841. Since 4>3.8414>3.841, reject independence (p≈0.046p\approx0.046).

Try it yourself

How many degrees of freedom does a table with 3 rows and 4 columns have?

Show the answer

(3−1)(4−1)=6(3-1)(4-1)=6 degrees of freedom: (rows − 1) times (columns − 1).

Keep going with practice

Vidi's probability lessons define independence, the exact condition this test checks.