```
library(marginaleffects)
library(marginaleffects)
# simple marginal means for each level of `cyl`
dat <- mtcars
dat$carb <- factor(dat$carb)
dat$cyl <- factor(dat$cyl)
dat$am <- as.logical(dat$am)
mod <- lm(mpg ~ carb + cyl + am, dat)
marginal_means(
mod,
variables = "cyl")
```

```
Term Value Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
cyl 4 23.1 1.66 13.9 <0.001 144.3 19.9 26.4
cyl 6 20.4 1.34 15.2 <0.001 171.9 17.8 23.0
cyl 8 16.2 1.07 15.1 <0.001 169.0 14.1 18.3
Results averaged over levels of: carb, am, cyl
Columns: term, value, cyl, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# collapse levels of cyl by averaging
by <- data.frame(
cyl = c(4, 6, 8),
by = c("4 & 6", "4 & 6", "8"))
marginal_means(mod,
variables = "cyl",
by = by)
```

```
By Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
4 & 6 21.7 1.13 19.2 <0.001 270.8 19.5 24.0
8 16.2 1.07 15.1 <0.001 169.0 14.1 18.3
Results averaged over levels of: carb, am, cyl
Columns: by, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# pairwise differences between collapsed levels
marginal_means(mod,
variables = "cyl",
by = by,
hypothesis = "pairwise")
```

```
Term Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
4 & 6 - 8 5.54 1.51 3.66 <0.001 12.0 2.57 8.5
Results averaged over levels of: carb, am, cyl
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# cross
marginal_means(mod,
variables = c("cyl", "carb"),
cross = TRUE)
```

```
Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
25.8 1.26 20.43 <0.001 305.7 23.34 28.3
25.6 1.17 21.93 <0.001 351.8 23.30 27.9
25.3 2.37 10.71 <0.001 86.6 20.70 30.0
21.9 1.90 11.51 <0.001 99.4 18.15 25.6
20.3 3.77 5.39 <0.001 23.7 12.91 27.7
19.8 3.81 5.18 <0.001 22.1 12.29 27.2
23.1 1.77 13.08 <0.001 127.4 19.63 26.5
22.9 1.87 12.24 <0.001 112.0 19.20 26.5
22.6 2.37 9.56 <0.001 69.5 17.98 27.2
19.1 1.34 14.31 <0.001 151.8 16.53 21.8
17.6 3.00 5.85 <0.001 27.6 11.68 23.5
17.0 3.48 4.89 <0.001 19.9 10.21 23.9
18.9 1.94 9.74 <0.001 72.1 15.11 22.7
18.7 1.57 11.90 <0.001 106.0 15.61 21.8
18.4 1.83 10.07 <0.001 76.8 14.85 22.0
15.0 1.20 12.53 <0.001 117.2 12.63 17.3
13.4 3.36 3.99 <0.001 13.9 6.81 20.0
12.9 3.00 4.28 <0.001 15.7 6.98 18.8
Results averaged over levels of: am
Columns: cyl, carb, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# collapsed cross
by <- expand.grid(
cyl = unique(mtcars$cyl),
carb = unique(mtcars$carb))
by$by <- ifelse(
by$cyl == 4,
paste("Control:", by$carb),
paste("Treatment:", by$carb))
# Convert numeric variables to categorical before fitting the model
dat <- mtcars
dat$am <- as.logical(dat$am)
dat$carb <- as.factor(dat$carb)
mod <- lm(mpg ~ hp + am + carb, data = dat)
# Compute and summarize marginal means
marginal_means(mod)
```

```
Term Value Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
am FALSE 17.9 1.244 14.37 <0.001 153.0 15.4 20.3
am TRUE 23.1 0.974 23.72 <0.001 410.9 21.2 25.0
carb 1 22.0 1.345 16.35 <0.001 197.2 19.4 24.6
carb 2 21.5 1.025 20.95 <0.001 321.5 19.5 23.5
carb 3 20.6 1.780 11.55 <0.001 100.1 17.1 24.0
carb 4 18.8 1.042 18.06 <0.001 239.9 16.8 20.9
carb 6 18.5 3.019 6.12 <0.001 30.0 12.6 24.4
carb 8 21.6 4.055 5.33 <0.001 23.3 13.7 29.6
Results averaged over levels of: hp, am, carb
Columns: term, value, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# Contrast between marginal means (carb2 - carb1), or "is the 1st marginal means equal to the 2nd?"
# see the vignette on "Hypothesis Tests and Custom Contrasts" on the `marginaleffects` website.
lc <- c(-1, 1, 0, 0, 0, 0)
marginal_means(mod, variables = "carb", hypothesis = "b2 = b1")
```

```
Term Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
b2=b1 -0.514 1.48 -0.348 0.728 0.5 -3.41 2.38
Results averaged over levels of: am, carb
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

`marginal_means(mod, variables = "carb", hypothesis = lc)`

```
Term Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
custom -0.514 1.48 -0.348 0.728 0.5 -3.41 2.38
Results averaged over levels of: am, carb
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```

```
# Multiple custom contrasts
lc <- matrix(c(
-2, 1, 1, 0, -1, 1,
-1, 1, 0, 0, 0, 0
),
ncol = 2,
dimnames = list(NULL, c("A", "B")))
marginal_means(mod, variables = "carb", hypothesis = lc)
```

```
Term Mean Std. Error z Pr(>|z|) S 2.5 % 97.5 %
A 1.199 6.15 0.195 0.845 0.2 -10.85 13.25
B -0.514 1.48 -0.348 0.728 0.5 -3.41 2.38
Results averaged over levels of: am, carb
Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
Type: response
```