Chi-Square Test: Formula, Worked Example and How to Read the Result

Contents
Chi-Square Test: Formula, Worked Example and How to Read the Result

A chi-square test (χ²) compares the counts you observed in each category with the counts you would expect if nothing were going on. In survey work it answers two questions: are two categorical questions related (the test of independence), and does one question's split match a known distribution (goodness of fit). Below you will find the formula, a worked example on 400 survey answers that you can reproduce in our free calculator, how to read the p-value and Cramér's V, and what to do when counts are small.

Key points

  • The chi-square test works on counts of categorical answers, never on percentages or means.
  • The expected count of a cell is row total × column total ÷ N, and the degrees of freedom are (rows − 1) × (columns − 1).
  • In the worked example, χ²(2, N = 400) = 11.61, p = .003: satisfaction is related to plan type, and Cramér's V = .17 says the association is small.
  • Check expected counts before you trust the p-value. When they are small, merge categories or, for a 2×2 table, use Fisher's exact test.

What is a chi-square test?

A chi-square test is a hypothesis test for categorical data. It measures how far each observed count sits from the count expected under the null hypothesis and adds those gaps into one number; the larger the number, the worse the data fit "no relationship" or "no difference". The typical input is a single-choice question: plan type, region, yes or no, a satisfaction group. Answers like these are nominal or ordinal categories (see levels of measurement), so means and standard deviations make no sense for them, while counts do.

There are three versions. They share the formula and differ in the question they answer and in how the expected counts are built.

TestQuestion it answers, with a survey example
Test of independenceAre two questions related within one sample? Example: Is satisfaction related to plan type?
Test of homogeneityDo groups sampled separately share one distribution? Example: Do customers of three stores, each surveyed on its own, prefer the same contact channel?
Goodness of fitDoes one question match a known split? Example: Do respondents' regions match the customer base?

Independence and homogeneity are computed in exactly the same way. The difference lies in the design: one sample split by two questions, or group sizes fixed in advance. Statistics packages report both as "Pearson chi-square". In each case the null hypothesis (H0) says there is no relationship or no difference, and the alternative (H1) says there is one; our guide to the research hypothesis covers how to write both before you collect data.

Chi-square formula, expected counts and degrees of freedom

The chi-square formula is χ² = Σ (O − E)² / E, where O is the observed count in a cell, E is the expected count, and Σ means "add over all cells". These formulas cover everything you need:

WhatFormula
Chi-square statisticχ² = Σ (O − E)² / E
Expected count, test of independenceE = row total × column total / N
Expected count, goodness of fitE = n × expected share of the category
Degrees of freedom, two-way tabledf = (rows − 1) × (columns − 1)
Degrees of freedom, goodness of fitdf = number of categories − 1

Squaring stops positive and negative gaps from cancelling out. Dividing by E puts each gap in proportion: 10 extra answers matter a lot in a cell that expects 20 and very little in a cell that expects 200. The p-value is the area of the chi-square distribution with the given degrees of freedom to the right of your statistic. At a significance level of .05 the critical values are 3.84 for 1 degree of freedom, 5.99 for 2, 7.81 for 3 and 9.49 for 4: a statistic above the critical value gives p below .05.

Chi-square test of independence: a worked example

A subscription app surveys 400 customers (example data). One single-choice question records the plan: 240 customers pay monthly and 160 annually. Another asks "How satisfied are you with the app overall?" on a 5-point scale, which the team decided in advance to group as Satisfied (4–5), Neutral (3) and Dissatisfied (1–2). The observed counts:

PlanSatisfiedNeutralDis­satisfied
Monthly1206060
Annual1043620
Total2249680

