# Chi-square goodness of fit test
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tulip <- c(red = 81, yellow = 50, white = 27)
# Q1: Are the colors equally common?
chisq_test(tulip)
pairwise_chisq_gof_test(tulip)
# Q2: comparing observed to expected proportions
chisq_test(tulip, p = c(1/2, 1/3, 1/6))
pairwise_chisq_test_against_p(tulip, p = c(0.5, 0.33, 0.17))
# Homogeneity of proportions between groups
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Data: Titanic
xtab <- as.table(rbind(
c(203, 118, 178, 212),
c(122, 167, 528, 673)
))
dimnames(xtab) <- list(
Survived = c("Yes", "No"),
Class = c("1st", "2nd", "3rd", "Crew")
)
xtab
# Chi-square test
chisq_test(xtab)
# Compare the proportion of survived between groups
pairwise_prop_test(xtab)
# Test of independence using the data-frame interface
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
df <- data.frame(
gender = rep(c("M", "F"), each = 100),
smoker = rep(c("yes", "no", "yes", "no"), times = c(30, 70, 60, 40))
)
# Positional columns
df %>% chisq_test(gender, smoker)
# Equivalent, using vars (keeps `correct` settable)
df %>% chisq_test(vars = c("gender", "smoker"), correct = FALSE)
Run the code above in your browser using DataLab