# NOT RUN {
library(marginaleffects)
library(magrittr)
# Linear model
tmp <- mtcars
tmp$am <- as.logical(tmp$am)
mod <- lm(mpg ~ am + factor(cyl), tmp)
comparisons(mod, contrast_factor = "reference") %>% tidy()
comparisons(mod, contrast_factor = "sequential") %>% tidy()
comparisons(mod, contrast_factor = "pairwise") %>% tidy()
# GLM with different scale types
mod <- glm(am ~ factor(gear), data = mtcars)
comparisons(mod, type = "response") %>% tidy()
comparisons(mod, type = "link") %>% tidy()
# Contrasts at the mean
comparisons(mod, newdata = "mean")
# Contrasts between marginal means
comparisons(mod, newdata = "marginalmeans")
# Contrasts at user-specified values
comparisons(mod, newdata = datagrid(am = 0, cyl = tmp$cyl))
# Numeric contrasts
mod <- lm(mpg ~ hp, data = mtcars)
comparisons(mod, contrast_numeric = 1) %>% tidy()
comparisons(mod, contrast_numeric = 5) %>% tidy()
comparisons(mod, contrast_numeric = c(90, 100)) %>% tidy()
comparisons(mod, contrast_numeric = "iqr") %>% tidy()
comparisons(mod, contrast_numeric = "sd") %>% tidy()
comparisons(mod, contrast_numeric = "minmax") %>% tidy()
# Adjusted Risk Ratio (see Case Study vignette on the website)
mod <- glm(vs ~ mpg, data = mtcars, family = binomial)
cmp <- comparisons(mod, transform_pre = "lnratioavg")
summary(cmp, transform_post = exp)
# Adjusted Risk Ratio: Manual specification of the `transform_pre`
cmp <- comparisons(mod, transform_pre = function(hi, lo) log(mean(hi) / mean(lo)))
summary(cmp, transform_post = exp)
# Interactions between contrasts
mod <- lm(mpg ~ factor(cyl) * factor(gear) + hp, data = mtcars)
cmp <- comparisons(mod, variables = c("cyl", "gear"))
summary(cmp)
# variable-specific contrasts
cmp <- comparisons(mod, variables = list(gear = "sequential", hp = 10))
summary(cmp)
# }
Run the code above in your browser using DataLab