library(marginaleffects)
library(magrittr)
# Linear model
tmp <- mtcars
tmp$am <- as.logical(tmp$am)
mod <- lm(mpg ~ am + factor(cyl), tmp)
comparisons(mod, variables = list(cyl = "reference")) %>% tidy()
comparisons(mod, variables = list(cyl = "sequential")) %>% tidy()
comparisons(mod, variables = list(cyl = "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, gear = tmp$gear))
comparisons(mod, newdata = datagrid(am = unique, gear = max))
m <- lm(mpg ~ hp + drat + factor(cyl) + factor(am), data = mtcars)
comparisons(m, variables = "hp", newdata = datagrid(FUN_factor = unique, FUN_numeric = median))
# Numeric contrasts
mod <- lm(mpg ~ hp, data = mtcars)
comparisons(mod, variables = list(hp = 1)) %>% tidy()
comparisons(mod, variables = list(hp = 5)) %>% tidy()
comparisons(mod, variables = list(hp = c(90, 100))) %>% tidy()
comparisons(mod, variables = list(hp = "iqr")) %>% tidy()
comparisons(mod, variables = list(hp = "sd")) %>% tidy()
comparisons(mod, variables = list(hp = "minmax")) %>% tidy()
# using a function to specify a custom difference in one regressor
dat <- mtcars
dat$new_hp <- 49 * (dat$hp - min(dat$hp)) / (max(dat$hp) - min(dat$hp)) + 1
modlog <- lm(mpg ~ log(new_hp) + factor(cyl), data = dat)
fdiff <- \(x) data.frame(x, x + 10)
comparisons(modlog, variables = list(new_hp = fdiff)) %>% summary()
# Adjusted Risk Ratio: see the contrasts vignette
mod <- glm(vs ~ mpg, data = mtcars, family = binomial)
cmp <- comparisons(mod, transform_pre = "lnratioavg")
summary(cmp, transform_avg = 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_avg = exp)
# cross contrasts
mod <- lm(mpg ~ factor(cyl) * factor(gear) + hp, data = mtcars)
cmp <- comparisons(mod, variables = c("cyl", "gear"), cross = TRUE)
summary(cmp)
# variable-specific contrasts
cmp <- comparisons(mod, variables = list(gear = "sequential", hp = 10))
summary(cmp)
# hypothesis test: is the `hp` marginal effect at the mean equal to the `drat` marginal effect
mod <- lm(mpg ~ wt + drat, data = mtcars)
comparisons(
mod,
newdata = "mean",
hypothesis = "wt = drat")
# same hypothesis test using row indices
comparisons(
mod,
newdata = "mean",
hypothesis = "b1 - b2 = 0")
# same hypothesis test using numeric vector of weights
comparisons(
mod,
newdata = "mean",
hypothesis = c(1, -1))
# two custom contrasts using a matrix of weights
lc <- matrix(c(
1, -1,
2, 3),
ncol = 2)
comparisons(
mod,
newdata = "mean",
hypothesis = lc)
# `by` argument
mod <- lm(mpg ~ hp * am * vs, data = mtcars)
cmp <- comparisons(mod, variables = "hp", by = c("vs", "am"))
summary(cmp)
library(nnet)
mod <- multinom(factor(gear) ~ mpg + am * vs, data = mtcars, trace = FALSE)
by <- data.frame(
group = c("3", "4", "5"),
by = c("3,4", "3,4", "5"))
comparisons(mod, type = "probs", by = by)
Run the code above in your browser using DataLab