# R code for chapter 37 paired nominal data: mcnemars test # Book details: # http://www.amazon.co.uk/Health-Science-Statistics-using-Commander/dp/190790431X ############# Basic analysis: observed, total, null hypothesis proportion binom.test(2,14,0.5) ###### if sample >30 can use: x <- array(c(20,2, 12, 16), dim=c(2,2)) mcnemar.test(x) ########## odds: odds_ratioa <- 12/2 odds_ratioa # or odds_ratiob <- 2/12 odds_ratiob probabilitya <- odds_ratioa/(odds_ratioa +1) probabilitya probabilityb <- odds_ratiob/(odds_ratiob +1) probabilityb ############# for confidence intervals # use mcnemar.exact in the exact2x2 package, also provides a odds ratio install.packages("exact2x2") library(exact2x2) mcnemar.exact(x) ############# for situations with more than 2 categories install.packages("coin"); library(coin) tablex <- as.table(x) tablex mh_test(tablex) install.packages("irr"); library(irr) stuart.maxwell.mh(x) ################ improved version of the Stuart-Maxwell statistic # known as the marginal homogeneity statistic W of Bhapkar # code available online # source function sometimes problematic can use source_url() in the devtools package instead: # library(devtools) # source_url("http:/www.robin-beaumont.co.uk/virtualclassroom/book2data/r_code/bhapkar_test_R.txt") # first approach: ### you may need to edit the "Rprofile.site" file: # C:\Program Files\R\R-2.15.2\etc\Rprofile.site # Open this "Rprofile.site" file in Notepad # and add the following line on a new line at the end of the file: # utils::setInternet2(TRUE) ######### source("http://www.karlin.mff.cuni.cz/~omelka/Soubory/nmsa331/cviceni-10/bhapkar.test.R") # or load("http://www.karlin.mff.cuni.cz/~omelka/Soubory/nmsa331/cviceni-10/bhapkar.test.R") source("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/r_code/bhapkar_test_R.txt") bhapkar.test(x) ###### st_result <- stuart.maxwell.mh(x) st_result str(st_result) st_result$value w <- st_result$value/(1 - st_result$value/st_result$subjects) w #####