How to Run a Chi-square Test of Independence

Tests whether the rows and the columns of a two-row contingency table are related. The expected count of each cell is row total × column total ÷ grand total, and χ² = Σ(observed − expected)² ÷ expected. The degrees of freedom are the number of columns − 1. Any number of columns is allowed.

A test of independence asks whether the two ways of splitting a contingency table are related: treatment against recovery, sex against opinion, region against choice.

If the rows and the columns were unrelated, every cell would hold row total × column total ÷ grand total. Call that the expected count EE and compare it with the observed count OO.

χ2=(OE)2E\chi^2 = \sum \dfrac{(O - E)^2}{E}

The degrees of freedom are (rows − 1) × (columns − 1). This calculator takes two rows, so they come to the number of columns − 1. Any number of columns is allowed.

Example

In the defaults, of 50 people given a drug 30 recovered and 20 did not; of 50 given nothing, 15 recovered and 35 did not. Enter 30, 20 as row 1 and 15, 35 as row 2.

In all, 45 recovered and 55 did not, out of 100. The expected count of the first cell is 50×45÷100=22.550 \times 45 \div 100 = 22.5, and the four expected counts are 22.5, 27.5, 22.5 and 27.5.

χ2=7.5222.5+7.5227.5+7.5222.5+7.5227.59.0909\chi^2 = \dfrac{7.5^2}{22.5} + \dfrac{7.5^2}{27.5} + \dfrac{7.5^2}{22.5} + \dfrac{7.5^2}{27.5} \approx 9.0909

On 1 degree of freedom the upper-tail p-value is 0.0026, below 0.05, and χ2\chi^2 exceeds the critical value 3.8415. The drug and the recovery are related.

Watch out

A relationship is not a cause. The test says the two splits go together; it says nothing about which one drives the other.

With an expected count below 5 in any cell the test is unreliable, and for a two-by-two table Fisher's exact test is the usual answer. Some traditions apply Yates's continuity correction to a two-by-two table, which lowers χ2\chi^2 a little.