A chi-square test (χ² test) checks whether the counts you’ve observed in categories differ from the counts you’d expect by chance. Most often it tests whether two categorical variables are related (the test of independence), or whether one variable matches an expected distribution (the goodness-of-fit test).
You work it out by comparing observed and expected counts in every cell of your table and adding up (O − E)²/E. Then you read the total against the chi-square distribution. The test only works on counts, never on percentages. A two-by-two table has one degree of freedom, and a result above 3.841 is significant at the 5% level.
Doll and Hill’s 1950 study of smoking and lung cancer printed χ² = 5.76 for its women patients. The worked example below reproduces that figure step by step.
When do you use a chi-square test?
Use a chi-square test when both of your variables are categories and your data are counts of people or things in each category. Think of smoker or non-smoker, treatment group or control, pass or fail. Each person falls into one category of each variable, and you count how many land in each combination.
If your outcome is a measurement, such as reaction time or blood pressure, you need a different test. Comparing average scores between groups is a job for a t-test or ANOVA. The chi-square test is non-parametric (distribution-free): it doesn’t assume your data follow a normal distribution.
| Your data | Test to consider |
|---|---|
| Two categorical variables, independent groups | Chi-square test of independence |
| One categorical variable against expected proportions | Chi-square goodness-of-fit test |
| A two-by-two table with small expected counts | Fisher’s exact test |
| The same people measured twice (paired categories) | McNemar’s test |
| A measurement compared between two groups | t-test (not chi-square) |
Source: BMJ, Statistics at Square One, chapter 8; McHugh (2013), Biochemia Medica.
Our guide to the statistical treatment of data introduces the two broad kinds of statistics, descriptive and inferential. The chi-square test sits on the inferential side: it asks whether a pattern in your sample is likely to reflect more than chance.
The two types of chi-square test
The two tests use the same formula. What differs is where your expected counts come from.
| Test of independence | Goodness-of-fit test | |
|---|---|---|
| Question | Are two categorical variables related? | Do the counts for one variable match a theory? |
| Data | A contingency table (rows × columns) | One row of counts |
| Null hypothesis | The two variables are independent | The counts follow the expected proportions |
| Expected counts from | Row and column totals | The theoretical proportions × the total |
| Degrees of freedom | (rows − 1) × (columns − 1) | Number of categories − 1 (when the proportions come from theory) |
Source: BMJ, Statistics at Square One, chapter 8; NIST/SEMATECH e-Handbook of Statistical Methods.
A goodness-of-fit example: the BMJ’s Statistics at Square One describes a laboratory population of 784 mice (where it’s called the chi-squared test). Genetic theory predicts 51.0% entirely white, 40.8% with a small brown patch and 8.2% with a large one. The expected counts are those proportions multiplied by 784, and the test has two degrees of freedom. The result falls well below the 5% critical value, so the counts don’t differ significantly from what the theory predicts.
The rest of this guide follows the chi-square test of independence, the version for comparing two variables. If you’re still pinning down what kind of study you’re running, our overview of types of research sets out the main ways research is classified.
How to calculate a chi-square test, step by step
You can calculate a chi-square test by hand in five steps.
- State the null hypothesis. For a test of independence, it’s that the two variables aren’t related. The alternative hypothesis is that they are.
- Work out the expected count (also called the expected frequency) for each cell. Multiply the cell’s row total by its column total, then divide by the grand total.
- Calculate (O − E)²/E for each cell. O is the observed count and E the expected count.
- Add up the cell values. The sum is your chi-square statistic. In full, the chi-square formula is χ² = Σ (O − E)²/E.
- Compare it with the chi-square distribution at your degrees of freedom, using a critical value table or the p-value from software.
A quick check after step 2: your expected counts should add up to the same totals as your observed counts. If they don’t, there’s an arithmetic slip somewhere.
Chi-square test example: smoking and lung cancer in 1950
In 1950 Richard Doll and Austin Bradford Hill published their preliminary report on smoking and lung cancer in the BMJ. They interviewed patients with lung cancer in 20 London hospitals, and compared them with control patients who had diseases other than cancer.
We’re using their table because it’s real, published data with a chi-square value printed beside it. That means you can check every step of the method against the authors’ own answer.
Doll and Hill defined a smoker as someone who had smoked at least one cigarette a day for at least a year. So ‘non-smoker’ below means someone who had never smoked that regularly, not someone who had never touched a cigarette.
One thing to notice about the design: each control was chosen to match a lung cancer patient by sex and five-year age group, and usually by hospital. Doll and Hill compared the two groups as independent samples, and we do the same here to reproduce their figure. With your own matched data, the analysis should take the matching into account (see McNemar’s test below).
Step 1: the observed counts
Here are the figures for women in their Table IV:
| Women | Non-smokers | Smokers | Total |
|---|---|---|---|
| Lung cancer patients | 19 | 41 | 60 |
| Control patients | 32 | 28 | 60 |
| Total | 51 | 69 | 120 |
Source: Doll and Hill (1950), BMJ, Table IV.
The null hypothesis is that smoking status and lung cancer are independent in these patients. Put another way, the proportion of non-smokers should be about the same in both groups. In fact 19 of 60 lung cancer patients (31.7%) were non-smokers, against 32 of 60 controls (53.3%). The test asks whether a gap that size could be chance.
Step 2: the expected counts
If smoking and lung cancer were unrelated, each group of 60 would contain the same share of non-smokers as the whole sample. That share is 51 out of 120.
- Expected non-smokers in each group: 60 × 51 / 120 = 25.5
- Expected smokers in each group: 60 × 69 / 120 = 34.5
Each row adds up to 60 and each column to its observed total, so the check passes.
Steps 3 and 4: each cell’s contribution and the total
| Cell | Observed (O) | Expected (E) | (O − E)² / E |
|---|---|---|---|
| Lung cancer, non-smokers | 19 | 25.5 | 1.657 |
| Lung cancer, smokers | 41 | 34.5 | 1.225 |
| Controls, non-smokers | 32 | 25.5 | 1.657 |
| Controls, smokers | 28 | 34.5 | 1.225 |
| Chi-square | 5.763 |
Source: our calculation from Doll and Hill (1950), Table IV.
Every cell is 6.5 away from its expected count, so each (O − E)² is 42.25. Rounded to two decimal places, the total is 5.76. That’s exactly the figure Doll and Hill printed: ‘χ² = 5·76; n = 1’, where n is their notation for degrees of freedom.
Step 5: the decision
A two-by-two table has (2 − 1) × (2 − 1) = one degree of freedom. At one degree of freedom, the 5% critical value is 3.841 and the 1% value is 6.635. Our 5.76 sits between them, so P lies between 0.01 and 0.05. Doll and Hill narrowed it to 0.01 < P < 0.02, and software gives the p-value as about 0.016.
So you reject the null hypothesis at the 5% level. Among these patients, women with lung cancer were less likely to be non-smokers than the controls. Doll and Hill’s own reading was that smoking was much less common among women than men, but significantly more frequent among women with lung cancer.
What this means for you: a chi-square test tells you whether two variables are associated in your data. On its own, it can’t tell you why. Doll and Hill went on to look at how much each patient smoked, not just whether they smoked.
The men’s figures
The men’s numbers were far larger: 649 lung cancer patients and 649 controls. Only 2 of the lung cancer patients were non-smokers (0.3%), against 27 of the controls (4.2%). If you run the same steps, the expected number of non-smokers in each group is 14.5, and chi-square comes to 22.04.
Doll and Hill didn’t report a chi-square for the men’s figures in Table IV. They used an exact method instead, which gave P = 0.00000064. Either way, the evidence of an association is far stronger than for the women.
How to read the result: degrees of freedom, critical values and p
Your chi-square value means nothing until you set it against the right number of degrees of freedom. The larger your table, the larger the value you need to reach significance.
| Degrees of freedom | 5% critical value (p = 0.05) | 1% critical value (p = 0.01) |
|---|---|---|
| 1 | 3.841 | 6.635 |
| 2 | 5.991 | 9.210 |
| 3 | 7.815 | 11.345 |
| 4 | 9.488 | 13.277 |
| 5 | 11.070 | 15.086 |
Source: NIST/SEMATECH e-Handbook of Statistical Methods, upper-tail critical values of the chi-square distribution.
So what does 0.05 mean in a chi-square test? It’s the conventional significance level. If your value is above the critical value, or your p-value is below 0.05, you reject the null hypothesis of independence. If your p-value is 0.05 or more, you don’t reject the null hypothesis: there isn’t enough evidence of an association.
When software reports ‘p = .000’, the p-value is just too small to display. Write p < .001 instead.
A significant result covers the whole table. To see where the difference comes from, look at the cell contributions: the cells with the largest (O − E)²/E values drive the result. In the women’s table, the two non-smoker cells (1.657 each) contribute most, because their expected counts are smaller.
Chi-square test assumptions, and what to do when they fail
The chi-square test is only reliable when your data meet a few conditions.
- Counts, not percentages. Run the test on the actual number of cases in each cell, never on percentages, proportions or averages.
- One cell per person. Each person or item counts once, in one category of each variable. If you’re unsure whether your cases are individuals or groups, our guide to the unit of analysis explains, with examples, when it’s each.
- Independent groups. The test doesn’t work for paired data, such as the same people measured twice, or parents matched with their children.
- Large enough expected counts. This is the rule people trip over, and it’s about expected counts, not observed ones.
For the expected-count condition, the published rules of thumb agree in substance. For tables bigger than two by two, no more than about a fifth of cells (20%) should have an expected count below 5. None should be below 1.
A two-by-two table has its own rule. The test isn’t appropriate if the total is under 20, or between 20 and 40 with any expected count under 5.
SPSS checks this for you. A footnote under its chi-square table reports how many cells have an expected count below 5. Read it before you trust the p-value.
If your expected counts are too small
For a two-by-two table, use Fisher’s exact test instead: with small numbers the chi-square approximation becomes poor, and Fisher’s test doesn’t rely on it. For a larger table, you can sometimes leave out very small categories, as long as your question still stands without them.
Yates’ continuity correction
On two-by-two tables some researchers apply Yates’ correction, which subtracts 0.5 from the size of each difference between observed and expected counts. There’s no precise rule for when to use it. Common practice is to apply it when the table’s total is under 100, or any cell holds fewer than 10.
The correction reduces the value of chi-square. For Doll and Hill’s women it gives 4.91 instead of 5.76, with p ≈ 0.027: still significant at 5%. With smaller numbers, though, it can turn a significant result into a non-significant one.
If your data are paired
If the same people answered before and after, or each case is matched to a control, use McNemar’s test. It’s a version of the chi-square test built for paired categorical data.
How strong is the association? Cramér’s V
A significant chi-square tells you the pattern is unlikely to be chance alone. It doesn’t tell you how strong the association is.
Cramér’s V is the most commonly used measure of strength for a chi-square test, and you read it like a correlation. For a two-by-two table it’s the square root of chi-square divided by the sample size.
For Doll and Hill’s women: √(5.763 / 120) = 0.22. McHugh’s 2013 guide to the test treats a value of 0.26 as a weak correlation, so 0.22 is a weak association. So the women’s result is significant but modest, which is exactly why reporting both numbers matters.
How to report a chi-square test in your dissertation or thesis
Report the test statistic with its degrees of freedom and the p-value, then the effect size. A common format puts them in brackets after a plain sentence:
There was a significant association between [variable 1] and [variable 2] (χ²([df]) = [value], p = [value], Cramér’s V = [value]).
For Doll and Hill’s women, that becomes:
Among female patients, lung cancer was significantly associated with smoking status (χ²(1) = 5.76, p = .016, Cramér’s V = .22).
A few habits make the write-up stronger:
- Give the counts table (with row or column percentages) alongside the test, so readers can see the pattern.
- Say if you used Yates’ correction or Fisher’s exact test, and why.
- Mention that the expected-count assumption was met, or what you did when it wasn’t.
- Follow your department’s style guide on details like italics and leading zeros.
If your data came from a questionnaire, your methods chapter should also describe the research instrument you used to collect the responses.
Running a chi-square test in SPSS, R and Excel
You’ll usually run the test in software, but it pays to know what each one does by default.
- SPSS: go to Analyze > Descriptive Statistics > Crosstabs, put one variable in Rows and the other in Columns, and tick Chi-square under Statistics. Read the Pearson Chi-Square row. For two-by-two tables SPSS also prints Fisher’s exact test and the continuity-corrected (Yates) result.
- R: use the chisq.test() function. For two-by-two tables it applies Yates’ correction by default (correct = TRUE). To reproduce the uncorrected 5.76 above, set correct = FALSE.
- Excel: the function CHISQ.TEST(actual_range, expected_range) returns the p-value, not the chi-square statistic. You need to build the table of expected counts yourself first.
Common mistake: comparing an R result with a hand calculation and finding they don’t match. On a two-by-two table, R has usually applied Yates’ correction.
Using the chi-square test in your own research
Before you run a chi-square test, set out your counts table and check the expected counts. If any are small, decide on your fallback before you see the result: Fisher’s exact test, or leaving out very small categories. Then write it up with the template above.
Your categories may have a natural order, and you may want to know whether one variable rises with another. If so, a monotonic relationship and a rank correlation may answer your question better. Enjoying the statistics more than you expected? Our guide to doing a PhD in statistics covers research areas, entry requirements, funding and careers.
Ready to take your research further? Browse PhD opportunities across the sciences.
Sources
- Biochemia Medica: McHugh (2013), The chi-square test of independence
- NIST/SEMATECH e-Handbook of Statistical Methods: Chi-square goodness-of-fit test
- NIST/SEMATECH e-Handbook of Statistical Methods: Critical values of the chi-square distribution
- Kent State University Libraries: SPSS tutorials, Chi-Square Test of Independence
- Newcastle University: Cross tabulation (chi-square test)
- R documentation: chisq.test
- Microsoft Support: CHISQ.TEST function

