Chi-squared Test of Independence Explained with Worked Examples

For a contingency table the expected count in cell under independence is (row total times column total over the grand total), and with degrees of freedom.

Write 2x or 2*x to multiply, ^ for powers, and ; between simultaneous equations. You can also type factor, expand, derivative of, integrate … from a to b, limit … as x->0 or 15% of 80.

Try an example

Before you start

This page is easier if you are comfortable with:

What Chi-squared test of independence means

Take the table : row and column totals are all 50 and , so every expected count is , and on 1 degree of freedom, just above the 5% critical value 3.841. For : totals 30, 70 and 40, 60 give expected counts 12, 18, 28, 42, and : no evidence of association.

The expected count rule comes from multiplying probabilities of independent events (independent events): , times cases. For , . All expected counts should be at least about 5 for the approximation to hold. The statistic and its degrees of freedom are explained in the chi-squared test statistic and chi-squared degrees of freedom.

The test checks association, not causation, and with large tiny differences become significant. It is a special case of the likelihood-ratio test (the likelihood-ratio statistic) for the multinomial model (the multinomial distribution), and the strength of association in a 2 × 2 table is measured by the phi coefficient (the correlation of the indicator variables, the correlation coefficient).

What is it used for?

The test compares observed table counts with the counts expected if the two variables were independent.

How to use chi-squared test of independence

  1. Compute the row totals, column totals and .
  2. Find for each cell.
  3. Sum over all cells.
  4. Compare with on degrees of freedom.

Graph

Contributions to χ² for the table (10, 20 / 30, 40)cell 1: 0.333cell 2: 0.222cell 3: 0.143cell 4: 0.09500.511.522.533.544.5500.050.10.150.20.250.30.350.4Contributions to χ² for the table (10, 20 / 30, 40)cellyGraph with x from 0 to 5 and y from 0 to 0.4. Shows bars (4); point cell 1: 0.333 at (1, 0.3333); point cell 2: 0.222 at (2, 0.2222); point cell 3: 0.143 at (3, 0.1429); point cell 4: 0.095 at (4, 0.0952).

The contributions add to 0.794, well below the 5% critical value 3.84.

Worked examples

These examples are worked by hand, step by step, so you can compare your own working.

Example 1: a 2 × 2 table

Find for .

Worked example
Answer
4.04.0
  1. Expected counts
  2. χ²

Example 2: no association

Find for .

Worked example
Answer
0.7940.794
  1. Expected
  2. χ²

Example 3: an expected count

Find for .

Worked example
Answer
2020
  1. R₁, C₁, N

Special cases

A few situations come up again and again. Knowing them saves time and helps you spot answers that cannot be right.

Table (10 20; 30 40)

E₁₁ = 30 × 40/100

12.

Other expected counts

E₁₂ = 18, E₂₁ = 28, E₂₂ = 42

They sum to 100.

χ²

4/12 + 4/18 + 4/28 + 4/42

0.7937.

Degrees of freedom

(2 − 1)(2 − 1)

1.

Word problems

Under independence E_ij = R_i C_j / N, and χ² = Σ(O − E)²/E with (r − 1)(c − 1) degrees of freedom. Large χ² is evidence of association.

Expected count

Row 1 total 30, column 1 total 40, N = 100. Find E₁₁.

  1. 30 × 40 ÷ 100
  2. Check: ✓12

All cells

E₁₂ = 18, E₂₁ = 28, E₂₂ = 42 with E₁₁ = 12. Check the sum.

  1. 12 + 18 + 28 + 42
  2. Check: equals N ✓100

The statistic Higher

Each |O − E| = 2. Find Σ4/E.

  1. 0.3333 + 0.2222 + 0.1429 + 0.0952
  2. Check: ✓0.7937

Degrees of freedom Higher

Find (r − 1)(c − 1) for a 2 × 2 table.

  1. 1 × 1
  2. Check: ✓1

Common mistakes

E = O

using observed ✗

E = RC/N ✓

Expected under independence.

df = rc

wrong ✗

(r − 1)(c − 1) ✓

Margins fixed.

Small expected counts

ignored ✗

need E ≥ 5 ✓

Approximation.

Percentages

use counts ✗

use raw counts ✓

χ² needs counts.

Exam tips

  1. E = RC/N.
  2. df = (r − 1)(c − 1).
  3. E ≥ 5.
  4. Counts, not percentages.
  5. Compare to the χ² table.

Practice questions

Try each one on paper, then open the answer. Use the solver above to see the full working.

1. E for R = 50, C = 50, N = 100 Foundation

25

2. E for R = 20, C = 30, N = 100 Foundation

6

3. χ² for (20 30; 30 20), E = 25 Foundation

4

4. df for a 3 × 4 table Higher

6

5. (O − E)²/E for O = 18, E = 12 Higher

3

6. E for R = 40, C = 60, N = 200 Higher

12

Practice generator

Unlimited fresh questions with instant marking. Choose a level, work it out on paper, then type your answer.

Solve: …

Key terms

Chi-squared test
Tests association.
Contingency table
Cross-tabulation.
Expected count
RC/N.
Observed count
Data value.
Degrees of freedom
(r − 1)(c − 1).
Independence
No association.

Frequently asked questions

What does the chi-squared test of independence test?

Whether two categorical variables are associated.

How are expected counts found?

Row total × column total ÷ N.

What are the degrees of freedom?

(r − 1)(c − 1).

When is it valid?

When expected counts are at least about 5.

Which page covers likelihood ratios?

likelihood-ratio statistic.

Which page covers permutations?

permutations.

More in Advanced statistics and stochastic processes and the formula sheets.