The Chi-Squared Test for Independence

Do the rows and the columns move together?

The hypotheses

Sixty pet owners were sorted by pet and by home. In the city, 24 keep cats and 6 keep dogs; on farms, 16 keep cats and 14 keep dogs. Is the kind of pet linked to the kind of home, or could counts like these arise by chance?

The null hypothesis is H₀: the kind of pet is independent of the kind of home. The alternative is H₁: the two are linked. The test is at the 5 percent level.

cityfarmtotalcats241640dogs61420total303060

The observed counts with their totals: 40 cat owners and 20 dog owners, 30 in the city and 30 on farms.

What H₀ expects

If H₀ is true, each expected count is the row total times the column total, divided by the grand total. Cats in the city: 40 × 30 ÷ 60 = 20. Cats on farms: 20 as well. Dogs in the city: 20 × 30 ÷ 60 = 10, and dogs on farms 10.

The observed counts miss these. There are 24 city cat owners against 20 expected, and 14 farm dog owners against 10 expected. The question is whether misses this size are too large to put down to chance.

One number for all the gaps

For each cell, subtract the expected count E from the observed count O. The four gaps are 24 − 20 = 4, 16 − 20 = −4, 6 − 10 = −4 and 14 − 10 = 4. They add up to 0, as they always do, so they are squared first: each square is 16.

Then each square is divided by its expected count. A gap of 4 matters more when only 10 are expected than when 20 are expected. So the four terms are 16/20 = 0.8, 16/20 = 0.8, 16/10 = 1.6 and 16/10 = 1.6.

Their total is the test statistic: χ² = 0.8 + 0.8 + 1.6 + 1.6 = 4.8. In symbols, χ² = Σ (O − E)²/E, added over every cell. It is 0 when every count matches, and it grows as the counts move away from what H₀ expects.

Degrees of freedom

With the totals fixed, choose one cell of the 2 × 2 table, say 24 city cat owners. Then the city dogs must be 30 − 24 = 6, the farm cats 40 − 24 = 16, and the farm dogs 20 − 6 = 14. Only one cell is free, so the table has 1 degree of freedom.

In general, a table with r rows and c columns has (r − 1) × (c − 1) degrees of freedom: the last cell of every row and of every column is fixed by its total. A table with 2 rows and 3 columns has (2 − 1) × (3 − 1) = 2.

The chi-squared distribution

If H₀ is true, the statistic follows, approximately, the chi-squared distribution with that many degrees of freedom. With 1 degree of freedom, values close to 0 are the most common, and large values are rare.

The top 5 percent of that distribution starts at 3.841: P(χ² ≥ 3.841) = 0.05. So the critical region at the 5 percent level is χ² ≥ 3.84.

χ²3.844.8

The gold curve is the chi-squared distribution with 1 degree of freedom; it runs off the top near 0, where it is highest. The shaded tail from 3.84 holds 5 percent of the area, and the observed total, 4.8, lies inside it.

The verdict

4.8 > 3.84, so the statistic is in the critical region: reject H₀. The gaps between observed and expected counts are too large to be chance alone.

The p-value says the same: P(χ² ≥ 4.8) = 0.0285, which is below 0.05. It is above 0.01, so at the 1 percent level, whose critical value is 6.635, H₀ would not be rejected.

The conclusion in context

State the result about the pets: there is evidence at the 5 percent level that the kind of pet kept depends on the kind of home. The gaps show which way: more city owners keep cats than independence predicts, and more farm owners keep dogs.

The test shows a link, not its cause. It does not say that living in a city makes people choose cats.

When the test can be used

The expected counts should all be at least 5; here the smallest is 10. With smaller expected counts the chi-squared distribution is a poor approximation, and rows or columns are combined first.

The test needs counts, not percentages. If the same proportions came from 120 owners, every O and every E would double, each (O − E)²/E would double, and χ² would be 9.6. More data showing the same pattern is stronger evidence, and the statistic grows to match.

The usual mistakes

Dividing by the observed count. Each square is divided by E, the count H₀ expects, not by O.

Counting every cell as a degree of freedom. A 2 × 2 table has 1, not 4 or 3: the totals fix the other three cells.

Using the critical value for the wrong degrees of freedom. 3.841 is for 1 degree of freedom; for 2 it is 5.991, and for 3 it is 7.815.

Saying that independence is proved when χ² is small. A small total means only that the counts do not contradict H₀.

Film preferences at a cinema

