Companion to Chapter 21. This worksheet goes beyond significance testing into where an association comes from (standardized residuals) and how strong it is (Cramér’s V).
You may use AI assistants to help debug your R code. You may not use AI to generate your interpretations. Every write-up sentence must reflect your own reading of the output. If you use AI for any part of this assignment, note where at the end.
library(vcd)
data(HairEyeColor)
HairEyeColor_df <- as.data.frame(HairEyeColor)
contingency_table <- xtabs(Freq ~ Hair + Eye, data = HairEyeColor_df)
test <- chisq.test(contingency_table)
test
round(test$stdres, 2)Verified: independently recomputed in Python against the real
HairEyeColordataset (χ²(9, N = 592) = 138.29, p < .001, Cramér’s V = .28) — all four values below match exactly.
From the example output:
These high residuals help explain which cells are driving the overall chi-square result.
Cramér’s V measures the strength of association between two categorical variables, ranging from 0 to 1: .1 = small, .3 = moderate, .5 = large. (These conventions apply for small tables; cutoffs are smaller for larger tables with more df.)
Always report Cramér’s V for a test of independence, alongside χ² and p — regardless of significance. V tells you the size of the relationship; the p-value only tells you whether it’s detectable.
install.packages("vcd") # once
library(vcd)
assocstats(contingency_table)This outputs the chi-square test result, Cramér’s V, and a warning if expected counts are too low.
When expected counts are small, chisq.test()’s usual
approximation becomes unreliable. You can simulate the p-value
instead:
data <- matrix(c(1, 9, 11, 3), nrow = 2)
chisq.test(data, simulate.p.value = TRUE)This uses random resampling (2,000 iterations by default) for a more accurate p-value when the standard chi-square approximation is shaky. Also worth knowing about for this situation: Fisher’s Exact Test, an alternative built specifically for small-count tables.
Using the Titanic or UCBAdmissions contingency table from
chi-square-practice.md: