- Compute expected counts and the chi-square statistic, and explain what the statistic measures.
- Carry out and interpret a chi-square goodness-of-fit test for one categorical variable.
- Carry out a chi-square test of independence for a two-way table, and distinguish it from a test of homogeneity.
- Find degrees of freedom, check conditions, and find p-values using the chi-square distribution.
- Use standardized residuals to locate where a table departs from independence, and Cramer's V to measure how strongly.
- Use Fisher's exact test when expected counts are small.
- Compute and interpret risk difference, relative risk, and odds ratio for a 2 by 2 table, including a confidence interval for the odds ratio.
Everything in Chapters 9 to 11 concerned means and proportions. But a great deal of data is simply counted: how many customers chose each plan, how many patients improved under each treatment, how many voters in each age band support a proposal. This chapter develops the tools for such data. Their common idea is disarmingly simple: compare the counts you observed with the counts you would have expected if some hypothesis were true, and ask whether the gap is bigger than chance alone would produce.
Counts, expected counts, and the kinds of question
Suppose a variable has \(k\) categories. The observed count \(O_i\) is how many individuals fell in category \(i\). A hypothesis about the population tells us the expected count \(E_i\), the number we would see on average in a sample of this size if the hypothesis were true. The discrepancies \(O_i - E_i\) are the raw material of every test in this chapter.
| Question | Data | Test | Null hypothesis says |
|---|---|---|---|
| Does one categorical variable follow a claimed distribution? | One sample, one variable, \(k\) categories | Goodness of fit (12.3) | The population proportions are \(p_{10}, p_{20}, \dots, p_{k0}\) |
| Are two categorical variables related? | One sample, classified by two variables | Independence (12.4) | The two variables are independent |
| Do several populations have the same distribution of one variable? | Separate samples from each population | Homogeneity (12.5) | The distribution of the response is the same in every population |
| Same as independence but with small counts, or a 2 by 2 table where exact answers matter | 2 by 2 table | Fisher's exact test (12.7) | No association (odds ratio 1) |
The first three use one and the same statistic. The fourth uses exact probabilities instead of an approximation. Section 12.8 then asks a different question: not is there an association, but how large is it, in risk terms a doctor or manager can use.
The chi-square statistic and its distribution
Each cell contributes the squared discrepancy, scaled by the expected count. The statistic is zero only when observed and expected counts agree exactly, and it grows as they drift apart.
Why this particular form?
- Squared, because raw discrepancies \(O - E\) sum to zero (an excess in one cell is a deficit in others). Squaring stops positives and negatives from cancelling and treats both directions as evidence against \(H_0\).
- Divided by \(E\), because a discrepancy of 10 means very different things at different scales. Ten more than the 20 expected is dramatic; ten more than the 1,000 expected is noise. Dividing by \(E\) puts every cell on a comparable footing. (For counts, the variance of a cell is roughly \(E\), so each term is approximately a squared z-score.)
The chi-square distribution
If \(H_0\) is true and the sample is large enough, the statistic follows (approximately) a chi-square distribution with \(k\) degrees of freedom, written \(\chi^2_k\). It arises as a sum of \(k\) independent squared standard normal variables, and so:
- it takes only nonnegative values and is right-skewed, especially for small \(k\);
- its mean is \(k\) and its variance is \(2k\), so a statistic near its degrees of freedom is unremarkable, and one far above is suspicious;
- as \(k\) grows it becomes more symmetric and approaches a normal curve;
- a chi-square test is always right-tailed: the p-value is \(P(\chi^2_k \ge \text{observed statistic})\). Large statistics mean poor agreement with \(H_0\). Small statistics do not reject anything (they mean the fit is good, perhaps suspiciously so).
| df | 1 | 2 | 3 | 4 | 5 | 6 | 10 |
|---|---|---|---|---|---|---|---|
| \(\chi^2\) for right-tail area 0.05 | 3.841 | 5.991 | 7.815 | 9.488 | 11.070 | 12.592 | 18.307 |
Appendix B computes any critical value or p-value. Note that \(3.841 = 1.96^2\): with one degree of freedom, a chi-square test is a squared z test.
Goodness of fit: does one variable follow a claimed distribution?
A goodness-of-fit test asks whether the distribution of a single categorical variable matches a specified set of proportions. The specified proportions might come from a scientific theory (Mendel's 9:3:3:1), from a claim (all six faces equally likely), or from an earlier census (the age mix of a population).
\(H_0\!: p_1 = p_{10},\ p_2 = p_{20},\ \dots,\ p_k = p_{k0}\) (the claimed distribution is correct).
\(H_a\!:\) at least one proportion differs from its claimed value.
Expected counts: \(E_i = n\,p_{i0}\). Statistic: \(\chi^2 = \sum (O_i - E_i)^2/E_i\). Degrees of freedom: \(k - 1\) (once \(k-1\) counts are known, the last is fixed by the total \(n\)). p-value: \(P(\chi^2_{k-1} \ge \chi^2_{\text{obs}})\).
Conditions: (1) counts, not percentages or measurements; (2) a random sample or randomized process; (3) independence (each observation in one category only, and \(n\) less than 10 percent of the population when sampling without replacement); (4) every expected count is at least 5.
Example 12.1: Mendel's peas (classic data)
Gregor Mendel crossed pea plants and classified the 556 offspring seeds by shape and color. His theory of independent inheritance predicts the four types in the ratio 9 : 3 : 3 : 1. His counts:
| Type | Observed \(O\) | Expected \(E\) | \((O-E)^2/E\) |
|---|---|---|---|
| Round, yellow | 315 | 312.75 | 0.0162 |
| Round, green | 108 | 104.25 | 0.1349 |
| Wrinkled, yellow | 101 | 104.25 | 0.1013 |
| Wrinkled, green | 32 | 34.75 | 0.2176 |
| Total | 556 | 556 | 0.4700 |
The expected counts are \(556 \times 9/16 = 312.75\), \(556 \times 3/16 = 104.25\) (twice), and \(556 \times 1/16 = 34.75\). All exceed 5. With \(\text{df} = 4 - 1 = 3\), \(\chi^2 = 0.470\) and the p-value is \(P(\chi^2_3 \ge 0.470) = 0.925\).
Conclusion. There is no evidence against Mendel's 9 : 3 : 3 : 1 model; the data are entirely consistent with it. (A p-value this large does not prove the model, and the statistician R. A. Fisher famously noted in 1936 that Mendel's results, taken across all his experiments, agree with theory more closely than chance usually allows. Very large p-values deserve a second glance too.)
Example 12.2: Is the die fair? (synthetic data)
A die is rolled 120 times (a simulated data set). The counts of faces 1 to 6 are 15, 22, 18, 17, 14, 34. If the die is fair, each face has probability \(1/6\), so each expected count is \(120/6 = 20\).
\(\chi^2 = \dfrac{(15-20)^2}{20} + \dfrac{(22-20)^2}{20} + \dfrac{(18-20)^2}{20} + \dfrac{(17-20)^2}{20} + \dfrac{(14-20)^2}{20} + \dfrac{(34-20)^2}{20} = 1.25 + 0.20 + 0.20 + 0.45 + 1.80 + 9.80 = 13.70.\)
With \(\text{df} = 5\), the 5 percent critical value is 11.070 and \(P(\chi^2_5 \ge 13.70) = 0.0176\). At \(\alpha = 0.05\) we reject \(H_0\): there is convincing evidence that the die is not fair. Notice which cell carries the evidence: the face 6 alone contributes 9.80 of the 13.70. The test says something is off; the cell contributions (and the residuals of Section 12.6) say where.
Estimated parameters. If the claimed distribution has parameters that you estimate from the same data (for example, testing whether counts follow a Poisson distribution with a mean estimated from the sample), subtract one degree of freedom for each estimated parameter: \(\text{df} = k - 1 - m\).
Percentages are not counts. The statistic depends on the actual counts. Ten percent of 50 and ten percent of 5,000 are very different amounts of evidence. Always convert to counts before computing \(\chi^2\).
Test of independence: are two categorical variables related?
In Chapter 4 you built two-way tables and compared conditional distributions. If two variables are independent, the distribution of one is the same whatever the value of the other, and the conditional percentages match across rows. A chi-square test of independence asks whether the association seen in a sample table is stronger than sampling variability could produce if the variables were truly unrelated in the population.
Expected counts under independence
If the variables are independent, then \(P(\text{row } i \text{ and column } j) = P(\text{row } i)\,P(\text{column } j)\) (the multiplication rule of Chapter 5). Estimating the two probabilities from the margins of the table and multiplying by \(n\):
\[E_{ij} = n \cdot \frac{R_i}{n} \cdot \frac{C_j}{n} = \frac{R_i \times C_j}{n} = \frac{(\text{row } i \text{ total})(\text{column } j \text{ total})}{\text{grand total}}.\]\(H_0\!:\) the two variables are independent (no association in the population).
\(H_a\!:\) the two variables are associated.
\(\chi^2 = \sum_{\text{all cells}} \dfrac{(O_{ij} - E_{ij})^2}{E_{ij}}\), with \(\text{df} = (r - 1)(c - 1)\) for a table with \(r\) rows and \(c\) columns. The p-value is the right-tail area.
Conditions: one random sample (or randomized experiment) classified on both variables; independent observations (\(n\) less than 10 percent of the population); every expected count at least 5.
Going deeper: Why \((r-1)(c-1)\) degrees of freedom?
Degrees of freedom count how many cell counts are free to vary once the constraints are imposed. Under the independence null the expected counts are computed from the margins, so the observed table has the same row totals and column totals as the expected table. In an \(r \times c\) table the row totals fix \(r\) constraints and the column totals fix \(c\), but one of these is redundant (both sum to \(n\)), so there are \(r + c - 1\) independent constraints. That leaves \(rc - (r + c - 1) = (r-1)(c-1)\) free cells.
A 2 by 2 table has 1 free cell: once you know a single cell and the margins, the other three follow. A 3 by 4 table has \(2 \times 3 = 6\). This also explains the goodness-of-fit rule: with one row of \(k\) categories and one total, \(k - 1\) counts are free.
Example 12.3: Class and survival on the Titanic (classic data)
The classic Titanic data (Dawson, 1995) classify the 2,201 people aboard by class (first, second, third, crew) and whether they survived.
| Survived | Died | Total | |
|---|---|---|---|
| First class | 203 | 122 | 325 |
| Second class | 118 | 167 | 285 |
| Third class | 178 | 528 | 706 |
| Crew | 212 | 673 | 885 |
| Total | 711 | 1,490 | 2,201 |
Descriptive first. Survival rates were 62.5 percent (first), 41.4 percent (second), 25.2 percent (third) and 24.0 percent (crew). Overall 711/2201 = 32.3 percent survived.
Expected counts. For first-class survivors, \(E = 325 \times 711/2201 = 104.99\); for first-class deaths, \(E = 325 \times 1490/2201 = 220.01\). The full table of expected counts:
| Survived | Died | |
|---|---|---|
| First class | 104.99 | 220.01 |
| Second class | 92.06 | 192.94 |
| Third class | 228.06 | 477.94 |
| Crew | 285.89 | 599.11 |
All are well above 5. Statistic. The eight cell contributions are 91.50, 43.66, 7.31, 3.49, 10.99, 5.24, 19.10 and 9.11, so \(\chi^2 = 190.40\) with \(\text{df} = (4-1)(2-1) = 3\).
p-value. \(P(\chi^2_3 \ge 190.4)\) is about \(5 \times 10^{-41}\), essentially zero. There is overwhelming evidence that survival and class were associated. (The 2,201 people are everyone aboard, not a random sample, so the test is best read as asking whether so large an association could plausibly arise if survival had been unrelated to class.)
Conclusion. Survival was strongly associated with class. The test does not say in what way; that is the job of residuals (Section 12.6). It also does not say that class caused survival: class was tangled up with sex, age and cabin location.
Homogeneity: same distribution in several populations?
Sometimes the design is different. Instead of one sample cross-classified two ways, we draw separate samples from several populations (fixing the sample size of each) and record a single categorical response. The question is whether the response distribution is the same in every population. This is a test of homogeneity. The arithmetic is identical to the test of independence: same expected counts formula, same statistic, same degrees of freedom. Only the sampling design and the wording of the hypotheses differ.
| Independence | Homogeneity | |
|---|---|---|
| Sampling | One random sample; both variables recorded for each individual | Separate random samples from each population (or treatment groups); one response variable recorded |
| Row (or column) totals | Random: they come out of the data | Fixed in advance by the design |
| \(H_0\) | The two variables are independent | The distribution of the response is the same in all populations |
| Calculation | \(E = (\text{row total})(\text{column total})/n\), \(\chi^2 = \sum (O-E)^2/E\), \(\text{df} = (r-1)(c-1)\) | |
A randomized experiment with several treatment groups is a homogeneity design: the groups are fixed by the randomization and the question is whether the response distribution is the same across treatments.
Example 12.4: Support for a new timetable at three campuses (hypothetical)
A university randomly samples 150 students at each of three campuses and asks whether they support a proposed new timetable.
| Support | Oppose | Total | |
|---|---|---|---|
| Campus A | 62 | 88 | 150 |
| Campus B | 48 | 102 | 150 |
| Campus C | 81 | 69 | 150 |
| Total | 191 | 259 | 450 |
\(H_0\!:\) the proportion supporting the timetable is the same at all three campuses. \(H_a\!:\) it differs for at least one campus.
The pooled support rate is \(191/450 = 0.4244\), so under \(H_0\) each campus is expected to have \(150 \times 191/450 = 63.67\) supporters and \(86.33\) opponents. All expected counts exceed 5.
\(\chi^2 = 14.97\) with \(\text{df} = (3-1)(2-1) = 2\), giving a p-value of \(0.00056\). We reject \(H_0\): there is convincing evidence that support for the timetable is not the same at the three campuses (sample support rates 41.3, 32.0 and 54.0 percent). To see which campuses differ from the pooled pattern we use residuals in the next section.
Where is the difference? Residuals and effect size
A significant chi-square tells you that the whole table departs from \(H_0\), but a table has many cells. Which ones are responsible? The cell-by-cell measure is the standardized residual:
\[z_{ij} = \frac{O_{ij} - E_{ij}}{\sqrt{E_{ij}}}.\]Its square is that cell's contribution to \(\chi^2\), so \(\chi^2 = \sum z_{ij}^2\). Positive residuals mean more observations than expected under \(H_0\); negative, fewer. As a rough guide, residuals beyond \(\pm 2\) indicate cells worth attention, and beyond \(\pm 3\) indicate strong departures. (Software often reports adjusted standardized residuals, which divide by \(\sqrt{E_{ij}(1 - R_i/n)(1 - C_j/n)}\) so that they are truly standard normal under \(H_0\); they are somewhat larger in size than the simple ones.)
Example 12.5: Residuals for the Titanic and the timetable data
Titanic. Standardized residuals \((O - E)/\sqrt{E}\), for the survived column: first class \(+9.57\), second class \(+2.70\), third class \(-3.32\), crew \(-4.37\) (the died column has the same magnitudes with opposite signs). First-class passengers survived far more often than independence predicts, and third-class passengers and crew far less often. That is the story hidden in the single number 190.4.
Timetable. Support residuals: campus A \(-0.21\), campus B \(-1.96\), campus C \(+2.17\). The overall rejection is driven mostly by campus C (more support than expected) and campus B (less). Campus A is right at the pooled rate.
Cramer's V: how strong is the association?
Because \(\chi^2\) grows with the sample size, it is a measure of evidence, not of strength. Double every count and the statistic doubles although the proportions are unchanged. Cramer's V rescales the statistic to lie between 0 (no association) and 1 (perfect association):
\[V = \sqrt{\frac{\chi^2}{n\,(m - 1)}}, \qquad m = \min(r, c).\]For the Titanic table, \(V = \sqrt{190.4/(2201 \times 1)} = 0.294\), a moderate association. For a 2 by 2 table, \(V\) equals the absolute value of the phi coefficient (the correlation between two 0/1 variables). Rough guides for \(m - 1 = 1\) are 0.1 small, 0.3 medium, 0.5 large; for larger tables, the same labels apply at smaller values of \(V\).
With 100,000 observations, an association with \(V = 0.02\) (practically nothing) is "significant" at any conventional level. With 40 observations, a strong association may fail to be. Always report the conditional percentages or an effect measure (\(V\), or the risk difference and odds ratio of Section 12.8) next to the p-value, as in Chapter 10.
Small counts: Fisher's exact test
The chi-square distribution is only an approximation to the distribution of the statistic, and the approximation is good when expected counts are not tiny. The usual guideline is that every expected count should be at least 5 (some authors relax this to 80 percent of cells at least 5 and none below 1). When the condition fails in a 2 by 2 table, use Fisher's exact test, which computes the p-value from an exact probability model instead of an approximation.
Condition on the margins of the table (treat the row and column totals as fixed). Then if there is no association, the count \(a\) in the top-left cell follows a hypergeometric distribution (the same distribution as drawing without replacement in Chapter 6):
\[P(a) = \frac{\binom{R_1}{a}\binom{R_2}{C_1 - a}}{\binom{n}{C_1}},\]where \(R_1, R_2\) are the row totals, \(C_1\) is the first column total, and \(n\) the grand total. The one-sided p-value adds up \(P(a)\) for tables at least as extreme in the direction of interest; the two-sided p-value (the version most software reports) adds up the probabilities of all tables with \(P(a)\) no larger than that of the observed table.
Example 12.6: The lady tasting tea (Fisher, 1935)
In Fisher's famous experiment, a lady claimed she could tell whether milk or tea had been poured into the cup first. Eight cups were prepared, four of each kind, presented in random order; she was told four were of each kind. Suppose she correctly identifies all four "milk first" cups.
| Says milk first | Says tea first | Total | |
|---|---|---|---|
| Milk first (truth) | 4 | 0 | 4 |
| Tea first (truth) | 0 | 4 | 4 |
If she is merely guessing, every choice of which four cups to call "milk first" is equally likely: there are \(\binom{8}{4} = 70\) of them and exactly one is fully correct. So the one-sided p-value is \(1/70 = 0.0143\). The (two-sided) Fisher p-value is \(2/70 = 0.0286\), since the fully wrong pattern is equally extreme. All expected counts are 2, far below 5, so the chi-square approximation would be unreliable here. With only 8 cups, even a perfect score is evidence at the 5 percent level but never overwhelming, which is exactly why Fisher recommended more cups.
Example 12.7: When the approximation misleads (hypothetical)
A small pilot trial: 7 of 10 patients improve on a new treatment versus 2 of 9 on the control.
| Improved | Did not improve | Total | |
|---|---|---|---|
| Treatment | 7 | 3 | 10 |
| Control | 2 | 7 | 9 |
| Total | 9 | 10 | 19 |
Expected counts: the smallest is \(10 \times 9/19 = 4.74\) and \(9 \times 9/19 = 4.26\), both below 5, so the chi-square conditions fail. The (uncorrected) chi-square statistic is 4.34 with p-value 0.037, which would be declared "significant." The exact answer differs. Given the margins, the probabilities of the top-left count \(a = 0, 1, \dots, 9\) are 0.0000, 0.0010, 0.0175, 0.1091, 0.2864, 0.3437, 0.1910, 0.0468, 0.0044 and 0.0001. The observed \(a = 7\) has probability 0.0468. Summing every table whose probability is at most 0.0468 (that is, \(a = 0, 1, 2, 7, 8, 9\)) gives the two-sided Fisher p-value of 0.0698. The approximation was too optimistic: with these data we cannot reject at the 5 percent level.
(A continuity-corrected chi-square, known as Yates' correction, gives 0.105, overcorrecting in the other direction. Exact tests avoid the dilemma.)
The 2 by 2 calculator in the next section computes the Fisher exact p-value, the ordinary chi-square p-value and warns when expected counts are below 5. Load the "small pilot trial" preset to reproduce Example 12.7, and see how the hypergeometric distribution of the top-left count decides the p-value.
Risk, relative risk, and odds ratio
A chi-square test answers "is there an association?" Practitioners usually need "how big is the effect?" For a 2 by 2 table comparing an exposed (or treated) group with an unexposed (or control) group on a yes/no outcome, three measures are standard. Write the table as
| Outcome | No outcome | Total | |
|---|---|---|---|
| Exposed | \(a\) | \(b\) | \(a + b\) |
| Unexposed | \(c\) | \(d\) | \(c + d\) |
Risk (proportion with the outcome): \(\hat{p}_1 = \dfrac{a}{a+b}\) in the exposed group and \(\hat{p}_2 = \dfrac{c}{c+d}\) in the unexposed group.
Risk difference: \(\hat{p}_1 - \hat{p}_2\) (absolute change in risk; the number needed to treat is \(1/|\hat{p}_1 - \hat{p}_2|\)).
Relative risk (risk ratio): \(\text{RR} = \dfrac{\hat{p}_1}{\hat{p}_2}\).
Odds of the outcome in a group: \(\dfrac{\hat{p}}{1 - \hat{p}}\). Odds ratio:
\[\text{OR} = \frac{a/b}{c/d} = \frac{ad}{bc}.\]Relative risk and odds ratio are both 1 when there is no association, above 1 when the outcome is more common in the exposed group, and below 1 when it is less common. The relative risk is the easier to interpret ("the risk is 0.55 times as large"). The odds ratio is harder to interpret, but it is symmetric (it is the same whichever variable you call the outcome), it is the natural scale for logistic regression (Chapter 15), and it is the only measure you can estimate from a case-control study, where the investigator chooses how many cases and controls to sample and so cannot estimate risks at all.
When the outcome is rare in both groups (say under 10 percent), the odds are nearly equal to the risks and \(\text{OR} \approx \text{RR}\). When the outcome is common they diverge, and the OR overstates the RR: with risks 0.6 and 0.4, \(\text{RR} = 1.5\) but \(\text{OR} = (0.6/0.4)/(0.4/0.6) = 2.25\). Reporting an odds ratio as if it were "times as likely" is a common error.
Confidence interval for the odds ratio
The odds ratio has a skewed sampling distribution, but its logarithm is approximately normal with standard error
\[\text{SE}(\ln \widehat{\text{OR}}) = \sqrt{\frac{1}{a} + \frac{1}{b} + \frac{1}{c} + \frac{1}{d}}.\]A confidence interval for \(\ln \text{OR}\) is \(\ln \widehat{\text{OR}} \pm z^*\,\text{SE}\); exponentiate both endpoints to get an interval for the OR itself. The interval for the relative risk is built the same way with \(\text{SE}(\ln \widehat{\text{RR}}) = \sqrt{\frac{1}{a} - \frac{1}{a+b} + \frac{1}{c} - \frac{1}{c+d}}\). An interval that does not contain 1 corresponds to a significant association at the matching level.
Example 12.8: Aspirin and heart attacks (Physicians' Health Study, 1989)
In a large randomized trial, 11,037 male physicians took aspirin and 11,034 took a placebo. Over about five years, 104 in the aspirin group and 189 in the placebo group had a heart attack.
| Heart attack | No heart attack | Total | |
|---|---|---|---|
| Aspirin | 104 | 10,933 | 11,037 |
| Placebo | 189 | 10,845 | 11,034 |
Risks. \(\hat{p}_1 = 104/11037 = 0.00942\) (aspirin), \(\hat{p}_2 = 189/11034 = 0.01713\) (placebo). Risk difference \(= -0.00771\): 0.77 percentage points fewer heart attacks. The number needed to treat is \(1/0.00771 \approx 130\): about 130 men would take aspirin for the five years to prevent one heart attack. Relative risk \(= 0.00942/0.01713 = 0.550\): aspirin cut the risk by about 45 percent.
Odds ratio. \(\text{OR} = (104 \times 10845)/(10933 \times 189) = 0.546\), nearly equal to the RR because heart attacks were rare. \(\text{SE}(\ln \text{OR}) = \sqrt{1/104 + 1/10933 + 1/189 + 1/10845} = 0.1228\); the 95 percent interval for \(\ln\text{OR}\) is \(\ln(0.5458) \pm 1.96(0.1228)\), so for the OR it is \((0.429,\ 0.694)\). For the RR the interval is \((0.434,\ 0.698)\). Both exclude 1 comfortably.
Test. Chi-square \(= 25.01\), \(\text{df} = 1\), p-value \(= 5.7 \times 10^{-7}\). Because the study was a randomized experiment, we may conclude that aspirin caused a reduction in heart attacks (in this population of men over the study period).
The link to Chapter 11. The 2 by 2 chi-square test is exactly the two-proportion z test with a pooled standard error: \(z = (0.00942 - 0.01713)/\sqrt{0.01327(0.98673)(1/11037 + 1/11034)} = -5.001\), and \(z^2 = 25.01 = \chi^2\). The two tests always agree in a 2 by 2 table (two-sided).
Pitfalls and how to report
- Use counts, never percentages. The statistic needs the actual sample size.
- The test is omnibus. A significant result says the table is not what independence (or the claimed distribution) predicts. It does not say which cells, or how strongly: follow up with residuals, conditional percentages and an effect size.
- No direction. The chi-square test is inherently two-sided. If you want to test a specific direction for a 2 by 2 table, use a one-sided two-proportion z test or a one-sided Fisher test.
- Every observation once. The observations must be independent. Before-and-after counts on the same people, or matched pairs, violate this; use the paired version for a 2 by 2 table (McNemar's test), which uses only the discordant pairs.
- Check expected counts, not observed counts. The condition concerns \(E\), and it is quite possible for observed counts to be large while some expected counts are small.
- Do not merge categories after peeking just to make a table significant. Combining sparse categories to satisfy the expected-count condition is acceptable if done for sensible substantive reasons and reported.
- Association is not causation (Chapters 2 and 4). Only randomized experiments support causal wording. Watch for confounding and Simpson's paradox: a relationship in a collapsed table can reverse within subgroups.
- Huge samples make trivial associations significant; report \(V\), the risk difference, or the odds ratio with its interval.
Going deeper: McNemar's test and the likelihood-ratio (G) test
McNemar's test compares two proportions from paired binary data, for example the same 200 people classified as "approve" or "disapprove" before and after a speech. Only the discordant pairs matter: \(b\) people who changed from yes to no and \(c\) from no to yes. Under \(H_0\) (no net change), each discordant pair is equally likely to fall either way, and \(\chi^2 = (b - c)^2/(b + c)\) with 1 degree of freedom (exact version: a binomial test on \(b\) out of \(b + c\) with \(p = 0.5\)). It is the categorical analogue of the paired t test of Chapter 11.
The likelihood-ratio statistic \(G^2 = 2\sum O \ln(O/E)\) is an alternative to Pearson's \(\chi^2\) with the same chi-square reference distribution and the same degrees of freedom. For large samples the two agree closely. \(G^2\) is the statistic that generalizes naturally to log-linear and logistic models.
"A chi-square test of independence found a significant association between [variable 1] and [variable 2], \(\chi^2(\text{df}, N = n) = \) [value], \(p =\) [value], Cramer's \(V =\) [value]. The largest departure from independence was [cell], where [group] had [more/fewer] than expected ([observed] versus [expected]; standardized residual = [value])."
Chapter summary
- All chi-square tests compare observed with expected counts through \(\chi^2 = \sum (O - E)^2/E\); large values are evidence against \(H_0\), and the p-value is a right-tail area of \(\chi^2_{\text{df}}\).
- Goodness of fit: \(E_i = np_{i0}\), \(\text{df} = k - 1\) (minus one per estimated parameter).
- Independence and homogeneity: \(E_{ij} = (\text{row total})(\text{column total})/n\), \(\text{df} = (r-1)(c-1)\). The calculation is the same; the design and the hypotheses differ.
- Conditions: counts, random sampling or randomization, independence, and all expected counts at least 5.
- Standardized residuals \((O - E)/\sqrt{E}\) show which cells drive a significant result; Cramer's \(V = \sqrt{\chi^2/(n(m-1))}\) measures strength.
- Fisher's exact test uses the hypergeometric distribution and is the right choice for small 2 by 2 tables.
- For a 2 by 2 table: risk difference, relative risk \(\hat{p}_1/\hat{p}_2\), odds ratio \(ad/bc\) with a log-scale interval \(\pm z^*\sqrt{1/a + 1/b + 1/c + 1/d}\). OR approximates RR only when the outcome is rare.
- A 2 by 2 chi-square test equals the two-proportion z test squared.
Key terms
| Term | Meaning |
|---|---|
| Observed and expected counts | Actual cell counts, and the counts predicted by the null hypothesis |
| Chi-square statistic | \(\sum (O-E)^2/E\), a measure of total discrepancy |
| Goodness-of-fit test | Tests whether one categorical variable follows a specified distribution |
| Test of independence | Tests whether two categorical variables in one sample are unrelated |
| Test of homogeneity | Tests whether several populations share the same response distribution |
| Standardized residual | \((O-E)/\sqrt{E}\) for a cell; locates the source of a significant result |
| Cramer's V | Chi-square-based strength of association from 0 to 1 |
| Fisher's exact test | Exact test for a 2 by 2 table based on the hypergeometric distribution |
| Relative risk and odds ratio | Ratios of risks, and of odds, between two groups; both equal 1 under no association |