Skip to content
Home / Statistics Guides

9 min read

Chi-Square Test Explained: Goodness-of-Fit vs Test of Independence

Most introductory hypothesis tests compare means, so they need quantitative data. The chi-square test is what you reach for when your data is categorical — counts of how many observations fall into each category. It answers questions like 'is this die fair?' and 'is study method related to whether students pass?', neither of which has a mean to compare.

The two chi-square tests are taught side by side and easily confused, because they share a statistic and a table. This guide explains what separates them, how to compute expected counts, how the degrees of freedom differ, and which assumptions have to hold. Both worked examples use small datasets invented purely as illustrations.

The one statistic behind both tests

Every chi-square test compares what you observed with what you would expect if the null hypothesis were true. For each category or cell you take the observed count O and the expected count E, and accumulate χ² = Σ (O − E)² ÷ E. Squaring makes over- and under-counts both contribute positively; dividing by E scales each discrepancy against how big it should have been, so a gap of 5 matters far more where 10 was expected than where 500 was.

Because every term is non-negative, the statistic is 0 for a perfect fit and grows as the data drifts from expectation. That makes the test right-tailed: only large values count as evidence against the null, and the p-value is always the area to the right of your statistic. A very small χ² is not evidence for the null — it just means the data matched expectation closely.

One rule underpins everything: the test is computed on raw counts, never on percentages or proportions. Feeding it percentages silently replaces your real sample size with 100, inventing or destroying evidence. If your table is in percentages, convert back to counts first.

  • χ² = Σ (O − E)² ÷ E, summed over every category or cell.
  • The test is right-tailed: only large χ² values count as evidence against the null.
  • Always use raw counts — never percentages, proportions, or averages.

Goodness-of-fit: does one variable match a claimed distribution?

The goodness-of-fit test uses one categorical variable and asks whether its counts are consistent with a hypothesised distribution. The null states those proportions — often 'all categories are equally likely', but it can be any set you want to test — and expected counts come straight from it: E = n × the hypothesised proportion.

Degrees of freedom are df = k − 1 − (parameters estimated from the data), where k is the number of categories. If the null fully specifies the proportions, nothing is estimated and df = k − 1. If you first had to estimate a parameter from the sample — the mean of a Poisson model whose fit you are testing, say — subtract one more for each.

Worked example, with made-up data. Roll a six-sided die 120 times and record 18, 23, 16, 21, 26, and 16. Under H0 the die is fair, so each face is expected 120 ÷ 6 = 20 times. The six terms of (O − E)² ÷ E are 0.20, 0.45, 0.80, 0.05, 1.80, and 0.80, giving χ² = 4.10 with df = 6 − 1 = 5. The 0.05 critical value at df 5 is 11.070 and the p-value is about 0.535, so you fail to reject: an ordinary pattern for a fair die.

  • One categorical variable, compared against a claimed set of proportions.
  • E = n × hypothesised proportion; df = k − 1 − (parameters estimated from the data).
  • In the die example: χ² = 4.10, df = 5, p ≈ 0.535 — no evidence the die is unfair.

Test of independence: are two variables related?

The test of independence uses two categorical variables measured on the same sample, laid out in a contingency table. The null is that they are independent — knowing one tells you nothing about the other. No distribution is claimed in advance; expected counts come from the table's own margins, using expected count = (row total × column total) ÷ grand total. Degrees of freedom are df = (r − 1)(c − 1), because the margins are fixed by the data.

Worked example, again with invented numbers. Suppose 300 students are classified by revision method and outcome. Flashcards: 90 passed, 30 failed. Re-reading: 100 passed, 80 failed. Row totals are 120 and 180, column totals 190 and 110, grand total 300. Expected counts are 120 × 190 ÷ 300 = 76 and 120 × 110 ÷ 300 = 44 in the first row, 180 × 190 ÷ 300 = 114 and 180 × 110 ÷ 300 = 66 in the second — all above 5.

The four terms are 2.579, 4.455, 1.719, and 2.970, summing to χ² = 11.72 with df = (2 − 1)(2 − 1) = 1. The 0.05 critical value at df 1 is 3.841 and the p-value is about 0.0006, so you reject independence: pass rates differ by method (75% versus about 55.6%). Note what the test does not deliver — a direction, an effect size, or a cause.

  • Two categorical variables on one sample, arranged in a contingency table.
  • Expected count = (row total × column total) ÷ grand total; df = (r − 1)(c − 1).
  • In the study-method example: χ² = 11.72, df = 1, p ≈ 0.0006 — reject independence.

