Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 23 additions & 4 deletions R/table_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,43 @@
# 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."
)
}
}

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."
)
}

Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-table_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
})
Loading