# R code for chi-square test of independence calculations (for reference)
# Example 1: Flu Vaccine Effectiveness
flu_vaccine_data <- matrix(c(24, 9, 13, 289, 100, 565),
nrow = 2, byrow = TRUE,
dimnames = list(c("Flu", "No Flu"),
c("No Vaccine", "One Shot", "Two Shot")))
vaccine_test <- chisq.test(flu_vaccine_data, correct = FALSE)
vaccine_test
Pearson's Chi-squared test
data: flu_vaccine_data
X-squared = 17.313, df = 2, p-value = 0.000174
# Example 2: Political Views & Space Exploration
politics_data <- matrix(c(8, 10, 12, 12, 17, 6, 10, 13, 12),
nrow = 3, byrow = TRUE,
dimnames = list(c("Strong", "Moderate", "Weak"),
c("Republican", "Democrat", "Independent")))
politics_test <- chisq.test(politics_data, correct = FALSE)
politics_test
Pearson's Chi-squared test
data: politics_data
X-squared = 4.5397, df = 4, p-value = 0.3379
# Example 3: Smoking and Lung Disease
smoking_data <- matrix(c(45, 15, 105, 135),
nrow = 2, byrow = TRUE,
dimnames = list(c("Lung Disease", "No Disease"),
c("Smoker", "Non-Smoker")))
smoking_test <- chisq.test(smoking_data, correct = FALSE)
smoking_test
Pearson's Chi-squared test
data: smoking_data
X-squared = 18.75, df = 1, p-value = 1.49e-05
# Example 4: Education Level and Voting Behavior
education_data <- matrix(c(20, 30, 10, 25, 15, 20, 15, 5, 10),
nrow = 3, byrow = TRUE,
dimnames = list(c("High School", "College", "Graduate"),
c("Candidate A", "Candidate B", "Candidate C")))
education_test <- chisq.test(education_data, correct = FALSE)
education_test
Pearson's Chi-squared test
data: education_data
X-squared = 13.958, df = 4, p-value = 0.007429