Guides  /  chi square test

The chi-square test: a complete guide

Chi-square is the test for categorical data, and the one most often run on data that break its assumptions. This guide covers the two versions and when each applies, the expected-count rule that decides whether your result is trustworthy, a full calculation by hand, effect sizes, and how to work out which cells drove a significant result.

Hafiz Ahmad Tariq Written and reviewed by Hafiz Ahmad Tariq, Senior Biostatistician
Updated 17 August 202618 min read
What is a chi-square test?

A chi-square test compares the counts you observed in categories against the counts you would expect if there were no relationship in the population. There are two main versions: the goodness-of-fit test compares one categorical variable against an expected distribution, and the test of independence examines whether two categorical variables are associated. A significant result means the observed pattern is unlikely to have arisen by chance.

Definition

What chi-square does

Chi-square compares the counts you actually observed with the counts you would expect if nothing were going on. The bigger the gap between observed and expected, the larger the statistic, and the less plausible it becomes that the pattern arose by chance.

The arithmetic is one formula applied to every cell of your table and then summed:

χ² = Σ (O − E)² / E

Each cell contributes an amount proportional to how far it strays from expectation, scaled by how big the expectation was. A discrepancy of 10 matters enormously when you expected 5 and barely at all when you expected 500 — dividing by E is what encodes that.

df = 2 df = 6 12.59 critical value, α = .05 χ² = 6.83 Chi-square is always positive and right-skewed; only the upper tail rejects. As df rises the whole distribution shifts right, so bigger tables need bigger statistics.
Chi-square is positive and right-skewed. Only large values reject the null, so the test is always one-tailed in practice even when the hypothesis is non-directional.
Chi-square works on counts, never on percentages

This is the single most common fatal error. If you feed percentages into the formula, the result depends on the arbitrary base of 100 rather than your actual sample, and the p-value is meaningless. Always use raw frequencies. If your dataset only holds percentages and the original counts are genuinely unavailable, no chi-square can be computed from it at all.

Choosing

The two chi-square tests

TestVariablesQuestiondf
Goodness of fitOne categoricalDoes the distribution match what was expected?k − 1
Test of independenceTwo categoricalAre the two variables associated?(r−1)(c−1)

A third variant, the test of homogeneity, is arithmetically identical to the test of independence and differs only in how the sample was collected — whether you sampled one population and cross-classified, or sampled fixed numbers from several populations. The calculation and the reporting are the same.

When chi-square is not the right test

Your outcome is continuous — use a t-test or ANOVA, not a chi-square on binned data
Your data are paired (same people measured twice) — use McNemar's test
Expected counts are very small — use Fisher's exact test
You want to control for other variables — use logistic regression
Your categories are ordered and you want to test a trend — use the linear-by-linear association test
Not sure chi-square is the right test for your data?

Send the variables and how they were measured. A named statistician confirms the test, runs it, and returns annotated output with the reporting sentence.

Get a fixed quote

Assumptions

Assumptions, and the expected-count rule

Chi-square has few assumptions, but they are strict and the third one is violated constantly.

AssumptionWhat it meansIf violated
Independent observationsEach participant appears in exactly one cellUse McNemar or a mixed model
Counts, not percentagesRaw frequencies in every cellRecalculate from the raw data
Expected counts large enoughAll ≥ 5, or at least 80% of cells ≥ 5 and none below 1Use Fisher's exact test, or merge categories
Mutually exclusive categoriesNo participant in two cellsRestructure the variable

The independence assumption is the one with the least room for negotiation, and it is broken more often than people realise. It requires that each participant contributes exactly one observation to exactly one cell. If you asked 60 people three questions each and cross-tabulated all 180 responses, your table looks like it has 180 independent observations when it really has 60 people answering repeatedly. The statistic is then inflated, sometimes dramatically, and no correction will rescue it. The fix is structural rather than statistical: analyse one response per person, or move to a model that accounts for the repeated measurements, such as a generalised estimating equation or a mixed-effects logistic regression.

The expected-count rule in practice

Note that the rule applies to expected counts, not observed ones. A cell can contain zero observations and still be fine, provided the expected count for that cell is 5 or more. Every statistics package reports the minimum expected count alongside the test — check it every time, and report it.

What to do when the rule fails

For a 2×2 table, use Fisher's exact test, which gives an exact p-value with no minimum-count requirement. For larger tables, either merge sparse categories into a sensible combined category — deciding the merge on substantive grounds, before seeing the result — or use an exact test if your software supports it. Do not simply report the chi-square and hope nobody checks.

Worked example

Worked example: test of independence

167 students are asked whether they agree that statistics training should be compulsory. We want to know whether responses differ between undergraduates and postgraduates.

Observed counts, with expected in brackets AgreeNeutralDisagreeTotal UndergraduatePostgraduateTotal 45(38.3) 22(24.0) 13(17.7) 35(41.7) 28(26.1) 24(19.3) 8087 8050 37167 expected = (row total × column total) / grand total e.g. undergraduates who agree: (80 × 80) / 167 = 38.3 Chi-square measures how far the observed counts stray from what independence would predict.
Observed counts with expected counts in brackets. Each expected count is the row total times the column total, divided by the grand total.

