Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper control-info for marginalize = "individual" #360

Merged
merged 7 commits into from
Jan 20, 2025
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 R/estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ estimate_contrasts <- function(model,

# Table formatting
attr(out, "table_title") <- c(ifelse(
marginalize == "individual",
marginalize == "specific",
"Model-based Contrasts Analysis",
"Marginal Contrasts Analysis"
), "blue")
Expand Down
5 changes: 3 additions & 2 deletions R/estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ estimate_means <- function(model,
# validate input
marginalize <- insight::validate_argument(
marginalize,
c("average", "population", "individual")
c("average", "population", "specific")
)

if (backend == "emmeans") {
Expand All @@ -204,12 +204,13 @@ estimate_means <- function(model,

# Table formatting
attr(means, "table_title") <- c(ifelse(
marginalize == "individual",
marginalize == "specific",
"Model-based Predictions",
"Estimated Marginal Means"
), "blue")
attr(means, "table_footer") <- .table_footer(
means,
type = ifelse(marginalize == "specific", "predictions", "means"),
by = info$by,
model = model,
info = info
Expand Down
4 changes: 2 additions & 2 deletions R/get_marginalmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ get_marginalmeans <- function(model,
# validate input
marginalize <- insight::validate_argument(
marginalize,
c("average", "population", "individual")
c("average", "population", "specific")
)

# Guess arguments
Expand All @@ -60,7 +60,7 @@ get_marginalmeans <- function(model,
} else {
# setup arguments to create the data grid
dg_factors <- switch(marginalize,
individual = "reference",
specific = "reference",
"all"
)
dg_args <- list(
Expand Down
25 changes: 16 additions & 9 deletions R/table_footer.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
model = NULL,
info = NULL) {
# extract necessary information from attributes
predict <- info$predict

Check warning on line 10 in R/table_footer.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/table_footer.R,line=10,col=3,[object_overwrite_linter] 'predict' is an exported object from package 'stats'. Avoid re-using such symbols.
comparison <- info$comparison
datagrid <- info$datagrid
p_adjust <- info$p_adjust
Expand All @@ -32,19 +32,26 @@


# predictors controlled (non-focal terms) ----------------------------------

if (!is.null(adjusted_for) && length(adjusted_for) >= 1 && !all(is.na(adjusted_for))) {
# if we have values of adjusted terms, add these here
if (all(adjusted_for %in% colnames(x))) {
ref_cat_data <- x
} else if (all(adjusted_for %in% colnames(datagrid))) {
ref_cat_data <- datagrid
} else {
ref_cat_data <- NULL
}
if (!is.null(ref_cat_data)) {
# get values at which non-focal terms are hold constant
adjusted_values <- sapply(adjusted_for, function(i) {
x[[i]][1]
})
# at values to names of non-focal terms (table_footer)
if (is.numeric(adjusted_values)) {
adjusted_for <- sprintf("%s (%.2g)", adjusted_for, adjusted_values)
} else {
adjusted_for <- sprintf("%s (%s)", adjusted_for, adjusted_values)
adjusted_values <- lapply(adjusted_for, function(i) ref_cat_data[[i]][1])
# at values to names of non-focal terms (table_footer). we have to iterate
# over the list, because we may have different types of data
for (av in seq_along(adjusted_values)) {
if (is.numeric(adjusted_values[[av]])) {
adjusted_for[av] <- sprintf("%s (%.2g)", adjusted_for[av], adjusted_values[[av]])
} else if (identical(type, "predictions")) {
adjusted_for[av] <- sprintf("%s (%s)", adjusted_for[av], adjusted_values[[av]])
}
}
}
average_string <- switch(type,
Expand Down Expand Up @@ -74,7 +81,7 @@
"Predictions"
)
# exceptions
predict <- switch(predict,

Check warning on line 84 in R/table_footer.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/table_footer.R,line=84,col=5,[object_overwrite_linter] 'predict' is an exported object from package 'stats'. Avoid re-using such symbols.
none = "link",
prediction = ,
expectations = ,
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/_snaps/estimate_contrasts.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@

Variable predicted: neg_c_7
Predictors contrasted: e42dep, c172code
Predictors averaged: c12hour, barthtot, c161sex
Predictors averaged: c12hour (42), barthtot (65), c161sex
p-values are uncorrected.
Parameters:
b6 = e42dep [slightly dependent], c172code [intermediate level of education]
Expand Down Expand Up @@ -393,7 +393,7 @@

Variable predicted: neg_c_7
Predictors contrasted: barthtot
Predictors averaged: e16sex, barthtot
Predictors averaged: e16sex, barthtot (65)
p-values are uncorrected.

---
Expand Down Expand Up @@ -424,7 +424,7 @@

Variable predicted: neg_c_7
Predictors contrasted: barthtot
Predictors averaged: barthtot
Predictors averaged: barthtot (65)
p-values are uncorrected.

# estimate_contrasts - simple contrasts and with - in levels works
Expand All @@ -443,7 +443,7 @@

Variable predicted: Sepal.Length
Predictors contrasted: Species
Predictors averaged: Sepal.Width
Predictors averaged: Sepal.Width (3.1)
p-values are uncorrected.

---
Expand Down Expand Up @@ -603,7 +603,7 @@

Variable predicted: count
Predictors contrasted: mined, spp
Predictors averaged: cover, site
Predictors averaged: cover (8.7e-11), site
p-values are uncorrected.
Contrasts are on the response-scale.

Expand All @@ -627,7 +627,7 @@

Variable predicted: count
Predictors contrasted: mined
Predictors averaged: cover, site
Predictors averaged: cover (8.7e-11), site
p-values are uncorrected.
Contrasts are on the response-scale.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/windows/print.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@

Variable predicted: neg_c_7
Predictors modulated: c12hour, c172code, c161sex
Predictors averaged: barthtot
Predictors averaged: barthtot (65)

---

Expand Down Expand Up @@ -550,7 +550,7 @@

Variable predicted: neg_c_7
Predictors modulated: c160age=[fivenum], c161sex
Predictors controlled: barthtot (65), e16sex (1)
Predictors controlled: barthtot (65), e16sex (male)

---

Expand All @@ -574,5 +574,5 @@

Variable predicted: neg_c_7
Predictors modulated: c160age=[fivenum], c161sex
Predictors averaged: barthtot, e16sex
Predictors averaged: barthtot (65), e16sex

4 changes: 2 additions & 2 deletions tests/testthat/test-estimate_expectation.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ test_that("estimate_expectation - error", {
})


test_that("estimate_relation and marginalize individual", {
test_that("estimate_relation and marginalize specific", {
skip_if_not_installed("ggeffects")
data(efc, package = "ggeffects")
efc <- datawizard::to_factor(efc, c("c161sex", "c172code", "e16sex", "e42dep"))
fit <- lm(neg_c_7 ~ c12hour + barthtot + c161sex + e42dep + c172code, data = efc)
out1 <- estimate_means(fit, "e42dep", marginalize = "individual", backend = "marginaleffects")
out1 <- estimate_means(fit, "e42dep", marginalize = "specific", backend = "marginaleffects")
out2 <- estimate_relation(fit, by = "e42dep")
expect_equal(out1$Mean, out2$Predicted, tolerance = 1e-4)
})
19 changes: 19 additions & 0 deletions tests/testthat/test-table_footer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
skip_if_not_installed("marginaleffects")
skip_if_not_installed("emmeans")

test_that("table_footer", {
skip_if_not_installed("ggeffects")
data(efc, package = "ggeffects")
efc <- datawizard::to_factor(efc, c("c161sex", "c172code", "e16sex", "e42dep"))
m <- lm(neg_c_7 ~ c12hour + barthtot + c161sex + e42dep + c172code, data = efc)
out <- utils::capture.output(estimate_means(m, "c172code", marginalize = "specific", verbose = FALSE))
expect_identical(
out[11],
"Predictors controlled: c12hour (42), barthtot (65), c161sex (Male), e42dep (independent)"
)
out <- utils::capture.output(estimate_means(m, "c172code", marginalize = "average", verbose = FALSE))
expect_identical(
out[11],
"Predictors averaged: c12hour (42), barthtot (65), c161sex, e42dep"
)
})
Loading