# McNemar's test for paired binary data in R

I have some data encoded with the number 0,1,2 before and after treatment. 0 - No improvement, 1- Improvement, 2-Moderate improvement. The data obtained from patient's heart. They divided the heart into 16 parts and they encoded each part with either 0,1 or 2.

The data look as below.

I want to run a test to check if there is a statistical significance before and after treatment.

I read that McNemar's test is for paired binary data but I don't know if it's the best one for my case. Any help will be appreciated.

• If you have six patients then I suspect any quest for statistical significance is likely to be fruitless. – mdewey Aug 8 '17 at 16:16

Usually for McNemar test --- or the extended McNemar-Bowker test---, the data are arranged in a contingency table with the same levels on each axis, but that one axis is e.g. Before and one is e.g. After. For this experiment, this might make sense if you have a relatively large number of patients, and if it makes sense to look at each Part individually.

For the data you've shown:

###  For Part 1 only

Input =("
Before         After.0   After.1   After.2
Before.0        4        0          0
Before.1        0        0          0
Before.2        0        1          1
")

row.names=1))

mcnemar.test(Matrix)

In this case, the mcnemar.test function will fail, because there are 0's in certain critical spots.

There is a function in the rcompanion package that will conduct the McNemar-Bowker test as a multinomial test, which won't have this trouble. . Adapted from SAEPER: Tests for paired nominal data.

library(rcompanion)

nominalSymmetryTest(Matrix,
method="fdr",
digits = 3)

However, it will probably be better to include all the Parts in one model.

One approach to do this is to use multinomial logistic regression.

However, it sounds like the the Ratings you have can be considered ordinal in nature, and so ordinal regression could be used. It is not clear to me from the descriptions if the order of the levels of the Ratings should be "0", "1", "2", or "0", "2", "1". The following example assumes the levels go "0", "1", "2". (Adapted from SAEPER: Ordinal regression.)

Input =("
Part  Time    Patient  Rating
1     Before  1        0
1     Before  2        0
1     Before  3        2
1     Before  4        2
1     Before  5        0
1     Before  6        0
2     Before  1        0
2     Before  2        0
2     Before  3        2
2     Before  4        0
2     Before  5        2
2     Before  6        0
1     After   1        0
1     After   2        0
1     After   3        2
1     After   4        1
1     After   5        0
1     After   6        0
2     After   1        0
2     After   2        0
2     After   3        2
2     After   4        0
2     After   5        0
2     After   6        0
")

Data$Part = factor(Data$Part,
levels=unique(Data$Part)) Data$Time = factor(Data$Time, levels=c("Before", "After")) Data$Patient = factor(Data$Patient, levels=unique(Data$Patient))

Data$Rating = factor(Data$Rating, ordered=TRUE)

library(ordinal)

model = clmm(Rating ~ Part + Time + (1|Patient),
data = Data)

library(car)

library(RVAideMemoire)

Anova(model,
type = "II")