Skip to content

Instantly share code, notes, and snippets.

@cmrnp
Created April 16, 2022 06:51
Show Gist options
  • Save cmrnp/7f5a3fbb3fcde982bef212c7105cae04 to your computer and use it in GitHub Desktop.
Save cmrnp/7f5a3fbb3fcde982bef212c7105cae04 to your computer and use it in GitHub Desktop.
library(emmeans)
data(mtcars)
# in the example here, all models give the same point estimates and similar
# SEs because there is only a single, categorical, variable in the model.
# if you add a continuous predictor, they will no longer, because the relationship
# assumed by the model will be different for the three models!
m <- glm(am ~ vs, data = mtcars, family = binomial)
em1 <- emmeans(m, "vs")
pairs(em1, type = "response", infer = TRUE)
# risk difference from logit model
em2 <- regrid(emmeans(m, "vs"), transform = "response")
pairs(em2, type = "response", infer = TRUE)
# relative risk from logit model
em3 <- regrid(em2, transform = "log")
pairs(em3, type = "response", infer = TRUE)
# relative risk from quasipoisson model
m2 <- glm(am ~ vs, data = mtcars, family = quasipoisson)
em4 <- emmeans(m2, "vs")
pairs(em4, type = "response", infer = TRUE)
# risk difference from linear model (beware: not using robust SEs here)
m3 <- lm(am ~ vs, data = mtcars)
em5 <- emmeans(m3, "vs")
pairs(em5, type = "response", infer = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment