是的,你可以使用emmeans来计算47岁和33岁女性的比值比.(我把年龄四舍五入了,因为47.356和32.634似乎对一个人的年龄来说很特殊.
为了清晰起见,我重新调整了模型,没有rms
个铃铛和口哨.我也会计算emmeans
和marginaleffects
的对比度.
# ... code to generate myData ...
fit.lrm <- lrm(y ~ age * sex, data = myData)
# Log odds ratio
k <- rms::contrast(
fit.lrm,
list(sex = "female", age = 47),
list(sex = "female", age = 33)
)
# Odds ratio
print(k, fun = exp)
#> sex Contrast S.E. Lower Upper Z Pr(>|z|)
#> 1 female 12.70547 NA 4.68864 34.42982 5 0
#>
#> Confidence intervals are 0.95 individual intervals
fit.glm <- glm(y ~ age * sex, family = binomial(), data = myData)
对于emmeans
,首先我们指定参考网格(在本例中,性别=两个不同年龄的女性),然后我们计算成对对比度.
emm <- emmeans(fit.glm, ~ age + sex, at = list(sex = "female", age = c(47, 33)))
# Use `type = "response"` to get the odds ratio (rather than the log odds ratio)
emmeans::contrast(emm, method = "pairwise", type = "response")
#> contrast odds.ratio SE df null z.ratio p.value
#> age47 female / age33 female 12.7 6.46 Inf 1 4.998 <.0001
#>
#> Tests are performed on the log odds ratio scale
# `pairs` is short-hand for `contrast(emm, method = "pairwise")`
confint(pairs(emm, type = "response"))
#> contrast odds.ratio SE df asymp.LCL asymp.UCL
#> age47 female / age33 female 12.7 6.46 Inf 4.69 34.4
#>
#> Confidence level used: 0.95
#> Intervals are back-transformed from the log odds ratio scale
我们用marginaleffects
来计算对比度(即.比较)一步.
marginaleffects::comparisons(
fit.glm,
variables = list(age = c(47, 33)),
newdata = datagrid(sex = "female"),
type = "link",
transform = exp
)
#>
#> Term Contrast sex Estimate Pr(>|z|) S 2.5 % 97.5 % age
#> age 47 - 33 female 12.7 <0.001 20.7 4.69 34.4 40.4
#>
#> Columns: rowid, term, contrast, estimate, p.value, s.value, conf.low, conf.high, sex, predicted_lo, predicted_hi, predicted, y, age
#> Type: link