Interactive demo · Probability and Statistical Inference · Chapter 15

The chi-squared test of independence, simulated

If the row and column variables of a table are independent, the expected count of cell (i, j) is Eij = (row total)(column total)/n. Pearson's statistic U = Σ(Xij − Eij)2/Eij and the likelihood ratio statistic T = 2ΣXij log(Xij/Eij) both measure the distance from independence, and both are approximately χ2 with (I − 1)(J − 1) degrees of freedom when n is large. Here you can see that null distribution directly: the page simulates many tables for which independence holds and draws the histogram of their statistics next to the χ2 curve. The top panel shows which cells drive the association: the colour of each cell is its adjusted residual, roughly N(0, 1) under independence.

The table (one row per line)

Statistic in the histogram

Simulation under independence

Simulated tables B

Try these

Multinomial: each simulated table puts n individuals into the cells with probabilities (row proportion)×(column proportion), the fitted independence model; its margins vary. Permutation: the column labels of the n individuals are shuffled, which keeps both margins exactly as observed and gives the exact null distribution given the margins (the idea of Fisher's exact test). Monte Carlo p-value: (1 + #{simulated ≥ observed})/(B + 1). Adjusted residual: (X − E)/√(E(1 − row proportion)(1 − column proportion)). The χ2 approximation is usually trusted when every Eij is at least about 5. Random numbers are seeded, so a given seed always gives the same tables.

In the lecture notes