```
library(marginaleffects)
# Unit-level (conditional) Marginal Effects
<- glm(am ~ hp * wt, data = mtcars, family = binomial)
mod <- slopes(mod)
mfx head(mfx)
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 0.006989 0.005850 1.195 0.232 2.1 -0.004476 0.018454
hp 0.016403 0.012294 1.334 0.182 2.5 -0.007693 0.040499
hp 0.002832 0.003770 0.751 0.453 1.1 -0.004557 0.010220
hp 0.001934 0.002441 0.792 0.428 1.2 -0.002850 0.006717
hp 0.002991 0.003212 0.931 0.352 1.5 -0.003304 0.009287
hp 0.000148 0.000321 0.459 0.646 0.6 -0.000482 0.000777
Columns: rowid, term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted, am, hp, wt
Type: response
```

```
# Average Marginal Effect (AME)
avg_slopes(mod, by = TRUE)
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 0.00265 0.0021 1.26 0.20723 2.3 -0.00147 0.00678
wt -0.43571 0.1434 -3.04 0.00238 8.7 -0.71676 -0.15465
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# Marginal Effect at the Mean (MEM)
slopes(mod, newdata = datagrid())
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 0.00852 0.00825 1.03 0.301 1.7 -0.00764 0.0247
wt -1.74663 1.55762 -1.12 0.262 1.9 -4.79950 1.3062
Columns: rowid, term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted, am, hp, wt
Type: response
```

```
# Marginal Effect at User-Specified Values
# Variables not explicitly included in `datagrid()` are held at their means
slopes(mod, newdata = datagrid(hp = c(100, 110)))
```

```
Term hp Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 100 0.00117 0.0017 0.684 0.494 1.0 -0.00217 0.00451
hp 110 0.00189 0.0024 0.788 0.431 1.2 -0.00282 0.00660
wt 100 -0.19499 0.2993 -0.651 0.515 1.0 -0.78163 0.39165
wt 110 -0.33208 0.4295 -0.773 0.439 1.2 -1.17397 0.50981
Columns: rowid, term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, hp, predicted_lo, predicted_hi, predicted, am, wt
Type: response
```

```
# Group-Average Marginal Effects (G-AME)
# Calculate marginal effects for each observation, and then take the average
# marginal effect within each subset of observations with different observed
# values for the `cyl` variable:
<- lm(mpg ~ hp * cyl, data = mtcars)
mod2 avg_slopes(mod2, variables = "hp", by = "cyl")
```

```
Term Contrast cyl Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp mean(dY/dX) 4 -0.0917 0.0353 -2.596 0.00942 6.7 -0.1610 -0.0225
hp mean(dY/dX) 6 -0.0523 0.0204 -2.561 0.01044 6.6 -0.0923 -0.0123
hp mean(dY/dX) 8 -0.0128 0.0143 -0.891 0.37280 1.4 -0.0409 0.0153
Columns: term, contrast, cyl, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted
Type: response
```

```
# Marginal Effects at User-Specified Values (counterfactual)
# Variables not explicitly included in `datagrid()` are held at their
# original values, and the whole dataset is duplicated once for each
# combination of the values in `datagrid()`
<- slopes(mod,
mfx newdata = datagrid(hp = c(100, 110),
grid_type = "counterfactual"))
head(mfx)
```

```
Term wt hp Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 2.62 100 0.012042 0.00995 1.211 0.226 2.1 -0.00745 0.03153
hp 2.62 110 0.006989 0.00585 1.195 0.232 2.1 -0.00448 0.01845
hp 2.88 100 0.014156 0.01051 1.347 0.178 2.5 -0.00644 0.03475
hp 2.88 110 0.016403 0.01229 1.334 0.182 2.5 -0.00769 0.04050
hp 2.32 100 0.001566 0.00220 0.713 0.476 1.1 -0.00274 0.00587
hp 2.32 110 0.000657 0.00118 0.557 0.577 0.8 -0.00165 0.00297
Columns: rowid, rowidcf, term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, am, wt, hp, predicted_lo, predicted_hi, predicted
Type: response
```

```
# Heteroskedasticity robust standard errors
<- slopes(mod, vcov = sandwich::vcovHC(mod))
mfx head(mfx)
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 0.006989 0.009047 0.773 0.440 1.2 -0.010743 0.024722
hp 0.016403 0.012425 1.320 0.187 2.4 -0.007949 0.040755
hp 0.002832 0.004879 0.580 0.562 0.8 -0.006732 0.012395
hp 0.001934 0.002034 0.951 0.342 1.5 -0.002053 0.005920
hp 0.002991 0.002927 1.022 0.307 1.7 -0.002746 0.008728
hp 0.000148 0.000234 0.629 0.529 0.9 -0.000312 0.000607
Columns: rowid, term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high, predicted_lo, predicted_hi, predicted, am, hp, wt
Type: response
```

```
# hypothesis test: is the `hp` marginal effect at the mean equal to the `drat` marginal effect
<- lm(mpg ~ wt + drat, data = mtcars)
mod
slopes(
mod,newdata = "mean",
hypothesis = "wt = drat")
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
wt=drat -6.23 1.05 -5.92 <0.001 28.2 -8.29 -4.17
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# same hypothesis test using row indices
slopes(
mod,newdata = "mean",
hypothesis = "b1 - b2 = 0")
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
b1-b2=0 6.23 1.05 5.92 <0.001 28.2 4.17 8.29
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# same hypothesis test using numeric vector of weights
slopes(
mod,newdata = "mean",
hypothesis = c(1, -1))
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
custom 6.23 1.05 5.92 <0.001 28.2 4.17 8.29
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# two custom contrasts using a matrix of weights
<- matrix(c(
lc 1, -1,
2, 3),
ncol = 2)
colnames(lc) <- c("Contrast A", "Contrast B")
slopes(
mod,newdata = "mean",
hypothesis = lc)
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
Contrast A 6.23 1.05 5.92 <0.001 28.2 4.17 8.29
Contrast B -11.46 4.91 -2.33 0.0197 5.7 -21.10 -1.83
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```