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", 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 8ee4ab9ea..859f1bf37 100644 --- a/R/get_marginalmeans.R +++ b/R/get_marginalmeans.R @@ -341,7 +341,7 @@ 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) + dots = dots ) class(means) <- unique(c("marginaleffects_means", class(means))) @@ -578,7 +578,7 @@ get_marginalmeans <- function( model_info = NULL, info = NULL, model = NULL, - re_form_na = NULL + dots = NULL ) { attr(x, "at") <- by attr(x, "by") <- by @@ -588,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 @@ -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-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, diff --git a/tests/testthat/test-table_footer.R b/tests/testthat/test-table_footer.R index 415050675..385b58619 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,59 @@ 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." + ) +}) + + +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])) +})