# McNemar procedure demonstrated with an example

## The McNemar's test

Simar to the contigency test, McNemar's test can be used to analyze categorical data in survey and questionnarie. But when the data are dependent, McNemar's test is more appropiate. For example, assume that each of several test subjects is afflickted with tinea pedis(athlete's foot) on each foot, and each subject is given a treatment X on one foot and Y on the other foot. Frequency count from matched pairs of foot is measured for both cured and not cured. Because left and right foot of the same subject are dependent, contingency test cannot be used and McNemar's test should be used. The table below demonstrates the frequency count.

```		2 X 2 Table with Frequency Counts from Mathed Pairs
___________________________________________________________________
Treatment X
___________________________________
Cured		Not Cured
___________________________________________________________________
Cured	      a			   b
Treatment
Not cured	      c			   d
___________________________________________________________________

```

In summary, McNemar's test is appropiate if the following assumptions are met:

1. The sample data have been randomly selected.
2. The sample data consist of matched pairs.
3. The frequency count can be organized into a 2 X 2 table shown above. There should be 2 variables (Treatment and Result), each has two categories (X/Y; cured/not cured).
4. As shown in the table above, the frequencies are big enough such that b + c &ge 10.

## Analyzing a survey data with McNemar's test

The clinical Trials for treatments for athelete's foot is in the file "athelete.csv".

The question is to test whether treatments X or Y matter to the results.

Open the data set from SAS. Or import with the following command. Note that the procedure "format" organize the lable from 'c' to 'cured' and 'nc' to 'not cured'.

```
proc format;
value \$result 'c'='cured'
'nc'='Not cured';

data athelete;
infile "H:\sas\data\athelete.csv" dlm=',' firstobs=2;
input subject  treatX \$ treatY \$;
format treatX treatY \$result. ;
run;

```

The McNemar's test can be run as follows. Note that the "agree" option on the tables statement produces a both McNemer's test statistics as well as a measure of agreement called Kapped, which is often used as a measure of interrater reliability.

```	proc freq data=athelete;
title "McNemar's test for Paired Samples";
tables treatX*treatY /agree expected norow nocol nopercent;
run;

```

The control option "agree" requests tests and measures of classification agreement for square tables. it provides McNemar's test for 2×2 tables and Bowker's test of symmetry for tables with more than two response categories.

The control option "expected" asks for expected value for assumption checking.

The other options such as "norow" eliminate unwanted outputs for clarity.

```            		McNemar's test for Paired Samples

The FREQ Procedure

Table of treatX by treatY

treatX     treatY

Frequency
Expected  cured   Not cured  Total
______________________________________________
cured        12      8 	     20
13      7
______________________________________________
Not cured    40      20        60
39      21
______________________________________________
Total        52      28       80

Statistics for Table of treatX by treatY

McNemar's Test
__________________________
Statistic (S)    21.3333
DF                     1
Pr > S            <.0001

Simple Kappa Coefficient
____________________________________
Kappa                    -0.0435
ASE                       0.0821
95% Lower Conf Limit     -0.2044
95% Upper Conf Limit      0.1174

Sample Size = 80

```

## Interpreting the result

This output shows McNemar's test statistics is 21.33, with a corresponding p-value <0.0001. Thus we conclude that the treatment make a difference in the result.