Tests · 7 min read

The chi-square test, explained

For counts in categories rather than measurements. A worked 2×2 example, the assumptions that matter, and how to report the result.

Advertisement
Updated November 2026 · Worked example included

Not all data are measurements. Often you have counts in categories: how many patients improved or did not, how many survey respondents chose each option, how many seeds germinated under each treatment. For this kind of data, t-tests and ANOVA do not apply. The standard tool is the chi-square (χ²) test of independence. This guide explains the idea, works through a 2×2 example by hand, and covers the assumptions that trip people up.

The question it answers

Are two categorical variables related, or are the counts consistent with no association? For example: is the proportion of patients who improve the same with and without treatment?

The data are arranged in a contingency table, with one variable in the rows and the other in the columns. Each subject is counted in exactly one cell.

A worked example

One hundred patients are randomly assigned to a new treatment or a control, and we record whether they improved:

ImprovedNot improvedTotal
Treatment302050
Control183250
Total4852100

60% improved with treatment versus 36% with control. Could a difference this large happen by chance?

Step 1: expected counts

If treatment made no difference, both groups would show the overall improvement rate of 48%. The expected count for each cell is (row total × column total) / grand total:

Step 2: compare observed with expected

For each cell, compute (observed − expected)² / expected, then add them up:

χ² = 1.50 + 1.50 + 1.38 + 1.38 = 5.77

Step 3: degrees of freedom and p-value

Degrees of freedom = (rows − 1) × (columns − 1) = 1 for a 2×2 table. Comparing 5.77 with the chi-square distribution with 1 df gives p = 0.016. The improvement rate differs between the groups more than chance alone would easily explain.

Assumptions and pitfalls

Each subject counted once

The cells must contain counts of independent subjects. If the same person appears in several cells (for example, answers measured before and after), the chi-square test is wrong; McNemar's test handles paired yes/no data.

Use counts, never percentages

The test depends on the actual numbers. 60% vs 36% is convincing with 50 per group but means nothing with 5 per group. Running a chi-square test on percentages as if they were counts gives a meaningless result.

Expected counts should not be too small

The chi-square distribution is only an approximation. The usual guideline is that all expected counts should be at least 5 (some allow up to 20% of cells between 1 and 5 in larger tables). When counts are small, use Fisher's exact test, which calculates the p-value exactly and is valid at any sample size.

Yates' continuity correction

For 2×2 tables, some software applies Yates' correction by default, which makes the test more conservative. Applied to our example, it gives χ² = 4.85 and p = 0.028. Either is acceptable if you say which you used, and many statisticians now prefer the uncorrected test or Fisher's exact test instead.

How strong is the association?

Like any p-value, the chi-square p-value does not measure the size of the effect. Useful effect sizes for tables are:

Larger tables

The same procedure works for any number of rows and columns, for example three treatments by four outcome categories (df = 2 × 3 = 6). A significant result says the variables are associated somewhere in the table, but not where. Look at the standardized residuals, (observed − expected) / √expected, for each cell: values beyond about ±2 show where the counts depart most from independence. If you then compare specific pairs of rows, correct for multiple comparisons (see post-hoc tests).

Chi-square goodness-of-fit

A related test compares the counts of one variable with expected proportions, for example whether offspring follow a 3:1 Mendelian ratio, or whether customers choose four product designs equally often. The calculation is the same, with df = number of categories − 1.

How to run it

StatFlo focuses on numeric measurements and does not currently run chi-square tests. For a quick calculation in a spreadsheet, put the observed counts in one block and the expected counts (row total × column total / grand total) in another, then use =CHISQ.TEST(observed_range, expected_range), which returns the p-value. In R, chisq.test(matrix(c(30, 18, 20, 32), nrow = 2)) runs the test (with Yates' correction by default for 2×2; add correct = FALSE to turn it off), and fisher.test() runs Fisher's exact test.

How to report it

"More patients improved with treatment than with control (30/50, 60% vs 18/50, 36%; χ²(1, N = 100) = 5.77, p = 0.016; relative risk 1.67)."

Working with measurements instead of counts?

StatFlo runs t-tests, ANOVA and their non-parametric alternatives on your CSV or Excel data.

Open StatFlo