The notes for the topics on this page can be found in the lectures 5 and 7 folders on Canvas.

R

Odds ratios and risk ratios can be calculated from contingency tables using the functions oddsratio() and riskratio() from the epitools package. Risk difference can be calculated from contingency tables using the riskdiff() function from the catfun package. For further help, please watch the video example.

library(epitools)
library(catfun)

trial <- tibble(
  disease = c(rep("yes", 2), rep("no", 2)), 
  treatment = c(rep(c("estrogen", "placebo"), 2)), 
  count = c(751, 623, 7755, 7479)
)

trial <- trial %>% 
  mutate(disease = as.factor(disease), 
         treatment = as.factor(treatment),
         disease = fct_relevel(disease, "yes", "no"), 
         treatment = fct_relevel(treatment, "estrogen", "placebo"))

For these examples, the oddsratio() and riskratio() are used in combination with xtabs() while riskdiff() only calls upon the dataset.

Odds ratio

xtabs(count ~ treatment + disease, data = trial) %>% 
  oddsratio(method = "wald")
## $data
##           disease
## treatment   yes    no Total
##   estrogen  751  7755  8506
##   placebo   623  7479  8102
##   Total    1374 15234 16608
## 
## $measure
##           odds ratio with 95% C.I.
## treatment  estimate   lower    upper
##   estrogen 1.000000      NA       NA
##   placebo  1.162555 1.04056 1.298853
## 
## $p.value
##           two-sided
## treatment   midp.exact fisher.exact  chi.square
##   estrogen          NA           NA          NA
##   placebo  0.007681115  0.008067297 0.007702585
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

Risk ratio

xtabs(count ~ treatment + disease, data = trial) %>% 
  riskratio()
## $data
##           disease
## treatment   yes    no Total
##   estrogen  751  7755  8506
##   placebo   623  7479  8102
##   Total    1374 15234 16608
## 
## $measure
##           risk ratio with 95% C.I.
## treatment  estimate    lower    upper
##   estrogen   1.0000       NA       NA
##   placebo    1.0125 1.003305 1.021779
## 
## $p.value
##           two-sided
## treatment   midp.exact fisher.exact  chi.square
##   estrogen          NA           NA          NA
##   placebo  0.007681115  0.008067297 0.007702585
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

Risk difference

trial %>% 
  riskdiff("treatment", "disease", weight = "count")
## 
## Risk difference: 0.0114 
## 95% confidence interval: 0.003 0.0198 
## 
## Proportion 1 = 0.0883 
## Proportion 2 = 0.0769 
## --------------------------------------------- 
## 
## Contingency table: 
## 
##           disease
## treatment   yes   no
##   estrogen  751 7755
##   placebo   623 7479

R demonstration

SAS

To caculate measures of association from a contingency table in SAS you use the frequency procedure with the relrisk argument in table statement to generate the relative risk/odds ratio and the riskdiff argument to generate the risk difference. This will always produce confidence intervals for generated measures. The syntax is:

proc freq data = <dataset>; 
  table <row variable>*<column variable> / <relrisk> <riskdiff>; 
  weight <weighting variable if summary data>; 
run; 

For further help, please watch the video example.

data trial; 
  input disease $ treatment $ count; 
  cards; 
  yes estrogen 751
  yes placebo 623
  no estrogen 7755
  no placebo 7479
  ; 
run; 
  
proc freq data = trial order = data; 
  table treatment*disease / riskdiff relrisk; 
  weight count; 
run; 
##                                            The FREQ Procedure
## 
##                                      Table of treatment by disease
## 
##                                   treatment     disease
## 
##                                   Frequency|
##                                   Percent  |
##                                   Row Pct  |
##                                   Col Pct  |yes     |no      |  Total
##                                   ---------+--------+--------+
##                                   estrogen |    751 |   7755 |   8506
##                                            |   4.52 |  46.69 |  51.22
##                                            |   8.83 |  91.17 |
##                                            |  54.66 |  50.91 |
##                                   ---------+--------+--------+
##                                   placebo  |    623 |   7479 |   8102
##                                            |   3.75 |  45.03 |  48.78
##                                            |   7.69 |  92.31 |
##                                            |  45.34 |  49.09 |
##                                   ---------+--------+--------+
##                                   Total        1374    15234    16608
##                                                8.27    91.73   100.00
## 
## 
##                               Statistics for Table of treatment by disease
## 
##                                         Column 1 Risk Estimates
##  
##                                                    (Asymptotic) 95%         (Exact) 95%
##                                Risk        ASE     Confidence Limits     Confidence Limits
##              -----------------------------------------------------------------------------
##              Row 1           0.0883     0.0031     0.0823     0.0943     0.0823     0.0945
##              Row 2           0.0769     0.0030     0.0711     0.0827     0.0712     0.0829
##              Total           0.0827     0.0021     0.0785     0.0869     0.0786     0.0870
## 
##              Difference      0.0114     0.0043     0.0030     0.0198                      
## 
##                                      Difference is (Row 1 - Row 2)
## 
## 
##                                         Column 2 Risk Estimates
##  
##                                                    (Asymptotic) 95%         (Exact) 95%
##                                Risk        ASE     Confidence Limits     Confidence Limits
##              -----------------------------------------------------------------------------
##              Row 1           0.9117     0.0031     0.9057     0.9177     0.9055     0.9177
##              Row 2           0.9231     0.0030     0.9173     0.9289     0.9171     0.9288
##              Total           0.9173     0.0021     0.9131     0.9215     0.9130     0.9214
## 
##              Difference     -0.0114     0.0043    -0.0198    -0.0030                      
## 
##                                      Difference is (Row 1 - Row 2)
## 
## 
##                                      Odds Ratio and Relative Risks
##  
##                    Statistic                        Value       95% Confidence Limits
##                    ------------------------------------------------------------------
##                    Odds Ratio                      1.1626        1.0406        1.2989
##                    Relative Risk (Column 1)        1.1482        1.0371        1.2712
##                    Relative Risk (Column 2)        0.9877        0.9787        0.9967
## 
##                                           Sample Size = 16608

SAS demonstration