Skip to contents

Native unit-level partial derivatives \(dY/dX\) for lm and glm fits with the defaults of marginaleffects::slopes(): a centred finite difference \((f(x + e/2) - f(x - e/2)) / e\) with \(e = 10^{-4} \times\) the range of x in the model data, on the response scale, and a delta-method SE from the analytic Jacobian of that difference. Categorical predictors give differences from the reference level, as in marginaleffects. by = TRUE averages (avg_slopes()). Cross-validated against marginaleffects in the tests.

Usage

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

Arguments

model

A fitted lm or glm object.

variables

Character vector of focal variables. When NULL, all predictors.

...

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

Value

A data frame shaped like morie_effects_comparisons() with contrast = "dY/dX" for numeric predictors.

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_slopes(fit, variables = "x"))
#>   rowid term contrast    estimate std.error  statistic   p.value   s.value
#> 1     1    x    dY/dX -0.03529005 0.1223341 -0.2884727 0.7729849 0.3714879
#> 2     2    x    dY/dX -0.03529005 0.1223341 -0.2884727 0.7729849 0.3714879
#> 3     3    x    dY/dX -0.03529005 0.1223341 -0.2884727 0.7729849 0.3714879
#> 4     4    x    dY/dX -0.03529005 0.1223341 -0.2884727 0.7729849 0.3714879
#> 5     5    x    dY/dX -0.03529005 0.1223341 -0.2884727 0.7729849 0.3714879
#> 6     6    x    dY/dX -0.03529005 0.1223341 -0.2884727 0.7729849 0.3714879
#>     conf.low conf.high          y           x g predicted_lo predicted_hi
#> 1 -0.2750605 0.2044804 -0.6264538  2.40161776 a   0.16035857   0.16034372
#> 2 -0.2750605 0.2044804  0.1836433 -0.03924000 b  -0.02058817  -0.02060302
#> 3 -0.2750605 0.2044804 -0.8356286  0.68973936 a   0.22077085   0.22075601
#> 4 -0.2750605 0.2044804  1.5952808  0.02800216 b  -0.02296115  -0.02297600
#> 5 -0.2750605 0.2044804  0.3295078 -0.74327321 a   0.27134194   0.27132710
#> 6 -0.2750605 0.2044804 -0.8204684  0.18879230 b  -0.02863545  -0.02865029