Ask Mallard
Analysis method

Chi-square test of independence

4 min read

A chi-square test of independence asks whether two categorical variables are associated in the population.

Fit

When to use it, and when not to

Use it when

  • Each person contributes one cell in a table of two categorical variables.
  • The expected cell counts are large enough for a chi-square approximation.

Not when

Before you start

What you need

Decide these first
  1. The observed count in every cellUse mutually exclusive categories. Keep zeros and verify the table totals.
  2. The expected counts under independenceFor each cell, multiply its row total by its column total, then divide by the grand total.
  3. How people were sampledShared clinics, weights or paired records need methods that reflect that structure.
It rests on these
  1. People contribute independently to the table.How to check: Count people once. A second measurement on the same person is not independent.
  2. Expected counts support the large-sample approximation.How to check: Check expected, not only observed counts. Sparse tables need a justified exact or other suitable method.

Plain language

How it works

Under independence, row membership does not change the expected column proportions. Pearson’s statistic adds the squared observed-minus-expected gaps, divided by expected counts. The degrees of freedom equal (rows minus one) multiplied by (columns minus one).

What the sources show

Mallard’s tool covers only a 2×2 table and applies Yates’ continuity correction. This changes the statistic and p-value. Lydersen and colleagues compare small-table tests; their studies show that no single approximation or exact method is always best.

The method at a glance
Chi-square test of independence: Independent categorical counts. Then Expected counts under independence. Then Chi-square statistic and p-value.Chi-square test of independenceIndependent categoricalcountsExpected counts underindependenceChi-square statistic andp-value
Conceptual diagram. It shows the structure of the idea, not data from any study.

Worked example

A worked example

Invented for illustration
Setting
Two independent groups each include 100 patients with a binary outcome.
Question
Is outcome status associated with group?
Approach
Group 1 counts: 18 positive, 82 negative. Group 2 counts: 30 positive, 70 negative.
Result
With Yates’ correction, chi-square = 3.32, df = 1, p = 0.069. The observed risks are 18 in 100 and 30 in 100.
What it means
The risk difference is -12 per 100, group 1 minus group 2. The p-value does not prove equal risks.

In practice

Recent studies PubMed search · not vetted

Searching PubMed…

Watch out

Common mistakes

The one to remember

Use counts from independent people, not paired outcomes or percentages alone. Expected counts govern approximation quality. Mallard’s 2×2 calculator applies Yates’ continuity correction.

  • Choosing a test from observed cell counts alone.Instead: Compute the expected counts and consider the sampling design.
  • Reading a p-value as the size of an association.Instead: Report risks and a relevant effect estimate with its interval.

Inside Mallard

How Mallard uses this

Mallard links this article to its 2×2 chi-square calculator with Yates’ correction. The method registry links this method to its published analysis article and code.

Code example in Mallard: Two-by-two table: chi-square, Fisher's exact test and the odds ratio (chi-square-two-by-two).
Sample size: Mallard sizes the main comparison with two proportions. It does not size adjustment or any secondary analysis.
Calculators: Chi-square (2×2, Yates) (§2.1).
Atlas node analysis.chi_square_independence, the id a plan cue records.
Code for this method
Loading…

Work it out

Related Learn pages

Related designs and methods

Evidence

Sources & further reading

  1. Power comparison of two-sided exact tests for association in 2 x 2 contingency tables using standard, mid p and randomized test versions (opens in a new tab) · Stat Med · doi 10.1002/sim.1671
  2. Recommended tests for association in 2 x 2 tables (opens in a new tab) · Stat Med · doi 10.1002/sim.3531
Prepared with AI assistance. Source records verified by PMID, title, authors, year and DOI on 2026-11-06. No independent professional approval is claimed. The two primary method-comparison studies support the caution about small-table test choice. The first compares conditional exact versions, not the calculator’s asymptotic Yates implementation.
Last source review: 2026-11-06
AI-generated educational content
Mallard uses reputable methodological and reporting sources, but this material may contain errors or omit important context. Verify important methodological decisions with the cited sources and, when appropriate, a qualified research methodologist or statistician.