Chi-squared test

Also known as: chi-square test

On this page 7

The chi-squared test (also written chi-square test) is a family of statistical hypothesis tests that compares observed frequencies in categories with the frequencies expected under a null hypothesis. In statistics, it is the standard tool for asking whether categorical data fit a specified distribution or whether two categorical variables are associated in a contingency table.

The test summarizes the gap between observed and expected counts in a single number, the chi-squared statistic, and compares it with the chi-squared distribution to obtain a p-value. It works on counts, not on means or measurements, which separates it from the t-test and ANOVA.

How the chi-squared statistic is calculated

The chi-squared statistic is the sum of squared differences between observed and expected counts, each divided by the expected count. Written in plain notation, it is the sum over all cells of (O minus E) squared divided by E. Dividing by E scales each discrepancy, so a difference of 10 matters more in a cell expecting 20 than in a cell expecting 2,000.

When the null hypothesis is true and the sample is large enough, the statistic approximately follows a chi-squared distribution with a specific number of degrees of freedom. Large values signal that the observed counts depart from expectation more than random sampling variation would plausibly explain, which produces a small p-value.

Degrees of freedom depend on the design. For a goodness-of-fit test with k categories, they equal k minus 1, minus any parameters estimated from the data. For a contingency table with r rows and c columns, they equal (r minus 1) times (c minus 1). The table below shows critical values at the conventional significance level.

Degrees of freedomCritical valueTypical design
13.842 by 2 contingency table
25.99Three-category goodness of fit
49.493 by 3 contingency table
612.593 by 4 contingency table
1018.31Eleven-category goodness of fit
Critical values of the chi-squared distribution at alpha 0.05

Main types of chi-squared test

The name covers several distinct tests that share the same statistic but answer different questions. Choosing the correct variant matters because the expected counts and degrees of freedom are computed differently in each case.

The goodness-of-fit test checks one categorical variable against a theoretical distribution, such as whether a die is fair. The test of independence checks whether two variables measured on one sample are related. The test of homogeneity uses the same arithmetic but compares the distribution of one variable across several independently sampled groups.

VariantQuestion answeredExpected counts come from
Goodness of fitDoes one variable follow a stated distributionHypothesized proportions times sample size
Test of independenceAre two variables in one sample associatedRow total times column total over grand total
Test of homogeneityDo several populations share one distributionPooled proportions across groups
McNemar testDo paired binary outcomes changeDiscordant pairs only
Variants of the chi-squared test

Pearson’s original formulation is the most common, but the likelihood-ratio G-test is an asymptotically equivalent alternative that some fields prefer. For paired or repeated categorical data, the McNemar test replaces the ordinary test of independence, because the independence assumption fails for matched observations.

Assumptions and conditions for valid results

The chi-squared test relies on a large-sample approximation, so its p-values are trustworthy only when certain conditions hold. Violating them usually produces inflated false-positive rates or misleading conclusions.

  • Independent observations: each subject contributes to exactly one cell, with no repeated measurements.
  • Counts, not percentages: the test must use raw frequencies, because proportions hide the sample size.
  • Adequate expected counts: a common rule requires every expected count of at least 5, or at least 80 percent of cells at 5 or more with none below 1.
  • Mutually exclusive categories: each observation falls into one and only one category.

When expected counts are too small, analysts typically merge sparse categories or switch to Fisher’s exact test, which computes an exact p-value for small 2 by 2 tables. Yates’ continuity correction is sometimes applied to 2 by 2 tables, although many statisticians consider it overly conservative.

Interpreting results and effect size

A significant chi-squared result shows only that some departure from the null hypothesis exists; it does not show where, how large or in which direction. Analysts therefore inspect standardized residuals, where values beyond about plus or minus 2 identify the cells driving the result.

Because the statistic grows with sample size, a trivial association becomes significant in very large samples. Reporting an effect size is essential, and the table below lists the usual measures for contingency tables.

MeasureApplies toRangeCommon reading
Phi coefficient2 by 2 tables0 to 10.1 small, 0.3 medium, 0.5 large
Cramer’s VTables larger than 2 by 20 to 1Thresholds shrink as table size grows
Odds ratio2 by 2 tables0 to infinity1 means no association
Effect size measures used with chi-squared tests

Common mistakes with the chi-squared test

Most errors come from applying the test to the wrong data structure or overinterpreting its output. The following table pairs frequent mistakes with their consequences and corrections.

MistakeConsequenceRemedy
Using percentages instead of countsStatistic and p-value are wrongEnter raw frequencies
Ignoring small expected countsUnreliable p-valuesMerge categories or use Fisher’s exact test
Testing paired data as independentUnderestimated variabilityUse the McNemar test
Reading significance as strengthOverstated findingsReport Cramer’s V or phi
Claiming causation from associationUnsupported conclusionsLimit claims to association
Frequent errors and remedies

A further subtlety concerns ordinal categories such as satisfaction levels. The standard test ignores order, so a chi-squared test for trend or a rank-based method usually has more power when categories have a natural sequence.

The idea in brief

The chi-squared test checks whether the counts you actually see match the counts you would expect if nothing interesting were going on. Imagine rolling a die 600 times. You expect about 100 of each number, but you will never get exactly 100.

The test measures how far the real counts drift from the expected ones and asks whether that drift is small enough to be ordinary luck. If the drift is too large to be luck, something real is probably going on, such as a loaded die or a genuine link between two characteristics.

A practical scenario

An online retailer tests three checkout page designs. Over two weeks, 3,000 visitors are randomly assigned to the designs, and the analytics team records whether each visitor completed a purchase. Design A converts 120 of 1,000 visitors, design B 150 of 1,000 and design C 95 of 1,000.

The analyst builds a 3 by 2 contingency table of design against purchase outcome and runs a chi-squared test of independence with 2 degrees of freedom. The statistic is well above the critical value of 5.99, giving a p-value below 0.01. Standardized residuals show that design B outperforms expectation and design C underperforms.

Cramer’s V of about 0.07 indicates a small but commercially meaningful effect. The team adopts design B and schedules a follow-up test to confirm the gain over a longer period.