In the application below, 200 customers in two age groups choose action, comedy or drama. The table has 2 rows and 3 columns, so there are six expected frequencies and 2 degrees of freedom.

Worked example: Film Preferences of Cinema Customers in Two Age Groups, Tested for Independence

Question A cinema asks 200 customers which kind of film they prefer. Of the 120 customers under 30, 64 choose action, 28 comedy and 28 drama; of the 80 aged 30 or over, 26 choose action, 32 comedy and 22 drama. (a) Find the expected frequencies if the preferred kind of film is independent of age group, and the value of the test statistic χ2. (b) Test at the 5% level whether preference and age group are independent.

  1. 1.H0: the preferred kind of film is independent of age group. H1: it is not independent. The column totals are 64 + 26 = 90 for action, 28 + 32 = 60 for comedy and 28 + 22 = 50 for drama.

    actioncomedydramatotalunder 3064282812030 or over26322280total906050200H0: film and age group are independentE = row total × column total/200
    actioncomedydramatotalunder 3064282812030 or over26322280total906050200H0: film and age group are independentE = row total × column total/200
    The observed table, with its row and column totals in gold.
  2. 2.For customers under 30 the expected frequencies are 120 × 90200 = 54, 120 × 60200 = 36 and 120 × 50200 = 30; for customers aged 30 or over they are 36, 24 and 20. Every one is at least 5, so the test can be used.

    actioncomedydramatotalunder 3064E 5428E 3628E 3012030 or over26E 3632E 2422E 2080total906050200under 30: 54, 36, 3030 or over: 36, 24, 20
    actioncomedydramatotalunder 3064E 5428E 3628E 3012030 or over26E 3632E 2422E 2080total906050200under 30: 54, 36, 3030 or over: 36, 24, 20
    Under each observed frequency is its expected frequency, row total times column total divided by 200.
  3. 3.(a) χ2 = ∑ (O − E)2E = 10254 + 8236 + 2230 + 10236 + 8224 + 2220 = 9.407.

    actioncomedydramatotalunder 30641.85281.78280.1312030 or over262.78322.67220.2080total906050200each cell: (O − E) × (O − E)/Echi-sq = 9.407
    actioncomedydramatotalunder 30641.85281.78280.1312030 or over262.78322.67220.2080total906050200each cell: (O − E) × (O − E)/Echi-sq = 9.407
    (a) Under each observed frequency is its term (O − E)2E; the six terms add up to χ2 = 9.407.
  4. 4.The table has 2 rows and 3 columns, so there are (2 − 1)(3 − 1) = 2 degrees of freedom. The critical value of χ2(2) at the 5% level is 5.991.

    5.9910chi-sq(2) if H0 is true(2 − 1) × (3 − 1) = 2 degrees of freedomchi-sq(2) at 5%: 5.991
    5.9910chi-sq(2) if H0 is true(2 − 1) × (3 − 1) = 2 degrees of freedomchi-sq(2) at 5%: 5.991
    A 2 × 3 table has 2 degrees of freedom, and 5% of χ2(2) lies beyond 5.991.
  5. 5.(b) 9.407 > 5.991, so reject H0. There is evidence at the 5% level that the kind of film customers prefer depends on their age group: customers under 30 choose action more often than independence would give, and older customers choose comedy more often. Check: in each row the differences O − E add up to 0, since 10 − 8 − 2 = 0.

    5.9910chi-sq = 9.407chi-sq(2) if H0 is true9.407 > 5.991: reject H0preference depends on age group
    5.9910chi-sq = 9.407chi-sq(2) if H0 is true9.407 > 5.991: reject H0preference depends on age group
    (b) 9.407 is in the critical region, so reject H0: the kind of film preferred depends on age group.

Answer: (a) the expected frequencies are 54, 36 and 30 under 30, and 36, 24 and 20 at 30 or over; χ2 = 9.407; (b) 9.407 > 5.991 on 2 degrees of freedom, so reject H0: the kind of film preferred depends on age group

Common mistakes

  • Using 6 − 1 = 5 degrees of freedom, one fewer than the number of cells. Once the row and column totals are fixed, only 2 cells of a 2 × 3 table can be chosen freely, so there are (2 − 1)(3 − 1) = 2.
  • Dividing each (O − E)2 by the observed frequency O instead of the expected frequency E. The statistic measures the distance from what H0 predicts, so each term is divided by E.

More statistical inference problems, worked step by step →

Practice The Chi-Squared Test for Independence in the app