Chi-square test of independence for contingency tables
Use differences between observed and expected counts to assess statistical association between two categorical variables
Adjust survey data
Adjust survey data
Do different AI usage proportions in two years imply a population association?
Continuing the previous lesson, randomly survey first-year and third-year students about whether they have used generative AI tools. The sample proportions differ, but could this simply reflect sampling variability?
Alternative hypothesis H₁: In the population under study, the two variables are not independent (statistically associated).
| Observed counts O | Use AI | Do not use AI | Total |
|---|---|---|---|
| First year | 62 | 38 | 100 |
| Third year | 128 | 32 | 160 |
| Grand total | 190 | 70 | 260 |
Observe usage proportions within the two groups
The usage proportion is 62.0% for first-year students and 80.0% for third-year students. The difference in percentage points is 18.0.
Observing a difference in proportions is not enough. Next ask: if the variables were independent, approximately how many people would we expect in each cell?
Adjust survey data
Adjust survey data
Expected counts E: how many people per cell if the variables are independent?
If year of study and AI use are independent, both groups should have the same usage proportion. Use the row totals, column totals, and grand total to calculate the count expected under H₀ in each cell.
| Observed counts O | Use AI | Do not use AI | Total |
|---|---|---|---|
| First year | 62 | 38 | 100 |
| Third year | 128 | 32 | 160 |
| Grand total | 190 | 70 | 260 |
The left table shows the observed counts (O); the right table shows the expected counts (E) calculated under independence.
| Expected counts E | Use AI | Do not use AI | Total |
|---|---|---|---|
| First year | 73.08 | 26.92 | 100 |
| Third year | 116.92 | 43.08 | 160 |
| Grand total | 190 | 70 | 260 |
For example, the first-year “Use AI” cell:
Calculate the other three cells in the same way.
Adjust survey data
Adjust survey data
The χ² statistic: add the deviations from all four cells
Standardize each cell's deviation using its expected count, then add the contributions. Larger differences produce a larger χ² statistic; χ² cannot be below 0.
| Cell | O | E | (O−E)²/E |
|---|---|---|---|
| First year · Use AI | 62 | 73.08 | 1.679 |
| First year · Do not use AI | 38 | 26.92 | 4.557 |
| Third year · Use AI | 128 | 116.92 | 1.049 |
| Third year · Do not use AI | 32 | 43.08 | 2.848 |
Substitute values: calculate each cell, then sum
- Cell 1: (62 − 73.08)² / 73.08 ≈ 1.679
- Cell 2: (38 − 26.92)² / 26.92 ≈ 4.557
- Cell 3: (128 − 116.92)² / 116.92 ≈ 1.049
- Cell 4: (32 − 43.08)² / 43.08 ≈ 2.848
- 1.679 + 4.557 + 1.049 + 2.848 ≈ 10.134
Adjust survey data
Adjust survey data
Larger χ² values provide more evidence against independence; the p-value is the right-tail area
Under the null hypothesis, when the approximation conditions are met, the Pearson χ² statistic approximately follows a chi-square distribution with 1 degree of freedom. The test always looks in the right tail for a sufficiently large deviation.
- χ² distribution (df=1)
- Right-tail p-value
- Critical value
P < α = 0.05. Reject H₀. There is statistical evidence that the two categorical variables are not independent.
When P < α, reject the null hypothesis that year of study and AI use are independent; otherwise, fail to reject it. Statistical association does not establish causation.
The χ² statistic squares deviations. Observed counts above or below expected counts increase χ², so larger values provide more evidence against H₀.
Adjust survey data
Adjust survey data
A complete chi-square test of independence: from research question to statistical conclusion
Research question: are year of study and AI tool use associated in the population? Adjust the counts and examine every step.
| Observed counts O | Use AI | Do not use AI | Total |
|---|---|---|---|
| First year | 62 | 38 | 100 |
| Third year | 128 | 32 | 160 |
| Grand total | 190 | 70 | 260 |
H₀: year of study and AI use are independent; H₁: year of study and AI use are not independent.
Expected counts (in order: first-year users/nonusers, third-year users/nonusers) are 73.08, 26.92, 116.92, 43.08. df=(2−1)(2−1)=1.
Right-tail p-value 0.001455, α=0.05; critical value χ²=3.841.
Reject H₀. At the 5% significance level, the data support a statistical association between year of study and AI use.
④ Conditions for the test: check before drawing a formal conclusion
Each student is counted only once; the survey and sampling design should support independent observations. Sliders and contingency tables cannot automatically verify this condition.
The total sample size in this example is 260. There is no universal “at least 30 people” threshold for all χ² tests. Consider the sampling design and expected cell counts.
This lesson uses a conservative classroom rule of thumb for the 2×2 table: all four expected counts ≥5. Current minimum expected count: 26.92.
All four expected counts are ≥5, meeting this lesson's rule of thumb. The researcher must still confirm independent observations and the sampling design.
If expected counts are too small, consider Fisher's exact test. If observations are not independent, the ordinary Pearson chi-square test of independence cannot be applied directly either. Even a significant result only indicates statistical association and does not directly establish causation.