This vignette shows how to calculate marginal effects that take the random-effect variances for mixed models into account.

Basically, the type of predictions, i.e. whether to account for the uncertainty of random effects or not, can be set with the `type`

-argument. The default, `type = "fe"`

, means that predictions are on the population-level and do not account for the random effect variances. Intervals are *confidence intervals* for the predicted values.

```
library(ggeffects)
library(lme4)
data(sleepstudy)
m <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)
pr <- ggpredict(m, "Days")
pr
#>
#> # Predicted values of Reaction
#> # x = Days
#>
#> x | Predicted | SE | 95% CI
#> ----------------------------------------
#> 0 | 251.41 | 6.82 | [238.03, 264.78]
#> 1 | 261.87 | 6.79 | [248.57, 275.17]
#> 2 | 272.34 | 7.09 | [258.44, 286.24]
#> 3 | 282.81 | 7.71 | [267.71, 297.91]
#> 5 | 303.74 | 9.58 | [284.96, 322.52]
#> 6 | 314.21 | 10.73 | [293.17, 335.25]
#> 7 | 324.68 | 11.97 | [301.21, 348.14]
#> 9 | 345.61 | 14.63 | [316.94, 374.29]
#>
#> Adjusted for:
#> * Subject = 0 (population-level)
plot(pr)
```

When `type = "re"`

, the predicted values *are still on the population-level*. However, the random effect variances are taken into account, meaning that the intervals are actually *prediction intervals* and become larger. More technically speaking, `type = "re"`

accounts for the uncertainty of the fixed effects *conditional on the estimates* of the random-effect variances and conditional modes (BLUPs).

The random-effect variance is the *mean* random-effect variance. Calculation is based on the proposal from *Johnson et al. 2014*, which is also implemented in functions like `performance::r2()`

or `insight::get_variance()`

to get r-squared values or random effect variances for mixed models with more complex random effects structures.

As can be seen, compared to the previous example with `type = "fe"`

, predicted values are identical (both on the population-level). However, standard errors, and thus the resulting confidence (or prediction) intervals are much larger .

```
pr <- ggpredict(m, "Days", type = "re")
pr
#>
#> # Predicted values of Reaction
#> # x = Days
#>
#> x | Predicted | SE | 95% CI
#> ----------------------------------------
#> 0 | 251.41 | 41.77 | [169.54, 333.27]
#> 1 | 261.87 | 41.76 | [180.02, 343.73]
#> 2 | 272.34 | 41.82 | [190.38, 354.30]
#> 3 | 282.81 | 41.92 | [200.64, 364.98]
#> 5 | 303.74 | 42.31 | [220.82, 386.67]
#> 6 | 314.21 | 42.58 | [230.74, 397.67]
#> 7 | 324.68 | 42.91 | [240.57, 408.79]
#> 9 | 345.61 | 43.73 | [259.90, 431.32]
#>
#> Adjusted for:
#> * Subject = 0 (population-level)
plot(pr)
```

The reason why both `type = "fe"`

and `type = "re"`

return predictions at population-level is because `ggpredict()`

returns predicted values of the response *at specific levels* of given model predictors, which are defined in the data frame that is passed to the `newdata`

-argument (of `predict()`

). The data frame requires data from *all* model terms, including random effect terms. This again requires to choose certain levels or values also for each random effect term, or to set those terms to zero or `NA`

(for population-level). Since there is no general rule, which level(s) of random effect terms to choose in order to represent the random effects structure in the data, using the population-level seems the most clear and consistent approach.

To get predicted values for a specific level of the random effect term, simply define this level in the `condition`

-argument.

```
ggpredict(m, "Days", type = "re", condition = c(Subject = 330))
#>
#> # Predicted values of Reaction
#> # x = Days
#>
#> x | Predicted | SE | 95% CI
#> ----------------------------------------
#> 0 | 275.09 | 41.77 | [193.22, 356.96]
#> 1 | 280.75 | 41.76 | [198.89, 362.60]
#> 2 | 286.40 | 41.82 | [204.44, 368.36]
#> 3 | 292.05 | 41.92 | [209.88, 374.22]
#> 5 | 303.36 | 42.31 | [220.44, 386.28]
#> 6 | 309.01 | 42.58 | [225.55, 392.48]
#> 7 | 314.67 | 42.91 | [230.56, 398.78]
#> 9 | 325.97 | 43.73 | [240.26, 411.68]
```

Finally, it is possible to obtain predicted values by simulating from the model, where predictions are based on `simulate()`

.