Step 1 — expected counts

E = (row total × column total) / grand total. For undergraduates who agree: (80 × 80) / 167 = 38.3. Repeat for all six cells. The smallest expected count is 17.7, comfortably above 5, so the test is valid.

Step 2 — the cell contributions

CellOEO − E(O−E)² / E
UG / Agree4538.36.71.16
UG / Neutral2224.0−2.00.16
UG / Disagree1317.7−4.71.26
PG / Agree3541.7−6.71.07
PG / Neutral2826.11.90.15
PG / Disagree2419.34.71.16
χ²4.96

Step 3 — degrees of freedom and the decision

df = (r − 1)(c − 1) = (2−1)(3−1) = 2. The critical value for df = 2 at α = .05 is 5.99. Our statistic of 4.96 is smaller, so p = .084 and we do not reject the null.

Reading a near-miss honestly

At p = .084 the sensible conclusion is that this sample provides insufficient evidence of an association — not that no association exists. The pattern in the table runs in a consistent direction, and a larger sample might well detect it. Say that, rather than declaring the variables independent.

Worked example

Worked example: goodness of fit

A department claims its intake is evenly split across four subject backgrounds. Among 120 students, the observed counts are 38, 24, 31 and 27.

If the claim were true, each category would contain 120 / 4 = 30 students.

BackgroundObservedExpected(O−E)² / E
Sciences38302.13
Humanities24301.20
Social sciences31300.03
Business27300.30
χ²1201203.66

df = k − 1 = 3, and the critical value is 7.81. Since 3.66 < 7.81, p = .300: the data are consistent with an even split.

Expected proportions need not be equal

Goodness of fit works against any specified distribution, not just a uniform one. If national figures said the split should be 40/20/25/15, use those proportions to compute the expected counts. State where the expected distribution came from — an examiner will ask.

Effect size

Effect size: phi and Cramer's V

Chi-square is heavily influenced by sample size. Multiply every count in a table by ten and the statistic multiplies by ten too, while the pattern of association is unchanged. With a large enough sample, trivial departures from independence become significant. An effect size is what separates the two.

MeasureUse forFormula
Phi (φ)2×2 tables only√(χ² / n)
Cramer's VAny size table√(χ² / (n × df*)) where df* is the smaller of (r−1), (c−1)
Odds ratio2×2 tables(a×d) / (b×c)

For our worked example: V = √(4.96 / (167 × 1)) = √0.0297 = 0.172 — a small association.

Cramer's VInterpretation (df* = 1)
0.10Small
0.30Medium
0.50Large
Always report an effect size

A chi-square of 45.2 on 12,000 participants sounds decisive and may correspond to a Cramer's V of 0.06 — an association so weak it has no practical meaning. Reporting V alongside the test is what stops a large sample manufacturing an important-sounding finding.

Get the test, the effect size and the interval together

Our chi-square calculator gives the statistic, exact p-value, degrees of freedom, expected counts and Cramer's V, with a sentence formatted for reporting.

Use the calculator

Follow-up

Which cells caused the result?

A significant chi-square on a table larger than 2×2 tells you the variables are associated somewhere, but not where. Reporting only the overall test leaves the interesting part unsaid.

Adjusted standardised residuals

For each cell, the adjusted standardised residual measures how far the observed count sits from expectation in standard-error units. Because they are approximately standard normal, they read like z-scores:

Beyond ±1.96 — that cell differs from expectation at p < .05
Beyond ±2.58 — p < .01
Positive — more observations than expected under independence
Negative — fewer observations than expected

Because you are inspecting several cells at once, apply a Bonferroni correction: divide .05 by the number of cells. For a 2×3 table that gives .05/6 = .0083, corresponding to a residual beyond about ±2.64.

Say which direction the association runs

“There was a significant association” is not a finding an examiner can use. “Postgraduates were significantly more likely than undergraduates to disagree (adjusted residual = 2.1)” is. The residuals are what let you write the second sentence.

Special case

2×2 tables: Yates, Fisher and the odds ratio

The 2×2 table is the most common chi-square in applied research — treated versus untreated, improved versus not — and it has its own set of conventions worth knowing.

Yates' continuity correction

Chi-square uses a continuous distribution to approximate discrete counts, and with only one degree of freedom that approximation is at its roughest. Yates' correction subtracts 0.5 from each absolute difference before squaring, which shrinks the statistic and raises the p-value:

χ²ₖₐₜₑₛ = Σ (|O − E| − 0.5)² / E

Whether to apply it is genuinely contested. The correction is conservative — it can be too conservative, costing real power in moderate samples. The common position is to use Fisher's exact test when expected counts are small, and the uncorrected chi-square when they are comfortably above 5, rather than relying on Yates as a middle path. SPSS reports it by default for 2×2 tables, so state which row of the output you are quoting.

A worked 2×2

ImprovedNot improvedTotal
Intervention341650
Control222850
Total5644100

Expected counts are 28, 22, 28 and 22 — all well above 5, so no exact test is needed. The uncorrected statistic is χ²(1, N = 100) = 5.844, p = .016.

