Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions vignettes/mixed_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,17 @@ if (can_evaluate) {

Mixed models, with their ability to account for hierarchical or clustered data, offer powerful tools for understanding complex relationships. A key aspect of interpreting these models is understanding **estimated marginal means (EMMs)**, which represent the predicted outcome for specific groups or conditions, while holding other variables constant, or averaging over them. However, calculating EMMs in mixed models is not always straightforward, and the results can vary depending on the approach taken.

One crucial distinction is between **conditional** and **marginal** predictions (or effects). Conditional predictions are specific to a particular level of the random effect (e.g., the predicted outcome for a specific individual in a study). Marginal predictions, on the other hand, average over the random effects, providing an overall estimate of the effect in the population. This is a crucial difference, as the marginal effect is often the quantity of interest when we want to generalize to the population [@heiss_marginal_2022].
In mixed models, two choices determine an estimated mean. The first is which random effects enter each prediction. The second is which predictions to average [@heiss_marginal_2022].

::: custom_note
<p>Based on the definitions from Heiss (2022), we can say that the conditional vs. marginal distinction applies to any sort of hierarchical structure (<em>clusters</em>) in multilevel models:</p>
With `backend = "marginaleffects"`, `estimate_means()` includes the fitted random effects for each group represented in the prediction data by default (`re.form = NULL`). Use `re.form = NA` to set the random effects to zero for each prediction. The [glmmTMB documentation](https://glmmtmb.github.io/glmmTMB/reference/predict.glmmTMB.html) calls predictions with zero random effects *population-level* predictions.

- **Conditional effect** = the effect of a variable in an *average cluster* (i.e., group-specific, subject-specific or cluster-specific effect, or an average or a typical cluster, `re.form = NA`)
- **Marginal effect** = effect of a variable *across clusters on average* (i.e., global/population-level effect, or clusters on average, `re.form = NULL`)

For `backend = "marginaleffects"`, the `re.form` argument is set to `NULL` for mixed models by default, to calculate _marginal predictions_. You can use for instance `re.form = NA` in your `estimate_means()` call to change the default value (`NA` will produce _conditional predictions_).

As outlined in the [vignette on marginalization methods](https://easystats.github.io/modelbased/articles/technical_marginalization.html), marginalization in mixed models involves two distinct dimensions: marginalizing across *random effects (clusters)* and marginalizing across *covariates (observations)*.

We can produce *marginal predictions* regarding the higher-level units (random effects) *conditioned on* a "typical" observation based on a balanced data grid, or generate predictions that are *fully marginalized* across both clusters and observations.
The `estimate` argument determines the prediction data. The default, `estimate = "typical"`, uses a balanced grid. The `"average"` option averages predictions over the observed rows within each level of the focal predictor. The `"population"` option assigns each focal predictor level to every observed row and then averages the predictions for that level. With a nonlinear link, averaging response-scale predictions that include fitted random effects can yield a different mean from predicting with the random effects set to zero. See the [vignette on marginalization methods](https://easystats.github.io/modelbased/articles/technical_marginalization.html) for details on these grids.
:::

The default (`estimate = "typical"`) calculates predictions for a balanced data grid representing all combinations of focal predictor levels (specified in `by`), which is particularly useful for comparing groups. Setting `estimate = "average"` or `estimate = "population"` is useful when you want to calculate the average expected outcome across the actual observations from the sample at hand.
For **brms** models, the same choices are available through `re_formula`. The [`brms::conditional_effects()`](https://paulbuerkner.com/brms/reference/conditional_effects.brmsfit.html) function evaluates predictions at covariate values given by `conditions`. With `re_formula = NULL`, you can specify group levels in `conditions` to obtain predictions for those groups. To compare those predictions with an estimated mean, check which covariate values and group levels each function uses and how it averages predictions.

Note that for *linear* mixed models, `re.form = NA` will only have an effect when `estimate = "average"` or `estimate = "population"`.
:::
For a Bayesian estimated mean, the interval summarizes uncertainty about the average prediction. Averaging group-specific predictions within each posterior draw can produce a narrower interval than predicting with the random effects set to zero. To include variation across groups in a prediction for a new group, use `brms::posterior_epred()` with a new group level in `newdata`, `allow_new_levels = TRUE`, and `sample_new_levels = "uncertainty"`. See the [brms prediction documentation](https://paulbuerkner.com/brms/reference/prepare_predictions.html) for other ways to sample new group effects.

## Linear mixed models

Expand Down Expand Up @@ -106,7 +99,7 @@ estimate_means(model, "Days", estimate = "average")

This section explores the impact of imbalanced data on EMM calculations in linear mixed models. We'll use the `penguins` dataset, which has imbalanced groups that we use as higher-level unit, as well as imbalanced predictors.

Since `estimate = "typical"` creates a balanced data grid, *including* the levels of the random effects, we get a "typical" observation marginalized over a *balanced* grid of random effects levels. In this case, setting `re.form = NA` has no effect when we have *linear* mixed models.
Here, `estimate = "typical"` averages predictions over a balanced grid that includes each species. The estimated random effects average to zero on this grid, which gives the same means as setting `re.form = NA` for this model.

```{r}
data(penguins)
Expand Down Expand Up @@ -192,10 +185,10 @@ This vignette has demonstrated the nuances of calculating estimated marginal mea

Furthermore, the default approach of calculating predictions for a balanced data grid, while useful for comparing groups, does not necessarily reflect the actual distribution of observations in the sample. For imbalanced data, this can yield significantly different results even in linear mixed models in comparison to average predictions (using `estimate = "average"`).

**Therefore, we recommend combining marginal predictions (using the default `backend = "marginaleffects"`) with averaging across all observations (by setting `estimate = "average"` or `estimate = "population"`) when working with mixed models.** This approach effectively incorporates the variation inherent in the random effects (higher-level units) and provides EMMs that more accurately reflect the overall patterns observed within the actual sample data. By averaging over the observed data, we obtain a more robust and representative estimate of the population-level effects, making it a preferred strategy for interpreting mixed model results.
Use the default `backend = "marginaleffects"` with `estimate = "average"` or `estimate = "population"` when the target is an average over observed covariates and grouping levels. Choose `estimate = "average"` for predictions within the observed groups of the focal predictor. Choose `estimate = "population"` to average predictions after assigning each focal predictor level to all rows.

::: custom_note
One exception for using `estimate = "average"` can be when continuous focal predictors are involved, see the [vignette on marginalization methods](https://easystats.github.io/modelbased/articles/technical_marginalization.html). Especially for **visualizations**, *conditional* predictions or `estimate = "population"` typically yield smoother curves for continuous focal predictors, whereas *marginal* predictions with `estimate = "average"` may introduce noisy visual artifacts.
For plots over a continuous focal predictor, `estimate = "average"` can give an irregular curve because it averages a different set of observed rows at each predictor value. The `estimate = "typical"` option uses a balanced grid, and `estimate = "population"` reuses the same observed rows at each predictor value. Both options can yield smoother curves. See the [vignette on marginalization methods](https://easystats.github.io/modelbased/articles/technical_marginalization.html).
:::

```{r echo=FALSE}
Expand Down
Loading