As row percentages, 25% of monthly customers are dissatisfied against 12.5% of annual ones (our article on row and column percentages in survey tables explains which percentage to read). The chi-square test asks whether a gap like this is larger than chance alone would produce with 400 answers.

  1. Set the hypotheses and the significance level before looking at the test. H0: satisfaction is independent of plan type. H1: the two are related. α = .05.
  2. Compute each expected count. Monthly × Satisfied: 240 × 224 / 400 = 134.4. The other five cells follow the same rule and are listed in the table below.
  3. For each cell, compute (O − E)² / E. Monthly × Dissatisfied: (60 − 48)² / 48 = 3.00. Add all six: χ² = 11.61.
  4. Count the degrees of freedom: (2 − 1) × (3 − 1) = 2.
  5. Find the p-value for χ² = 11.61 with 2 degrees of freedom: p = .003. It is below .05, so you reject H0.
CellOE(O − E)² / E
Monthly, Satisfied120134.41.54
Monthly, Neutral6057.60.10
Monthly, Dissatisfied6048.03.00
Annual, Satisfied10489.62.31
Annual, Neutral3638.40.15
Annual, Dissatisfied2032.04.50
Sum40040011.61

Observed and expected counts for 400 customers. Monthly: satisfied 120 vs 134.4 expected, neutral 60 vs 57.6, dissatisfied 60 vs 48. Annual: satisfied 104 vs 89.6, neutral 36 vs 38.4, dissatisfied 20 vs 32. Chi-square is 11.61 with 2 degrees of freedom, p = .003.

The two Dissatisfied cells contribute 7.5 of the 11.61. To check the arithmetic, type two lines, “120 60 60” and “104 36 20”, into the chi-square calculator. It returns χ² = 11.607, p = 0.003, 2 degrees of freedom, Cramér's V = 0.170 and a smallest expected count of 32. It computes Pearson's chi-square without the Yates continuity correction.

How to interpret a chi-square result: p-value and Cramér's V

A chi-square p-value is the probability of getting a statistic at least this large if the two questions were truly independent. Here p = .003: if plan type and satisfaction were unrelated in the whole customer base, a table at least this far from the expected counts would turn up in about 3 samples out of 1,000.

A chi-square curve with 2 degrees of freedom. The critical value at the 5% level is 5.99. The example statistic of 11.61 lies far to the right, with only 0.3% of the area beyond it, so p = .003.

It does not mean there is a 0.3% chance that the variables are unrelated, and it says nothing about how strong the relationship is. Our article on the p-value covers the usual misreadings in detail.

For strength, use Cramér's V = √(χ² / (N × (k − 1))), where k is the smaller of the number of rows and columns. It runs from 0 (no association) to 1. Here V = √(11.607 / (400 × 1)) = 0.17. Coventry University's sigma statistics support centre tabulates guidelines derived from Cohen's 1988 book on power analysis, and they depend on k − 1, which is 1 for any table with 2 rows or 2 columns, 2 for 3×3 to 3×5 tables, 3 for 4×4 and 4×5, and 4 for 5×5:

k − 1SmallMediumLarge
10.100.300.50
20.070.210.35
30.060.170.29
40.050.150.25

Treat them as rough benchmarks for when your field has none of its own. By them, 0.17 is a small association. In business terms the gap is still clear: monthly customers are dissatisfied twice as often, 25% against 12.5%. Whether 12.5 percentage points matter is a decision for the team, and it is the number to put in front of them.

The test also says nothing about why. Customers may choose an annual plan after they already like the product, so the arrow can run from satisfaction to plan as easily as the other way. Do not conclude that moving people to annual billing will make them happier; the article on correlation in surveys covers this trap.

A report sentence you can adapt: "A chi-square test of independence showed that satisfaction was related to plan type, χ²(2, N = 400) = 11.61, p = .003, Cramér's V = .17. Monthly subscribers were dissatisfied twice as often as annual subscribers (25% vs 12.5%)." Replace the degrees of freedom, N, χ², p, V and the percentages with your own.

Which cells drive the result: adjusted residuals

The chi-square statistic is one number for the whole table. To see which cells make it large, compute the adjusted (standardized) residual of each cell: (O − E) / √(E × (1 − row share) × (1 − column share)), where the row share is the row total divided by N and the column share is the column total divided by N.

