TL;DR: The chi-square test of independence checks whether two categorical variables are related. Put the counts in a table, work out the expected count for each cell, measure how far each observed count is from its expected count, and add the results up. In our worked example — coffee vs tea preference by gender, 200 people — the chi-square value is 4.51 with 1 degree of freedom, above the 5% critical value of 3.841, so preference and gender are associated in this sample.
Does drink preference depend on gender? Does shopping habit depend on age group? Does passing an exam depend on the study method used? These are all questions about whether two categorical variables are related — and the chi-square test of independence is the standard way to answer them. If you are new to the test itself, start with what the chi-square test is; this guide walks through the independence test step by step, with every number in the example worked out in full. You can check the result with our free Chi-Square Calculator.
What does “independence” mean here?
Two variables are independent if knowing one tells you nothing about the other. If drink preference is independent of gender, then men and women prefer coffee in the same proportions, and knowing that someone is a man gives you no clue about their drink. If the variables are associated (the opposite of independent), the proportions differ between groups — one group leans one way and the other leans the other.
The test starts by assuming independence — that is the null hypothesis — and asks whether the data is far enough from that assumption to reject it. It never proves the variables are independent; it only says whether the evidence against independence is strong enough.
Step 1: Put the counts in a contingency table
Everything starts with a table of counts, called a contingency table. Suppose 200 people were asked whether they prefer coffee or tea: 100 men and 100 women. The answers were:
| Coffee | Tea | Row total | |
|---|---|---|---|
| Men | 60 | 40 | 100 |
| Women | 45 | 55 | 100 |
| Column total | 105 | 95 | 200 |
At a glance, men lean towards coffee (60 out of 100) and women lean slightly towards tea (55 out of 100). But is that gap real, or the kind of wobble you would see in any random sample of 200 people? The test answers that question. Note that the table uses raw counts, not percentages — the test needs counts, because a percentage hides the sample size.
Step 2: Work out the expected frequencies
Next, work out the counts you would expect if the two variables were independent. The rule for each cell is:
Expected = (row total × column total) / grand total
For the men-coffee cell: the row total is 100 men, the column total is 105 coffee drinkers, and the grand total is 200 people, so the expected count is 100 × 105 / 200 = 52.5. Do the same for the other three cells:
| Coffee | Tea | |
|---|---|---|
| Men | 100 × 105 / 200 = 52.5 | 100 × 95 / 200 = 47.5 |
| Women | 100 × 105 / 200 = 52.5 | 100 × 95 / 200 = 47.5 |
Two things are worth checking. First, the expected counts in a row add up to the row total (52.5 + 47.5 = 100), and the same holds for the columns — if they do not, something went wrong. Second, expected counts can be fractions like 52.5; that is normal, because they are averages, not real people.
Step 3: Apply the formula
Now measure how far each observed count is from its expected count, using the chi-square formula:
χ² = Σ (O − E)² / E
For each cell, subtract the expected count from the observed count, square the difference, and divide by the expected count. Then add the four results together:
| Cell | Observed (O) | Expected (E) | (O − E)² / E |
|---|---|---|---|
| Men — Coffee | 60 | 52.5 | (7.5)² / 52.5 = 56.25 / 52.5 = 1.07 |
| Men — Tea | 40 | 47.5 | (−7.5)² / 47.5 = 56.25 / 47.5 = 1.18 |
| Women — Coffee | 45 | 52.5 | (−7.5)² / 52.5 = 56.25 / 52.5 = 1.07 |
| Women — Tea | 55 | 47.5 | (7.5)² / 47.5 = 56.25 / 47.5 = 1.18 |
Adding the four terms: 1.07 + 1.18 + 1.07 + 1.18 = χ² = 4.51. Notice that every cell is 7.5 away from its expected count in this example — that is a property of a 2×2 table, where the four gaps are always the same size. The terms still differ, because dividing by a smaller expected count (47.5) gives a bigger term than dividing by a larger one (52.5).
Step 4: Find the degrees of freedom
For a test of independence, the degrees of freedom are (rows − 1) × (columns − 1). This table has 2 rows and 2 columns, so df = (2 − 1) × (2 − 1) = 1. The same χ² value would mean something different with more degrees of freedom, so always report the two together: “χ² = 4.51, df = 1”.
Step 5: Compare with the critical value and conclude
At the usual 5% significance level, the critical value for 1 degree of freedom is 3.841. Our value, 4.51, is above it — so we reject the idea that preference and gender are independent. The p-value is about 0.034, which is below 0.05 and tells the same story: a gap this big would appear by chance only about 3.4% of the time if the variables were truly independent.
The conclusion, in plain words: in this sample of 200 people, drink preference is associated with gender — men leaned towards coffee and women leaned slightly towards tea, and the gap is bigger than chance alone would explain. What the test does not say is why: it could be culture, habit, or something else entirely, and the test cannot distinguish between those explanations.
What to check before you trust the result
Three conditions matter. First, the expected counts should mostly be 5 or more. Here the smallest expected count is 47.5, so there is no problem. If several expected counts fall below 5, combine categories where that makes sense, collect more data, or use Fisher’s exact test for a 2×2 table.
Second, the data should come from a random sample, with each person counted once. If the same people appear in several cells, the test’s maths does not apply.
Third, remember that “associated” is not “caused”. The test shows that two variables move together; it says nothing about which one causes the other, or whether a third factor is behind both.
Common mistakes
The most common mistake is running the test on percentages instead of counts. Percentages throw away the sample size, and the test’s answer depends on it: 60% out of 200 people is far stronger evidence than 60% out of 20. Always use the raw counts.
Another mistake is reading too much into a significant result. A significant test with a huge sample can flag a tiny, practically meaningless difference — always look at the actual percentages as well as the p-value. And a non-significant result does not prove the variables are independent; it only means this sample did not provide enough evidence against independence.
Frequently Asked Questions
What is the chi-square test of independence used for?
It tests whether two categorical variables are related. For example, it can test whether drink preference is related to gender, or whether shopping habit is related to age group. If the variables are independent, knowing one tells you nothing about the other.
How do you calculate expected frequencies?
For each cell, expected frequency = (row total x column total) / grand total. So if a row has 100 people, a column has 105 people, and 200 people were surveyed in total, the expected count for that cell is 100 x 105 / 200 = 52.5.
What are the degrees of freedom for a 2x2 table?
1. Degrees of freedom for a test of independence are (rows - 1) x (columns - 1), so a 2x2 table gives (2 - 1) x (2 - 1) = 1.
What does it mean if the chi-square value is bigger than the critical value?
It means you reject the idea that the two variables are independent, at that significance level. The variables are associated in your sample — the pattern is bigger than chance alone would explain.
Can I use the test of independence on a table bigger than 2x2?
Yes. The steps are exactly the same for any size of table, and the degrees of freedom are (rows - 1) x (columns - 1). Just check that the expected counts are mostly 5 or more, or the result becomes unreliable.
References
- Pearson, Karl (1900). "On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling." Philosophical Magazine, Series 5, 50(302), 157–175. Source
- Illowsky, Barbara & Dean, Susan (2023). Introductory Statistics 2e. OpenStax, Rice University. Chapter 11, "The Chi-Square Distribution". Source