Skip to content

Commit

Permalink
marginalmeans backend basic
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Oct 16, 2022
1 parent c099b8d commit fb20ff4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
36 changes: 20 additions & 16 deletions R/estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,34 @@ estimate_means <- function(model,
# Compute means
if (backend == "emmeans") {
estimated <- get_emmeans(model, at, fixed, transform = transform, ...)

# Summarize and clean
if (insight::model_info(model)$is_bayesian) {
means <- parameters::parameters(estimated, ci = ci, ...)
means <- .clean_names_bayesian(means, model, transform, type = "mean")
means <- cbind(estimated@grid, means)
means$`.wgt.` <- NULL # Drop the weight column
} else {
means <- as.data.frame(stats::confint(estimated, level = ci))
means$df <- NULL
means <- .clean_names_frequentist(means)
}
# Remove the "1 - overall" column that can appear in cases like at = NULL
means <- means[names(means) != "1"]

info <- attributes(estimated)
} else {
estimated <- .get_marginalmeans(model, at, fixed, transform = transform, ...)
}
info <- attributes(estimated)

# Summarize and clean
if (insight::model_info(model)$is_bayesian) {
means <- parameters::parameters(estimated, ci = ci, ...)
means <- .clean_names_bayesian(means, model, transform, type = "mean")
means <- cbind(estimated@grid, means)
means$`.wgt.` <- NULL # Drop the weight column
} else {
means <- as.data.frame(stats::confint(estimated, level = ci))
means$df <- NULL
means <- .clean_names_frequentist(means)
means <- .get_marginalmeans(model, at, fixed, transform = transform, ...)

info <- attributes(means)
}
# Remove the "1 - overall" column that can appear in cases like at = NULL
means <- means[names(means) != "1"]

# Restore factor levels
means <- datawizard::data_restoretype(means, insight::get_data(model))


# Table formatting

attr(means, "table_title") <- c("Estimated Marginal Means", "blue")
attr(means, "table_footer") <- .estimate_means_footer(means, info$at, type = "means")

Expand Down
7 changes: 7 additions & 0 deletions R/get_marginalmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,12 @@
names(means)[names(means) %in% c("value")] <- term # Replace 'value' col by var name
means$term <- NULL

# Drop stats
means$p <- NULL
means$t <- NULL

# Store attributes
attr(means, "at") <- args$at

means
}
2 changes: 1 addition & 1 deletion tests/testthat/test-estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,6 @@ if (require("testthat") &&
estim1 <- estimate_contrasts(model, lmer.df = "satterthwaite")
estim2 <- estimate_contrasts(model, lmer.df = "kenward-roger")

expect_true(any(as.data.frame(estim1) != as.data.frame(estim2)))
expect_true(any(estim1$CI_low != estim2$CI_low))
})
}
10 changes: 10 additions & 0 deletions tests/testthat/test-get_marginalmeans.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if (require("testthat") && require("modelbased") && require("marginaleffects") && require("lme4")) {
test_that("get_marginaleffects", {
model <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)

estimated <- estimate_means(model, backend = "marginaleffects")
expect_equal(dim(estimated), c(3, 5))

# get_marginaleffects(model, trend = "Petal.Length", at = "Species", length = 10)
})
}

0 comments on commit fb20ff4

Please sign in to comment.