Penn State's STAT 504 course notes on two-way tables explain that these residuals follow a standard normal distribution approximately when H0 holds, so a value beyond 2 or 3 in absolute terms points to a cell that does not fit independence. For Monthly × Dissatisfied: (60 − 48) / √(48 × 0.4 × 0.8) = 12 / 3.92 = 3.06.

Adjusted residuals for the 2×3 table. Monthly: satisfied −2.96, neutral +0.57, dissatisfied +3.06. Annual: satisfied +2.96, neutral −0.57, dissatisfied −3.06. Only the satisfied and dissatisfied cells go beyond ±2.

The picture is clear. Monthly customers have more dissatisfied answers than expected (+3.06) and fewer satisfied ones (−2.96); annual customers mirror that. The Neutral column (±0.57) plays no part: 25% and 22.5% neutral are practically the same. In a table with two rows the residuals of the two rows are always mirror images. The same notes warn that the more cells a table has, the more likely one of them crosses 2 by chance, so treat a single residual of 2.1 in a 5×5 table with caution. In a spreadsheet, residuals take one formula per cell.

Chi-square assumptions and the expected count of 5 rule

The chi-square test assumes three things: each respondent is counted in exactly one cell, respondents are independent of one another, and expected counts are large enough for the chi-square distribution to approximate the real one. To generalize from respondents to a population, the sample also has to be random.

The last assumption has two common versions. Penn State's introductory STAT 200 lesson on chi-square tests asks for every expected count to be at least 5. The STAT 504 notes give a minimum that is less strict: all expected counts at least 1 and at least 80% of them 5 or more. Our calculator warns you whenever an expected count falls below 5 and quotes the looser version. The rule is about expected counts, not observed ones: a cell with an observed 2 is fine if its expected count is 6.

Here is how the rule plays out. Had the app team kept all five satisfaction points, "Very dissatisfied" would hold 12 answers in total: 10 monthly and 2 annual. The expected count for annual customers in that column is 160 × 12 / 400 = 4.8. One cell out of ten is below 5, so the table passes the 80% version and fails the strict one. Grouped into three categories, the smallest expected count is 32 and both versions pass.

When expected counts are too small, merge neighbouring categories that make sense together, such as "Dissatisfied" and "Very dissatisfied", and decide the grouping before you see the test result. For a 2×2 table, use Fisher's exact test, which the STAT 504 lesson on small samples describes. If neither fits, collect more responses.

Small counts can change the conclusion. In a pilot of a new onboarding email, 7 of 10 users who got version A completed setup against 4 of 14 who got version B. The smallest expected count is 4.58. Pearson's χ² = 4.03 gives p = .045, which looks significant. Fisher's exact test, which does not rely on the approximation, gives p = .095 (two-sided). With 24 people the honest answer is "not enough evidence yet", and the sample size guide shows how to plan the next round.

One more property worth knowing: the chi-square test ignores the order of categories. Swap the Neutral and Dissatisfied columns and χ² stays 11.61. If your question is ordered and the hypothesis is directional ("annual customers are more satisfied"), a test that uses the order, such as Mann-Whitney on the original 1–5 answers, can be more sensitive. The t-test guide covers when to switch to it.

Chi-square goodness of fit: does your sample match the population?

A goodness-of-fit test compares one question's counts with shares you know from outside the survey. A common use is checking whether respondents mirror the population on a variable you can verify, such as region.

Say the customer base splits 30% North, 45% Central and 25% South, and 300 customers answer the survey (example data). Expected counts are 300 × 0.30 = 90, 135 and 75.

RegionOE(O − E)² / E
North609010.00
Central1601354.63
South80750.33
Total30030014.96

With 3 − 1 = 2 degrees of freedom, χ² = 14.96 gives p < .001: the sample does not match the customer base, mainly because the North is under-represented (20% instead of 30%). You can either weight the responses to the known shares or set quotas by region in the next wave.

