[R] McNemar test in R & SPSS
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Tue Dec 26 23:52:19 CET 2006
Bob Green wrote:
> Peter,
>
> Thanks for your reply. To perform the analysis in R, I used the table
> format suggested in the book by Everitt & Hothorn, whereas in SPSS the
> analysis was performed directly from the 2 variables, rather than using
> count data.
>
You still need the right table.
matrix(c(128,29,331,430), ncol =2,....)
consists of the two marginal tables, which has strictly less information than the crosstabulation of pre and post values. I expect every text on the McNemar test makes this point, and I'd be highly surprised if E&H really suggested that you should use the table that you did.
> There is still something I don't understand - I tried to replicate the
> syntax from your e-mail but my computer just kept waiting - what am I still
> doing wrong?
>
You need to give it the data when it starts with the "0:"-style prompt,
end with a blank line, as shown. It's just a device for cutting and
pasting your table. I might as well have used
d <- matrix(c(311,119,20,9), ncol=2)
Or, with the raw data to hand
d <- table(preMHT, postMHT)
>
> mctest <- as.table(matrix(c(128,29,331,430), ncol =2, dimnames =
> list(group=c("preMHT","postMHT"), assault=c("yes","no"))))
> > d <- read.table(stdin())
> 0: mcnemar.test(as.matrix(d),correct=F)
> 1:
>
> Thanks again
>
> Bob
>
>
> At 10:52 PM 26/12/2006 +0100, Peter Dalgaard wrote:
>
>> Bob Green wrote:
>>
>>> Hello,
>>>
>>> I am hoping someone can clarify why I might obtain a quite different
>>> value in R & SPSS for a McNemar test I ran.
>>>
>>> Firstly, here is the R syntax & output
>>>
>>>
>>> R OUTPUT
>>>
>>> > mctest <- as.table(matrix(c(128,29,331,430),
>>> + ncol =2, dimnames = list(group=c("preMHT","postMHT"),
>>> + assault=c("yes","no"))))
>>>
>>> > mctest
>>> assault
>>> group yes no
>>> preMHT 128 331
>>> postMHT 29 430
>>>
>>> > mcnemar.test(mctest,correct=F)
>>>
>>> McNemar's Chi-squared test
>>>
>>> data: mctest
>>> McNemar's chi-squared = 253.3444, df = 1, p-value < 2.2e-16
>>>
>>>
>>> SPSS OUTPUT
>>>
>>> The same data was inputted in SPSS. Regarding the first table SPSS
>>> generated - it lists the number of cases in each combination of
>>> categories. The diagonal contains the number of cases with the same
>>> response on both variables, while the off diagonal contains cases that
>>> have different responses on the 2 variables. The overall chisquare value
>>> is much lower than the value obtained using R, though still significant.
>>>
>>>
>>> pre02 & post02
>>>
>>> pre02 post02
>>>
>>> 0 1
>>> 0 311 20
>>> 1 119 9
>>>
>>>
>>> Test Statistics(b)
>>>
>>> pre02 & post02
>>>
>>> N 459
>>> Chi-Square(a) 69.094
>>> Asymp. Sig. .000
>>>
>>> a Continuity Corrected
>>> b McNemar Test
>>>
>>>
>>> Any assistance is much appreciated,
>>>
>>>
>> Well, you can't expect R to give the correct result if you feed it the
>> wrong matrix, can you?
>>
>>
>>> d <- read.table(stdin())
>>>
>> 0: 0 1
>>
>> 1: 0 311 20
>>
>> 2: 1 119 9
>>
>> 3:
>>
>>> d
>>>
>> X0 X1
>>
>> 0 311 20
>>
>> 1 119 9
>>
>>
>>> mcnemar.test(as.matrix(d),correct=F)
>>>
>> McNemar's Chi-squared test
>>
>> data: as.matrix(d)
>> McNemar's chi-squared = 70.5108, df = 1, p-value < 2.2e-16
>>
>>
>>> mcnemar.test(as.matrix(d),correct=T)
>>>
>> McNemar's Chi-squared test with continuity correction
>>
>> data: as.matrix(d)
>> McNemar's chi-squared = 69.0935, df = 1, p-value < 2.2e-16
>>
>>
>>
>>
>>> Bob Green
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch 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.
>>>
>>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
More information about the R-help
mailing list