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

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 0.006983 0.005847 1.194 0.232 2.1 -0.004476 0.018442
hp 0.016404 0.012295 1.334 0.182 2.5 -0.007693 0.040501
hp 0.002828 0.003764 0.751 0.452 1.1 -0.004550 0.010206
hp 0.001935 0.002442 0.792 0.428 1.2 -0.002851 0.006721
hp 0.002993 0.003213 0.931 0.352 1.5 -0.003305 0.009291
hp 0.000148 0.000321 0.459 0.646 0.6 -0.000482 0.000778
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.2069 2.3 -0.00147 0.00677
wt -0.43578 0.1435 -3.04 0.0024 8.7 -0.71712 -0.15445
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 0.00853 0.00824 1.03 0.301 1.7 -0.00763 0.0247
wt -1.74453 1.55594 -1.12 0.262 1.9 -4.79411 1.3051
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.00171 0.684 0.494 1.0 -0.00218 0.00451
hp 110 0.00190 0.00240 0.788 0.431 1.2 -0.00282 0.00661
wt 100 -0.19468 0.29895 -0.651 0.515 1.0 -0.78061 0.39126
wt 110 -0.33154 0.42907 -0.773 0.440 1.2 -1.17250 0.50942
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:
mod2 <- lm(mpg ~ hp * cyl, data = mtcars)
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.00943 6.7 -0.1610 -0.0225
hp mean(dY/dX) 6 -0.0523 0.0204 -2.561 0.01045 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()`
mfx <- slopes(mod,
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.012035 0.00994 1.211 0.226 2.1 -0.00744 0.03151
hp 2.62 110 0.006983 0.00585 1.194 0.232 2.1 -0.00448 0.01844
hp 2.88 100 0.014161 0.01051 1.347 0.178 2.5 -0.00644 0.03476
hp 2.88 110 0.016404 0.01229 1.334 0.182 2.5 -0.00769 0.04050
hp 2.32 100 0.001564 0.00220 0.712 0.476 1.1 -0.00274 0.00587
hp 2.32 110 0.000656 0.00118 0.557 0.577 0.8 -0.00165 0.00296
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
mfx <- slopes(mod, vcov = sandwich::vcovHC(mod))
head(mfx)
```

```
Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
hp 0.006983 0.009047 0.772 0.440 1.2 -0.010748 0.024715
hp 0.016404 0.012419 1.321 0.187 2.4 -0.007936 0.040744
hp 0.002828 0.004876 0.580 0.562 0.8 -0.006728 0.012385
hp 0.001935 0.002035 0.951 0.342 1.5 -0.002054 0.005924
hp 0.002993 0.002928 1.022 0.307 1.7 -0.002747 0.008732
hp 0.000148 0.000235 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
mod <- lm(mpg ~ wt + drat, data = mtcars)
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.16
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.16 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.16 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
lc <- matrix(c(
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.16 8.29
Contrast B -11.46 4.92 -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
```