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 freedom | Critical value | Typical design |
|---|---|---|
| 1 | 3.84 | 2 by 2 contingency table |
| 2 | 5.99 | Three-category goodness of fit |
| 4 | 9.49 | 3 by 3 contingency table |
| 6 | 12.59 | 3 by 4 contingency table |
| 10 | 18.31 | Eleven-category goodness of fit |
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.
| Variant | Question answered | Expected counts come from |
|---|---|---|
| Goodness of fit | Does one variable follow a stated distribution | Hypothesized proportions times sample size |
| Test of independence | Are two variables in one sample associated | Row total times column total over grand total |
| Test of homogeneity | Do several populations share one distribution | Pooled proportions across groups |
| McNemar test | Do paired binary outcomes change | Discordant pairs only |
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.
| Measure | Applies to | Range | Common reading |
|---|---|---|---|
| Phi coefficient | 2 by 2 tables | 0 to 1 | 0.1 small, 0.3 medium, 0.5 large |
| Cramer’s V | Tables larger than 2 by 2 | 0 to 1 | Thresholds shrink as table size grows |
| Odds ratio | 2 by 2 tables | 0 to infinity | 1 means no association |
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.
| Mistake | Consequence | Remedy |
|---|---|---|
| Using percentages instead of counts | Statistic and p-value are wrong | Enter raw frequencies |
| Ignoring small expected counts | Unreliable p-values | Merge categories or use Fisher’s exact test |
| Testing paired data as independent | Underestimated variability | Use the McNemar test |
| Reading significance as strength | Overstated findings | Report Cramer’s V or phi |
| Claiming causation from association | Unsupported conclusions | Limit claims to association |
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.