From 2674ea87716a3794948d55d0c0e3240f235c10bf Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 22 Nov 2026 10:28:50 +0100 Subject: [PATCH 1/5] Handle `re.form=NA` in table footer --- R/get_marginalmeans.R | 17 +++++---- R/table_footer.R | 57 ++++++++++++++---------------- tests/testthat/test-table_footer.R | 40 ++++++++++++++++++++- 3 files changed, 77 insertions(+), 37 deletions(-) diff --git a/R/get_marginalmeans.R b/R/get_marginalmeans.R index 8ee4ab9ea..2860251ea 100644 --- a/R/get_marginalmeans.R +++ b/R/get_marginalmeans.R @@ -341,7 +341,10 @@ get_marginalmeans <- function( ), # needed for some additional information for mixed models model = model, - re_form_na = !is.null(dots$re.form) && is.na(dots$re.form) + re_form_na = !is.null(dots$re.form) && + !inherits(dots$re.form, "formula") && + is.na(dots$re.form), + re_form_formula = !is.null(dots$re.form) && inherits(dots$re.form, "formula") ) class(means) <- unique(c("marginaleffects_means", class(means))) @@ -578,7 +581,8 @@ get_marginalmeans <- function( model_info = NULL, info = NULL, model = NULL, - re_form_na = NULL + re_form_na = NULL, + re_form_formula = NULL ) { attr(x, "at") <- by attr(x, "by") <- by @@ -606,11 +610,11 @@ get_marginalmeans <- function( # we definitely don't want the random-intercept in the "adjusted for" list info$adjusted_for <- setdiff(info$adjusted_for, re_intercept) # save information about random effects (cluster/grouping variable) - if (!isTRUE(re_form_na)) { - info$re_intercept <- re_intercept - } + info$re_intercept <- re_intercept # save info if re.form was NA info$re_form_na <- isTRUE(re_form_na) + # save info if re.form was a formula + info$re_form_formula <- isTRUE(re_form_formula) } if (!is.null(info) && length(info)) { @@ -636,7 +640,8 @@ get_marginalmeans <- function( "contrast", "estimate", "p_adjust", "transform", "transform_arg", "datagrid", "preserve_range", "coef_name", "slope", "ci", "model_info", "contrast_filter", "null", "iterations", "keep_iterations", "joint_test", "omnibus_test", "vcov", - "equivalence", "context_effects", "renamed_focal", "re_intercept", "re_form_na" + "equivalence", "context_effects", "renamed_focal", "re_intercept", "re_form_na", + "re_form_formula" ) } diff --git a/R/table_footer.R b/R/table_footer.R index 3de091d5d..3f1cb56da 100644 --- a/R/table_footer.R +++ b/R/table_footer.R @@ -16,6 +16,7 @@ adjusted_for <- info$adjusted_for re_intercept <- info$re_intercept re_form_na <- info$re_form_na + re_form_formula <- info$re_form_formula transform <- info$transform model_info <- info$model_info marginalization <- info$estimate @@ -179,46 +180,42 @@ # Random effects handling ------------------------------------------------ # -------------------------------------------------------------------------- - # 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 { - not_all_clusters <- !identical(setdiff(re_intercept, cleaned_by), re_intercept) - } - if ( - !is.null(re_intercept) && - !isTRUE(re_form_na) && - !is.null(marginalization) && + !isTRUE(re_form_formula) && + !is.null(re_intercept) && !all(re_intercept %in% cleaned_by) ) { - if (sample_average) { - table_footer <- paste0( - table_footer, - "\nRandom effects: Marginalized across ", - ifelse(not_all_clusters, "remaining ", ""), - "clusters." - ) - } else { + # check if we have random effects that are not yet in "by", i.e. if we + # marginalize across *all* cluster or not + not_all_clusters <- !identical(setdiff(re_intercept, cleaned_by), re_intercept) + + if (!isTRUE(re_form_na) && !is.null(marginalization)) { + if (sample_average) { + table_footer <- paste0( + table_footer, + "\nRandom effects: Marginalized across ", + ifelse(not_all_clusters, "remaining ", ""), + "clusters." + ) + } else { + table_footer <- paste0( + table_footer, + "\nRandom effects: Averaged over a balanced grid of ", + ifelse(not_all_clusters, "remaining ", ""), + "cluster levels." + ) + } + } + if (isTRUE(re_form_na)) { table_footer <- paste0( table_footer, - "\nRandom effects: Averaged over a balanced grid of ", + "\nRandom effects: Conditional on a typical ", ifelse(not_all_clusters, "remaining ", ""), - "cluster levels." + "cluster." ) } } - if (isTRUE(re_form_na)) { - table_footer <- paste0( - table_footer, - "\nRandom effects: Conditional on a typical ", - ifelse(not_all_clusters, "remaining ", ""), - "cluster." - ) - } - # P-value adjustment footer ------------------------------------------------ # -------------------------------------------------------------------------- diff --git a/tests/testthat/test-table_footer.R b/tests/testthat/test-table_footer.R index 415050675..7c64e133c 100644 --- a/tests/testthat/test-table_footer.R +++ b/tests/testthat/test-table_footer.R @@ -144,7 +144,7 @@ test_that("table_footer, no prediction intervals if insight falls back", { }) -test_that("table_footer, report random effects wheb actually occurs", { +test_that("table_footer, report random effects when actually occurs", { skip_if_not_installed("lme4") skip_if(getRversion() < "4.5.0") @@ -180,3 +180,41 @@ test_that("table_footer, report random effects wheb actually occurs", { "Random effects: Marginalized across clusters." ) }) + + +test_that("table_footer, report random effects when actually occurs, with re.form=NA", { + 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", re.form = NA) + expect_identical( + capture.output(out)[11], + "Predictors averaged within levels of species: sex, body_mass" + ) + + out <- estimate_means(model, "sex", estimate = "average", re.form = NA) + expect_identical( + capture.output(out)[11], + "Random effects: Conditional on a typical cluster." + ) + + model <- lme4::lmer( + bill_len ~ sex + body_mass + (1 | species) + (1 | island), + data = penguins + ) + + out <- estimate_means(model, "species", estimate = "average", re.form = NA) + expect_identical( + capture.output(out)[12], + "Random effects: Conditional on a typical remaining cluster." + ) + + out <- estimate_means(model, "sex", estimate = "average", re.form = NA) + expect_identical( + capture.output(out)[11], + "Random effects: Conditional on a typical cluster." + ) +}) From 269e72e8f7b5abd8a297d7289525274c64efbde6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 22 Nov 2026 10:30:51 +0100 Subject: [PATCH 2/5] add test --- tests/testthat/test-table_footer.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/testthat/test-table_footer.R b/tests/testthat/test-table_footer.R index 7c64e133c..385b58619 100644 --- a/tests/testthat/test-table_footer.R +++ b/tests/testthat/test-table_footer.R @@ -218,3 +218,21 @@ test_that("table_footer, report random effects when actually occurs, with re.for "Random effects: Conditional on a typical cluster." ) }) + + +test_that("table_footer, don't random effects when formula", { + 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", re.form = ~ (1 | species)) + expect_identical( + capture.output(out)[11], + "Predictors averaged within levels of species: sex, body_mass" + ) + + out <- estimate_means(model, "sex", estimate = "average", re.form = ~ (1 | species)) + expect_true(is.na(capture.output(out)[11])) +}) From 152b71669c443aad9812f814f63ae11abdc4fbbb Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 22 Nov 2026 10:48:28 +0100 Subject: [PATCH 3/5] address comment --- R/get_marginalcontrasts.R | 2 +- R/get_marginalmeans.R | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/get_marginalcontrasts.R b/R/get_marginalcontrasts.R index 920eadcc3..772056c42 100644 --- a/R/get_marginalcontrasts.R +++ b/R/get_marginalcontrasts.R @@ -255,7 +255,7 @@ get_marginalcontrasts <- function( ), # needed for some additional information for mixed models model = model, - re_form_na = !is.null(dots$re.form) && is.na(dots$re.form) + dots = dots ) out <- .rename_focal_attributes(out) diff --git a/R/get_marginalmeans.R b/R/get_marginalmeans.R index 2860251ea..859f1bf37 100644 --- a/R/get_marginalmeans.R +++ b/R/get_marginalmeans.R @@ -341,10 +341,7 @@ get_marginalmeans <- function( ), # needed for some additional information for mixed models model = model, - re_form_na = !is.null(dots$re.form) && - !inherits(dots$re.form, "formula") && - is.na(dots$re.form), - re_form_formula = !is.null(dots$re.form) && inherits(dots$re.form, "formula") + dots = dots ) class(means) <- unique(c("marginaleffects_means", class(means))) @@ -581,8 +578,7 @@ get_marginalmeans <- function( model_info = NULL, info = NULL, model = NULL, - re_form_na = NULL, - re_form_formula = NULL + dots = NULL ) { attr(x, "at") <- by attr(x, "by") <- by @@ -592,8 +588,12 @@ get_marginalmeans <- function( if (!is.null(model)) { response <- insight::find_response(model, combine = FALSE) re_intercept <- insight::find_random(model, flatten = TRUE, split_nested = TRUE) + re_form_na <- !is.null(dots$re.form) && + !inherits(dots$re.form, "formula") && + isTRUE(is.na(dots$re.form)) + re_form_formula <- !is.null(dots$re.form) && inherits(dots$re.form, "formula") } else { - response <- re_form_na <- re_intercept <- NULL + response <- re_form_na <- re_form_formula <- re_intercept <- NULL } # compact list From be21dddd5aa93aa2e8df40036291564829b883f7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 22 Nov 2026 10:49:46 +0100 Subject: [PATCH 4/5] version bump --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index e7ab2f48b..f7ca11de2 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.13 +Version: 0.18.0.14 Authors@R: c(person(given = "Dominique", family = "Makowski", From 1ba2001dd2af63b2ceb8704de9773b4cc3973aea Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 22 Nov 2026 11:35:17 +0100 Subject: [PATCH 5/5] skip tests on CI --- tests/testthat/test-brms.R | 2 +- tests/testthat/test-estimate_contrasts.R | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-brms.R b/tests/testthat/test-brms.R index e59441024..7cdee3578 100644 --- a/tests/testthat/test-brms.R +++ b/tests/testthat/test-brms.R @@ -1,5 +1,5 @@ skip_on_cran() -# skip_on_os("windows") +skip_on_ci() skip_if_not_installed("brms") skip_if_not_installed("BH") skip_if_not_installed("RcppEigen") diff --git a/tests/testthat/test-estimate_contrasts.R b/tests/testthat/test-estimate_contrasts.R index eefd326be..24360bf29 100644 --- a/tests/testthat/test-estimate_contrasts.R +++ b/tests/testthat/test-estimate_contrasts.R @@ -233,6 +233,7 @@ test_that("estimate_contrasts - Frequentist, Three factors 1", { ), tolerance = 1e-4 ) + skip_on_ci() expect_snapshot(print( estimate_contrasts( model,