Tal Galili
2009-Jul-19 12:01 UTC
[R] Can I use "mcnemar.test" for 3*3 tables (or is there a bug in the command?)
Hello all, I wish to perform a mcnemar test for a 3 by 3 matrix. By running the slandered R command I am getting a result but I am not sure I am getting the correct one. Here is an example code: (tt <- as.table(t(matrix(c(1,4,1 , 0,5,5, 3,1,5), ncol = 3)))) mcnemar.test(tt, correct=T) #And I get: McNemar's Chi-squared test data: tt McNemar's chi-squared = 7.6667, df = 3, p-value = *0.05343* Now I was wondering if the test I just performed is the correct one.>From looking at the Wikipedia article on mcnemar (http://en.wikipedia.org/wiki/McNemar's_test), it is said that: "The Stuart-Maxwell test<http://ourworld.compuserve.com/homepages/jsuebersax/mcnemar.htm> is different generalization of the McNemar test, used for testing marginal homogeneity in a square table with more than two rows/columns">From searching for a Stuart-Maxwelltest<http://ourworld.compuserve.com/homepages/jsuebersax/mcnemar.htm> in google, I found an algorithm here: http://www.m-hikari.com/ams/ams-password-2009/ams-password9-12-2009/abbasiAMS9-12-2009.pdf>From running this algorithm I am getting a different P value, here is the(somewhat ugly) code I produced for this: get.d <- function(xx) { length1 <- dim(xx)[1] ret1 <- margin.table(xx,1) - margin.table(xx,2) return(ret1) } get.s <- function(xx) { the.s <- xx for( i in 1:dim(xx)[1]) { for(j in 1:dim(xx)[2]) { if(i == j) { the.s[i,j] <- margin.table(xx,1)[i] + margin.table(xx,2)[i] - 2*xx[i,i] } else { the.s[i,j] <- -(xx[i,j] + xx[j,i]) } } } return(the.s) } chi.statistic <- t(get.d(tt)[-3]) %*% solve(get.s(tt)[-3,-3]) %*% get.d(tt)[-3] paste("the P value:", pchisq(chi.statistic, 2)) #and the result was: "the P value: 0.268384371053358" So to summarize my questions: 1) can I use "mcnemar.test" for 3*3 (or more) tables ? 2) if so, what test is being performed ( Stuart-Maxwell<http://ourworld.compuserve.com/homepages/jsuebersax/mcnemar.htm>) ? 3) Do you have a recommended link to an explanation of the algorithm employed? Thanks, Tal -- ---------------------------------------------- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs: http://www.r-statistics.com/ http://www.talgalili.com http://www.biostatistics.co.il [[alternative HTML version deleted]]
David Freedman
2009-Jul-19 12:47 UTC
[R] Can I use "mcnemar.test" for 3*3 tables (or is there a bug in the command?)
There is a function mh_test in the coin package. library(coin) mh_test(tt) The documentation states, "The null hypothesis of independence of row and column totals is tested. The corresponding test for binary factors x and y is known as McNemar test. For larger tables, Stuart?s W0 statistic (Stuart, 1955, Agresti, 2002, page 422, also known as Stuart-Maxwell test) is computed." hth, david freedman Tal Galili wrote:> > Hello all, > > I wish to perform a mcnemar test for a 3 by 3 matrix. > By running the slandered R command I am getting a result but I am not sure > I > am getting the correct one. > Here is an example code: > > (tt <- as.table(t(matrix(c(1,4,1 , > 0,5,5, > 3,1,5), ncol = 3)))) > mcnemar.test(tt, correct=T) > #And I get: > McNemar's Chi-squared test > data: tt > McNemar's chi-squared = 7.6667, df = 3, p-value = *0.05343* > > > Now I was wondering if the test I just performed is the correct one. >>From looking at the Wikipedia article on mcnemar ( > http://en.wikipedia.org/wiki/McNemar's_test), it is said that: > "The Stuart-Maxwell > test<http://ourworld.compuserve.com/homepages/jsuebersax/mcnemar.htm> > is > different generalization of the McNemar test, used for testing marginal > homogeneity in a square table with more than two rows/columns" > >>From searching for a Stuart-Maxwell > test<http://ourworld.compuserve.com/homepages/jsuebersax/mcnemar.htm> > in > google, I found an algorithm here: > http://www.m-hikari.com/ams/ams-password-2009/ams-password9-12-2009/abbasiAMS9-12-2009.pdf > >>From running this algorithm I am getting a different P value, here is the > (somewhat ugly) code I produced for this: > get.d <- function(xx) > { > length1 <- dim(xx)[1] > ret1 <- margin.table(xx,1) - margin.table(xx,2) > return(ret1) > } > > get.s <- function(xx) > { > the.s <- xx > for( i in 1:dim(xx)[1]) > { > for(j in 1:dim(xx)[2]) > { > if(i == j) > { > the.s[i,j] <- margin.table(xx,1)[i] + margin.table(xx,2)[i] - > 2*xx[i,i] > } else { > the.s[i,j] <- -(xx[i,j] + xx[j,i]) > } > } > } > return(the.s) > } > > chi.statistic <- t(get.d(tt)[-3]) %*% solve(get.s(tt)[-3,-3]) %*% > get.d(tt)[-3] > paste("the P value:", pchisq(chi.statistic, 2)) > > #and the result was: > "the P value: 0.268384371053358" > > > > So to summarize my questions: > 1) can I use "mcnemar.test" for 3*3 (or more) tables ? > 2) if so, what test is being performed ( > Stuart-Maxwell<http://ourworld.compuserve.com/homepages/jsuebersax/mcnemar.htm>) > ? > 3) Do you have a recommended link to an explanation of the algorithm > employed? > > > Thanks, > Tal > > > > > > -- > ---------------------------------------------- > > > My contact information: > Tal Galili > Phone number: 972-50-3373767 > FaceBook: Tal Galili > My Blogs: > http://www.r-statistics.com/ > http://www.talgalili.com > http://www.biostatistics.co.il > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Can-I-use-%22mcnemar.test%22-for-3*3-tables-%28or-is-there-a-bug-in-the-command-%29-tp24556414p24556693.html Sent from the R help mailing list archive at Nabble.com.