Skip to content

Commit

Permalink
Merge branch 'main' into paper
Browse files Browse the repository at this point in the history
  • Loading branch information
rempsyc authored Feb 11, 2025
2 parents 7ca3a07 + 95ba671 commit 034e8e5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
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.9.0.13
Version: 0.9.0.14
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
2 changes: 1 addition & 1 deletion R/estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ estimate_means <- function(model,
# validate input
estimate <- insight::validate_argument(
estimate,
c("average", "population", "specific")
c("average", "population", "specific", "sample")
)

if (backend == "emmeans") {
Expand Down
11 changes: 9 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
estimate <- insight::validate_argument(
estimate,
c("average", "population", "specific")
c("average", "population", "specific", "sample")
)

# model details
Expand Down Expand Up @@ -126,7 +126,7 @@ get_marginalmeans <- function(model,
fun_args$variables <- lapply(datagrid, unique)[datagrid_info$at_specs$varname]
} else {
# all other "marginalizations"
if (is.null(dots$newdata)) {
if (is.null(dots$newdata) && estimate != "sample") {
# we allow individual "newdata" options, so do not
# # overwrite if explicitly set
fun_args$newdata <- datagrid
Expand Down Expand Up @@ -181,6 +181,13 @@ get_marginalmeans <- function(model,
# just need to add "hypothesis" argument
means <- .call_marginaleffects(fun_args)

# filter "by" rows when we have "sample" marginalization, because we don't
# pass data grid in such situations
if (identical(estimate, "sample") && all(datagrid_info$at_specs$varname %in% colnames(means))) {
means <- datawizard::data_match(means, datagrid[datagrid_info$at_specs$varname])
}


# =========================================================================
# only needed to estimate_contrasts() with custom hypothesis ==============
# =========================================================================
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,14 @@ test_that("estimate_contrasts - test all combinations of contrast and by, with f
expect_snapshot(print(estimate_contrasts(model2, "time=c(1,2)", by = "grp"), zap_small = TRUE, table_width = Inf)) # nolint
expect_snapshot(print(estimate_contrasts(model2, c("grp", "time=2")), zap_small = TRUE, table_width = Inf)) # nolint
})


test_that("estimate_contrast, full averaging", {
data(efc, package = "modelbased")
efc <- datawizard::to_factor(efc, c("c161sex", "c172code", "e16sex", "e42dep"))
levels(efc$c172code) <- c("low", "mid", "high")
m <- lm(neg_c_7 ~ c12hour + barthtot + e42dep + c161sex * c172code, data = efc)

out <- estimate_contrasts(m, "c161sex", by = "c172code", estimate = "sample")
expect_equal(out$Difference, c(1.09591, 0.68736, 0.92224), tolerance = 1e-4)
})
23 changes: 23 additions & 0 deletions tests/testthat/test-estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,26 @@ test_that("estimate_means, values inside correct bounds", {
expect_true(all(out2$CI_low >= 0 & out2$CI_low <= 1))
expect_true(all(out2$CI_high >= 0 & out2$CI_high <= 1))
})


test_that("estimate_means, full averaging", {
data(efc, package = "modelbased")
efc <- datawizard::to_factor(efc, c("c161sex", "c172code", "e16sex", "e42dep"))
levels(efc$c172code) <- c("low", "mid", "high")
m <- lm(neg_c_7 ~ c12hour + barthtot + e42dep + c161sex * c172code, data = efc)

estim1 <- marginaleffects::avg_predictions(m, by = c("c161sex", "c172code"))
estim2 <- estimate_means(m, by = c("c161sex", "c172code"), estimate = "sample")
expect_equal(estim1$estimate, estim2$Mean, tolerance = 1e-4)

estim1 <- marginaleffects::avg_predictions(m, variables = c("c161sex", "c172code"))
estim2 <- estimate_means(m, by = c("c161sex", "c172code"), estimate = "population")
expect_equal(estim1$estimate, estim2$Mean, tolerance = 1e-4)

estim1 <- marginaleffects::avg_predictions(m, newdata = "balanced", by = c("c161sex", "c172code"))
estim2 <- estimate_means(m, by = c("c161sex", "c172code"), estimate = "average")
expect_equal(estim1$estimate, estim2$Mean, tolerance = 1e-4)

estim2 <- estimate_means(m, by = c("c161sex", "c172code='mid'"), estimate = "sample")
expect_identical(dim(estim2), c(6L, 8L))
})

0 comments on commit 034e8e5

Please sign in to comment.