Biostatistics Study NotesFrom concepts and derivations to interpretation

CHAPTER 05 · TOPIC 04

Chi-Square Test of Independence

Derive expected counts from marginal totals and test association between two categorical variables.

On this page
  1. Hypotheses
  2. Deriving expected count from independence
  3. Expected Counts in a 2×2 Table
  4. Pearson chi-square statistic
  5. Degrees of Freedom
  6. Simplified formula for 2×2 table
  7. The p-Value Comes from the Right Tail
  8. Selection of approximation conditions and method

The chi-square independence test evaluates whether two categorical variables observed in a population are independent using the entire contingency table. The difference between the observed frequencies and the expected frequencies if they were independent is summarized in the Pearson chi-square statistic.

Hypotheses#

null hypothesisH0:X and Y are independentH_0:X\text{ and }Y\text{ are independent}
alternative hypothesisH1:X and Y are not independentH_1:X\text{ and }Y\text{ are not independent}
Probability condition for H₀P(X=i,Y=j)=P(X=i)P(Y=j)P(X=i,Y=j)=P(X=i)P(Y=j)

Deriving expected count from independence#

Marginal probability of i-th rowP^(X=i)=ni+N\widehat P(X=i)=\frac{n_{i+}}N
Marginal probability of jth columnP^(Y=j)=n+jN\widehat P(Y=j)=\frac{n_{+j}}N
joint probability at independenceP^ij=ni+Nn+jN\widehat P_{ij}=\frac{n_{i+}}N\frac{n_{+j}}N
Expected countEij=NP^ij=ni+n+jNE_{ij}=N\widehat P_{ij}=\frac{n_{i+}n_{+j}}N

Expected Counts in a 2×2 Table#

cellobserved countExpected count under independence
AA(A+B)(A+C)/N
BB(A+B)(B+D)/N
CC(C+D)(A+C)/N
DD(C+D)(B+D)/N

Pearson chi-square statistic#

Pearson residualrij=OijEijEijr_{ij}=\frac{O_{ij}-E_{ij}}{\sqrt{E_{ij}}}
statisticsχ2=i=1rj=1c(OijEij)2Eij\chi^2=\sum_{i=1}^{r}\sum_{j=1}^{c}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}

Standardize the difference in each cell by the expected count, then square it and add it to remove the sign. The larger χ² is, the harder the table is to explain using an independent model. Also check the residuals to see which cells contributed to the difference.

Degrees of Freedom#

Unrestricted joint distributionrc1rc-1
Marginal parameters of independent model(r1)+(c1)(r-1)+(c-1)
degree of freedom as a differencedf=(rc1)[(r1)+(c1)]=(r1)(c1)df=(rc-1)-[(r-1)+(c-1)]=(r-1)(c-1)

Simplified formula for 2×2 table#

χdf=12=N(ADBC)2(A+B)(C+D)(A+C)(B+D)\chi^2_{df=1}=\frac{N(AD-BC)^2}{(A+B)(C+D)(A+C)(B+D)}

In a 2×2 table, only one cell is free once the marginal totals are determined. If you divide the deviation of A, the following common term will appear, and if you put the Pearson terms of the four cells together, you will get a simplified formula.

Deviation of A cellAEA=AN(A+B)(A+C)N=ADBCNA-E_A=\frac{AN-(A+B)(A+C)}N=\frac{AD-BC}{N}
diagonal cellAEA=DED=ADBCNA-E_A=D-E_D=\frac{AD-BC}{N}
off-diagonal cellsBEB=CEC=ADBCNB-E_B=C-E_C=-\frac{AD-BC}{N}
χ2=(ADBC)2N2(1EA+1EB+1EC+1ED)\chi^2=\frac{(AD-BC)^2}{N^2}\left(\frac1{E_A}+\frac1{E_B}+\frac1{E_C}+\frac1{E_D}\right)

The p-Value Comes from the Right Tail#

p=P ⁣(χdf2χobs2H0)p=P\!\left(\chi^2_{df}\geq\chi^2_{\mathrm{obs}}\mid H_0\right)

Since χ² is non-negative and increases in value as the deviation from H₀ increases, only the right tail is used regardless of direction. However, even if it becomes significant, we do not know the direction or strength of the association, so we also report the cell percentage, residual, OR, and RR.

Selection of approximation conditions and method#

  • Each observation enters only one cell, and the observation unit is independent.
  • Make sure that the expected count is not extremely small. Standards are not fixed absolute laws.
  • Consider Fisher exact test for small sample 2×2 tables
  • If you want to make the approximation conservative in a 2×2 table, Yates correction is also an option, but it can be too conservative.
  • Use McNemar test instead of independence test for paired binary data