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.16.0
Version: 0.16.0.1
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# modelbased (devel)

## Changes

* `ci = NULL` now suppresses calculation of standard errors and confidence
intervals.

# modelbased 0.16.0

## Changes
Expand Down
6 changes: 3 additions & 3 deletions R/estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
#' number, only as many columns as indicated in `keep_iterations` will be added
#' to the output. You can reshape them to a long format by running
#' [`bayestestR::reshape_iterations()`].
#' @param ci Confidence Interval (CI) level. Must be numeric between 0 and 1.
#' Defaults to `0.95`. Use `NULL` to suppress calculation of standard errors and
#' confidence intervals.
Comment on lines +118 to +120
#' @param verbose Use `FALSE` to silence messages and warnings.
#' @param ... Other arguments passed, for instance, to [insight::get_datagrid()],
#' to functions from the **emmeans** or **marginaleffects** package, or to process
Expand Down Expand Up @@ -151,9 +154,6 @@
#' `estimate = "population"` without specifying the `offset`, to average
#' predictions over the distribution of the offset (if appropriate).
#'
#' @inheritParams parameters::model_parameters.default
#' @inheritParams estimate_expectation
#'
#' @details
#' The [estimate_slopes()], [estimate_means()] and [estimate_contrasts()]
#' functions are forming a group, as they are all based on *marginal*
Expand Down
6 changes: 6 additions & 0 deletions R/get_marginalmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ get_marginalmeans <- function(
fun_args$re.form <- NULL
}

# missing or NA for conf_level? If so, we want to suppress SE and CI
if (is.null(fun_args$conf_level) || is.na(fun_args$conf_level)) {
fun_args$conf_level <- NULL # for NA
fun_args$vcov <- FALSE
}

# transform reponse?
if (isTRUE(transform)) {
transform <- insight::get_transformation(model, verbose = FALSE)$inverse
Expand Down
6 changes: 6 additions & 0 deletions R/get_marginaltrends.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ get_marginaltrends <- function(
fun_args$type <- predict
}

# missing or NA for conf_level? If so, we want to suppress SE and CI
if (is.null(fun_args$conf_level) || is.na(fun_args$conf_level)) {
fun_args$conf_level <- NULL # for NA
fun_args$vcov <- FALSE
}

# Third step: compute marginal slopes ---------------------------------------
# ---------------------------------------------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion man/estimate_contrasts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/estimate_expectation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/estimate_means.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/estimate_slopes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/get_emmeans.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 94 additions & 20 deletions tests/testthat/test-estimate_means_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,56 @@ test_that("estimate_means() - ci frequentist", {
x <- capture.output(out)
expect_identical(x[5], "setosa | 4.75 | 0.09 | [4.61, 4.89] | 54.34")

out <- estimate_contrasts(mod2, contrast = "Species", backend = "marginaleffects", ci = 0.95)
out <- estimate_contrasts(
mod2,
contrast = "Species",
backend = "marginaleffects",
ci = 0.95
)
x <- capture.output(out)
expect_identical(x[5], "versicolor | setosa | 1.43 | 0.12 | [1.19, 1.68] | 11.78 | < .001")

out <- estimate_contrasts(mod2, contrast = "Species", backend = "marginaleffects", ci = 0.89)
expect_identical(
x[5],
"versicolor | setosa | 1.43 | 0.12 | [1.19, 1.68] | 11.78 | < .001"
)

out <- estimate_contrasts(
mod2,
contrast = "Species",
backend = "marginaleffects",
ci = 0.89
)
x <- capture.output(out)
expect_identical(x[5], "versicolor | setosa | 1.43 | 0.12 | [1.24, 1.63] | 11.78 | < .001")
expect_identical(
x[5],
"versicolor | setosa | 1.43 | 0.12 | [1.24, 1.63] | 11.78 | < .001"
)

# Estimate slopes with different CIs
out <- estimate_slopes(mod2, trend = "Sepal.Width", by = "Species", backend = "marginaleffects", ci = 0.95)
out <- estimate_slopes(
mod2,
trend = "Sepal.Width",
by = "Species",
backend = "marginaleffects",
ci = 0.95
)
x <- capture.output(out)
expect_identical(x[5], "setosa | 0.69 | 0.17 | [0.36, 1.02] | 4.17 | < .001")

out <- estimate_slopes(mod2, trend = "Sepal.Width", by = "Species", backend = "marginaleffects", ci = 0.89)
out <- estimate_slopes(
mod2,
trend = "Sepal.Width",
by = "Species",
backend = "marginaleffects",
ci = 0.89
)
x <- capture.output(out)
expect_identical(x[5], "setosa | 0.69 | 0.17 | [0.42, 0.96] | 4.17 | < .001")

# suppress CI
out <- estimate_means(mod2, by = "Species", backend = "marginaleffects", ci = NULL)
expect_named(out, c("Species", "Mean", "df"))
out <- estimate_means(mod2, by = "Species", backend = "marginaleffects", ci = NA)
expect_named(out, c("Species", "Mean", "df"))
})


