From ed5defe4b21d59221cfc1a6346befe3078293e6d Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 21 Nov 2026 17:37:31 +0100 Subject: [PATCH 1/3] Make sure we have correct scale in footer for mixed models (GLMM) --- R/get_marginalmeans.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_marginalmeans.R b/R/get_marginalmeans.R index 79d805b3b..8ee4ab9ea 100644 --- a/R/get_marginalmeans.R +++ b/R/get_marginalmeans.R @@ -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)" } # ========================================================================= From e5484be6e0eef1a542afe838cbb4000fd07b6f55 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 21 Nov 2026 17:49:47 +0100 Subject: [PATCH 2/3] update snaps --- tests/testthat/_snaps/estimate_means_mixed.md | 2 +- .../_snaps/windows/estimate_means_counterfactuals.md | 4 ++-- vignettes/mixed_models.Rmd | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/testthat/_snaps/estimate_means_mixed.md b/tests/testthat/_snaps/estimate_means_mixed.md index 9ccd7cf80..c029d17cf 100644 --- a/tests/testthat/_snaps/estimate_means_mixed.md +++ b/tests/testthat/_snaps/estimate_means_mixed.md @@ -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. --- diff --git a/tests/testthat/_snaps/windows/estimate_means_counterfactuals.md b/tests/testthat/_snaps/windows/estimate_means_counterfactuals.md index b0fe89f1b..632d0008c 100644 --- a/tests/testthat/_snaps/windows/estimate_means_counterfactuals.md +++ b/tests/testthat/_snaps/windows/estimate_means_counterfactuals.md @@ -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. --- @@ -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. --- diff --git a/vignettes/mixed_models.Rmd b/vignettes/mixed_models.Rmd index a29d32496..c2be4823a 100644 --- a/vignettes/mixed_models.Rmd +++ b/vignettes/mixed_models.Rmd @@ -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]. @@ -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 @@ -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. From 37a13c0499a71758b751f4774442905c9986a63d Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 21 Nov 2026 18:08:16 +0100 Subject: [PATCH 3/3] capture warning --- tests/testthat/test-estimate_contrasts.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-estimate_contrasts.R b/tests/testthat/test-estimate_contrasts.R index 75f02991c..eefd326be 100644 --- a/tests/testthat/test-estimate_contrasts.R +++ b/tests/testthat/test-estimate_contrasts.R @@ -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) })