library(haven) # import Stata data and labelslibrary(dplyr) # data manipulationlibrary(marginaleffects) # predictions and marginal effectslibrary(questionr) # odds ratios# Health and Retirement Study 2006hrs <-read_dta("https://tdmize.github.io/data/data/hrs06.dta")# Treat binary/nominal variables as factors using their value labelshrs <- hrs |>mutate(black =as_factor(black), woman =as_factor(woman), ed4cat =as_factor(ed4cat))diabmod <-glm(diabetes ~ age + black + bmi + woman + ed4cat,family =binomial(link ="logit"), data = hrs)odds.ratio(diabmod, level = .95, digits =4)
di 0.2479810 / (1 - 0.2479810) // Odds when black = 1
.32975364
di 0.1668597 / (1 - 0.1668597) // Odds when black = 0
.20027803
di %6.4f .32975364 / .20027803 // Odds ratio
1.6465
# Compute OR for effect of race: predictions with other variables at means/modesprs_black <-predictions(diabmod, newdata =datagrid(black =c("White", "Black")))prs_black
black Estimate Pr(>|z|) S 2.5 % 97.5 %
White 0.157 <0.001 Inf 0.147 0.168
Black 0.235 <0.001 308.6 0.215 0.256
Type: invlink(link)
comparisons(diabmod, variables ="black", newdata ="mean") # MEM
Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
0.0778 0.00909 8.56 <0.001 56.3 0.06 0.0956
Term: black
Type: response
Comparison: Black - White
odds_black <- prs_black$estimate / (1- prs_black$estimate)odds_black # Odds when black = 0 and black = 1
[1] 0.1866140 0.3072561
odds_black[2] / odds_black[1] # Odds ratio
[1] 1.64648
Reminder—odds (Ω) are just a transformation of a probability (π):
odds_bmi <- prs_bmi$estimate / (1- prs_bmi$estimate)odds_bmi # Odds when bmi = mean and bmi = mean + 1
[1] 0.1865501 0.2063179
odds_bmi[2] / odds_bmi[1] # Odds ratio
[1] 1.105965
Given that the OR is simply a different way to present the same information a MEM summarizes—which would you prefer to interpret? Which is easier to understand?