The chi-square test: comparing counts in categories
Of the people who got a treatment and the people who did not, how many recovered? How many people have each blood type? When you sort people into boxes and count them, this test asks whether the counts are further off than chance would usually make them. Edit the four numbers yourself. All data on this page is made up.
Short answer. The chi-square test works on counts (how many people landed in each box). You assume the two things are unrelated, work out how many people each box should then hold, and add up how far the real counts are from that: χ² = Σ (observed − expected)² ÷ expected. Then you ask whether that total is too big to be chance. If it is (the p-value is small), the "unrelated" assumption is hard to believe.
Edit the number boxes or press − and + (each cell takes 0 to 9999). The table on the right shows how many people each cell should hold if treatment and recovery were unrelated. The darker the orange, the more that cell adds to χ². In the lower left picture, green is each group's share that recovered and the orange dashed line is the overall share. With no relationship, both bars would reach that line. In the lower right picture, the orange tail area is the p-value and the purple dashed line is the 5% line. This page explains a statistics idea only. It says nothing about whether any treatment works.
Definitions
The page below works with a 2×2 table, but the definitions are the same for any number of rows and columns. In a table with r rows and c columns, write for the observed count in row i, column j, for the total of row i, for the total of column j, and N for the grand total.
- observed and expected frequency
- The observed frequency is the number of sampled items that actually fall in that cell. The expected frequency is the count the cell should hold if the null hypothesis (the two variables are independent) is true. It comes from the marginal totals:
- chi-square statistic
- The sum, over all cells, of the squared difference between observed and expected frequency divided by the expected frequency. If the null hypothesis is true and N is large enough, it approximately follows the chi-square distribution with (r − 1)(c − 1) degrees of freedom.
- chi-square distribution
- If are independent and each follows the standard normal distribution, the sum of their squares follows the chi-square distribution with k degrees of freedom, written . Its mean is k and its variance is 2k.
- degrees of freedom
- The k of the chi-square distribution that the statistic approximately follows. It equals the number of cells you can choose freely once the marginal totals (for a goodness-of-fit test, the grand total) are fixed. For the test of independence on an r × c table, and for a goodness-of-fit test with k categories and shares fixed in advance, it is:
- test of independence
- A test of whether two categorical variables are independent. With row categories and column categories , the null hypothesis is that the equation below holds for every cell. The statistic is the χ² above, with (r − 1)(c − 1) degrees of freedom.
- goodness-of-fit test
- A test of whether, for one variable with k categories, the probability of category i equals a share fixed in advance (the shares add up to 1). The expected frequency is and the degrees of freedom are k − 1.
1. When to use it: category × category, counts instead of averages
For measured values such as height or blood pressure, you compare the average of each group. But values like "recovered / not recovered" or "A / B / O / AB" are names, and you cannot average names. Values like these are called categories. With categorical data you count how many people fall in each box. A table that sorts people by two things at once is a contingency table (cross-tabulation). The 2×2 table in the lab above is one.
| Shape of the data | What you compare | Method |
|---|---|---|
| Measured values, two groups | Averages | t-test |
| Measured values, three or more groups | Averages | ANOVA |
| Category × category | Counts (shares) in each box | Chi-square test of independence (this page) |
| One category | Difference from shares set in advance | Chi-square goodness-of-fit test (section 8) |
| Measured value × measured value | How much they move together | Correlation and regression |
The chi-square test has two uses, depending on the question. The formula is the same.
| Test of independence | Goodness-of-fit test | |
|---|---|---|
| Question | Are two things (treatment, recovery) related? | Does one thing (blood type) match shares set in advance? |
| Table | r rows × c columns | One row (k categories) |
| Expected count | row total × column total ÷ N | N × the share set in advance |
| Degrees of freedom | (r − 1) × (c − 1) | k − 1 |
| χ² | The same: Σ (observed − expected)² ÷ expected | |
If you fixed the size of each group before collecting (you recruited 40 treated people and 40 untreated people separately), the question becomes "is the share recovered the same in every group?". That is sometimes called a test of homogeneity. The arithmetic is exactly the same as the test of independence.
2. How many to expect if they are unrelated: expected counts
Keep the edges of the table as they are: the row totals (treated 40, not treated 40), the column totals (recovered 50, not recovered 30) and the overall total (80). Now assume that treatment and recovery are unrelated. Then the chance of recovering is the same with or without treatment, and the best guess for it is the overall share that recovered, 50 ÷ 80 = 62.5%. So out of the 40 treated people you would expect 40 × 62.5% = 25 to recover.
This comes from the multiplication rule for independent events on the probability page. If two things are unrelated, "the chance that both happen = the product of their chances". P(treated and recovered) = 40/80 × 50/80, and multiplying by the 80 people gives 40 × 50 ÷ 80 = 25.
- An expected count can be a decimal. It means "this many on average".
- The expected table has the same row and column totals as the real counts. So if you add up "count − expected" along any row or column, you always get 0.
- We assume "unrelated" because it gives one exact baseline to compare against. "Related" could mean any amount of relationship, each with different expected counts. An assumption set up as a baseline like this is called the null hypothesis.
3. How the formula is built
Using the table from the first lab, we build χ² = Σ (O − E)² ÷ E one piece at a time. O is the count you observed, E is the expected count, and Σ means "add this up over every cell". Each time you press Next, one piece joins the formula (orange background), the picture changes by that much, and the reason appears below.
The picture and numbers use the table from the first lab. Change the numbers up there and this changes too. In the square on the left, the blue vertical line is the expected split (the overall share recovered), and the dark solid lines are the share that really recovered in each row.
Written only as formulas, the steps above look like this, for an r × c table (symbols as in the definitions).
- Expected count:
E = row total × column total ÷ N. N is the total number of people. Split a square of area 1 across by the column shares (recovered 50/80, not 30/80) and down by the row shares (treated 40/80, not treated 40/80). A cell's area is "row share × column share", which is the chance of landing in that cell if the two things are unrelated. Multiply by N to get the expected count. For treated and recovered:0.5 × 0.625 × 80 = 25. - How far off:
O − E. The four cells give+5, −5, −5, +5. In the square, the dark solid lines (30/40 = 75% in the treated row, 20/40 = 50% in the other) sit away from the blue dashed line (62.5%). The strip between them is the difference: its area times N is exactly |O − E|. But the four add up to 0. With fixed totals, if one cell has extra people, another cell in the same row has that many fewer. A plain sum cannot measure how far off the table is. - Square it:
(O − E)². All four become 25. Being off upward and being off downward both count now, and nothing cancels. It is the same reason distances from the mean are squared when you build the variance. - Divide by the expected count:
(O − E)² ÷ E.25 ÷ 25 = 1and25 ÷ 15 ≈ 1.67. Being 5 people off is ordinary in a cell that expects 1,000, but surprising in a cell that expects 15. Counts wobble by roughly√Ethrough chance alone, so(O − E) ÷ √Esays "how many wobbles off", and its square is(O − E)² ÷ E. The same difference of 5 counts for more in the smaller "not recovered" cells. - Add them all up:
χ² = Σ (O − E)² ÷ E.1 + 1.67 + 1 + 1.67 = 5.33. One number now says how far the whole table is from "unrelated". 0 means it matches the expected counts exactly. The bigger it is, the further off. - Compare with the curve. Even with no relationship, chance makes χ² bigger than 0. With enough people, χ² under no relationship roughly follows the chi-square distribution with
(r − 1) × (c − 1)degrees of freedom (not proven here, see section 5). A 2×2 table has 1 degree of freedom, and the area to the right of 5.33 is the p-value, 0.021.
Try this. ① With the starting values, press Next step by step. The bars on the right go from expected counts 25, 15, 25, 15 → differences +5, −5, −5, +5 → four squares of 25 → 1, 1.67, 1, 1.67 → one joined bar of 5.33. At the fourth step the bars for the cells that expect 15 are longer. ② In the first lab press "Unrelated" and look again: both solid lines sit on the blue dashed line and from the second step on every bar is 0. χ² = 0 and p = 1. ③ Press "Small counts": the expected counts are 5, 3, 5, 3 and the differences are ±2. In a cell that expects 3, a difference of 2 counts as 4 ÷ 3 ≈ 1.33.
4. Work it out by hand: a 2×2 table from start to finish
These are the starting values of the first lab: 30 of the 40 treated people recovered, and 20 of the 40 untreated people did (made-up data).
| Cell | Count O | Expected E | O − E | (O − E)² | (O − E)² ÷ E |
|---|---|---|---|---|---|
| Treated, recovered | 30 | 40 × 50 ÷ 80 = 25 | +5 | 25 | 1.00 |
| Treated, not | 10 | 40 × 30 ÷ 80 = 15 | −5 | 25 | 1.67 |
| Untreated, recovered | 20 | 40 × 50 ÷ 80 = 25 | −5 | 25 | 1.00 |
| Untreated, not | 20 | 40 × 30 ÷ 80 = 15 | +5 | 25 | 1.67 |
| Total | 80 | 80 | 0 | χ² = 5.33 |
- Totals. Rows 40 and 40. Columns 50 and 30. All 80.
- Expected counts. Row total × column total ÷ 80: 25, 15, 25, 15.
- χ².
1 + 1.667 + 1 + 1.667 = 5.333. - Degrees of freedom.
(2 − 1) × (2 − 1) = 1. - Compare with the line. In the chi-square distribution with 1 degree of freedom, the point with 5% of the area to its right (the critical value) is 3.84. 5.33 is to the right of it, so the result is significant at the 5% level. The exact area to the right is p = 0.021.
- Read it. "If treatment and recovery were unrelated, a table at least this far off would come up about 2.1% of the time."
- Size. The shares recovered are 75% and 50%, a gap of 25 percentage points. Cramér's V is
√(5.33 ÷ (80 × 1)) ≈ 0.26, the odds ratio is(30 × 20) ÷ (10 × 20) = 3, and the relative risk is0.75 ÷ 0.5 = 1.5(section 11).
The same working, written as formulas:
A 2×2 table also has a shortcut that gives the same value in one line. Call the four cells a b / c d:
With the starting values, 80 × (30 × 20 − 10 × 20)² ÷ (40 × 40 × 50 × 30) = 12,800,000 ÷ 2,400,000 = 5.33. If ad − bc is 0 (both rows have the same share), χ² is 0 too.
5. The chi-square distribution and the p-value
The chi-square distribution is what you get if you draw k independent values from the normal distribution with mean 0 and standard deviation 1, square them, and add them up. k is called the degrees of freedom. Because it is a sum of squares it can never be below 0, and it has a long tail to the right. Its mean is k and its variance is 2k. With more degrees of freedom it moves to the right, spreads out, and looks more like a bell.
Why compare with this curve: with no relationship and enough people, each cell's (O − E) ÷ √E roughly follows a normal distribution. But the fixed totals tie the cells together, so only (r − 1) × (c − 1) of them can really move on their own (section 6). That is why their sum of squares, χ², roughly follows the chi-square distribution with (r − 1) × (c − 1) degrees of freedom. The chi-square statistic and this approximation first appeared in Pearson's 1900 paper (see sources), which was about goodness of fit. We do not prove it here.
| Degrees of freedom | 1 | 2 | 3 | 4 | 5 | 6 | 9 |
|---|---|---|---|---|---|---|---|
| 5% critical value | 3.84 | 5.99 | 7.81 | 9.49 | 11.07 | 12.59 | 16.92 |
These numbers are not copied from a printed table. The lab computes them from the area under the curve, using the regularized incomplete gamma function, and they differ from Python's scipy by less than 10⁻¹⁴.
Only the right tail counts. χ² adds up squared differences, so it grows whichever way the counts are off. The evidence for "far from expected" lives only in the right tail, so the p-value is the single area to the right of your χ². The flip side: the chi-square test does not tell you which way the counts are off. Read the direction from the table (the share bars).
The same χ² means different things at different degrees of freedom. χ² = 5.33 gives p = 0.021 with 1 degree of freedom, but p = 0.255 with 4 and p = 0.804 with 9. More cells means more terms in the sum, so chance alone makes χ² bigger.
Wrong ways to read it. ① "p = 0.021, so there is a 2.1% chance that treatment and recovery are unrelated": wrong. The p-value is "the chance of a table this far off if they were unrelated", not the chance that the assumption is true. For that you need a prior probability, as with Bayes' theorem. ② "p = 0.3, so we proved they are unrelated": wrong. The evidence is just not strong enough. With few people you can miss a difference that is really there (power). ③ "p is tiny, so the relationship is huge": wrong (section 11). ④ "They are related, so the treatment caused the recovery": can be wrong if people were not assigned at random. Healthier people might have been the ones who got treated (confounding, randomization). There is more on the hypothesis testing and p-values page.
Try this. (first lab) ① The starting values give χ² = 5.33 and p = 0.021, and on the curve the dark line sits to the right of the purple 5% line (3.84). ② Set treated/recovered to 28 and treated/not recovered to 12, moving just two people. Now χ² = 3.33 and p = 0.068, to the left of the line. 5% is a line drawn for convenience, so results just either side of it are really about the same strength of evidence. ③ "Unrelated" has 75% recovering in both groups: χ² = 0 and p = 1.
6. Bigger tables and degrees of freedom
With r rows and c columns the arithmetic is the same. For every cell work out expected = row total × column total ÷ N, then add up (O − E)² ÷ E over every cell. Only the degrees of freedom change: (r − 1) × (c − 1). Below is a made-up table of age group × blood pressure level.
Counts (made-up data)
Expected count (top) and adjusted residual (below). Blue = more than expected, orange = fewer
Pick 2 to 4 rows and columns and edit the numbers. If you shrink the table and grow it again, your edits are still there. "Starting values" goes back to 3 × 3 and the original numbers.
Try this. ① The starting 3 × 3 table gives χ² = 15.12, 4 degrees of freedom, p = 0.004, and Cramér's V = 0.21. ② Shrink it to 2 × 2 (20s and 40s × normal and elevated): χ² = 2.47 and p = 0.116. The 60s row and the high column, where the counts were furthest from expected, are gone. ③ Grow it to 4 × 4: χ² = 33.78 with 9 degrees of freedom and p < 0.001, and a warning appears because the 80s/very high cell expects only 4.76.
Degrees of freedom: with the totals fixed, how many cells can you choose freely?
Fix the row totals and column totals, then fill in the cells one by one. Once you fill the first cells of a row, the last cell is forced: it is "row total − the rest". The bottom row is forced the same way by the column totals. So the cells you can choose freely are the ones outside the last row and the last column: (r − 1) × (c − 1) of them. That is the degrees of freedom. The table below uses the same numbers as the lab above.
Green cells were chosen freely, blue cells were forced by a row or column total, and ? cells are not known yet. With the starting 3 × 3 table, two presses of "Fill one cell" force the third cell of the first row, and the fourth press fills every cell. A 4 × 4 table takes 9 presses, a 2 × 2 table just 1.
Goodness of fit (section 8) works the same way. With k categories and a fixed total, only k − 1 can be chosen freely, so there are k − 1 degrees of freedom.
7. Which cell is off: residuals
A single χ² only says "somewhere in the table, the counts are off". In a big table you have to look for where. Each cell's (O − E)² ÷ E shows where χ² came from, and the signed version (O − E) ÷ √E is called the Pearson residual.
In the second table of the section 6 lab, the number under each expected count is a slightly corrected version, the adjusted residual:
With no relationship, each cell's adjusted residual roughly follows the standard normal distribution, so a size beyond 1.96 (about 2) marks a cell with clearly more or fewer people than expected. In the starting 3 × 3 table, 20s/normal (+3.23) and 60s/high (+2.46) have more than expected, and 60s/normal (−3.28) and 20s/high (−2.04) have fewer. The 40s row is near 0 everywhere, so it looks like the overall shares. In a 2×2 table, all four adjusted residuals have the same size, √χ² (√5.33 ≈ 2.31 for the starting values).
Be careful: with many cells, checking each one at 5% makes it easy for some cell to cross the line by chance. Use residuals to decide where to look more closely. To draw a conclusion about each cell, think about multiple comparisons.
8. Goodness of fit: a one-row table
Here there is only one category, and the shares are set in advance. The question is "do the counts match those shares?". The expected count is N × the share, the degrees of freedom are number of categories − 1, and χ² is the same formula.
Edit the counts and the expected shares. Only the proportions of the shares matter (40, 30, 20, 10 is the same as 4, 3, 2, 1). The shares scaled to add up to 100% appear in brackets next to the expected count.
Try this. ① The blood type example uses made-up shares 40 : 30 : 20 : 10 and 200 people. The expected counts are 80, 60, 40, 20, and χ² = 1.25 + 1.67 + 0.63 + 1.25 = 4.79 with 3 degrees of freedom, p = 0.188. ② Double every count (180, 100, 90, 30). The shares are unchanged, but χ² = 9.58 and p = 0.022. The more people you have, the harder the same mismatch is to blame on chance. ③ In the die example, 60 rolls gave the faces 1 to 6 these counts: 5, 8, 9, 8, 10, 20. χ² = 13.4 with 5 degrees of freedom, p = 0.020, and 10 of that comes from the cell for 6 alone. Change the 20 to 10 and χ² = 2.08, p = 0.838.
9. When counts are small: Fisher's exact test
The chi-square curve is an approximation that works when there are many people. If some cells expect only a few people, the real distribution of χ² drifts away from the curve and the p-value goes wrong. A common rule of thumb is "every cell expects at least 5". A looser version is "no more than 20% of cells expect fewer than 5, and none expects fewer than 1". The labs on this page show a warning if any cell expects fewer than 5.
For a 2×2 table, use Fisher's exact test. It counts directly instead of approximating. Keep the totals fixed, list every table you could get with those totals, and work out the chance of each table if the two things are unrelated. In a 2×2 table, the top-left cell a decides all the others, so these chances form the distribution of a (the hypergeometric distribution):
C(n, k) is the number of ways to pick k people out of n. Then add up the chances of every table that is as likely as the one you have, or less likely. That sum is the two-sided p-value.
Counts (made-up data)
Try this. ① In the starting table, 7 of 8 treated people and 3 of 8 untreated people recovered. The totals are 8, 10 and 16, so a can be anything from 2 to 8, with chances 0.003, 0.056, 0.245, 0.392, 0.245, 0.056, 0.003. The current table (a = 7) has chance 0.056, and adding up a = 2, 3, 7, 8 (as likely or less likely) gives p = 0.119. The chi-square test gave p = 0.039, below 5%, but the exact test is above it. Two cells expect only 3 people, so the approximation does not hold. ② Double every cell (14, 2, 6, 10). Every cell now expects at least 6, the warning goes away, and the chi-square p = 0.003 and Fisher p = 0.009 are both below 0.05.
Yates' continuity correction
To make the approximation a little better for a 2×2 table, this correction shrinks each |O − E| by 0.5 before squaring. Python's scipy chi2_contingency and R's chisq.test apply it to 2×2 tables by default. So if you feed in the starting table (30, 10, 20, 20) you get 4.32 (p = 0.038), not this page's 5.33. Turn it off (correction=False) and you get the same value as this page. The corrected test is often said to be too cautious (its p-values come out too big), so with small counts it is cleaner to look at the exact test.
10. Assumptions: when not to use it
| Assumption | Meaning | Example of breaking it |
|---|---|---|
| Independent observations | One person's result is not tied to another's | Counting the same people before and after treatment (section 12), counting members of one family together |
| Each person in exactly one cell | The cells do not overlap, and they add up to the number of people | Putting answers from a "pick all that apply" survey straight into the table |
| Big enough expected counts | The chi-square curve only fits when each cell expects a fair number of people | Cells that expect fewer than 5 (section 9) |
| The table holds counts | χ² grows with the number of people | Putting in percentages like 75% and 25%, or averages. The test then acts as if there were 100 people |
One more thing: the chi-square test does not know the order of categories. Even for ordered categories like blood pressure levels, reordering the columns leaves χ² unchanged. If your question is about a trend ("older groups have more people in the higher levels"), a test that uses the order catches that trend better.
11. Significant is not the same as large: effect size
With the shares unchanged, χ² grows in proportion to the number of people. Multiply every cell of the starting table by 10 (300, 100, 200, 200): the shares recovered stay at 75% and 50%, but χ² becomes 53.33 and p falls far below 0.001. The other way around, with a huge number of people even a tiny gap comes out "significant". The "Many people, small gap" example in the first lab has 10,000 people with 52% and 48% recovering. χ² = 16 and p < 0.001, but the gap is only 4 percentage points.
So report the size of the relationship (the effect size) next to the p-value.
- Cramér's V =
√(χ² ÷ (N × (min(r, c) − 1))). It divides out the number of people: 0 means unrelated, 1 means completely split. The starting values give 0.26, the same after multiplying by 10, and "Many people, small gap" gives 0.04. For a 2×2 table it equals the size of the phi coefficient (φ). - Difference in shares, relative risk, odds ratio (2×2). The starting values give 25 percentage points, a relative risk of 1.5 and an odds ratio of 3. "Many people, small gap" gives 4 percentage points, 1.08 and 1.17. Their meaning and confidence intervals are on the odds ratio and relative risk page.
Results are often written together like this: χ²(1, N = 80) = 5.33, p = 0.021, V = 0.26. The brackets hold the degrees of freedom and the number of people.
12. Counting the same people twice: McNemar's test
Say you asked the same 50 people whether they had a symptom before treatment and after. Putting this into a "before / after × yes / no" table and running a chi-square test is wrong, because each person is counted twice and the observations are not independent. Instead, make the table "yes or no before × yes or no after" and look only at the people who changed. If b people went from yes to no and c people went from no to yes, compare (b − c)² ÷ (b + c) with the chi-square distribution with 1 degree of freedom. This is McNemar's test. In a made-up example with b = 15 and c = 5, (15 − 5)² ÷ 20 = 5.0 and p = 0.025. People who did not change (yes both times, no both times) say nothing about change, so they drop out. When b + c is small, you count exactly with the binomial distribution.
13. Key properties
| Property | In plain words |
|---|---|
| χ² ≥ 0 | 0 means the counts match the expected counts exactly. Being off in either direction makes it bigger. It does not tell you the direction |
| Grows with the number of people | Multiply every cell by k with the shares unchanged and χ² is multiplied by k too. So the p-value alone cannot tell you how big a relationship is |
| Swapping rows and columns changes nothing | Flip the table or reorder rows or columns and χ² stays the same. It does not tell you what causes what, or about trends across an order |
| Degrees of freedom (r − 1)(c − 1) | The number of cells you can choose freely once the totals are fixed. With no relationship, χ² averages out at about the degrees of freedom |
| It is an approximation | When expected counts are small it drifts from the chi-square curve. For 2×2, Fisher's exact test counts directly |
Where it is used
- Comparing shares between two groups. In clinical trials and observational studies, outcomes that end in "yes / no", like the share of people with a side effect or the share readmitted to hospital, are compared between groups. Report the effect size along with the result.
- Checking ratios in genetics. A goodness-of-fit test checks whether the counts from a breeding experiment match the ratio a theory predicts (for example 3 : 1).
- Surveys and epidemiology. A first look at how two categories relate, such as age group × answer or region × having a disease or not. If there is a relationship, use residuals to find the cells and look for confounding separately.
- A/B tests on websites. Put the number of people who signed up after seeing version A or version B into a 2×2 table and compare. If you keep peeking at the results and stop as soon as they look significant, your false positive rate goes up (multiple comparisons).
- Checking random numbers and draws. A goodness-of-fit test checks whether a random number generator or a lottery machine gives each category evenly.
# Python scipy
from scipy.stats import chi2_contingency, fisher_exact, chisquare
table = [[30, 10], [20, 20]]
chi2_contingency(table, correction=False) # χ² 5.33, p 0.021, 1 degree of freedom, expected [[25, 15], [25, 15]]
chi2_contingency(table) # the 2×2 default uses Yates' correction: χ² 4.32, p 0.038
fisher_exact(table) # odds ratio 3.0, two-sided p 0.037
chisquare([90, 50, 45, 15], f_exp=[80, 60, 40, 20]) # goodness of fit: χ² 4.79, p 0.188
Frequently asked questions
When do I use a chi-square test?
When you have counts of people or things sorted into categories (how many landed in each box). To ask whether two things are related, use the test of independence. To ask whether one thing matches shares you set in advance, use the goodness-of-fit test. To compare averages of measured values like height or blood pressure, use a t-test or ANOVA instead.
If p is 0.03, is there a 3% chance that there is no relationship?
No. The p-value is the chance of a table at least this far off, assuming there is no relationship. It is not the chance that the assumption is true. To talk about the chance of no relationship, you need a Bayesian approach with a prior probability.
What if some cells have an expected count below 5?
The p-value from the chi-square curve may be off. For a 2×2 table, use Fisher's exact test. For a bigger table, merge categories that mean similar things, or get the p-value from an exact test or a simulation.
Can I put percentages in the table?
No. Put in counts (numbers of people). The chi-square value grows with the number of people even when the shares stay the same, so percentages make the test act as if there were 100 people, and the p-value comes out wrong.
What if I counted the same people before and after treatment?
The two observations of one person are tied together, so the ordinary chi-square test does not apply. Use McNemar's test, which looks only at the people whose answer changed.
Words
| Words used here | What books call it |
|---|---|
| A value that is one of a few names | Categorical variable. Nominal if there is no order, ordinal if there is |
| A table that sorts people by two things | Contingency table, cross-tabulation |
| Count O | Observed frequency |
| Expected count E (if unrelated) | Expected frequency |
| Row totals, column totals, overall total | Marginal totals, grand total (N) |
| Unrelated | Independent |
| Are two things related / is the share the same in every group / does it match set shares | Test of independence / test of homogeneity / goodness-of-fit test |
| The "unrelated" assumption used as a baseline | Null hypothesis (H₀) |
| The sum of how far off each cell is | Pearson's chi-squared statistic (χ²) |
| The number of cells you can choose freely | Degrees of freedom (df) |
| The 5% line | Critical value. The 5% is the significance level (α) |
| The area of the right tail | p-value |
| How far off one cell is | Residual: Pearson residual, adjusted (standardized) residual |
| The size of the relationship | Effect size: Cramér's V, phi coefficient (φ) |
| Counting every table with the same totals | Fisher's exact test. The chances follow the hypergeometric distribution |
| Shrinking |O − E| by 0.5 | Yates' continuity correction |
| Data where the same people are counted twice | Paired data. McNemar's test |
| The function behind the area under the chi-square curve | Regularized incomplete gamma function |
Sources
- Karl Pearson, 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 (1900). The paper that introduced the chi-square test.
- NIST/SEMATECH e-Handbook of Statistical Methods: 1.3.5.15 Chi-Square Goodness-of-Fit Test, 1.3.6.6.6 Chi-Square Distribution.
- NIST DLMF: §8.2 definitions of the incomplete gamma functions, §8.7 series, §8.9 continued fractions. The formulas the lab uses for the area under the chi-square curve.
- scipy documentation: chi2_contingency (Yates' correction on by default for 2×2), fisher_exact, chisquare.
- R documentation: chisq.test (continuity correction on by default for 2×2).
- Quinn McNemar, Note on the sampling error of the difference between correlated proportions or percentages, Psychometrika. McNemar's test.