Assumptions that actually bite

The statistic only approximately follows a chi-square distribution. Observations must be independent — one row per subject, no repeated measures, no clustering the design ignores. The data must be a random sample from the population you want to describe, and each observation must fall into exactly one category.

The sample-size condition fails most often: expected counts should be at least 5 in every cell. It is the expected counts that matter, not the observed ones. When cells fall short, collapse sparse categories into a meaningful combined one, collect more data, or switch to Fisher's exact test, standard for small 2 × 2 tables.

Two further cautions. For 2 × 2 tables some software applies Yates' continuity correction, subtracting 0.5 from each |O − E| before squaring; above it lowers χ² from 11.72 to 10.90, leaving the conclusion unchanged but the number different, so say whether you used it. And chi-square ignores category order, so for ordinal data a test built for ordered categories usually has more power.

  • Independent observations, a random sample, and mutually exclusive exhaustive categories.
  • Every expected count should be at least 5 — check E, not O.
  • Chi-square ignores category order, so ordinal data may deserve a different test.

Choosing the right one, and reading the result

The two tests are told apart by counting variables, not by the shape of the table. One categorical variable against claimed proportions is goodness-of-fit; two categorical variables on one sample is a test of independence. The test of homogeneity uses identical arithmetic but starts from separate samples drawn from different populations; only the design and the wording of the conclusion change.

Rejecting the null says the observed pattern is hard to reconcile with independence or with the claimed distribution — not where the disagreement lies. To find that, see which individual (O − E)² ÷ E contributions dominate the sum, or examine standardized residuals. Then pair the p-value with something conveying magnitude, because chi-square inherits the usual sample-size problem: with enough observations a trivial departure becomes significant. Row percentages, differences in proportions, or an effect size such as Cramér's V keep the conclusion honest. StatRise's Chi-Square Goodness of Fit and Chi-Square Independence calculators show every expected count and term of the sum; the always-free chi-square topic page and formula reference cover the definitions.

  • One variable versus claimed proportions = goodness-of-fit; two variables on one sample = independence.
  • Homogeneity uses identical arithmetic but starts from separate samples per population.
  • A significant χ² locates no specific cell — inspect the individual terms, and report percentages or an effect size.

Frequently asked questions

What is the difference between goodness-of-fit and a test of independence?

Goodness-of-fit uses one categorical variable and tests whether its counts match a claimed set of proportions, with df = k − 1 minus any parameters estimated. A test of independence uses two categorical variables on one sample and asks whether they are related, with df = (r − 1)(c − 1).

How do you calculate expected counts?

For goodness-of-fit, multiply the sample size by each hypothesised proportion. For a test of independence, use expected count = (row total × column total) ÷ grand total for each cell, which is the independence rule for probabilities expressed in counts.

What if an expected count is below 5?

The chi-square approximation becomes unreliable. Combine sparse categories into a meaningful larger one, collect more data, or use an exact alternative such as Fisher's exact test, which is standard for small 2 × 2 tables. Check the expected counts, not the observed ones.

Is the chi-square test one-tailed or two-tailed?

It is right-tailed. Every term of the statistic is non-negative, so only large values indicate a mismatch between observed and expected counts, and the p-value is always the area to the right of your statistic. A very small χ² simply means the data closely matched expectation.

Keep going

Try it in StatRise

Turn this into practice — run the numbers in a calculator, drill questions, or read the matching lesson.

Chi-square and other test calculatorsChi-square topic referenceChoosing the right statistical testHypothesis testing explainedFormula referenceAll guides

Free statistics reference

Related topic reference

  • Chi-Square TestInference
  • IndependenceProbability Foundations
  • Test Statistics & P-valuesInference
  • All statistics topics54 reference pages
CalculatorsLessonsPracticeGuidesTopicsPremiumRestore purchasePrivacyTerms

© 2026 StatRise. Statistics calculators, lessons, practice, and simulations — progress stays in your browser, no account required.

More study tools: CalcRef · Discretica · ScoreMint · PhysRef