vincentarelbundock / marginaleffects

R package to compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and ML models. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference
https://marginaleffects.com
Other
444 stars 46 forks source link

Ordering for in-formula factors #1134

Closed vincentarelbundock closed 3 months ago

vincentarelbundock commented 4 months ago
library(marginaleffects)

dat <- within(mtcars, {
    am = factor(am)
    cyl = factor(cyl)
})
mod <- lm(hp ~ cyl * am, dat)

emmeans(mod, consec ~ cyl)$contrasts

avg_predictions(mod, 
    by = "cyl", 
    hypothesis = "sequential", 
    newdata = datagrid(grid_type = "balanced"))

emmeans(mod, consec ~ cyl | am)$contrasts

sh <- specify_hypothesis(hypothesis = "sequential", by = "am")
avg_predictions(mod, 
    by = c("am", "cyl"), 
    hypothesis = sh, 
    newdata = datagrid(grid_type = "balanced"))
vincentarelbundock commented 4 months ago

Maybe it doesn't make sense to allow | because in emmeans the variable before | on the RHS is used to specify the marginalization variable for marginal means, in a one step thing. In marginaleffects, this should still be the by argument.

However, doing hypothesis=sequential~am for groupwise hypotheses would be a really nice interface.