Companion to Chapter 21 (Chi-Square). The chi-square (χ²) test determines whether there is a significant association between two categorical variables (Test of Independence), or whether the observed distribution of one categorical variable differs from an expected distribution (Goodness-of-Fit Test).
Both tests share the same foundation: they compare observed frequencies (actual counts from your data) to expected frequencies (counts predicted under a null hypothesis), using counts — not percentages or continuous measurements — for categorical data.
Purpose: Tests whether the distribution of one categorical variable matches a specific expected pattern.
Use it when: you have one categorical variable and want to know if the observed counts fit an expected distribution.
Hypotheses: H₀: observed frequencies = expected frequencies. H₁: observed frequencies ≠ expected frequencies.
Example: Do the colors of M&Ms in a bag match the company’s claimed distribution?
Key features: compares one set of observed frequencies to expected frequencies; assumes a known or uniform distribution; used for one variable.
Assumptions: independence of observations; sufficiently large sample size (expected frequencies generally ≥ 5).
Purpose: Tests whether there’s a relationship between two categorical variables.
Use it when: you have two categorical variables and want to know if they’re related.
Hypotheses: H₀: the variables are independent. H₁: the variables are associated (dependent).
Example: Is there a relationship between class standing and preferred social media platform?
Key features: focuses on the relationship between two variables; uses a contingency table to organize the data; tests for association, not differences in distributions. A significant result indicates association, but not strength or direction — that’s what effect size (below) is for.
Assumptions: independence of observations; sufficiently large sample size (expected frequencies generally ≥ 5).
Always report Cramér’s V for a Test of Independence, regardless of whether the result is statistically significant — the p-value tells you whether an association is detectable; V tells you how strong it is.
library(vcd)
assocstats(your_table)Interpretation (small tables): V = .1 (small), .3 (moderate), .5 (large). Cutoffs are smaller for larger tables with more df.
(Goodness-of-fit tests have their own effect size, Cohen’s w, which is beyond the scope of this course — a significant GOF test alone doesn’t tell you how large the deviation from expected is.)
A company claims the colors of their new candy are evenly distributed. A student opens a bag and counts: Red = 12, Green = 18, Blue = 10, Yellow = 20.
Question: Do the observed counts match an equal distribution among the 4 colors?
observed <- c(12, 18, 10, 20)
expected_proportions <- rep(0.25, 4)
chisq.test(x = observed, p = expected_proportions)A survey of 40 students asked whether they prefer coffee or tea, and whether they are morning or evening people:
| Coffee | Tea | |
|---|---|---|
| Morning | 10 | 5 |
| Evening | 8 | 17 |
Question: Is there a relationship between time-of-day preference and drink choice?
data <- matrix(c(10, 5, 8, 17), nrow = 2, byrow = TRUE)
colnames(data) <- c("Coffee", "Tea")
rownames(data) <- c("Morning", "Evening")
chisq.test(data)
library(vcd)
assocstats(data)Note on Yates’ correction: This is a 2×2 table, so R applies Yates’ continuity correction automatically. The output header will say “Pearson’s Chi-squared test with Yates’ continuity correction,” and the value will be slightly smaller than the uncorrected hand formula gives. Report R’s output directly — do not recalculate by hand.
Verified result: χ²(1, N = 40) = 3.26, p = .071 (not significant), Cramér’s V = .29 (small-to-moderate). (Independently recomputed for this migration — matches Ch 21’s own worked example exactly.)
| Feature | Goodness-of-Fit | Test of Independence |
|---|---|---|
| Number of variables | One categorical variable | Two categorical variables |
| Purpose | Compare observed to expected frequencies | Test for association between variables |
| Data arrangement | Single list of observed/expected frequencies | Contingency table |
| Hypothesis | Test if observed data fits expected data | Test if two variables are independent |
| Degrees of freedom | df = k − 1 | df = (r − 1)(c − 1) |