if (FALSE) { # interactive()
# Average Marginal Contrasts
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_marginal_contrasts(mod)
tidy_plus_plus(mod, tidy_fun = tidy_marginal_contrasts)
mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris)
tidy_marginal_contrasts(mod2)
tidy_marginal_contrasts(
mod2,
variables_list = variables_to_predict(
mod2,
continuous = 3,
categorical = "pairwise"
)
)
# Model with interactions
mod3 <- glm(
Survived ~ Sex * Age + Class,
data = df, family = binomial
)
tidy_marginal_contrasts(mod3)
tidy_marginal_contrasts(mod3, "no_interaction")
tidy_marginal_contrasts(mod3, "cross")
tidy_marginal_contrasts(
mod3,
variables_list = list(
list(variables = list(Class = "pairwise"), by = list(Sex = unique)),
list(variables = list(Age = "all")),
list(variables = list(Class = "sequential", Sex = "reference"))
)
)
mod4 <- lm(Sepal.Length ~ Petal.Length * Petal.Width + Species, data = iris)
tidy_marginal_contrasts(mod4)
tidy_marginal_contrasts(
mod4,
variables_list = list(
list(
variables = list(Species = "sequential"),
by = list(Petal.Length = c(2, 5))
),
list(
variables = list(Petal.Length = 2),
by = list(Species = unique, Petal.Width = 2:4)
)
)
)
# Marginal Contrasts at the Mean
tidy_marginal_contrasts(mod, newdata = "mean")
tidy_marginal_contrasts(mod3, newdata = "mean")
}
Run the code above in your browser using DataLab