Contingency tables: counting in two ways
A contingency table counts people (or things) sorted by two categories at once. Rows are one category (city). Columns are another (drink). Each box is a cell.
We only use counts. Never use percentages or means in a chi-square test.
| Tea | Coffee | Total | |
|---|---|---|---|
| City A | 60 | 40 | 100 |
| City B | 40 | 60 | 100 |
| Total | 100 | 100 | 200 |
Hypotheses: independence or homogeneity
Test of independence (association): one sample, two variables. H0: the two variables are independent (no association). H1: they are associated.
Test of homogeneity: several separate samples, one variable. H0: the distribution is the same in every group. H1: at least one group is different.
The arithmetic is the same. Only the way the data was collected and the wording of H0 change.
Goodness of fit is the one-row cousin: it checks whether counts match a given model (for example, a fair die). There df = k − 1 (k categories), minus 1 more for each parameter you estimate from the data.
Expected counts and the χ² statistic
If H0 is true, each cell should get its fair share:
E = (row total × column total) ÷ grand total
Then for every cell compute (O − E)² ÷ E and add them:
χ² = Σ (O − E)² ÷ E
Degrees of freedom: df = (rows − 1) × (columns − 1).
Conditions
- Data are counts from random samples (or random assignment).
- Observations are independent (sample under 10% of the population if no replacement).
- All expected counts are at least 5. If not, merge rows or columns (which lowers df).
Deciding: critical value, p-value and conclusion
A large χ² means the data are far from what H0 predicts. Use a table or calculator:
- If χ² > critical value (for your df and level), reject H0.
- Or: if p-value < α (often 0.05), reject H0.
Common 5% critical values: df 1 → 3.841, df 2 → 5.991, df 3 → 7.815, df 4 → 9.488.
Write the conclusion in context: "There is evidence at the 5% level that drink choice is associated with city." A test shows association, not cause. Look at the cells with the biggest (O − E)² ÷ E to say where the difference is.
Yates' continuity correction (2×2 tables)
χ² is a smooth curve, but counts jump in whole numbers. For a 2×2 table (df = 1) some courses use Yates' correction:
χ²(Yates) = Σ (|O − E| − 0.5)² ÷ E
It makes χ² a little smaller, so the test is a bit more careful. In our example: 4 × (10 − 0.5)² ÷ 50 = 7.22, still above 3.841.
Try it at home
Ask 20 friends or family: "Do you prefer mornings or evenings?" and note if they are under 18 or 18+. Build a 2×2 table, work out E for each cell, then χ². Is it bigger than 3.84? (With only 20 people some E may be under 5: that is the lesson about conditions!)
Key formulas and definitions
- E = (row total × column total) ÷ grand total
- χ² = Σ (O − E)² ÷ E
- df = (r − 1)(c − 1) for a table; df = k − 1 for goodness of fit
- Yates (2×2): χ² = Σ (|O − E| − 0.5)² ÷ E
- Reject H0 if χ² > critical value, or if p < α
- 5% critical values: df 1 = 3.841, df 2 = 5.991, df 3 = 7.815
Worked examples
1. In a 2×2 table the row totals are 30 and 70 and the column totals are 40 and 60. Find the expected count for row 1, column 1.
Grand total = 100. E = 30 × 40 ÷ 100 = 12.
2. Find df for a table with 3 rows and 4 columns.
df = (3 − 1)(4 − 1) = 2 × 3 = 6.
3. For one cell O = 18 and E = 12. Find its contribution to χ².
(18 − 12)² ÷ 12 = 36 ÷ 12 = 3.
4. City A: 60 tea, 40 coffee. City B: 40 tea, 60 coffee. Test at 5% if drink choice depends on city.
H0: independent. Totals: rows 100, 100; columns 100, 100; n = 200. Every E = 50. Each (O − E)² ÷ E = 100 ÷ 50 = 2. χ² = 8. df = 1, critical = 3.841. 8 > 3.841, so reject H0: evidence of association.
5. Apply Yates' correction to the table above.
|O − E| = 10 in every cell. (10 − 0.5)² ÷ 50 = 90.25 ÷ 50 = 1.805. χ² = 4 × 1.805 = 7.22 > 3.841. Still reject H0.
6. Three schools (samples of 100) report favourite sport: School P: cricket 50, football 50; School Q: cricket 40, football 60; School R: cricket 30, football 70. Test homogeneity at 5%.
Column totals: cricket 120, football 180, n = 300. E(cricket) = 100 × 120 ÷ 300 = 40 in each school; E(football) = 60. Pieces: P: 100/40 + 100/60 = 2.5 + 1.667; Q: 0 + 0; R: 2.5 + 1.667. χ² = 8.33. df = (3 − 1)(2 − 1) = 2, critical 5.991. 8.33 > 5.991, reject H0: the sport preferences are not the same in all schools (School P likes cricket more, R less).
Common mistakes
- Using percentages instead of counts. χ² only works on counts.
- Forgetting to divide by E (writing Σ (O − E)²).
- Wrong df: it is (r − 1)(c − 1), not r × c.
- Saying the test proves one variable causes the other. It only shows association.