Skip to content
Merged
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
2 changes: 1 addition & 1 deletion R/get_marginalmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ get_marginalmeans <- function(
if (predict_args$backtransform) {
means <- .backtransform_predictions(means, model, predict_args, ci, df = dots$df)
# make sure we have the original string value for the "predict" argument
predict_args$predict <- "response"
predict_args$predict <- "invlink(link)"
}

# =========================================================================
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/estimate_means_mixed.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Variable predicted: count
Predictors modulated: mined, spp
Random effects: Averaged over a balanced grid of cluster levels.
Predictions are on the response-scale.
Predictions are on the inverse-link-scale.

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
Predictors modulated: spp
Predictors averaged over a balanced grid: mined
Random effects: Averaged over a balanced grid of cluster levels.
Predictions are on the response-scale.
Predictions are on the inverse-link-scale.

---

Expand All @@ -92,7 +92,7 @@
Predictors modulated: spp
Predictors averaged over the full sample: mined
Random effects: Marginalized across clusters.
Predictions are on the response-scale.
Predictions are on the inverse-link-scale.

---

Expand Down
8 changes: 7 additions & 1 deletion tests/testthat/test-estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,13 @@ test_that("estimate_contrast, slopes with different estimate options", {
out <- estimate_contrasts(m, "bill_dep", by = "island")
expect_equal(out$Difference, c(0.08507, -0.00071, -0.08578), tolerance = 1e-4)

out <- estimate_contrasts(m, "bill_dep", by = "island", estimate = "average")
expect_warning(
{
out <- estimate_contrasts(m, "bill_dep", by = "island", estimate = "average")
},
regex = "`bill_dep` is a numeric",
fixed = TRUE
)
expect_equal(out$Difference, c(-0.05295, -0.07655, -0.0236), tolerance = 1e-4)
})

Expand Down
12 changes: 6 additions & 6 deletions vignettes/mixed_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if (can_evaluate) {

## Estimated Marginal Means in Mixed Effects Models: Navigating Conditional and Marginal Effects

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. However, calculating EMMs in mixed models is not always straightforward, and the results can vary depending on the approach taken.
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].

Expand Down Expand Up @@ -91,7 +91,7 @@ data(sleepstudy, package = "lme4")
model <- lme4::lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)

# `estimate = "typical"`, `re.form = NULL`: a "typical" observation
# across clusters on average
# across (balanced) clusters on average
estimate_means(model, "Days")

# `estimate = "typical"`, `re.form = NA`: a "typical" observation
Expand Down Expand Up @@ -168,20 +168,20 @@ model <- lme4::glmer(

# conditional predictions marginalized over
# a "balanced" grid of random effects
estimate_means(model, "camper")
estimate_means(model, "camper", verbose = FALSE)

# conditional predictions for a "typical" cluster
estimate_means(model, "camper", re.form = NA)
estimate_means(model, "camper", re.form = NA, verbose = FALSE)
```

When using `estimate = "average"` to average across all observations and clusters, the differences between marginal and conditional predictions (regarding random effects) persist, with marginal predictions showing higher means (1.52 and 4.54) than conditional predictions (1.20 and 3.19), but both results differ from the predictions for "typical" observations based on the default `estimate` option, as shown in the previous output.

```{r}
# (fully) marginal predictions
estimate_means(model, "camper", estimate = "average")
estimate_means(model, "camper", estimate = "average", verbose = FALSE)

# marginal predictions, conditioning on a "typical" cluster
estimate_means(model, "camper", estimate = "average", re.form = NA)
estimate_means(model, "camper", estimate = "average", re.form = NA, verbose = FALSE)
```

This highlights that in GLMMs, the choice between marginal and conditional predictions significantly impacts the estimated means and, consequently, the interpretation of the model's results.
Expand Down
Loading