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..3de091d5d 100644 --- a/R/table_footer.R +++ b/R/table_footer.R @@ -179,16 +179,33 @@ # Random effects handling ------------------------------------------------ # -------------------------------------------------------------------------- - if (!is.null(re_intercept) && !isTRUE(re_form_na) && !is.null(marginalization)) { + # 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) && + !all(re_intercept %in% cleaned_by) + ) { 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 +213,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." + ) +})