Understanding the chi-square test of independence — concept and principle
The chi-square test of independence checks whether two categorical variables are related, using a contingency table of observed counts. Typical uses: does preference differ between groups, or is a treatment linked to recovery. You enter the table row by row, and the calculator returns χ², the degrees of freedom, the p-value and Cramér's V as a measure of strength.
How it is calculated
- For every cell, the expected count is E = row total × column total ÷ grand total.
- The statistic is χ² = Σ (O − E)² ÷ E, summed over all cells, where O is the observed count.
- Degrees of freedom: df = (rows − 1) × (columns − 1).
- The p-value is the upper-tail area of the chi-square distribution with df degrees of freedom beyond χ².
- Cramér's V = √(χ² ÷ (N × min(r − 1, c − 1))), where N is the grand total.
The principle
If the two variables were independent, the share of each column would be the same in every row, so each cell would be expected to hold its row total times the overall column share. That is exactly the expected-count formula. The statistic adds up squared gaps between what you saw and what independence predicts, scaled by E. When the null hypothesis is true and counts are large enough, this sum approximately follows a chi-square distribution, a result due to Karl Pearson (1900).
Worked example
- Table: row 1 = 20, 30; row 2 = 30, 20. Row totals 50 and 50, column totals 50 and 50, grand total 100.
- Every expected count is 50 × 50 ÷ 100 = 25.
- Each cell contributes (±5)² ÷ 25 = 1, so χ² = 4.
- df = (2 − 1)(2 − 1) = 1.
- Upper tail of χ² with 1 df beyond 4: p ≈ 0.0455.
- Cramér's V = √(4 ÷ (100 × 1)) = 0.2.
How to read the result
A p-value below your chosen significance level (often 0.05) means the observed pattern would be unusual if the variables were independent, so you reject independence. Cramér's V runs from 0 (no association) to 1 (perfect association); for a 2 × 2 table it equals the absolute phi coefficient. A V of 0.2 is a modest association even though it is statistically significant, so always report both.
Common mistakes
- Entering percentages or averages instead of counts.
- Counting the same person in more than one cell; observations must be independent.
- Reading a significant result as proof of cause and effect.
Frequently asked questions
What is the difference between the test of independence and goodness of fit?
Independence compares two variables in one table; goodness of fit compares one variable's counts with a specified distribution.
Should I use Yates' correction?
It makes a 2 × 2 test more conservative. With the example table it gives χ² = 3.24 and p ≈ 0.072.
Sources: Pearson, K. (1900). Philosophical Magazine, 50(302), 157–175. · Agresti, A. An Introduction to Categorical Data Analysis (Wiley).