For the special case of a binary predictor, compute the OR from a 2 \(\times\) 2 table. In Table 6.1, the OR comparing the odds of “positive” between risk factor levels 2 and 1 is the cross-product \((d \times a) \div (b \times c)\). We started the cross-product at d since we wanted the odds of “positive” comparing risk factor level 2 to level 1. If, instead, we wanted the odds of “positive” comparing level 1 to level 2, we would start at b and compute the OR as \((b \times c) \div (d \times a)\) (as previously mentioned, if you change the order of comparison then you invert the OR).
|Condition negative||Condition positive|
|Risk factor level 1||a||b|
|Risk factor level 2||c||d|
Example 6.2: Using the 2019 National Survey of Drug Use and Health (NSDUH) teaching dataset (Section A.5), compute the OR comparing the odds of lifetime marijuana use (
mj_lifetime) between males and females (
load("Data/nsduh2019_adult_sub_rmph.RData") # Shorter name <- nsduh_adult_sub nsduh <- table(nsduh$demog_sex, nsduh$mj_lifetime) TAB TAB
## ## No Yes ## Male 206 260 ## Female 285 249
The way the table is set up, the cross-product \((d \times a) \div (b \times c)\) will give us the odds of “Yes” comparing females to males.
# (d*a) / (b*c) 2,2]*TAB[1,1])/(TAB[1,2]*TAB[2,1])(TAB[
##  0.6922
However, the question asked for the OR comparing males to females, so we need to use the formula \((b \times c) \div (d \times a)\).
# (b*c) / (d*a) 1,2]*TAB[2,1]) / (TAB[2,2]*TAB[1,1])(TAB[
##  1.445
Thus, the estimated OR for lifetime marijuana use comparing males to females is 1.44; males have 44% greater odds of lifetime marijuana use than females. We could have also obtained this OR by inverting the OR comparing females to males (1 / 0.69 = 1.44).
To review, here is a reminder of where the value of 1.44 is comes from:
- P(lifetime marijuana use among males) = 260 \(\div\) (206 + 260) = 0.56
- P(lifetime marijuana use among females) = 249 \(\div\) (285 + 249) = 0.47
- Odds for males = 0.56 \(\div\) (1 – 0.56) = 1.26 (odds \(>\) 1 because probability \(>\) 0.5)
- Odds for females = 0.47 \(\div\) (1 – 0.47) = 0.87 (odds \(<\) 1 because probability \(<\) 0.5)
- Odds ratio = 1.26 \(\div\) 0.87 = 1.44 (OR \(>\) 1 because odds for males \(>\) odds for females)
Below are some useful functions for converting probabilities to odds and ORs, along with the logit function and its inverse which we will use in the next section. These functions are all in
Functions_rmph.R which you loaded at the beginning of this chapter.
<- function(p) p/(1-p) odds <- function(p1, p2) odds(p1)/odds(p2) odds.ratio <- function(p) log(p/(1-p)) logit <- function(x) exp(x)/(1+exp(x)) ilogit # exp() is the exponential function # Example 6.1 <- TAB[1,2]/(TAB[1,2]+TAB[1,1]) # P(Yes | Male) PM <- TAB[2,2]/(TAB[2,2]+TAB[2,1]) # P(Yes | Female) PF <- odds(PM) # PM / (1 - PM) OM <- odds(PF) # PF / (1 - PF) OF <- odds.ratio(PM, PF) # Odds ratio OR.MvsF round(c(PM, PF, OM, OF, OR.MvsF), 2)
##  0.56 0.47 1.26 0.87 1.44