Topic contents

Lesson 3

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?

Null hypothesis H₀: In the population under study, year of study and AI tool use are independent.
Alternative hypothesis H₁: In the population under study, the two variables are not independent (statistically associated).
Observed counts OUse AIDo not use AITotal
First year6238100
Third year12832160
Grand total19070260

Observe usage proportions within the two groups

0%20%40%60%80%100%First year · Use AI: 62%First year · Do not use AI: 38%First yearThird year · Use AI: 80%Third year · Do not use AI: 20%Third year
  • Use AI
  • Do not use AI
AI usage proportions in 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.

Eij=row i total×column j totaltotal number of people{E}_{ij}=\frac{\text{row i total}\times \text{column j total}}{\text{total number of people}}
Expected counts come from H₀'s independence assumption; they are not arbitrary numbers chosen by the researcher. They are used both to calculate χ² and to check whether the chi-square distribution approximation is applicable.
Observed counts OUse AIDo not use AITotal
First year6238100
Third year12832160
Grand total19070260

The left table shows the observed counts (O); the right table shows the expected counts (E) calculated under independence.

Expected counts EUse AIDo not use AITotal
First year73.0826.92100
Third year116.9243.08160
Grand total19070260

For example, the first-year “Use AI” cell:

E11=100×190260=73.08E_{11}=\frac{100\times 190}{260}=73.08

Calculate the other three cells in the same way.

Class discussion: do observed counts O closer to expected counts E agree more closely with the hypothesis that the variables are independent?

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.

χ2=∑i,j(Oij−Eij)2Eij{\chi }^{2}={\sum }_{i,j}\frac{{({O}_{ij}-{E}_{ij})}^{2}}{{E}_{ij}}
CellOE(O−E)²/E
First year · Use AI6273.081.679
First year · Do not use AI3826.924.557
Third year · Use AI128116.921.049
Third year · Do not use AI3243.082.848
Degrees of freedom df = (2 − 1)(2 − 1) = 1

Substitute values: calculate each cell, then sum

  1. Cell 1: (62 − 73.08)² / 73.08 ≈ 1.679
  2. Cell 2: (38 − 26.92)² / 26.92 ≈ 4.557
  3. Cell 3: (128 − 116.92)² / 116.92 ≈ 1.049
  4. Cell 4: (32 − 43.08)² / 43.08 ≈ 2.848
  5. 1.679 + 4.557 + 1.049 + 2.848 ≈ 10.134
10.134χ² statistic
0.001455p-value
1Degrees of freedom df
Drag the two counts: how does χ² change as the two years' usage proportions become closer?

Adjust survey data

Adjust survey data

Significance level α

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.

051015χ²Critical valueObserved χ²Right-tail p-value
  • χ² distribution (df=1)
  • Right-tail p-value
  • Critical value
χ² distribution with 1 degree of freedom and right-tail p-value
Shading: area to the right of the observed χ² statistic (the p-value). Critical line: the critical value for the chosen α.
10.134χ² statistic
0.001455Right-tail p-value
3.841Critical 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.

Why not use both tails?

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

Significance level α

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 OUse AIDo not use AITotal
First year6238100
Third year12832160
Grand total19070260
0%20%40%60%80%100%First year · Use AI: 62%First year · Do not use AI: 38%First yearThird year · Use AI: 80%Third year · Do not use AI: 20%Third year
  • Use AI
  • Do not use AI
AI usage proportions in two groups
① Hypotheses

H₀: year of study and AI use are independent; H₁: year of study and AI use are not independent.

② Expected counts and statistic

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.

χ2=∑(O−E)2E=10.134\chi^2=\sum\frac{(O-E)^2}{E}=10.134
③ p-value and significance decision

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

Condition 1 · Independent observations

Each student is counted only once; the survey and sampling design should support independent observations. Sliders and contingency tables cannot automatically verify this condition.

Condition 2 · Sample size and sampling method

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.

Condition 3 · Expected counts must not be too small

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.

Dynamic check

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.

Adjust survey data

First year 62/100 · Third year 128/160 · α=5%