The odds ratio makes the size of the effect concrete: OR = (34 × 28) / (16 × 22) = 952 / 352 = 2.70. The odds of improvement were about 2.7 times higher in the intervention group.

Odds ratio is not risk ratio

An odds ratio of 2.70 does not mean participants were 2.7 times more likely to improve. The risk ratio here is (34/50)/(22/50) = 1.55. Odds ratios exaggerate relative to risk ratios whenever the outcome is common, and confusing the two overstates your effect — a mistake reviewers in health research catch immediately.

Get the odds ratio with its confidence interval

Our odds ratio calculator returns the OR, its 95% confidence interval and the risk ratio side by side, so you can report the one your design calls for.

Use the calculator

Pitfalls

Seven mistakes that cost marks

1. Running chi-square on percentages

The statistic scales with the total, so percentages produce an answer for a sample of 100 regardless of your real n. Always use raw counts.

2. Ignoring the expected-count rule

Report the minimum expected count. If any cell falls below 5, switch to Fisher's exact test or justify merging categories.

3. Using chi-square on paired data

If the same people are measured before and after, the observations are not independent. Use McNemar's test.

4. Reporting the test without an effect size

With a large sample, significance is close to guaranteed. Cramer's V is what tells the reader whether it matters.

5. Concluding causation

Chi-square establishes association between categorical variables. It says nothing about direction or cause.

6. Stopping at the overall test

For tables larger than 2×2, examine adjusted residuals so you can say where the association lies.

7. Binning a continuous variable to force a chi-square

Cutting a continuous outcome into categories throws away information and power. Use the test the measurement level actually supports.

Reporting

Reporting chi-square in APA style

APA 7 wants the symbol italicised, degrees of freedom and sample size in parentheses together, and an exact p-value.

SituationHow to write it
Significantχ²(2, N = 167) = 8.94, p = .011, V = .23
Not significantχ²(2, N = 167) = 4.96, p = .084, V = .17
Goodness of fitχ²(3, N = 120) = 3.66, p = .300
Fisher's exactFisher's exact test, p = .043
Very small pp < .001 — never p = .000
Include N inside the parentheses with df — this is specific to chi-square
Report the minimum expected count so the reader can judge validity
Give Cramer's V or phi for every test
Include the contingency table with both counts and row or column percentages
State the direction of any significant association in words
Say if you used Fisher's exact and why
A sentence that earns marks

“Agreement differed significantly by level of study, χ²(2, N = 167) = 8.94, p = .011, Cramer's V = .23. All expected counts exceeded 17. Inspection of adjusted residuals indicated postgraduates were more likely to disagree than expected (adjusted residual = 2.4).” That reports the test, its validity, the effect size and the direction.

Have your categorical analysis checked before submission

Send your dataset and draft results. A named statistician verifies expected counts, confirms the test suits the design, and returns APA tables you can use directly.

See SPSS data analysis

Answers

Frequently asked questions

What is a chi-square test used for?

It tests whether observed counts in categories differ from what would be expected by chance. The goodness-of-fit version compares one categorical variable against an expected distribution; the test of independence examines whether two categorical variables are associated. Both work on raw frequencies, never on percentages.

What are the assumptions of the chi-square test?

Observations must be independent, the data must be raw counts rather than percentages, categories must be mutually exclusive, and expected counts must be large enough — conventionally all at least 5, or at least 80% of cells at 5 or more with none below 1. The expected-count rule is the one most often broken.

What do I do if expected counts are less than 5?

For a 2×2 table use Fisher's exact test, which has no minimum-count requirement. For larger tables, either merge sparse categories on substantive grounds decided before you see the result, or use an exact test if your software offers one. Do not report the chi-square as though the assumption held.

How do I calculate degrees of freedom for chi-square?

For a test of independence, df = (rows − 1) × (columns − 1). For a goodness-of-fit test, df = number of categories − 1. Degrees of freedom depend only on the size of the table, never on the sample size.

What effect size should I report with chi-square?

Cramer's V for any table, or phi for a 2×2. Both range from 0 to 1, with .10 small, .30 medium and .50 large when df* = 1. An effect size is essential because chi-square scales with sample size, so a large study can produce a significant result for a trivially weak association.

Can chi-square tell me which groups differ?

Not on its own. For tables larger than 2×2 a significant result means an association exists somewhere. Examine adjusted standardised residuals to find which cells deviate from expectation — values beyond ±1.96 are significant at .05, though you should apply a Bonferroni correction for the number of cells.

What is the difference between chi-square and Fisher's exact test?

Chi-square uses an approximation that becomes unreliable with small expected counts. Fisher's exact test computes the probability directly, so it is valid at any sample size. Use Fisher's for 2×2 tables with small expected counts; chi-square is fine for larger samples and is easier to extend to bigger tables.

Can I use chi-square on paired data?

No. Chi-square requires independent observations, so it cannot be used when the same participants are measured twice. Use McNemar's test for paired binary data, or Cochran's Q for more than two paired measurements.

Send the data. Get a fixed quote.

Attach your dataset or just describe the project. A named statistician replies with a price and a deadline, usually within one working day.