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.13.0.8
Version: 0.13.0.9
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
5 changes: 5 additions & 0 deletions R/estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ estimate_contrasts.default <- function(
attr(out, "p_adjust") <- p_adjust
attr(out, "backend") <- backend

# we want to store the generic, not the ".default" method, in the call
cl <- match.call()
cl[[1L]] <- quote(estimate_contrasts)
attr(out, "call") <- cl

# add attributes from workhorse function
attributes(out) <- utils::modifyList(attributes(out), info[.info_elements()])

Expand Down
1 change: 1 addition & 0 deletions R/estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ estimate_means <- function(model,
attr(means, "ci") <- ci
attr(means, "backend") <- backend
attr(means, "coef_name") <- intersect(.valid_coefficient_names(model), colnames(means))
attr(means, "call") <- match.call()

# add attributes from workhorse function
attributes(means) <- utils::modifyList(attributes(means), info[.info_elements()])
Expand Down
1 change: 1 addition & 0 deletions R/estimate_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' confidence bands: Theory, implementation, and an application to SVARs.
#' Journal of Applied Econometrics, 34(1), 1–17. \doi{10.1002/jae.2656}
#'
#' @examplesIf all(insight::check_if_installed(c("marginaleffects", "emmeans", "effectsize", "mgcv", "ggplot2", "see"), quietly = TRUE))

Check warning on line 48 in R/estimate_slopes.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_slopes.R,line=48,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 136 characters.

Check warning on line 48 in R/estimate_slopes.R

View workflow job for this annotation

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

file=R/estimate_slopes.R,line=48,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 136 characters.
#' library(ggplot2)
#' # Get an idea of the data
#' ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) +
Expand Down Expand Up @@ -105,7 +105,7 @@
#' estimate_slopes(model, trend = "Petal.Length=seq(2, 4, 0.01)")
#' }
#'
#' @examplesIf all(insight::check_if_installed(c("marginaleffects", "emmeans"), quietly = TRUE)) && getRversion() >= "4.5.0"

Check warning on line 108 in R/estimate_slopes.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/estimate_slopes.R,line=108,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.

Check warning on line 108 in R/estimate_slopes.R

View workflow job for this annotation

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

file=R/estimate_slopes.R,line=108,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
#' \dontrun{
#' # marginal effects with different `estimate` options
#' data(penguins)
Expand Down Expand Up @@ -186,6 +186,7 @@
attr(trends, "model") <- model
attr(trends, "response") <- insight::find_response(model)
attr(trends, "ci") <- ci
attr(trends, "call") <- match.call()

# add attributes from workhorse function
attributes(trends) <- utils::modifyList(attributes(trends), info[.info_elements()])
Expand Down
38 changes: 28 additions & 10 deletions tests/testthat/test-attributes_estimatefun.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
model <- lm(Sepal.Length ~ Species + Sepal.Width, data = iris)

estim <- suppressMessages(estimate_means(model, "Species", backend = "emmeans"))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "row.names", "class", "at", "by", "table_title", "table_footer",
"model", "response", "ci", "backend", "coef_name", "focal_terms",
"model", "response", "ci", "backend", "coef_name", "call", "focal_terms",
"predict", "transform", "keep_iterations"
)
)
estim <- suppressMessages(estimate_means(model, "Species", backend = "marginaleffects"))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "class", "row.names", "at", "by", "focal_terms", "adjusted_for",
"predict", "estimate", "transform", "datagrid", "preserve_range",
"model_info", "keep_iterations", "joint_test", "vcov", "table_title",
"table_footer", "model", "response", "ci", "backend", "coef_name"
"table_footer", "model", "response", "ci", "backend", "coef_name", "call"
)
)
})
Expand All @@ -32,45 +34,58 @@
model <- lm(Sepal.Length ~ Species + Sepal.Width, data = iris)

