# R code for performing McNemar's test for # analyzing 2 by 2 table: # Comparing two proportions with paired samples. # We analyze the ice cream data from class: icecream <- matrix(c(38, 92, 13, 57), nrow = 2, ncol = 2, byrow = TRUE, dimnames = list("Name Brand" = c("Like", "Dislike"), "Economy Brand" = c("Like", "Dislike"))) print(icecream) mcnemar.test(icecream, correct = FALSE) ################### # Copy this function into R to get a CI for pi_1 - pi_2: mcnemar.CI <- function(datamatrix, conf.level=0.95) { alpha <- 1-conf.level my.n <- sum(datamatrix) margin.error <- qnorm(1-alpha/2, lower=TRUE)*sqrt(datamatrix[1,2] + datamatrix[2,1])/my.n lower.limit <- ((datamatrix[1,2] - datamatrix[2,1])/my.n) - margin.error upper.limit <- ((datamatrix[1,2] - datamatrix[2,1])/my.n) + margin.error return(round(c(lower.limit, upper.limit), 4)) } # Get a 95% CI for pi_1 - pi_2 for the ice cream data: mcnemar.CI(icecream, conf.level=0.95)