The notes for the topics on this page can be found in the lecture 21 folder on Canvas.

R

To conduct McNemar’s test in R you can use mcnemar.test() from the stats package. mcnemar.test() can accept either a 2 x 2 table in matrix format or you can use a dataset with individual level data without first building a 2 x 2 table. You could also use CrossTable(..., mcnemar = TRUE) from the descr package.

library(tidyverse)
library(magrittr)

genetic <- tibble(
  case = c("yes", "yes", "no", "no"), 
  control = rep(c("yes", "no"), 2), 
  count = c(9, 13, 3, 10)
)

genetic <- genetic %>% 
  mutate(case = fct_relevel(case, "yes", "no"), 
         control = fct_relevel(control, "yes", "no"))
  

gen_table <- xtabs(count ~ case + control, data = genetic)

mcnemar.test(gen_table, correct = FALSE)
## 
##  McNemar's Chi-squared test
## 
## data:  gen_table
## McNemar's chi-squared = 6.25, df = 1, p-value = 0.01242

Matched pairs odds ratio

There is currently no function for creating a matched pairs odds ratio in R. However, you could construct the odds ratio and confidence interval by “hand” as shown in the example below.

or <- gen_table[1, 2] / gen_table[2, 1]

or <- tibble(
  or = or, 
  lower = or * exp(qnorm(0.025) * sqrt(1/gen_table[1, 2] + 1/gen_table[2, 1])), 
  upper = or * exp(qnorm(0.025, lower.tail = FALSE) * sqrt(1/gen_table[1, 2] + 1/gen_table[2, 1]))
)

or
## # A tibble: 1 x 3
##      or lower upper
##   <dbl> <dbl> <dbl>
## 1  4.33  1.23  15.2

SAS

To conduct McNemar’s test in SAS you use the frequency procedure with the agree argument specified in the options of the table statement.

title 'McNemars Test Example: Ulcers and Genetics'; 
data genetic;
  input case $ control $ count;
  cards;
  yes yes 9 
  yes no 13
  no yes 3
  no no 10
  ;
run;

proc freq data = genetic order = data;
  table case*control / chisq agree;
  weight count;
run;
##                                McNemars Test Example: Ulcers and Genetics
## 
##                                            The FREQ Procedure
## 
##                                         Table of case by control
## 
##                                   case      control
## 
##                                   Frequency|
##                                   Percent  |
##                                   Row Pct  |
##                                   Col Pct  |yes     |no      |  Total
##                                   ---------+--------+--------+
##                                   yes      |      9 |     13 |     22
##                                            |  25.71 |  37.14 |  62.86
##                                            |  40.91 |  59.09 |
##                                            |  75.00 |  56.52 |
##                                   ---------+--------+--------+
##                                   no       |      3 |     10 |     13
##                                            |   8.57 |  28.57 |  37.14
##                                            |  23.08 |  76.92 |
##                                            |  25.00 |  43.48 |
##                                   ---------+--------+--------+
##                                   Total          12       23       35
##                                               34.29    65.71   100.00
## 
## 
##                                 Statistics for Table of case by control
## 
##                          Statistic                     DF       Value      Prob
##                          ------------------------------------------------------
##                          Chi-Square                     1      1.1533    0.2829
##                          Likelihood Ratio Chi-Square    1      1.1914    0.2750
##                          Continuity Adj. Chi-Square     1      0.4976    0.4806
##                          Mantel-Haenszel Chi-Square     1      1.1203    0.2898
##                          Phi Coefficient                       0.1815          
##                          Contingency Coefficient               0.1786          
##                          Cramer's V                            0.1815          
## 
##                           WARNING: 25% of the cells have expected counts less 
##                                    than 5. Chi-Square may not be a valid test.
## 
## 
##                                           Fisher's Exact Test
##                                    ----------------------------------
##                                    Cell (1,1) Frequency (F)         9
##                                    Left-sided Pr <= F          0.9278
##                                    Right-sided Pr >= F         0.2427
##                                                                      
##                                    Table Probability (P)       0.1705
##                                    Two-sided Pr <= P           0.4630
## 
## 
##                                             McNemar's Test
##                                         -----------------------
##                                         Statistic (S)    6.2500
##                                         DF                    1
##                                         Pr > S           0.0124
## 
## 
##                                         Simple Kappa Coefficient
##                                     --------------------------------
##                                     Kappa                     0.1541
##                                     ASE                       0.1381
##                                     95% Lower Conf Limit     -0.1166
##                                     95% Upper Conf Limit      0.4247
## 
##                                             Sample Size = 35

