From abe57d428e5bc8ca4d3df0914063d077df75bc7b Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 21 Nov 2026 18:58:48 +0100 Subject: [PATCH 1/3] Report random-effect averaging only when it actually occurs Fixes #711 --- DESCRIPTION | 2 +- R/table_footer.R | 22 +++++++++++++---- tests/testthat/test-table_footer.R | 38 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d6a0adda6..e7ab2f48b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: modelbased Title: Estimation of Model-Based Predictions, Contrasts and Means -Version: 0.18.0.12 +Version: 0.18.0.13 Authors@R: c(person(given = "Dominique", family = "Makowski", diff --git a/R/table_footer.R b/R/table_footer.R index d0769ec4d..1c650a2dc 100644 --- a/R/table_footer.R +++ b/R/table_footer.R @@ -179,16 +179,28 @@ # Random effects handling ------------------------------------------------ # -------------------------------------------------------------------------- - if (!is.null(re_intercept) && !isTRUE(re_form_na) && !is.null(marginalization)) { + if ( + !is.null(re_intercept) && + !isTRUE(re_form_na) && + !is.null(marginalization) && + !all(re_intercept %in% cleaned_by) + ) { + # check if we have random effects that are not yet in "by" + not_all_clusters <- !identical(setdiff(re_intercept, cleaned_by), re_intercept) + if (sample_average) { table_footer <- paste0( table_footer, - "\nRandom effects: Marginalized across clusters." + "\nRandom effects: Marginalized across ", + ifelse(not_all_clusters, "remaining ", ""), + "clusters." ) } else { table_footer <- paste0( table_footer, - "\nRandom effects: Averaged over a balanced grid of cluster levels." + "\nRandom effects: Averaged over a balanced grid of ", + ifelse(not_all_clusters, "remaining ", ""), + "cluster levels." ) } } @@ -196,7 +208,9 @@ if (isTRUE(re_form_na)) { table_footer <- paste0( table_footer, - "\nRandom effects: Conditional on a typical cluster." + "\nRandom effects: Conditional on a typical ", + ifelse(not_all_clusters, "remaining ", ""), + "cluster." ) } diff --git a/tests/testthat/test-table_footer.R b/tests/testthat/test-table_footer.R index 2ff1d34eb..415050675 100644 --- a/tests/testthat/test-table_footer.R +++ b/tests/testthat/test-table_footer.R @@ -142,3 +142,41 @@ test_that("table_footer, no prediction intervals if insight falls back", { ))) expect_false("Intervals are prediction intervals." %in% out) }) + + +test_that("table_footer, report random effects wheb actually occurs", { + skip_if_not_installed("lme4") + skip_if(getRversion() < "4.5.0") + + data(penguins) + + model <- lme4::lmer(bill_len ~ sex + body_mass + (1 | species), data = penguins) + out <- estimate_means(model, "species", estimate = "average") + expect_identical( + capture.output(out)[11], + "Predictors averaged within levels of species: sex, body_mass" + ) + + out <- estimate_means(model, "sex", estimate = "average") + expect_identical( + capture.output(out)[11], + "Random effects: Marginalized across clusters." + ) + + model <- lme4::lmer( + bill_len ~ sex + body_mass + (1 | species) + (1 | island), + data = penguins + ) + + out <- estimate_means(model, "species", estimate = "average") + expect_identical( + capture.output(out)[12], + "Random effects: Marginalized across remaining clusters." + ) + + out <- estimate_means(model, "sex", estimate = "average") + expect_identical( + capture.output(out)[11], + "Random effects: Marginalized across clusters." + ) +}) From 4a2a4b533c1970f1fab9a66ac1c612e1dca5eb58 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 21 Nov 2026 19:07:00 +0100 Subject: [PATCH 2/3] fix --- R/table_footer.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/R/table_footer.R b/R/table_footer.R index 1c650a2dc..f794d839c 100644 --- a/R/table_footer.R +++ b/R/table_footer.R @@ -179,15 +179,19 @@ # Random effects handling ------------------------------------------------ # -------------------------------------------------------------------------- + # check if we have random effects that are not yet in "by" + if (is.null(re_intercept)) { + not_all_clusters <- FALSE + } else { + not_all_clusters <- !identical(setdiff(re_intercept, cleaned_by), re_intercept) + } + if ( !is.null(re_intercept) && !isTRUE(re_form_na) && !is.null(marginalization) && !all(re_intercept %in% cleaned_by) ) { - # check if we have random effects that are not yet in "by" - not_all_clusters <- !identical(setdiff(re_intercept, cleaned_by), re_intercept) - if (sample_average) { table_footer <- paste0( table_footer, From 8b7f788f2c28876451640b4b0e1001d08d61c69f Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 21 Nov 2026 19:07:29 +0100 Subject: [PATCH 3/3] comment --- R/table_footer.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/table_footer.R b/R/table_footer.R index f794d839c..3de091d5d 100644 --- a/R/table_footer.R +++ b/R/table_footer.R @@ -179,7 +179,8 @@ # Random effects handling ------------------------------------------------ # -------------------------------------------------------------------------- - # check if we have random effects that are not yet in "by" + # check if we have random effects that are not yet in "by", i.e. if we + # marginalize across *all* cluster or not if (is.null(re_intercept)) { not_all_clusters <- FALSE } else {