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.13
Version: 0.18.0.14
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
2 changes: 1 addition & 1 deletion R/get_marginalcontrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 12 additions & 7 deletions R/get_marginalmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)) {
Expand All @@ -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"
)
}

Expand Down
57 changes: 27 additions & 30 deletions R/table_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) &&
Comment thread
strengejacke marked this conversation as resolved.
Resolved
!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 ------------------------------------------------
# --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ test_that("estimate_contrasts - Frequentist, Three factors 1", {
),
tolerance = 1e-4
)
skip_on_ci()
expect_snapshot(print(
estimate_contrasts(
model,
Expand Down
58 changes: 57 additions & 1 deletion tests/testthat/test-table_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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]))
})
Loading