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

R

To conduct a Cochran-Mantel-Haenszel test in R you can use mantelhaen.test() from the stats package. In this example, we first create our dataset and then 2 x 2 x 2 table as shown elsewhere. The 2 x 2 x 2 table is then piped into mantelhaen.test(). Notice that you must set correct = FALSE as the default is to conduct a test with a continuity correction.

library(tidyverse)
library(magrittr)

health_policy <- tibble(
  stress = rep(c("low", "low", "high", "high"), 2), 
  opinion = rep(c("favorable", "unfavorable"), 4), 
  region = c(rep("rural", 4), rep("urban", 4)), 
  count = c(55, 135, 7, 53, 48, 12, 96, 94)
) 

health_policy %<>%
  mutate(stress = fct_relevel(stress, c("low", "high")))

xtabs(count ~ stress + opinion + region, data = health_policy) %>%
  mantelhaen.test(correct = FALSE)
## 
##  Mantel-Haenszel chi-squared test without continuity correction
## 
## data:  .
## Mantel-Haenszel X-squared = 23.05, df = 1, p-value = 1.578e-06
## alternative hypothesis: true common odds ratio is not equal to 1
## 95 percent confidence interval:
##  2.066744 6.069367
## sample estimates:
## common odds ratio 
##          3.541726

SAS

To conduct a Cochran-Mantel-Haenszel test in SAS you use the frequency procedure with the cmh argument specified in the options of the table statement.

data healthpolicy;
  input stress $ opinion $ region $ count;
  cards;
    low favorable rural 55
    low unfavorable rural 135
    high favorable rural 7
    high unfavorable rural 53
    low favorable urban 48
    low unfavorable urban 12
    high favorable urban 96
    high unfavorable urban 94
    ;
run;
    
proc freq data = healthpolicy order = data;
  table region*stress*opinion / chisq relrisk cmh;
  weight count;
