Lesson 17 · Statistics
Chi-square test
Test whether two categories are related, using counts.
Set up
library(tidyverse)
library(rstatix)
students <- read_csv("data/students.csv", show_col_types = FALSE) %>%
mutate(LearningStyle = case_when(
LearningStyle == 0 ~ "Visual",
LearningStyle == 1 ~ "Kinaesthetic",
LearningStyle == 2 ~ "Auditory",
LearningStyle == 3 ~ "Reading/Writing"
))When to use it
When both variables are categories, there are no means to compare, only counts. Question: does preferred learning style differ between female and male students?
- H0: the two are unrelated. H1: they are related.
- The test compares the counts you observed with the counts you'd expect if H0 were true.
Count
style_table <- table(students$Gender, students$LearningStyle)
style_table
Auditory Kinaesthetic Reading/Writing Visual
Female 29 39 50 66
Male 19 22 30 45Groups differ in size (184 women, 116 men), so compare percentages within each row:
prop.table(style_table, margin = 1) %>% round(2)
Auditory Kinaesthetic Reading/Writing Visual
Female 0.16 0.21 0.27 0.36
Male 0.16 0.19 0.26 0.39The percentages are very similar for women and men.
ggplot(students, aes(x = Gender, fill = LearningStyle)) +
geom_bar(position = "fill") +
labs(y = "Proportion", fill = "Learning style")
position = "fill" stretches each bar to full height, so you compare proportions.
Test
chi <- chisq.test(style_table)
chiPearson's Chi-squared test data: style_table X-squared = 0.4013, df = 3, p-value = 0.94
- X-squared = χ² = 0.40: a tiny gap between observed and expected.
- df = (rows − 1) × (columns − 1) = 1 × 3 = 3.
- p = 0.94: not significant.
Check: expected counts of 5 or more
chi$expected
Auditory Kinaesthetic Reading/Writing Visual
Female 29.44 37.41333 49.06667 68.08
Male 18.56 23.58667 30.93333 42.92All above 5, so the test is valid. If any were below 5, use fisher.test(style_table).
Effect size: Cramér's V
cramer_v(style_table)[1] 0.03657392
From 0 (no link) to 1. Rough guide: .10 small, .30 medium, .50 large. Here, almost 0.
Exercises
Hall life and support
Are students who live in a hall more or less likely to use the university's wellbeing support (Used_Support)?
- Make the table and row percentages.
- Draw a proportion bar chart.
- Run the chi-square test, check the expected counts, and get Cramér's V.
- Report it.
Show answerHide answer
support_table <- table(students$Living, students$Used_Support)
support_table
prop.table(support_table, margin = 1) %>% round(2)
ggplot(students, aes(x = Living, fill = Used_Support)) +
geom_bar(position = "fill") +
labs(y = "Proportion", fill = "Used support")
chi2 <- chisq.test(support_table)
chi2
chi2$expected
cramer_v(support_table)
No Yes
Hall 82 43
Home 137 38
No Yes
Hall 0.66 0.34
Home 0.78 0.22
Pearson's Chi-squared test with Yates' continuity correction
data: support_table
X-squared = 5.3272, df = 1, p-value = 0.02099
No Yes
Hall 91.25 33.75
Home 127.75 47.25
[1] 0.1408719
Hall students were more likely to use wellbeing support (34%) than students living at home (22%), χ²(1, N = 300) = 5.33, p = .021, V = .14. For a 2 × 2 table R applies Yates' continuity correction, a small adjustment that makes the test a little more cautious.
Workshop and gender
Is taking the workshop related to gender? Run the test and report it.
Show answerHide answer
ws_table <- table(students$Gender, students$Workshop)
prop.table(ws_table, margin = 1) %>% round(2)
chisq.test(ws_table)
cramer_v(ws_table)
No Yes
Female 0.55 0.45
Male 0.67 0.33
Pearson's Chi-squared test with Yates' continuity correction
data: ws_table
X-squared = 3.655, df = 1, p-value = 0.0559
[1] 0.1173642Not significant, though close: χ²(1, N = 300) = 3.66, p = .056, V = .12. A p just above .05 is still not significant; report the exact value and let readers judge.
Key points
- Two categorical variables: chi-square test.
table()for counts,prop.table(margin = 1)for row percentages.- Check
$expectedis at least 5; otherwisefisher.test(). - Report χ²(df, N), p and Cramér's V.