Chi-square test
How comparing observed counts with the counts expected under a hypothesis tests whether categories are related or whether data follow a stated distribution.
What it is
The chi-square (χ2) test is a family of tests for categorical data, that is, counts of how many observations fall into each category. It compares the observed counts with the counts expected if a particular hypothesis were true, and measures how far apart they are. Introduced by Karl Pearson in 1900, it is used in genetics, medicine, quality control, social science and any field where data are classified rather than measured. The two most common versions are the test of independence (are two categorical variables related?) and the goodness-of-fit test (do the counts follow an expected distribution?).
It answers questions such as: Is smoking status associated with a disease? Do the offspring of a cross follow the expected 3:1 ratio? Do defects occur equally on all four production lines? Is a die fair? Is a treatment’s success rate different from a placebo’s? The test shows whether differences in counts are larger than chance would produce.
Fundamentals
The test statistic
For each category, the statistic adds the squared difference between observed (O) and expected (E) counts, scaled by the expected count:
χ2 = Σ (O − E)2 / E
If the null hypothesis is true, the statistic follows approximately a chi-square distribution with a number of degrees of freedom (df) that depends on the test. Large values mean the data are far from the hypothesis, and the p-value is the upper-tail probability of the chi-square distribution. The distribution is right-skewed, with mean equal to df. The test is always one-sided in this sense (large values reject), though it detects any kind of departure.
Goodness-of-fit test
This test checks one categorical variable against a specified distribution. For k categories and expected proportions pi, the expected count is Ei = n pi, and df = k − 1 − (number of parameters estimated from the data). Example: a cross predicts 3:1 dominant to recessive. In 400 offspring, E = 300 and 100; if O = 290 and 110, χ2 = (290 − 300)2/300 + (110 − 100)2/100 = 0.33 + 1.00 = 1.33, df = 1, p ≈ 0.25, so there is no evidence against the 3:1 ratio. The same approach tests whether data follow a Poisson or other distribution, after grouping values into bins and subtracting one df for each fitted parameter.
Test of independence and homogeneity
For a table with r rows and c columns (a contingency table), the null hypothesis is that the row and column variables are independent. Expected counts under independence are Eij = (row totali × column totalj) / N, and df = (r − 1)(c − 1). For a 2 × 2 table with counts a, b, c, d: χ2 = N (ad − bc)2 / [(a+b)(c+d)(a+c)(b+d)]. The test of homogeneity uses the same calculation to ask whether several groups sampled separately have the same distribution of a categorical outcome, the difference being in how data were collected, not the math. Standardized (adjusted) residuals, (O − E)/√E scaled by the margins, show which cells drive a significant result (absolute values above about 2). Collapsing or combining categories should be justified in advance.
Effect size, assumptions and alternatives
A small p-value does not say how strong the association is, and with large samples tiny effects become significant. Cramér’s V = √[χ2 / (N × min(r − 1, c − 1))] runs from 0 to 1 (about 0.1, 0.3, 0.5 small, medium, large for df = 1), and the phi coefficient is the 2 × 2 case. For 2 × 2 tables, odds ratio OR = ad/bc and relative risk RR = [a/(a+b)]/[c/(c+d)] with confidence intervals describe the direction and size. Assumptions: independent observations (each subject in one cell, so no repeated measures), random sampling, and adequate expected counts: no more than 20% of cells with E < 5 and none below 1. If this fails, use Fisher’s exact test (exact probability from the hypergeometric distribution, for 2 × 2 and larger tables), or Monte Carlo p-values, or combine categories. Yates’ continuity correction for 2 × 2 tables is often overly conservative and is less used today. For paired nominal data (before/after), use McNemar’s test, and for ordered categories, the Cochran–Armitage trend test or the Mantel–Haenszel test for stratified tables. Large datasets with many variables can use log-linear models, which generalize the test to multi-way tables.
Typical applications and limits
Applications include Mendelian ratios, case–control and survey data, A/B testing of conversion counts, and checking model fit. Limits are that the test needs counts, not percentages or means, that it does not show causation or direction, and that its approximation fails with small expected counts.
Basic operation
- State the hypotheses. Write the null (independence or specified proportions) and the alternative.
- Tabulate the counts. Build a contingency table or list of category counts from raw data.
- Compute expected counts. Use totals (independence) or hypothesized proportions (goodness of fit).
- Check assumptions. Confirm independence and expected counts of at least 5, or switch to an exact test.
- Calculate χ2 and df. Sum (O − E)2/E over all cells.
- Find the p-value. Compare with the chi-square distribution at the given df.
- Examine residuals and effect size. Identify which cells differ and compute Cramér’s V or odds ratio.
- Collect the output. You obtain χ2, df, p-value and effect size; exact tests and multiple comparisons are covered in the video.
Good practice
- Use raw counts. Never apply the test to percentages.
- Check expected counts. Use Fisher’s exact test when they are small.
- Keep observations independent. Do not count the same subject twice.
- Report effect size. Give Cramér’s V or odds ratios with intervals.
- Do not pool arbitrarily. Avoid combining categories after seeing results.
- Interpret carefully. Association is not causation, so consider confounding and Simpson’s paradox, and protect privacy in small cells of sensitive data.
Key terms
- Contingency table — a table of counts cross-classified by two or more categorical variables.
- Expected count — the count predicted under the null hypothesis.
- Degrees of freedom — the number of independent cells, (r − 1)(c − 1) for independence tests.
- Cramér’s V — a measure of association strength based on χ2.
- Odds ratio — the ratio of odds of an outcome between two groups.
- Fisher’s exact test — an exact test for small counts in contingency tables.
- Standardized residual — a scaled difference between observed and expected counts for one cell.
Video coming soon
About five minutes on Chi-square test: how it works in detail, how the data is acquired, and how it is analyzed.
More in 3.1 Foundations and Hypothesis Testing
