Chi-square test
A chi-square test compares observed counts with the counts expected under the null hypothesis: chi-square = sum of (O - E)^2 / E. A large value means the observed pattern is unlikely under the null. Degrees of freedom are (rows - 1)(columns - 1) for a table and categories - 1 for goodness of fit.
Syntax
chi-square = sum( (O - E)^2 / E ), E(cell) = row total * column total / grand total| Part | What it is |
|---|---|
O | the observed count in each cell |
E | the expected count under the null hypothesis |
df | (r - 1)(c - 1) for independence; k - 1 for goodness of fit |
Examples
2 x 2 table: Group A 30 yes / 20 no, Group B 20 yes / 30 no
all row and column totals 50, expected = 50 * 50 / 100 = 25 in every cell
chi-square = 4 * (5^2 / 25) = 4 * 1Result: chi-square = 4, df = 1, p = 0.0455
Excel: =CHISQ.TEST(observed_range, expected_range) returns the p-value
Goodness of fit: 60 die rolls, observed 8, 12, 10, 9, 11, 10, expected 10 each
(4 + 4 + 0 + 1 + 1 + 0) / 10Result: chi-square = 1.0, df = 5, p = 0.963 (fair die not rejected)
Excel: =CHISQ.DIST.RT(1, 5)
Critical value for alpha = 0.05, df = 1
=CHISQ.INV.RT(0.05, 1)Result: 3.841; 4 exceeds it, so reject independence
Common mistakes
- Using percentages instead of raw counts; the test only works on actual frequencies.
- Having expected counts below 5 in some cells; combine categories or use Fisher's exact test.
- Passing the observed table twice to CHISQ.TEST; the second argument must be the expected counts you computed.
Practice Chi-square test
These drills check your answer on live data, free, no account needed.
- Chi-square test of independence (2 x 3)A survey cross-tabulates preferred contact method by age group. Under 40: email 31, phone 30, chat 86. 40 and over: email 22, phone 35, chat 109. Compute the chi-square statistic for independence, then decide at α = 0.01. Round to 2 decimal places.
- Chi-square 2 x 2: p-value and decisionAn HR team checks whether remote work and promotion are related. Remote: promoted 21, not promoted 26. On site: promoted 118, not promoted 90. Find the p-value of the chi-square test of independence (no continuity correction), then decide at α = 0.05. Round to 4 decimal places.
Questions
What does a chi-square test of independence tell me?
Whether two categorical variables are related, for example whether purchase (yes/no) depends on device (mobile/desktop). It does not say how strong the relationship is; use Cramer's V for that.
Why is the chi-square test always right-tailed?
Because (O - E)^2 / E can only be positive and bigger differences give bigger values, so only large values count as evidence against the null.
Can I use chi-square with a 2 x 2 table?
Yes, with 1 degree of freedom. It is equivalent to a two-proportion z-test; chi-square equals z squared.
Related
- Two-proportion z-testHow to test whether two proportions differ with the two-proportion z-test: pooled proportion, standard error, z statistic and p-value, with a worked example.
- p-valueA p-value is the probability of a result at least as extreme as the observed one if the null is true. How to compute it from z and t, and common misreadings.
- Conditional probabilityHow to calculate conditional probability P(A|B) = P(A and B) / P(B), read it from a two-way table, check independence and avoid confusing P(A|B) with P(B|A).
- Hypothesis testing stepsThe five steps of a hypothesis test: state H0 and H1, choose alpha, compute the test statistic, find the p-value or critical value, decide. Worked example.
Previous: Type I and Type II errorsNext: Two-proportion z-test