Chi-square test of independence
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
- Small expected counts make the approximation doubtful. Consider Fisher’s exact test, covered in the categorical-tests overview. Read: Choosing a test for a table of counts
- The same people supply before and after categories. Use McNemar’s test, covered in the categorical-tests overview. Read: Choosing a test for a table of counts
- You need adjustment for confounding with a binary outcome. Use logistic regression. Read: Logistic regression
Before you start
What you need
- The observed count in every cellUse mutually exclusive categories. Keep zeros and verify the table totals.
- The expected counts under independenceFor each cell, multiply its row total by its column total, then divide by the grand total.
- How people were sampledShared clinics, weights or paired records need methods that reflect that structure.
- People contribute independently to the table.How to check: Count people once. A second measurement on the same person is not independent.
- 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).
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.
Worked example
A worked example
- 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
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.
chi-square-two-by-two).analysis.chi_square_independence, the id a plan cue records.Work it out
Related Learn pages
Related designs and methods
Evidence
Sources & further reading
- 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
- Recommended tests for association in 2 x 2 tables (opens in a new tab) · Stat Med · doi 10.1002/sim.3531