```
ggpredict(m, "Days", type = "sim")
#>
#> # Predicted values of Reaction
#> # x = Days
#>
#> x | Predicted | SE | 95% CI
#> ----------------------------------------
#> 0 | 251.25 | 25.53 | [201.67, 301.54]
#> 1 | 261.88 | 25.60 | [211.76, 311.88]
#> 2 | 272.32 | 25.61 | [222.48, 322.22]
#> 3 | 282.54 | 25.62 | [233.18, 332.34]
#> 5 | 303.71 | 25.63 | [253.72, 353.82]
#> 6 | 313.91 | 25.71 | [263.89, 364.38]
#> 7 | 325.03 | 25.33 | [275.82, 374.66]
#> 9 | 345.54 | 25.59 | [295.35, 394.97]
#>
#> Adjusted for:
#> * Subject = 0 (population-level)
```

For zero-inflated mixed effects models, typically fitted with the **glmmTMB** or **GLMMadaptive** packages, predicted values can be conditioned on

- the fixed effects of the conditional model only (
`type = "fe"`

) - the fixed effects and zero-inflation component (
`type = "fe.zi"`

) - the fixed effects of the conditional model only (population-level), taking the random-effect variances into account (
`type = "re"`

) - the fixed effects and zero-inflation component (population-level), taking the random-effect variances into account (
`type = "re.zi"`

) - all model parameters (
`type = "sim"`

)

```
library(glmmTMB)
data(Salamanders)
m <- glmmTMB(
count ~ spp + mined + (1 | site),
ziformula = ~ spp + mined,
family = truncated_poisson,
data = Salamanders
)
```

Similar to mixed models without zero-inflation component, `type = "fe"`

and `type = "re"`

for **glmmTMB**-models (with zero-inflation) both return predictions on the population-level, where the latter option accounts for the uncertainty of the random effects. In short, `predict(..., type = "link")`

is called (however, predicted values are back-transformed to the response scale).

```
ggpredict(m, "spp")
#>
#> # Predicted counts of count
#> # x = spp
#>
#> x | Predicted | SE | 95% CI
#> ---------------------------------------
#> GP | 0.94 | 0.21 | [0.62, 1.40]
#> PR | 0.56 | 0.31 | [0.30, 1.02]
#> DM | 1.17 | 0.19 | [0.80, 1.70]
#> EC-A | 0.77 | 0.24 | [0.48, 1.23]
#> EC-L | 1.79 | 0.18 | [1.25, 2.55]
#> DES-L | 1.71 | 0.18 | [1.20, 2.44]
#> DF | 0.98 | 0.20 | [0.67, 1.44]
#>
#> Adjusted for:
#> * mined = yes
#> * site = NA (population-level)
ggpredict(m, "spp", type = "re")
#>
#> # Predicted counts of count
#> # x = spp
#>
#> x | Predicted | SE | 95% CI
#> ---------------------------------------
#> GP | 0.94 | 0.31 | [0.51, 1.71]
#> PR | 0.56 | 0.38 | [0.26, 1.18]
#> DM | 1.17 | 0.30 | [0.65, 2.11]
#> EC-A | 0.77 | 0.33 | [0.40, 1.48]
#> EC-L | 1.79 | 0.29 | [1.00, 3.18]
#> DES-L | 1.71 | 0.29 | [0.96, 3.04]
#> DF | 0.98 | 0.30 | [0.54, 1.77]
#>
#> Adjusted for:
#> * mined = yes
#> * site = NA (population-level)
```

For `type = "fe.zi"`

, the predicted response value is the expected value `mu*(1-p)`

*without conditioning* on random effects. Since the zero inflation and the conditional model are working in “opposite directions”, a higher expected value for the zero inflation means a lower response, but a higher value for the conditional model means a higher response. While it is possible to calculate predicted values with `predict(..., type = "response")`

, standard errors and confidence intervals can not be derived directly from the `predict()`

-function. Thus, confidence intervals for `type = "fe.zi"`

are based on quantiles of simulated draws from a multivariate normal distribution (see also *Brooks et al. 2017, pp.391-392* for details).

```
ggpredict(m, "spp", type = "fe.zi")
#>
#> # Predicted counts of count
#> # x = spp
#>
#> x | Predicted | SE | 95% CI
#> ---------------------------------------
#> GP | 0.14 | 0.05 | [0.04, 0.23]
#> PR | 0.02 | 0.01 | [0.00, 0.03]
#> DM | 0.25 | 0.07 | [0.10, 0.39]
#> EC-A | 0.04 | 0.02 | [0.01, 0.08]
#> EC-L | 0.37 | 0.11 | [0.16, 0.59]
#> DES-L | 0.43 | 0.12 | [0.20, 0.66]
#> DF | 0.21 | 0.06 | [0.08, 0.33]
#>
#> Adjusted for:
#> * mined = yes
#> * site = NA (population-level)
```