Matched pairs odds ratio

SAS doesn’t have a built-in method for generating a matched pairs odds ratio and confidence interval. One possible way to calculate this is to create 2 x 2 x k table, where k is the number of matched pairs, and use the frequency procedure to caculate the Mantel-Haenszel odds ratio.

title 'McNemars Test is Equivalent to CMH Test';
data genetic2;
  input strata disease $ exposure $ @@;
  cards;
  1 case yes 1 cont yes 2 case yes 2 cont yes 3 case yes 3 cont yes
  4 case yes 4 cont yes 5 case yes 5 cont yes 6 case yes 6 cont yes
  7 case yes 7 cont yes 8 case yes 8 cont yes 9 case yes 9 cont yes
  10 case yes 10 cont no 11 case yes 11 cont no 12 case yes 12 cont no
  13 case yes 13 cont no 14 case yes 14 cont no 15 case yes 15 cont no
  16 case yes 16 cont no 17 case yes 17 cont no 18 case yes 18 cont no
  19 case yes 19 cont no 20 case yes 20 cont no 21 case yes 21 cont no
  22 case yes 22 cont no 
  23 case no 23 cont yes 24 case no 24 cont yes 25 case no 25 cont yes
  26 case no 26 cont no 27 case no 27 cont no 28 case no 28 cont no
  29 case no 29 cont no 30 case no 30 cont no 31 case no 31 cont no
  32 case no 32 cont no 33 case no 33 cont no 34 case no 34 cont no
  35 case no 35 cont no
;
run;

proc freq data = genetic2 order = data;
  table strata*disease*exposure / cmh noprint;
run;
##                                 McNemars Test is Equivalent to CMH Test
## 
##                                            The FREQ Procedure
## 
##                                Summary Statistics for disease by exposure
##                                          Controlling for strata
## 
##                       Cochran-Mantel-Haenszel Statistics (Based on Table Scores)
##  
##                     Statistic    Alternative Hypothesis    DF       Value      Prob
##                     ---------------------------------------------------------------
##                         1        Nonzero Correlation        1      6.2500    0.0124
##                         2        Row Mean Scores Differ     1      6.2500    0.0124
##                         3        General Association        1      6.2500    0.0124
## 
## 
##                                  Common Odds Ratio and Relative Risks
##  
##          Statistic                   Method                  Value       95% Confidence Limits
##          -------------------------------------------------------------------------------------
##          Odds Ratio                  Mantel-Haenszel        4.3333        1.2349       15.2064
##                                      Logit **               3.9482        1.2734       12.2417
## 
##          Relative Risk (Column 1)    Mantel-Haenszel        1.8333        1.1316        2.9702
##                                      Logit **               1.9870        1.0555        3.7405
## 
##          Relative Risk (Column 2)    Mantel-Haenszel        0.5652        0.3592        0.8894
##                                      Logit **               0.5033        0.2673        0.9474
## 
##                    ** These logit estimators use a correction of 0.5 in every cell  
##                         of those tables that contain a zero. Tables with a zero 
##                         row or a zero column are not included in computing the 
##                         logit estimators.
## 
## 
##                                           Breslow-Day Test for
##                                      Homogeneity of the Odds Ratios
##                                      ------------------------------
##                                      Chi-Square             24.9800
##                                      DF                          15
##                                      Pr > ChiSq              0.0502
## 
## 
##                                          Total Sample Size = 70