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

`hypothesis` formula interface #1133

Closed vincentarelbundock closed 3 months ago

vincentarelbundock commented 4 months ago

Emulate emmeans:

library(marginaleffects)

# need to sort for sequential to be the same
dat <- sort_by(mtcars, ~am + cyl)
mod <- lm(hp ~ factor(cyl) * factor(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"))