Skip to contents

Native unit-level counterfactual differences for lm and glm fits with the defaults of marginaleffects::comparisons(): a numeric predictor is moved from its observed value x to x + 1 (or x + step via variables = list(x = step)), a factor / character / logical / 0-1 predictor is moved from its reference level to each other level; the difference of predictions (response scale) gets a delta-method SE from the analytic Jacobian. Terms are ordered alphabetically. by = TRUE averages over units (the avg_comparisons() result). Cross-validated against marginaleffects in the tests.

Usage

morie_effects_comparisons(model, variables = NULL, ...)

Arguments

model

A fitted lm or glm object.

variables

Character vector of focal predictors, or a named list giving a numeric step for numeric predictors. When NULL, all predictors.

...

Optional native settings: newdata, type, by, conf_level, vcov, df and comparison (only "difference"). Any other argument is an error.

Value

A data frame with rowid (unit-level) or the by columns, term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high; unit-level rows also carry the data columns and predicted_lo / predicted_hi.

Examples

set.seed(1)
df <- data.frame(y = rnorm(60), x = rnorm(60), g = factor(rep(c("a", "b"), 30)))
fit <- stats::lm(y ~ x + g, data = df)
head(morie_effects_comparisons(fit, variables = "g"))
#>   rowid term contrast   estimate std.error statistic   p.value  s.value
#> 1     1    g    b - a -0.2670847 0.2218279 -1.204018 0.2285828 2.129211
#> 2     2    g    b - a -0.2670847 0.2218279 -1.204018 0.2285828 2.129211
#> 3     3    g    b - a -0.2670847 0.2218279 -1.204018 0.2285828 2.129211
#> 4     4    g    b - a -0.2670847 0.2218279 -1.204018 0.2285828 2.129211
#> 5     5    g    b - a -0.2670847 0.2218279 -1.204018 0.2285828 2.129211
#> 6     6    g    b - a -0.2670847 0.2218279 -1.204018 0.2285828 2.129211
#>     conf.low conf.high          y           x g predicted_lo predicted_hi
#> 1 -0.7018595   0.16769 -0.6264538  2.40161776 a    0.1603511 -0.106733600
#> 2 -0.7018595   0.16769  0.1836433 -0.03924000 b    0.2464892 -0.020595596
#> 3 -0.7018595   0.16769 -0.8356286  0.68973936 a    0.2207634 -0.046321318
#> 4 -0.7018595   0.16769  1.5952808  0.02800216 b    0.2441162 -0.022968576
#> 5 -0.7018595   0.16769  0.3295078 -0.74327321 a    0.2713345  0.004249774
#> 6 -0.7018595   0.16769 -0.8204684  0.18879230 b    0.2384419 -0.028642868