Learn R Programming

broom.helpers (version 1.17.0)

tidy_avg_comparisons: Marginal Contrasts with marginaleffects::avg_comparisons()

Description

[Experimental] Use marginaleffects::avg_comparisons() to estimate marginal contrasts and return a tibble tidied in a way that it could be used by broom.helpers functions. See marginaleffects::avg_comparisons() for a list of supported models.

Usage

tidy_avg_comparisons(x, conf.int = TRUE, conf.level = 0.95, ...)

Arguments

x

(a model object, e.g. glm)
A model to be tidied.

conf.int

(logical)
Whether or not to include a confidence interval in the tidied output.

conf.level

(numeric)
The confidence level to use for the confidence interval (between 0 ans 1).

...

Additional parameters passed to marginaleffects::avg_comparisons().

Details

By default, marginaleffects::avg_comparisons() estimate average marginal contrasts: a contrast is computed for each observed value in the original dataset (counterfactual approach) before being averaged. Marginal Contrasts at the Mean could be computed by specifying newdata = "mean". The variables argument can be used to select the contrasts to be computed. Please refer to the documentation page of marginaleffects::avg_comparisons().

See also tidy_marginal_contrasts() for taking into account interactions. For more information, see vignette("marginal_tidiers", "broom.helpers").

See Also

marginaleffects::avg_comparisons()

Other marginal_tieders: tidy_all_effects(), tidy_avg_slopes(), tidy_ggpredict(), tidy_marginal_contrasts(), tidy_marginal_means(), tidy_marginal_predictions(), tidy_margins()

Examples

Run this code
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_avg_comparisons(mod)
tidy_plus_plus(mod, tidy_fun = tidy_avg_comparisons)

mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris)
tidy_avg_comparisons(mod2)

# Custumizing the type of contrasts
tidy_avg_comparisons(
  mod2,
  variables = list(Petal.Width = 2, Species = "pairwise")
)

# Marginal Contrasts at the Mean
tidy_avg_comparisons(mod, newdata = "mean")
tidy_plus_plus(mod, tidy_fun = tidy_avg_comparisons, newdata = "mean")
}

Run the code above in your browser using DataLab