Chi-square test of independence

Analytika TeamLast reviewed 9 min read

The chi-square test of independence (also called Pearson’s chi-square test or the chi-square test of association) answers questions like “Is smoking status related to region?” or “Does recovery depend on which treatment patients got?”. Both variables are categories, and the data are counts in a crosstab. (For the one-variable version, see chi-square tests.)

Hypotheses

  • H₀: the two variables are independent. Knowing someone’s category on one tells you nothing about their category on the other.
  • H₁: the two variables are associated.

How it works

  1. Put the counts in a contingency table, one variable as rows and the other as columns.
  2. For each cell, calculate the count you’d expect under independence: row total × column total / grand total.
  3. Add up (observed − expected)² / expected over every cell. That sum is χ².
  4. Compare χ² with the chi-square distribution on (rows − 1) × (columns − 1) df.

Assumptions

  • Independent observations: each person is counted in exactly one cell.
  • Expected counts of at least 5 in every cell. Some texts allow up to 20% of cells below 5 in large tables, as long as none is below 1. When counts are too small, use Fisher’s exact test (or the Fisher-Freeman-Halton extension for tables larger than 2×2).

Worked example: hair colour and eye colour

In a 1974 survey, 592 statistics students at the University of Delaware recorded their hair and eye colour (Snee, 1974). The data are built into R as HairEyeColor. Are the two related?

Step 1: the observed counts

Hair \ EyesBlueBrownGreenHazelTotal
Black2068515108
Blond9471610127
Brown841192954286
Red1726141471
Total2152206493592

Step 2: expected counts and the assumption check

Take blond hair with blue eyes. If hair and eye colour were independent, we’d expect 127 × 215 / 592 ≈ 46.1 students in that cell. We observed 94, about twice as many. Blond hair with brown eyes: expected 47.2, observed only 7.

The smallest expected count in the table is 7.7 (red hair, green eyes), so every cell clears the threshold of 5. Analytika checks this before choosing, and picks the chi-square test.

Step 3: run the test

Analytika engine output
Chi-square statistic: 138.2898
P-value: 2.325e-25
Degrees of freedom: 9
Cramer's V: 0.2790
N: 592
95% CI: [0.2235, 0.3180]
Residual critical z: 2.9552 (Bonferroni over 16 cells)
Cell residual: Blue || Black: z=-4.2538
Cell residual: Blue || Blond: z=9.9676
Cell residual: Blue || Brown: z=-3.3979
Cell residual: Brown || Black: z=6.1365
Cell residual: Brown || Blond: z=-8.3282
Rows excluded for missing values: 0 of 592
R 4.6 output
> chisq.test(margin.table(HairEyeColor, c(1, 2)))

	Pearson's Chi-squared test

data:  margin.table(HairEyeColor, c(1, 2))
X-squared = 138.29, df = 9, p-value < 2.2e-16

The same χ² and df. R stops printing p below 2.2 × 10⁻¹⁶, while Analytika gives the full value. Either way, report it as p < .001. Analytika’s test suite checks the statistic and Cramér’s V against R on every change.

Step 4: interpret

  • χ²(9) = 138.29, p < .001: strong evidence that hair colour and eye colour are associated.
  • Cramér’s V = .28, 95% CI [.22, .32]: a moderate association. V runs from 0 (none) to 1 (perfect). See effect size.

Step 5: find which cells drive it

A significant χ² says the variables are related, but not how. Adjusted standardised residuals answer that. They say how many standard errors each cell sits above or below its expected count. Because 16 cells are tested at once, Analytika uses a Bonferroni-corrected cut-off (|z| > 2.96 here) and lists only the cells that pass it:

  • Blue eyes with blond hair: far more than expected (z = 9.97).
  • Brown eyes with black hair: more than expected (z = 6.14).
  • Brown eyes with blond hair: far fewer than expected (z = −8.33).
  • Blue eyes with black or brown hair: fewer than expected (z = −4.25 and −3.40).

So the association is mostly about lighter hair going with blue eyes, and darker hair with brown eyes.

Step 6: report it (APA 7)

A chi-square test of independence showed that hair colour and eye colour were associated, χ²(9, N = 592) = 138.29, p < .001, Cramér’s V = .28, 95% CI [.22, .32]. Adjusted standardised residuals indicated that blond hair occurred with blue eyes more often than expected under independence, and with brown eyes less often.

Include the full crosstab, with counts and row or column percentages, as a table.

Association is not causation

A chi-square test on survey data shows that two variables go together, not that one causes the other. Both may be driven by a third factor; here, genetic ancestry plausibly affects both. Causal language needs an experiment with random assignment.

Common mistakes

  • Checking observed instead of expected counts for the minimum-5 rule.
  • Entering percentages instead of counts.
  • Counting the same person in more than one cell, for example with multi-select survey questions.
  • Using it for paired yes/no data (the same people before and after). That needs McNemar’s test.
  • Reporting only χ² and p, with no effect size and no description of the pattern.

How to run it

  • SPSS: Analyze → Descriptive Statistics → Crosstabs. Tick Chi-square and Phi and Cramér’s V under Statistics, and Expected plus Adjusted standardized under Cells.
  • R: chisq.test(table(df$a, df$b)). The adjusted residuals are in $stdres.
  • Python: scipy.stats.chi2_contingency(pd.crosstab(df.a, df.b))
  • Analytika: ask whether the two variables are related. It checks expected counts, chooses chi-square or Fisher’s exact test, and reports V and the standout cells.

References

  • Snee, R. D. (1974). Graphical display of two-way contingency tables. The American Statistician, 28(1), 9–12.

More in Categorical data

Try it with the free tools

Run this on your own data

Upload a dataset and ask your question in plain English. Analytika checks the assumptions, picks the test, runs it, and writes the results up in APA format.

Try it free