40 Chi-Square Test
The Chi-square test is a non-parametric test used to examine categorical data. Data sorted into named groups rather than measured on a numeric scale. It compares observed frequencies against the frequencies expected under some assumption, and comes in two common forms:
- Chi-square Test of Independence: is there an association between two categorical variables?
- Chi-square Goodness-of-Fit Test: does a sample’s distribution across categories match a theoretical or expected distribution?
Both share the same test statistic:
\[\chi^2 = \sum \frac{(O_i - E_i)^2}{E_i}\]
where \(O_i\) is the observed frequency and \(E_i\) is the expected frequency in category \(i\).
40.1 Chi-Square Test of Independence
Tests whether two categorical variables are associated, using a contingency table of observed counts. Expected frequencies under independence are:
\[E_{ij} = \frac{R_i \times C_j}{N}\]
where \(R_i\) is the row total, \(C_j\) is the column total, and \(N\) is the grand total. The test statistic follows a chi-squared distribution with \((r-1)(c-1)\) degrees of freedom.
Hypotheses
- \(H_0\): the two variables are independent (no association).
- \(H_1\): the two variables are associated.
Example: Farm Size and Adoption of a Drought-Resistant Seed Variety
An extension officer wants to know whether adoption of a new drought-resistant seed variety is associated with farm size (small vs. large). A survey of 100 farmers gives:
| Farm Size | Adopted | Did Not Adopt | Row Total |
|---|---|---|---|
| Small | 20 | 10 | 30 |
| Large | 30 | 40 | 70 |
| Column Total | 50 | 50 | 100 |
Expected Frequencies and Test Statistic
\[E_{Small,Adopted} = \frac{30 \times 50}{100} = 15, \quad E_{Small,NotAdopted} = 15, \quad E_{Large,Adopted} = 35, \quad E_{Large,NotAdopted} = 35\]
The classic hand-calculation (uncorrected) formula gives:
\[\chi^2 = \frac{(20-15)^2}{15} + \frac{(10-15)^2}{15} + \frac{(30-35)^2}{35} + \frac{(40-35)^2}{35} = 1.67+1.67+0.71+0.71 = 4.76, \qquad df = 1\]
At \(\alpha=0.05\) and \(df=1\), the critical value is 3.841. Since \(4.76 > 3.841\), this uncorrected calculation would reject \(H_0\) (\(p \approx 0.029\)).
A Wrinkle: Yates’ Continuity Correction
For a 2×2 table, R’s chisq.test() applies Yates’ continuity correction by default. It doesn’t reproduce the raw hand-calculation above unless you explicitly turn the correction off. The correction shrinks each squared deviation by 0.5 before dividing, which pulls the statistic down:
\[\chi^2_{corrected} = \sum \frac{(|O_i-E_i|-0.5)^2}{E_i} = 3.86, \qquad p \approx 0.0495\]
That’s a materially different, borderline result: still significant at \(\alpha=0.05\), but only just. This is a good example of why it matters to know what a function actually computes by default, the hand-calculated 4.76/0.029 and R’s default-corrected 3.86/0.0495 are both “correct” for what they measure, but they aren’t interchangeable, and the correction can occasionally flip a conclusion. Use correct = FALSE in chisq.test() to reproduce the uncorrected hand calculation.
Effect Size
Cramér’s V measures association strength for a contingency table (0 = no association, 1 = perfect association), using the uncorrected \(\chi^2\):
\[V = \sqrt{\frac{\chi^2}{N(\min(r,c)-1)}} = \sqrt{\frac{4.76}{100 \times 1}} \approx 0.22\]
For a 2×2 table, \(V \approx 0.22\) indicates a small-to-moderate association.
Interpretation
With the (default, corrected) p-value of 0.0495 just under 0.05, there is weak but statistically significant evidence of an association between farm size and adoption of the drought-resistant variety. Large farms in this sample were relatively less likely to adopt it. Given how close the result sits to the significance threshold, and the modest effect size (Cramér’s V ≈ 0.22), this is a case for a larger follow-up survey before drawing firm policy conclusions.
Chi-Square Test of Independence in R
40.2 Chi-Square Goodness-of-Fit Test
Tests whether a sample’s distribution across categories matches a theoretical or expected distribution. The test statistic follows a chi-squared distribution with \(k-1\) degrees of freedom, where \(k\) is the number of categories.
Hypotheses
- \(H_0\): the observed distribution matches the expected (theoretical) distribution.
- \(H_1\): the observed distribution differs from the expected distribution.
Example: Testing a Mendelian Inheritance Ratio in a Crop Trait
The classic textbook application of goodness-of-fit is testing Mendelian inheritance ratios, and there’s no better source than Mendel’s own pea-breeding data. Crossing heterozygous plants for a single trait (seed color: yellow dominant, green recessive) should, under simple Mendelian inheritance, produce offspring in a 3∶1 ratio of yellow to green. Among 423 second-generation (\(F_2\)) pea plants Mendel counted:
| Seed Color | Observed |
|---|---|
| Yellow | 315 |
| Green | 108 |
Does this match the theoretical 3∶1 ratio?
Expected Frequencies and Test Statistic
With \(N = 423\) total plants, the expected counts under a 3∶1 ratio are:
\[E_{Yellow} = \frac{3}{4}(423) = 317.25, \qquad E_{Green} = \frac{1}{4}(423) = 105.75\]
\[\chi^2 = \frac{(315-317.25)^2}{317.25} + \frac{(108-105.75)^2}{105.75} = 0.016 + 0.048 = 0.064, \qquad df = 2-1 = 1\]
The critical value at \(\alpha=0.05\), \(df=1\) is 3.841. Since \(0.064 \ll 3.841\), we fail to reject \(H_0\) (\(p \approx 0.80\)).
Interpretation
The observed seed-color counts are extremely consistent with the theoretical 3∶1 Mendelian ratio. There is no statistical evidence the data departs from simple dominant-recessive inheritance. This is exactly the kind of question goodness-of-fit testing was built for: not “is there a pattern?” but “does the pattern I expected actually hold up?”