estim <- suppressMessages(estimate_contrasts(model, "Species", backend = "emmeans"))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "class", "row.names", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "predict",
"model", "response", "ci", "p_adjust", "backend", "call", "predict",
"comparison", "contrast", "transform", "keep_iterations", "joint_test"
)
)
estim <- suppressMessages(estimate_contrasts(model, "Species", backend = "marginaleffects"))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "row.names", "class", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
"adjusted_for", "predict", "comparison", "contrast", "estimate",
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
"keep_iterations", "joint_test", "vcov"
)
)
estim <- suppressMessages(estimate_contrasts(model, "Species=c('setosa','virginica')", backend = "marginaleffects"))
estim <- suppressMessages(estimate_contrasts(
model,
"Species=c('setosa','virginica')",
backend = "marginaleffects"
))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "row.names", "class", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
"adjusted_for", "predict", "comparison", "contrast", "estimate",
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
"keep_iterations", "joint_test", "vcov"
)
)
estim <- suppressMessages(estimate_contrasts(model, "Species=c('setosa','virginica')", backend = "marginaleffects", estimate = "average"))
estim <- suppressMessages(estimate_contrasts(
model,
"Species=c('setosa','virginica')",
backend = "marginaleffects",
estimate = "average"
))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "row.names", "class", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
"adjusted_for", "predict", "comparison", "contrast", "estimate",
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
"contrast_filter", "keep_iterations", "joint_test","vcov"

Check warning on line 88 in tests/testthat/test-attributes_estimatefun.R

View workflow job for this annotation

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

file=tests/testthat/test-attributes_estimatefun.R,line=88,col=58,[commas_linter] Put a space after a comma.
)
)
})
Expand All @@ -81,21 +96,23 @@
model <- lm(Sepal.Length ~ Species + Sepal.Width, data = iris)

