Chi-square test of independence
Association and correlation · reference distribution: χ²((r−1)(c−1))
When to use it
Check whether two categorical variables are associated, in a contingency table.
Null hypothesis
The two variables are independent.
Assumptions
- Independent observations
- Expected frequencies of at least 5
Test statistic
\chi^2 = \sum_{i,j} \dfrac{(O_{ij} - E_{ij})^2}{E_{ij}}, \qquad E_{ij} = \dfrac{R_i\, C_j}{N}Effect size
Cramér’s V, from 0 to 1; in a 2 × 2 table, it is φ.
V = \sqrt{\dfrac{\chi^2}{N\,(\min(r, c) - 1)}}How to report it
χ²(2, N = 200) = 7.4, p = .025, V = .19
In R and Python
R
chisq.test(table(df$a, df$b))
Python
stats.chi2_contingency(pd.crosstab(df["a"], df["b"]))
In Python, stats is scipy.stats and np is numpy.
Variants and alternatives
- Fisher’s exact test, for 2 × 2 tables with small counts
- Cramér’s V, to measure the strength of the association
Where it sits in the catalog
Association and correlation. Instead of comparing groups, they measure whether two variables move together, and how strongly.
In the decision tree
- What do you want to do? Measure the relationship between two variables
- What kind of variables are they? Both categorical
- Are all expected counts at least 5? Yes
It also appears alongside other tests:
- As an alternative to Two-proportion Z.