Chi-Square Practice in R Using Built-in Datasets

Companion to Chapter 21. This worksheet practices both chi-square goodness-of-fit and chi-square test of independence, using datasets built into R — no file download needed.

A Note on AI Tools

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.

Example: Goodness-of-Fit with HairEyeColor

Tests whether hair color frequencies are uniformly distributed.

Variable: Hair (Black, Brown, Red, Blond). H₀: hair colors are equally distributed. H₁: they are not.

data(HairEyeColor)

hair_counts <- margin.table(HairEyeColor, 1)

expected <- rep(sum(hair_counts) / length(hair_counts), length(hair_counts))

test <- chisq.test(hair_counts, p = rep(1/length(hair_counts), length(hair_counts)))
test

barplot(hair_counts, main = "Hair Color Distribution", col = "tan")

Report: χ², df, N, and p. Note: the effect size for goodness-of-fit tests is Cohen’s w, which is beyond the scope of this course — you don’t need to compute it, but a significant GOF test alone doesn’t tell you how large the deviation from expected is.

Example: Survival and Class on the Titanic

data(Titanic)
Titanic_df <- as.data.frame(Titanic)

contingency_table <- xtabs(Freq ~ Survived + Class, data = Titanic_df)
print(contingency_table)

chi_square_result <- chisq.test(contingency_table)
print(chi_square_result)

round(chi_square_result$stdres, 2)

mosaicplot(contingency_table,
           main = "Mosaic Plot: Survival by Class",
           xlab = "Passenger Class", ylab = "Survival", color = TRUE)

print(round(chi_square_result$expected, 1))

H₀: survival is independent of passenger class. H₁: survival is associated with passenger class.

Report: χ², df, p, and Cramér’s V (library(vcd); assocstats(contingency_table)) — report the effect size alongside χ² and p, regardless of significance.

Confound question: the Titanic dataset includes variables beyond class and survival. Name one that might be related to both who survived and what class they were in, and explain why it matters for interpreting the class–survival association.

Example: Hair Color and Eye Color (Test of Independence)

Variables: Hair (Black, Brown, Red, Blond), Eye (Brown, Blue, Hazel, Green). H₀: hair color is independent of eye color. H₁: they are associated.

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)

mosaicplot(contingency_table, main = "Hair Color vs Eye Color", color = TRUE)

On Your Own

1. Survival and sex. Return to the Titanic dataset. Is survival associated with sex? Note: survival × sex is a 2×2 table — R will apply Yates’ continuity correction automatically. Report the corrected χ² from R’s output, and run assocstats() for Cramér’s V.

2. Survival and age. Is survival associated with age? Report χ² and interpret it. Also report Cramér’s V — how strong is the association, and what does that mean in context?

3. UCBAdmissions. data(UCBAdmissions) records admissions and rejections for UC Berkeley applicants, broken down by gender and department. Variables: Gender (Male/Female), Admit (Admitted/Rejected). H₀: admission decision is independent of gender. H₁: it is associated with gender. Build the contingency table, run the test, and report χ², df, p, and Cramér’s V.