I have a contrast using lrm in the rms package. How can I recreate this in emmeans?
# example from rms package
library("rms")
set.seed(1)
age <- rnorm(200,40,12)
sex <- factor(sample(c('female','male'),200,TRUE))
logit <- (sex=='male') + (age-40)/5
y <- ifelse(runif(200) <= plogis(logit), 1, 0)
myData <- data.frame(y=y,sex=sex,age=age)
dd <- datadist(myData)
options(datadist="dd")
f <- lrm(y ~ age*sex,data=myData)
# what is the difference in log odds for two ages for females
k <- contrast(f, list(sex='female', age=47.356), list(sex='female', age=32.634))
# Can this be done in emmeans
print(k,fun=exp)
Yes, you can use emmeans to compute the odds ratio for females at the ages of 47 and 33. (I've rounded the ages as 47.356 and 32.634 seem oddly specific for a person's age.)
For clarity I refit the model without the
rmsbells and whistles. I'll also calculate the same contrast with bothemmeansandmarginaleffects.With
emmeansfirst we specify the reference grid (in this case, sex = female at two different ages) and then we calculate the pairwise contrast.With
marginaleffectswe calculate the contrast (ie. the comparison) in one step.