Our calculator handles two-way tables only, so run goodness of fit in a spreadsheet. Put observed counts in one column and expected counts in the next, then use CHISQ.TEST(observed, expected) in Excel. According to Microsoft's documentation, with a single column the function uses rows − 1 degrees of freedom, and it returns the p-value (0.00056 here), not χ² itself. A good fit on region also proves little about everything else: an opt-in sample can match the population on region and still differ in attitudes. The article on non-probability sampling explains why.

Common chi-square mistakes in survey analysis

Most errors come from feeding the test data it was not built for or reading more into the p-value than it holds. Check these before you report a result.

  • Enter counts of respondents. The same percentages from 40 or from 4,000 people give very different χ² values.
  • Count each person once. For a multiple-answer question, test each option as its own yes or no column.
  • When the same people answer twice, such as yes or no before and after a training, use McNemar's test for paired data instead.
  • Do not read p = .003 as a 0.3% chance that the null hypothesis is true.
  • Do not treat significance as importance. With 2,500 people per group, 50% against 53% gives p = .034 and a Cramér's V of only 0.03.
  • Do not claim that one variable causes the other.
  • Do not slice the data by every segment and report only the tables under .05. At α = .05, about 1 in 20 tests on unrelated questions comes out significant by chance.
  • Do not present a result from an opt-in sample as true for the whole population without saying how respondents were recruited.

How to run a chi-square test on your survey data in SurveyNinja

Ask the two questions as single-choice questions with categories that do not overlap. The customer satisfaction survey template is a ready starting point, or build your own in the survey maker. If you want to compare the sample with a known split, such as region, ask that variable as a single-choice question too. Should the goodness-of-fit test show a gap, set quotas for it in the next wave; quotas are available on every plan.

When responses are in, export them to CSV, XLSX or SPSS, which works on every plan, including Free. Count the answers for each pair of categories with a pivot table or COUNTIFS in a spreadsheet, or in SPSS. Then paste the counts into the free chi-square calculator: it takes tables from 2×2 to 5×5, works without an account and returns χ², the p-value, degrees of freedom, Cramér's V and the smallest expected count, with a warning when an expected count falls below 5.

Frequently asked questions

What is a chi-square test used for?

It tests whether counts in categories differ from what you would expect by chance. In surveys it checks whether two single-choice questions are related, such as plan type and satisfaction group (test of independence), or whether one question's split matches a known distribution, such as respondents' regions against the customer base (goodness of fit). It works on counts, not on percentages or means.

How do you calculate chi-square step by step?

Build a table of counts with totals. For each cell, compute the expected count as row total × column total ÷ N, then (observed − expected)² ÷ expected. Add these values to get χ². The degrees of freedom are (rows − 1) × (columns − 1). Look up the p-value for that χ² and df. In the 2×3 example of 400 answers, χ² = 11.61, df = 2 and p = .003.

What does the p-value of a chi-square test tell you?

It is the probability of a χ² at least as large as yours if the two variables were truly independent. A p-value below the significance level you chose in advance, usually .05, means you reject independence. It is not the probability that the variables are unrelated, and it does not measure how strong the relationship is; Cramér's V does that.

What if expected counts are less than 5?

Then the chi-square p-value may be inaccurate. A strict version of the rule wants every expected count to be at least 5; a looser one accepts all expected counts of at least 1 with at least 80% of them 5 or more. If your table fails, merge neighbouring categories that make sense together, use Fisher's exact test for a 2×2 table, or collect more responses.

How do you report a chi-square test in APA style?

Give the degrees of freedom, the sample size, χ² to two decimals, the p-value and an effect size, then describe the pattern in plain words. For example: χ²(2, N = 400) = 11.61, p = .003, Cramér's V = .17; monthly subscribers were dissatisfied twice as often as annual subscribers, 25% against 12.5%.

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

Goodness of fit uses one question and compares its counts with shares known in advance, so its degrees of freedom are the number of categories minus 1. The test of independence uses two questions from one sample and builds expected counts from the table's own totals, with (rows − 1) × (columns − 1) degrees of freedom. The formula χ² = Σ (O − E)² / E is the same for both.

0