Expand All @@ -48,26 +82,66 @@ test_that("estimate_means() - ci frequentist", {
# Estimate means with different CIs
out <- estimate_means(mod2, by = "Species", backend = "marginaleffects", ci = 0.95)
x <- capture.output(out)
expect_identical(x[5], "setosa | 4.76 | [4.59, 4.93] | 100% | [-0.10, 0.10] | 0%")
expect_identical(
x[5],
"setosa | 4.76 | [4.59, 4.93] | 100% | [-0.10, 0.10] | 0%"
)

out <- estimate_means(mod2, by = "Species", backend = "marginaleffects", ci = 0.89)
x <- capture.output(out)
expect_identical(x[5], "setosa | 4.76 | [4.62, 4.90] | 100% | [-0.10, 0.10] | 0%")

out <- estimate_contrasts(mod2, contrast = "Species", backend = "marginaleffects", ci = 0.95)
expect_identical(
x[5],
"setosa | 4.76 | [4.62, 4.90] | 100% | [-0.10, 0.10] | 0%"
)

out <- estimate_contrasts(
mod2,
contrast = "Species",
backend = "marginaleffects",
ci = 0.95
)
x <- capture.output(out)
expect_identical(x[5], "versicolor | setosa | 1.43 | [1.18, 1.67] | 100% | [-0.10, 0.10] | 0%")

out <- estimate_contrasts(mod2, contrast = "Species", backend = "marginaleffects", ci = 0.89)
expect_identical(
x[5],
"versicolor | setosa | 1.43 | [1.18, 1.67] | 100% | [-0.10, 0.10] | 0%"
)

out <- estimate_contrasts(
mod2,
contrast = "Species",
backend = "marginaleffects",
ci = 0.89
)
x <- capture.output(out)
expect_identical(x[5], "versicolor | setosa | 1.43 | [1.23, 1.63] | 100% | [-0.10, 0.10] | 0%")
expect_identical(
x[5],
"versicolor | setosa | 1.43 | [1.23, 1.63] | 100% | [-0.10, 0.10] | 0%"
)

# Estimate slopes with different CIs
out <- estimate_slopes(mod2, trend = "Sepal.Width", by = "Species", backend = "marginaleffects", ci = 0.95)
out <- estimate_slopes(
mod2,
trend = "Sepal.Width",
by = "Species",
backend = "marginaleffects",
ci = 0.95
)
x <- capture.output(out)
expect_identical(x[5], "setosa | 0.67 | [0.35, 0.99] | 100% | [-0.10, 0.10] | 0%")

out <- estimate_slopes(mod2, trend = "Sepal.Width", by = "Species", backend = "marginaleffects", ci = 0.89)
expect_identical(
x[5],
"setosa | 0.67 | [0.35, 0.99] | 100% | [-0.10, 0.10] | 0%"
)

out <- estimate_slopes(
mod2,
trend = "Sepal.Width",
by = "Species",
backend = "marginaleffects",
ci = 0.89
)
x <- capture.output(out)
expect_identical(x[5], "setosa | 0.67 | [0.41, 0.93] | 100% | [-0.10, 0.10] | 0%")
expect_identical(
x[5],
"setosa | 0.67 | [0.41, 0.93] | 100% | [-0.10, 0.10] | 0%"
)
})
6 changes: 6 additions & 0 deletions tests/testthat/test-estimate_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ test_that("estimate_slopes", {
)
expect_named(estim2, c("Petal.Length", "Slope", "SE", "CI_low", "CI_high", "z", "p"))

# suppress CI
out <- suppressMessages(estimate_slopes(model, ci = NULL))
expect_named(out, "Slope")
out <- suppressMessages(estimate_slopes(model, ci = NA))
expect_named(out, "Slope")
Comment on lines +92 to +95

model <- lm(Petal.Length ~ poly(Sepal.Width, 4), data = iris)

estim1 <- suppressMessages(estimate_slopes(
Expand Down
Loading