run;
##                                            The FREQ Procedure
## 
##                                       Table 1 of stress by opinion
##                                       Controlling for region=rural
## 
##                                   stress     opinion
## 
##                                   Frequency|
##                                   Percent  |
##                                   Row Pct  |
##                                   Col Pct  |favorabl|unfavora|  Total
##                                   ---------+--------+--------+
##                                   low      |     55 |    135 |    190
##                                            |  22.00 |  54.00 |  76.00
##                                            |  28.95 |  71.05 |
##                                            |  88.71 |  71.81 |
##                                   ---------+--------+--------+
##                                   high     |      7 |     53 |     60
##                                            |   2.80 |  21.20 |  24.00
##                                            |  11.67 |  88.33 |
##                                            |  11.29 |  28.19 |
##                                   ---------+--------+--------+
##                                   Total          62      188      250
##                                               24.80    75.20   100.00
## 
## 
##                               Statistics for Table 1 of stress by opinion
##                                       Controlling for region=rural
## 
##                          Statistic                     DF       Value      Prob
##                          ------------------------------------------------------
##                          Chi-Square                     1      7.3016    0.0069
##                          Likelihood Ratio Chi-Square    1      8.1976    0.0042
##                          Continuity Adj. Chi-Square     1      6.4044    0.0114
##                          Mantel-Haenszel Chi-Square     1      7.2724    0.0070
##                          Phi Coefficient                       0.1709          
##                          Contingency Coefficient               0.1685          
##                          Cramer's V                            0.1709          
## 
## 
##                                           Fisher's Exact Test
##                                    ----------------------------------
##                                    Cell (1,1) Frequency (F)        55
##                                    Left-sided Pr <= F          0.9988
##                                    Right-sided Pr >= F         0.0041
##                                                                      
##                                    Table Probability (P)       0.0029
##                                    Two-sided Pr <= P           0.0061
## 
## 
##                                      Odds Ratio and Relative Risks
##  
##                    Statistic                        Value       95% Confidence Limits
##                    ------------------------------------------------------------------
##                    Odds Ratio                      3.0847        1.3207        7.2045
##                    Relative Risk (Column 1)        2.4812        1.1945        5.1539
##                    Relative Risk (Column 2)        0.8044        0.7069        0.9153
## 
##                                            Sample Size = 250
## 
## 
##                                       Table 2 of stress by opinion
##                                       Controlling for region=urban
## 
##                                   stress     opinion
## 
##                                   Frequency|
##                                   Percent  |
##                                   Row Pct  |
##                                   Col Pct  |favorabl|unfavora|  Total
##                                   ---------+--------+--------+
##                                   low      |     48 |     12 |     60
##                                            |  19.20 |   4.80 |  24.00
##                                            |  80.00 |  20.00 |
##                                            |  33.33 |  11.32 |
##                                   ---------+--------+--------+
##                                   high     |     96 |     94 |    190
##                                            |  38.40 |  37.60 |  76.00
##                                            |  50.53 |  49.47 |
##                                            |  66.67 |  88.68 |
##                                   ---------+--------+--------+
##                                   Total         144      106      250
##                                               57.60    42.40   100.00
## 
## 
##                               Statistics for Table 2 of stress by opinion
##                                       Controlling for region=urban
## 
##                          Statistic                     DF       Value      Prob
##                          ------------------------------------------------------
##                          Chi-Square                     1     16.2198    <.0001
##                          Likelihood Ratio Chi-Square    1     17.3520    <.0001
##                          Continuity Adj. Chi-Square     1     15.0354    0.0001
##                          Mantel-Haenszel Chi-Square     1     16.1549    <.0001
##                          Phi Coefficient                       0.2547          
##                          Contingency Coefficient               0.2468          
##                          Cramer's V                            0.2547          
## 
## 
##                                           Fisher's Exact Test
##                                    ----------------------------------
##                                    Cell (1,1) Frequency (F)        48
##                                    Left-sided Pr <= F          1.0000
##                                    Right-sided Pr >= F         <.0001
##                                                                      
##                                    Table Probability (P)       <.0001
##                                    Two-sided Pr <= P           <.0001
## 
## 
##                                      Odds Ratio and Relative Risks
##  
##                    Statistic                        Value       95% Confidence Limits
##                    ------------------------------------------------------------------
##                    Odds Ratio                      3.9167        1.9575        7.8366
##                    Relative Risk (Column 1)        1.5833        1.3104        1.9131
##                    Relative Risk (Column 2)        0.4043        0.2389        0.6841
## 
##                                            Sample Size = 250
##  
##                                                                                                         
##  
##                                            The FREQ Procedure
## 
##                                 Summary Statistics for stress by opinion
##                                          Controlling for region
## 
##                       Cochran-Mantel-Haenszel Statistics (Based on Table Scores)
##  
##                     Statistic    Alternative Hypothesis    DF       Value      Prob
##                     ---------------------------------------------------------------
##                         1        Nonzero Correlation        1     23.0502    <.0001
##                         2        Row Mean Scores Differ     1     23.0502    <.0001
##                         3        General Association        1     23.0502    <.0001
## 
## 
##                                  Common Odds Ratio and Relative Risks
##  
##          Statistic                   Method                  Value       95% Confidence Limits
##          -------------------------------------------------------------------------------------
##          Odds Ratio                  Mantel-Haenszel        3.5417        2.0667        6.0694
##                                      Logit                  3.5593        2.0805        6.0891
## 
##          Relative Risk (Column 1)    Mantel-Haenszel        1.7518        1.4071        2.1808
##                                      Logit                  1.6286        1.3560        1.9560
## 
##          Relative Risk (Column 2)    Mantel-Haenszel        0.6607        0.5554        0.7861
##                                      Logit                  0.7735        0.6823        0.8769
## 
## 
##                                           Breslow-Day Test for
##                                      Homogeneity of the Odds Ratios
##                                      ------------------------------
##                                      Chi-Square              0.1830
##                                      DF                           1
##                                      Pr > ChiSq              0.6688
## 
## 
##                                         Total Sample Size = 500