For `type = "re.zi"`

, the predicted response value is the expected value `mu*(1-p)`

, accounting for the random-effect variances. Intervals are calculated in the same way as for `type = "fe.zi"`

, except that the mean random effect variance is considered and thus *prediction intervals* rather than confidence intervals are returned.

```
ggpredict(m, "spp", type = "re.zi")
#>
#> # Predicted counts of count
#> # x = spp
#>
#> x | Predicted | SE | 95% CI
#> ---------------------------------------
#> GP | 0.14 | 0.24 | [0.03, 0.37]
#> PR | 0.02 | 0.23 | [0.00, 0.05]
#> DM | 0.25 | 0.24 | [0.06, 0.63]
#> EC-A | 0.04 | 0.23 | [0.00, 0.12]
#> EC-L | 0.37 | 0.26 | [0.10, 0.94]
#> DES-L | 0.43 | 0.26 | [0.13, 1.05]
#> DF | 0.21 | 0.24 | [0.05, 0.52]
#>
#> Adjusted for:
#> * mined = yes
#> * site = NA (population-level)
```

Finally, it is possible to obtain predicted values by simulating from the model, where predictions are based on `simulate()`

(see *Brooks et al. 2017, pp.392-393* for details). To achieve this, use `type = "sim"`

.

```
ggpredict(m, "spp", type = "sim")
#>
#> # Predicted counts of count
#> # x = spp
#>
#> x | Predicted | SE | 95% CI
#> ---------------------------------------
#> GP | 1.08 | 1.28 | [0.00, 4.13]
#> PR | 0.29 | 0.66 | [0.00, 2.25]
#> DM | 1.52 | 1.54 | [0.00, 5.25]
#> EC-A | 0.54 | 0.94 | [0.00, 3.06]
#> EC-L | 2.19 | 2.11 | [0.00, 7.08]
#> DES-L | 2.26 | 2.06 | [0.00, 7.10]
#> DF | 1.31 | 1.37 | [0.00, 4.58]
#>
#> Adjusted for:
#> * mined = yes
#> * site = NA (population-level)
```

Marginal effects can also be calculated for each group level in mixed models. Simply add the name of the related random effects term to the `terms`

-argument, and set `type = "re"`

.

In the following example, we fit a linear mixed model and first simply plot the marginal effects, *not* conditioned on random-effect variances.

```
library(sjlabelled)
data(efc)
efc$e15relat <- as_label(efc$e15relat)
m <- lmer(neg_c_7 ~ c12hour + c160age + c161sex + (1 | e15relat), data = efc)
me <- ggpredict(m, terms = "c12hour")
plot(me)
```

Changing the type to `type = "re"`

still returns population-level predictions by default. Recall that the major difference between `type = "fe"`

and `type = "re"`

is the uncertainty in the variance parameters. This leads to larger confidence intervals (i.e. prediction intervals) for marginal effects with `type = "re"`

.

To compute marginal effects for each grouping level, add the related random term to the `terms`

-argument. In this case, confidence intervals are not calculated, but marginal effects are conditioned on each group level of the random effects.

Marginal effects, conditioned on random effects, can also be calculated for specific levels only. Add the related values into brackets after the variable name in the `terms`

-argument.

The most complex plot in this scenario would be a term (`c12hour`

) at certain values of two other terms (`c161sex`

, `c160age`

) for specific levels of random effects (`e15relat`

), so we have four variables in the `terms`

-argument.

```
me <- ggpredict(
m,
terms = c("c12hour", "c161sex", "c160age", "e15relat [child,sibling]"),
type = "re"
)
plot(me)
```

If the group factor has too many levels, you can also take a random sample of all possible levels and plot the marginal effects for this subsample of group levels. To do this, use `term = "<groupfactor> [sample=n]"`

.

```
set.seed(123)
m <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)
me <- ggpredict(m, terms = c("Days", "Subject [sample=7]"), type = "re")
plot(me)
```

You can also add the observed data points for each group using `add.data = TRUE`

.

Brooks ME, Kristensen K, Benthem KJ van, Magnusson A, Berg CW, Nielsen A, et al. glmmTMB Balances Speed and Flexibility Among Packages for Zero-inflated Generalized Linear Mixed Modeling. The R Journal. 2017;9: 378–400.

Johnson PC, O’Hara RB. 2014. Extension of Nakagawa & Schielzeth’s R2GLMM to random slopes models. Methods Ecol Evol, 5: 944-946. (doi: 10.1111/2041-210X.12225)