estim <- suppressMessages(estimate_slopes(model, "Sepal.Width", backend = "emmeans"))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "row.names", "class", "table_title", "table_footer",
"model", "response", "ci", "trend", "transform", "coef_name",
"model", "response", "ci", "call", "trend", "transform", "coef_name",
"keep_iterations"
)
)
estim <- suppressMessages(estimate_slopes(model, "Sepal.Width", backend = "marginaleffects"))
# fmt: skip
expect_named(
attributes(estim),
c(
"names", "class", "row.names", "trend", "p_adjust", "transform",
"coef_name", "slope", "ci", "model_info", "keep_iterations",
"vcov", "table_title", "table_footer", "model", "response"
"vcov", "table_title", "table_footer", "model", "response", "call"
)
)
})
Expand All @@ -109,6 +126,7 @@
by = c("Species", "Petal.Width = [fivenum]"),
preserve_range = FALSE
)
# fmt: skip
expect_named(
attributes(estim),
c(
Expand Down
47 changes: 35 additions & 12 deletions tests/testthat/test-keep_iterations.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ test_that("estimate_means() - posterior draws", {
m <- insight::download_model("brms_1")
skip_if(is.null(m))
out <- estimate_means(m, by = "wt", keep_iterations = 5)
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "at", "by", "focal_terms", "adjusted_for",
"predict", "estimate", "transform", "datagrid", "preserve_range",
"model_info", "keep_iterations", "joint_test", "table_title",
"table_footer", "model", "response", "ci", "backend", "coef_name"
"table_footer", "model", "response", "ci", "backend", "coef_name", "call"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -33,15 +35,17 @@ test_that("estimate_means() - posterior draws", {
expect_identical(dim(out), c(10L, 4009L))

out <- estimate_means(m, by = "wt")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "at", "by", "focal_terms", "adjusted_for",
"predict", "estimate", "transform", "datagrid", "preserve_range",
"model_info", "keep_iterations", "joint_test", "table_title",
"table_footer", "model", "response", "ci", "backend", "coef_name"
"table_footer", "model", "response", "ci", "backend", "coef_name", "call"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -57,16 +61,18 @@ test_that("estimate_contrasts() - posterior draws", {
m <- insight::download_model("brms_1")
skip_if(is.null(m))
out <- estimate_contrasts(m, "wt=c(3,4,5)", keep_iterations = 5)
# fmt: skip
expect_named(
attributes(out),
c(
"names", "row.names", "class", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
"adjusted_for", "predict", "comparison", "contrast", "estimate",
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
"keep_iterations", "joint_test"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -81,16 +87,18 @@ test_that("estimate_contrasts() - posterior draws", {
expect_identical(dim(out), c(3L, 4010L))

out <- estimate_contrasts(m, "wt=c(3,4,5)")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "row.names", "class", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "focal_terms",
"model", "response", "ci", "p_adjust", "backend", "call", "focal_terms",
"adjusted_for", "predict", "comparison", "contrast", "estimate",
"transform", "datagrid", "preserve_range", "coef_name", "model_info",
"keep_iterations", "joint_test"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -106,14 +114,16 @@ test_that("estimate_slopes() - posterior draws", {
m <- insight::download_model("brms_1")
skip_if(is.null(m))
out <- estimate_slopes(m, "wt", keep_iterations = 5)
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "trend", "p_adjust", "transform",
"coef_name", "slope", "ci", "model_info", "keep_iterations",
"table_title", "table_footer", "model", "response"
"table_title", "table_footer", "model", "response", "call"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -125,14 +135,16 @@ test_that("estimate_slopes() - posterior draws", {
expect_identical(dim(out), c(1L, 13L))

out <- estimate_slopes(m, "wt")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "trend", "p_adjust", "transform",
"coef_name", "slope", "ci", "model_info", "keep_iterations",
"table_title", "table_footer", "model", "response"
"table_title", "table_footer", "model", "response", "call"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -148,14 +160,16 @@ test_that("estimate_means() - posterior draws, emmeans", {
m <- insight::download_model("brms_1")
skip_if(is.null(m))
out <- estimate_means(m, by = "wt", keep_iterations = 5, backend = "emmeans")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "table_title", "table_footer",
"model", "response", "ci", "backend", "coef_name", "at", "by",
"model", "response", "ci", "backend", "coef_name", "call", "at", "by",
"focal_terms", "predict", "transform", "keep_iterations"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -166,11 +180,12 @@ test_that("estimate_means() - posterior draws, emmeans", {
expect_identical(dim(out), c(10L, 10L))

out <- estimate_means(m, by = "wt", keep_iterations = TRUE, backend = "emmeans")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "table_title", "table_footer",
"model", "response", "ci", "backend", "coef_name", "at", "by",
"model", "response", "ci", "backend", "coef_name", "call", "at", "by",
"focal_terms", "predict", "transform", "keep_iterations"
)
)
Expand All @@ -182,15 +197,17 @@ test_that("estimate_contrasts() - posterior draws, emmeans", {
m <- insight::download_model("brms_1")
skip_if(is.null(m))
out <- estimate_contrasts(m, by = "wt=c(3,4,5)", keep_iterations = 5, backend = "emmeans")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "at", "by",
"model", "response", "ci", "p_adjust", "backend", "call", "at", "by",
"predict", "comparison", "contrast", "transform", "keep_iterations",
"joint_test"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -201,11 +218,12 @@ test_that("estimate_contrasts() - posterior draws, emmeans", {
expect_identical(dim(out), c(3L, 12L))

out <- estimate_contrasts(m, by = "wt=c(3,4,5)", keep_iterations = TRUE, backend = "emmeans")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "table_title", "table_footer",
"model", "response", "ci", "p_adjust", "backend", "at", "by",
"model", "response", "ci", "p_adjust", "backend", "call", "at", "by",
"predict", "comparison", "contrast", "transform", "keep_iterations",
"joint_test"
)
Expand All @@ -218,14 +236,16 @@ test_that("estimate_slopes() - posterior draws, emmeans", {
m <- insight::download_model("brms_1")
skip_if(is.null(m))
out <- estimate_slopes(m, "wt", keep_iterations = 5, backend = "emmeans")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "table_title", "table_footer",
"model", "response", "ci", "trend", "transform", "coef_name",
"model", "response", "ci", "call", "trend", "transform", "coef_name",
"keep_iterations"
)
)
# fmt: skip
expect_named(
out,
c(
Expand All @@ -236,11 +256,12 @@ test_that("estimate_slopes() - posterior draws, emmeans", {
expect_identical(dim(out), c(1L, 10L))

out <- estimate_slopes(m, "wt", keep_iterations = TRUE, backend = "emmeans")
# fmt: skip
expect_named(
attributes(out),
c(
"names", "class", "row.names", "table_title", "table_footer",
"model", "response", "ci", "trend", "transform", "coef_name",
"model", "response", "ci", "call", "trend", "transform", "coef_name",
"keep_iterations"
)
)
Expand All @@ -252,6 +273,7 @@ test_that("estimate_slopes() - posterior draws, get_predicted", {
m <- insight::download_model("brms_1")
skip_if(is.null(m))
out <- estimate_relation(m, by = "wt", keep_iterations = 5)
# fmt: skip
expect_named(
attributes(out),
c(
Expand All @@ -261,6 +283,7 @@ test_that("estimate_slopes() - posterior draws, get_predicted", {
"adjusted_for", "at_specs", "at", "by", "reference", "data"
)
)
# fmt: skip
expect_named(
out,
c(
Expand Down
Loading