From ad4e985103605ecad518724cabfcb6cf76c8e2c6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 Sep 2023 13:46:36 +0200 Subject: [PATCH] Issues with `parameters()` on `marginaleffects` objects (#901) * Issues with `parameters()` on `marginaleffects` objects Fixes #887 * update RD * update * update * fix --- DESCRIPTION | 2 +- NEWS.md | 8 +++ R/display.R | 6 +- R/equivalence_test.R | 6 +- R/format.R | 19 +++-- R/methods_htest.R | 4 +- R/methods_lavaan.R | 14 ++-- R/methods_marginaleffects.R | 14 ++-- R/print.compare_parameters.R | 4 +- R/print.parameters_model.R | 6 +- R/print_html.R | 8 +-- R/print_md.R | 12 ++-- R/utils_format.R | 4 +- R/utils_model_parameters.R | 16 ++++- man/display.parameters_model.Rd | 10 +-- man/model_parameters.averaging.Rd | 12 ++-- man/print.parameters_model.Rd | 2 +- tests/testthat/_snaps/printing.md | 80 +++++++++++++++++++++ tests/testthat/test-marginaleffects.R | 30 ++++++-- tests/testthat/test-model_parameters.cgam.R | 2 - tests/testthat/test-printing.R | 43 +++++++---- 21 files changed, 222 insertions(+), 80 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 71ffa1698..5a16e4fa1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: parameters Title: Processing of Model Parameters -Version: 0.21.1.5 +Version: 0.21.1.6 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NEWS.md b/NEWS.md index d591d521d..8bec9493e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,13 @@ # parameters 0.21.2 +## Changes + +* The `ci_digits` argument of the `print()` method for `model_parameters()` now + defaults to the same value of `digits`. + +* `model_parameters()` for objects from package *marginaleffects* now also + accepts the `exponentiate` argument. + ## Bug fixes * Fixed issue with wrong calculation of test-statistic and p-values in diff --git a/R/display.R b/R/display.R index de09fd0bd..eb5af55fa 100644 --- a/R/display.R +++ b/R/display.R @@ -81,7 +81,7 @@ display.parameters_model <- function(object, footer = NULL, align = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, ci_brackets = c("(", ")"), @@ -132,7 +132,7 @@ display.parameters_brms_meta <- display.parameters_model display.compare_parameters <- function(object, format = "markdown", digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, select = NULL, column_labels = NULL, @@ -165,7 +165,7 @@ display.compare_parameters <- function(object, display.parameters_sem <- function(object, format = "markdown", digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_brackets = c("(", ")"), ...) { diff --git a/R/equivalence_test.R b/R/equivalence_test.R index 0c9f65afa..3ddeb0c50 100644 --- a/R/equivalence_test.R +++ b/R/equivalence_test.R @@ -693,7 +693,7 @@ equivalence_test.ggeffects <- function(x, #' @export format.equivalence_test_lm <- function(x, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_width = NULL, ci_brackets = NULL, @@ -736,7 +736,7 @@ format.equivalence_test_lm <- function(x, #' @export print.equivalence_test_lm <- function(x, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_brackets = NULL, zap_small = FALSE, @@ -794,7 +794,7 @@ plot.equivalence_test_lm <- function(x, ...) { #' #' @export #' print_md.equivalence_test_lm <- function(x, #' digits = 2, -#' ci_digits = 2, +#' ci_digits = digits, #' p_digits = 3, #' ci_brackets = NULL, #' zap_small = FALSE, diff --git a/R/format.R b/R/format.R index 6534b11c6..d87aa88d9 100644 --- a/R/format.R +++ b/R/format.R @@ -8,7 +8,7 @@ format.parameters_model <- function(x, split_components = TRUE, select = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_width = NULL, ci_brackets = NULL, @@ -259,7 +259,7 @@ format.compare_parameters <- function(x, split_components = TRUE, select = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_width = NULL, ci_brackets = NULL, @@ -468,16 +468,23 @@ format.compare_parameters <- function(x, #' @export format.parameters_sem <- function(x, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, format = NULL, ci_width = NULL, ci_brackets = TRUE, pretty_names = TRUE, ...) { - if (missing(digits)) digits <- .additional_arguments(x, "digits", 2) - if (missing(ci_digits)) ci_digits <- .additional_arguments(x, "ci_digits", 2) - if (missing(p_digits)) p_digits <- .additional_arguments(x, "p_digits", 3) + if (missing(digits)) { + digits <- .additional_arguments(x, "digits", 2) + } + if (missing(ci_digits)) { + ci_digits <- .additional_arguments(x, "ci_digits", digits) + } + if (missing(p_digits)) { + p_digits <- .additional_arguments(x, "p_digits", 3) + } + .format_columns_multiple_components( x, pretty_names = TRUE, diff --git a/R/methods_htest.R b/R/methods_htest.R index 96aeef286..f2fba2982 100644 --- a/R/methods_htest.R +++ b/R/methods_htest.R @@ -695,7 +695,7 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) { if ("ci_digits" %in% names(dot.arguments)) { attr(params, "ci_digits") <- eval(dot.arguments[["ci_digits"]]) } else { - attr(params, "ci_digits") <- 2 + attr(params, "ci_digits") <- NULL } if ("p_digits" %in% names(dot.arguments)) { @@ -747,7 +747,7 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) { if ("ci_digits" %in% names(dot.arguments)) { attr(params, "ci_digits") <- eval(dot.arguments[["ci_digits"]]) } else { - attr(params, "ci_digits") <- 2 + attr(params, "ci_digits") <- NULL } if ("p_digits" %in% names(dot.arguments)) { diff --git a/R/methods_lavaan.R b/R/methods_lavaan.R index 606556c57..860be03f0 100644 --- a/R/methods_lavaan.R +++ b/R/methods_lavaan.R @@ -152,11 +152,17 @@ p_value.blavaan <- p_value.BFBayesFactor # print --------------------------- #' @export -print.parameters_sem <- function(x, digits = 2, ci_digits = 2, p_digits = 3, ...) { +print.parameters_sem <- function(x, digits = 2, ci_digits = digits, p_digits = 3, ...) { # check if user supplied digits attributes - if (missing(digits)) digits <- .additional_arguments(x, "digits", 2) - if (missing(ci_digits)) ci_digits <- .additional_arguments(x, "ci_digits", 2) - if (missing(p_digits)) p_digits <- .additional_arguments(x, "p_digits", 3) + if (missing(digits)) { + digits <- .additional_arguments(x, "digits", 2) + } + if (missing(ci_digits)) { + ci_digits <- .additional_arguments(x, "ci_digits", digits) + } + if (missing(p_digits)) { + p_digits <- .additional_arguments(x, "p_digits", 3) + } verbose <- .additional_arguments(x, "verbose", TRUE) diff --git a/R/methods_marginaleffects.R b/R/methods_marginaleffects.R index 81181c060..2ea350374 100644 --- a/R/methods_marginaleffects.R +++ b/R/methods_marginaleffects.R @@ -7,6 +7,7 @@ #' @export model_parameters.marginaleffects <- function(model, ci = 0.95, + exponentiate = FALSE, ...) { insight::check_if_installed("marginaleffects") out <- insight::standardize_names( @@ -18,10 +19,7 @@ model_parameters.marginaleffects <- function(model, colnames(out)[colnames(out) == "contrast"] <- "Comparison" colnames(out) <- gsub("^contrast_", "Comparison: ", colnames(out)) - out <- tryCatch( - .add_model_parameters_attributes(out, model, ci, ...), - error = function(e) out - ) + out <- .safe(.add_model_parameters_attributes(out, model, ci, exponentiate = exponentiate, ...), out) attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model)) @@ -41,6 +39,8 @@ model_parameters.marginaleffects <- function(model, attr(out, "coefficient_name") <- "Estimate" } + # exponentiate coefficients and SE/CI, if requested + out <- .exponentiate_parameters(out, model = NULL, exponentiate) class(out) <- c("parameters_model", "see_parameters_model", class(out)) out @@ -70,6 +70,7 @@ model_parameters.slopes <- model_parameters.marginaleffects #' @export model_parameters.predictions <- function(model, ci = 0.95, + exponentiate = TRUE, ...) { insight::check_if_installed("marginaleffects") @@ -89,12 +90,15 @@ model_parameters.predictions <- function(model, out[[resp]] <- NULL } - out <- .safe(.add_model_parameters_attributes(out, model, ci, ...), out) + out <- .safe(.add_model_parameters_attributes(out, model, ci, exponentiate = exponentiate, ...), out) attr(out, "object_name") <- insight::safe_deparse_symbol(substitute(model)) attr(out, "coefficient_name") <- "Predicted" attr(out, "no_caption") <- TRUE + # exponentiate coefficients and SE/CI, if requested + out <- .exponentiate_parameters(out, model = NULL, exponentiate) + class(out) <- c("parameters_model", "see_parameters_model", class(out)) out } diff --git a/R/print.compare_parameters.R b/R/print.compare_parameters.R index f679c5d43..2bcdab704 100644 --- a/R/print.compare_parameters.R +++ b/R/print.compare_parameters.R @@ -5,7 +5,7 @@ print.compare_parameters <- function(x, subtitle = NULL, footer = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, zap_small = FALSE, groups = NULL, @@ -21,7 +21,7 @@ print.compare_parameters <- function(x, digits <- .additional_arguments(x, "digits", digits) } if (missing(ci_digits)) { - ci_digits <- .additional_arguments(x, "ci_digits", ci_digits) + ci_digits <- .additional_arguments(x, "ci_digits", digits) } if (missing(p_digits)) { p_digits <- .additional_arguments(x, "p_digits", p_digits) diff --git a/R/print.parameters_model.R b/R/print.parameters_model.R index 298202d25..0d02629ca 100644 --- a/R/print.parameters_model.R +++ b/R/print.parameters_model.R @@ -224,7 +224,7 @@ print.parameters_model <- function(x, caption = NULL, footer = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, show_sigma = FALSE, @@ -256,7 +256,7 @@ print.parameters_model <- function(x, digits <- .additional_arguments(x, "digits", digits) } if (missing(ci_digits)) { - ci_digits <- .additional_arguments(x, "ci_digits", ci_digits) + ci_digits <- .additional_arguments(x, "ci_digits", digits) } if (missing(p_digits)) { p_digits <- .additional_arguments(x, "p_digits", p_digits) @@ -371,7 +371,7 @@ print.parameters_random <- function(x, digits = 2, ...) { split_components = TRUE, select = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, zap_small = FALSE, ci_width = "auto", diff --git a/R/print_html.R b/R/print_html.R index 0d1b6f4cc..aaec47d4d 100644 --- a/R/print_html.R +++ b/R/print_html.R @@ -11,7 +11,7 @@ print_html.parameters_model <- function(x, footer = NULL, align = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, ci_brackets = c("(", ")"), @@ -30,7 +30,7 @@ print_html.parameters_model <- function(x, } if (missing(ci_digits)) { - ci_digits <- .additional_arguments(x, "ci_digits", ci_digits) + ci_digits <- .additional_arguments(x, "ci_digits", digits) } if (missing(p_digits)) { @@ -139,7 +139,7 @@ print_html.compare_parameters <- function(x, subtitle = NULL, footer = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, zap_small = FALSE, groups = NULL, @@ -155,7 +155,7 @@ print_html.compare_parameters <- function(x, } if (missing(ci_digits)) { - ci_digits <- .additional_arguments(x, "ci_digits", ci_digits) + ci_digits <- .additional_arguments(x, "ci_digits", digits) } if (missing(p_digits)) { diff --git a/R/print_md.R b/R/print_md.R index 8a75a2b0f..ad0393cd8 100644 --- a/R/print_md.R +++ b/R/print_md.R @@ -11,7 +11,7 @@ print_md.parameters_model <- function(x, footer = NULL, align = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, ci_brackets = c("(", ")"), @@ -26,7 +26,7 @@ print_md.parameters_model <- function(x, digits <- .additional_arguments(x, "digits", digits) } if (missing(ci_digits)) { - ci_digits <- .additional_arguments(x, "ci_digits", ci_digits) + ci_digits <- .additional_arguments(x, "ci_digits", digits) } if (missing(p_digits)) { p_digits <- .additional_arguments(x, "p_digits", p_digits) @@ -124,7 +124,7 @@ print_md.parameters_simulate <- print_md.parameters_model #' @export print_md.compare_parameters <- function(x, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, caption = NULL, subtitle = NULL, @@ -137,7 +137,7 @@ print_md.compare_parameters <- function(x, digits <- .additional_arguments(x, "digits", digits) } if (missing(ci_digits)) { - ci_digits <- .additional_arguments(x, "ci_digits", ci_digits) + ci_digits <- .additional_arguments(x, "ci_digits", digits) } if (missing(p_digits)) { p_digits <- .additional_arguments(x, "p_digits", p_digits) @@ -178,7 +178,7 @@ print_md.compare_parameters <- function(x, #' @export print_md.parameters_sem <- function(x, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_brackets = c("(", ")"), ...) { @@ -188,7 +188,7 @@ print_md.parameters_sem <- function(x, digits <- .additional_arguments(x, "digits", digits) } if (missing(ci_digits)) { - ci_digits <- .additional_arguments(x, "ci_digits", ci_digits) + ci_digits <- .additional_arguments(x, "ci_digits", digits) } if (missing(p_digits)) { p_digits <- .additional_arguments(x, "p_digits", p_digits) diff --git a/R/utils_format.R b/R/utils_format.R index 4bff287e2..faf8f978a 100644 --- a/R/utils_format.R +++ b/R/utils_format.R @@ -273,7 +273,7 @@ .format_columns_single_component <- function(x, pretty_names, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_width = "auto", ci_brackets = TRUE, @@ -758,7 +758,7 @@ pretty_names, split_column = "Component", digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, coef_column = NULL, format = NULL, diff --git a/R/utils_model_parameters.R b/R/utils_model_parameters.R index 31aeace3f..1f375b99a 100644 --- a/R/utils_model_parameters.R +++ b/R/utils_model_parameters.R @@ -159,7 +159,7 @@ if ("ci_digits" %in% names(dot.arguments)) { attr(params, "ci_digits") <- dot.arguments[["ci_digits"]] } else { - attr(params, "ci_digits") <- 2 + attr(params, "ci_digits") <- NULL } if ("p_digits" %in% names(dot.arguments)) { @@ -300,7 +300,17 @@ return(params) } - columns <- grepl(pattern = "^(Coefficient|Mean|Median|MAP|Std_Coefficient|CI_|Std_CI)", colnames(params)) + # pattern for marginaleffects objects + if (!is.null(attr(params, "coefficient_name"))) { + pattern <- sprintf( + "^(Coefficient|Mean|Median|MAP|Std_Coefficient|%s|CI_|Std_CI)", + attr(params, "coefficient_name") + ) + } else { + pattern <- "^(Coefficient|Mean|Median|MAP|Std_Coefficient|CI_|Std_CI)" + } + + columns <- grepl(pattern = pattern, colnames(params)) if (any(columns)) { if (inherits(model, "mvord")) { rows <- params$Component != "correlation" @@ -384,7 +394,7 @@ if ("ci_digits" %in% names(dot.arguments)) { attr(params, "ci_digits") <- eval(dot.arguments[["ci_digits"]]) } else { - attr(params, "ci_digits") <- 2 + attr(params, "ci_digits") <- NULL } if ("p_digits" %in% names(dot.arguments)) { diff --git a/man/display.parameters_model.Rd b/man/display.parameters_model.Rd index e2c79cfd1..5a014b01c 100644 --- a/man/display.parameters_model.Rd +++ b/man/display.parameters_model.Rd @@ -23,7 +23,7 @@ footer = NULL, align = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, ci_brackets = c("(", ")"), @@ -41,7 +41,7 @@ object, format = "markdown", digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_brackets = c("(", ")"), ... @@ -67,7 +67,7 @@ split_components = TRUE, select = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, ci_width = NULL, ci_brackets = NULL, @@ -87,7 +87,7 @@ footer = NULL, align = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, ci_brackets = c("(", ")"), @@ -112,7 +112,7 @@ footer = NULL, align = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, ci_brackets = c("(", ")"), diff --git a/man/model_parameters.averaging.Rd b/man/model_parameters.averaging.Rd index 2c81602f2..091d97fd9 100644 --- a/man/model_parameters.averaging.Rd +++ b/man/model_parameters.averaging.Rd @@ -285,17 +285,17 @@ ... ) -\method{model_parameters}{marginaleffects}(model, ci = 0.95, ...) +\method{model_parameters}{marginaleffects}(model, ci = 0.95, exponentiate = FALSE, ...) -\method{model_parameters}{comparisons}(model, ci = 0.95, ...) +\method{model_parameters}{comparisons}(model, ci = 0.95, exponentiate = FALSE, ...) -\method{model_parameters}{marginalmeans}(model, ci = 0.95, ...) +\method{model_parameters}{marginalmeans}(model, ci = 0.95, exponentiate = FALSE, ...) -\method{model_parameters}{hypotheses}(model, ci = 0.95, ...) +\method{model_parameters}{hypotheses}(model, ci = 0.95, exponentiate = FALSE, ...) -\method{model_parameters}{slopes}(model, ci = 0.95, ...) +\method{model_parameters}{slopes}(model, ci = 0.95, exponentiate = FALSE, ...) -\method{model_parameters}{predictions}(model, ci = 0.95, ...) +\method{model_parameters}{predictions}(model, ci = 0.95, exponentiate = TRUE, ...) \method{model_parameters}{margins}( model, diff --git a/man/print.parameters_model.Rd b/man/print.parameters_model.Rd index e556c0b9b..eda4ad2e1 100644 --- a/man/print.parameters_model.Rd +++ b/man/print.parameters_model.Rd @@ -13,7 +13,7 @@ caption = NULL, footer = NULL, digits = 2, - ci_digits = 2, + ci_digits = digits, p_digits = 3, footer_digits = 3, show_sigma = FALSE, diff --git a/tests/testthat/_snaps/printing.md b/tests/testthat/_snaps/printing.md index f13eb8e69..159a3626c 100644 --- a/tests/testthat/_snaps/printing.md +++ b/tests/testthat/_snaps/printing.md @@ -149,6 +149,86 @@ Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed using a Wald t-distribution approximation. +# digits and ci_digits + + Code + model_parameters(model, digits = 4) + Output + Parameter | Coefficient | SE | 95% CI | t(24) | p + ------------------------------------------------------------------------- + (Intercept) | 18.9880 | 7.4728 | [ 3.5648, 34.4112] | 2.5409 | 0.018 + hp | -0.0627 | 0.0199 | [-0.1038, -0.0217] | -3.1541 | 0.004 + gear [4] | 0.8223 | 2.2921 | [-3.9084, 5.5530] | 0.3587 | 0.723 + gear [5] | 5.1839 | 2.6751 | [-0.3373, 10.7051] | 1.9378 | 0.064 + vs | 1.9583 | 2.0920 | [-2.3593, 6.2759] | 0.9361 | 0.359 + cyl [6] | -2.3057 | 2.1418 | [-6.7262, 2.1148] | -1.0765 | 0.292 + cyl [8] | 0.9279 | 4.3980 | [-8.1490, 10.0049] | 0.2110 | 0.835 + drat | 2.3430 | 1.9741 | [-1.7313, 6.4172] | 1.1869 | 0.247 + Message + + Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed + using a Wald t-distribution approximation. + +--- + + Code + model_parameters(model, digits = 4, ci_digits = 1) + Output + Parameter | Coefficient | SE | 95% CI | t(24) | p + ------------------------------------------------------------------- + (Intercept) | 18.9880 | 7.4728 | [ 3.6, 34.4] | 2.5409 | 0.018 + hp | -0.0627 | 0.0199 | [-0.1, 0.0] | -3.1541 | 0.004 + gear [4] | 0.8223 | 2.2921 | [-3.9, 5.6] | 0.3587 | 0.723 + gear [5] | 5.1839 | 2.6751 | [-0.3, 10.7] | 1.9378 | 0.064 + vs | 1.9583 | 2.0920 | [-2.4, 6.3] | 0.9361 | 0.359 + cyl [6] | -2.3057 | 2.1418 | [-6.7, 2.1] | -1.0765 | 0.292 + cyl [8] | 0.9279 | 4.3980 | [-8.1, 10.0] | 0.2110 | 0.835 + drat | 2.3430 | 1.9741 | [-1.7, 6.4] | 1.1869 | 0.247 + Message + + Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed + using a Wald t-distribution approximation. + +--- + + Code + print(out, digits = 4) + Output + Parameter | Coefficient | SE | 95% CI | t(24) | p + ------------------------------------------------------------------------- + (Intercept) | 18.9880 | 7.4728 | [ 3.5648, 34.4112] | 2.5409 | 0.018 + hp | -0.0627 | 0.0199 | [-0.1038, -0.0217] | -3.1541 | 0.004 + gear [4] | 0.8223 | 2.2921 | [-3.9084, 5.5530] | 0.3587 | 0.723 + gear [5] | 5.1839 | 2.6751 | [-0.3373, 10.7051] | 1.9378 | 0.064 + vs | 1.9583 | 2.0920 | [-2.3593, 6.2759] | 0.9361 | 0.359 + cyl [6] | -2.3057 | 2.1418 | [-6.7262, 2.1148] | -1.0765 | 0.292 + cyl [8] | 0.9279 | 4.3980 | [-8.1490, 10.0049] | 0.2110 | 0.835 + drat | 2.3430 | 1.9741 | [-1.7313, 6.4172] | 1.1869 | 0.247 + Message + + Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed + using a Wald t-distribution approximation. + +--- + + Code + print(out, digits = 4, ci_digits = 1) + Output + Parameter | Coefficient | SE | 95% CI | t(24) | p + ------------------------------------------------------------------- + (Intercept) | 18.9880 | 7.4728 | [ 3.6, 34.4] | 2.5409 | 0.018 + hp | -0.0627 | 0.0199 | [-0.1, 0.0] | -3.1541 | 0.004 + gear [4] | 0.8223 | 2.2921 | [-3.9, 5.6] | 0.3587 | 0.723 + gear [5] | 5.1839 | 2.6751 | [-0.3, 10.7] | 1.9378 | 0.064 + vs | 1.9583 | 2.0920 | [-2.4, 6.3] | 0.9361 | 0.359 + cyl [6] | -2.3057 | 2.1418 | [-6.7, 2.1] | -1.0765 | 0.292 + cyl [8] | 0.9279 | 4.3980 | [-8.1, 10.0] | 0.2110 | 0.835 + drat | 2.3430 | 1.9741 | [-1.7, 6.4] | 1.1869 | 0.247 + Message + + Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed + using a Wald t-distribution approximation. + # select pattern Code diff --git a/tests/testthat/test-marginaleffects.R b/tests/testthat/test-marginaleffects.R index 1ad0663eb..2efbbe077 100644 --- a/tests/testthat/test-marginaleffects.R +++ b/tests/testthat/test-marginaleffects.R @@ -5,7 +5,14 @@ test_that("marginaleffects()", { # Frequentist x <- lm(Sepal.Width ~ Species * Petal.Length, data = iris) model <- marginaleffects::slopes(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length") - expect_equal(nrow(parameters(model)), 1) + out <- parameters(model) + expect_identical(nrow(out), 1L) + expect_named(out, c( + "Parameter", "Comparison", "Coefficient", "SE", "Statistic", + "p", "CI", "CI_low", "CI_high" + )) + out <- model_parameters(model, exponentiate = TRUE) + expect_equal(out$Coefficient, 1.394, tolerance = 1e-3) # Bayesian x <- suppressWarnings( @@ -18,14 +25,21 @@ test_that("marginaleffects()", { ) ) model <- marginaleffects::slopes(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length") - expect_equal(nrow(parameters(model)), 1) + expect_identical(nrow(parameters(model)), 1L) }) test_that("predictions()", { x <- lm(Sepal.Width ~ Species * Petal.Length, data = iris) p <- marginaleffects::avg_predictions(x, by = "Species") - expect_equal(nrow(parameters(p)), 3) + out <- parameters(p) + expect_identical(nrow(out), 3L) + expect_named(out, c( + "Predicted", "SE", "CI", "CI_low", "CI_high", "Statistic", + "p", "Species", "s.value" + )) + out <- parameters(p, exponentiate = TRUE) + expect_equal(out$Predicted, c(30.81495, 15.95863, 19.57004), tolerance = 1e-4) }) @@ -33,7 +47,9 @@ test_that("comparisons()", { # Frequentist x <- lm(Sepal.Width ~ Species * Petal.Length, data = iris) m <- marginaleffects::comparisons(x, newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length") - expect_equal(nrow(parameters(m)), 1) + expect_identical(nrow(parameters(m)), 1L) + out <- parameters(m, exponentiate = TRUE) + expect_equal(out$Coefficient, 1.393999, tolerance = 1e-4) # Bayesian x <- suppressWarnings( @@ -50,7 +66,7 @@ test_that("comparisons()", { newdata = insight::get_datagrid(x, at = "Species"), variables = "Petal.Length" ) - expect_equal(nrow(parameters(m)), 1) + expect_identical(nrow(parameters(m)), 1L) }) @@ -60,14 +76,14 @@ test_that("marginalmeans()", { dat$gear <- factor(dat$gear) x <- lm(mpg ~ cyl + gear, data = dat) m <- marginaleffects::marginalmeans(x) - expect_equal(nrow(parameters(m)), 6) + expect_identical(nrow(parameters(m)), 6L) }) test_that("hypotheses()", { x <- lm(mpg ~ hp + wt, data = mtcars) m <- marginaleffects::hypotheses(x, "hp = wt") - expect_equal(nrow(parameters(m)), 1) + expect_identical(nrow(parameters(m)), 1L) }) diff --git a/tests/testthat/test-model_parameters.cgam.R b/tests/testthat/test-model_parameters.cgam.R index 7d02fcf3f..9b4828683 100644 --- a/tests/testthat/test-model_parameters.cgam.R +++ b/tests/testthat/test-model_parameters.cgam.R @@ -56,7 +56,6 @@ test_that("model_parameters - cgam", { coefficient_name = "Coefficient", zi_coefficient_name = "Log-Odds", digits = 2, - ci_digits = 2, p_digits = 3, footer_digits = 3, class = c("parameters_model", "see_parameters_model", "data.frame"), @@ -152,7 +151,6 @@ test_that("model_parameters - cgamm", { coefficient_name = "Coefficient", zi_coefficient_name = "Log-Odds", digits = 2, - ci_digits = 2, p_digits = 3, footer_digits = 3, class = c("parameters_model", "see_parameters_model", "data.frame"), object_name = "ans" diff --git a/tests/testthat/test-printing.R b/tests/testthat/test-printing.R index 9336f9b28..ce3891fa1 100644 --- a/tests/testthat/test-printing.R +++ b/tests/testthat/test-printing.R @@ -38,24 +38,37 @@ withr::with_options( out <- model_parameters(model, drop = "^\\(Intercept") expect_snapshot( print(out, groups = list( - "Engine" = c("cyl6", "cyl8", "vs", "hp"), - "Interactions" = c("gear4:vs", "gear5:vs"), - "Controls" = c(2, 3, 7) + Engine = c("cyl6", "cyl8", "vs", "hp"), + Interactions = c("gear4:vs", "gear5:vs"), + Controls = c(2, 3, 7) )) ) expect_snapshot( print(out, sep = " ", groups = list( - "Engine" = c("cyl6", "cyl8", "vs", "hp"), - "Interactions" = c("gear4:vs", "gear5:vs"), - "Controls" = c(2, 3, 7) + Engine = c("cyl6", "cyl8", "vs", "hp"), + Interactions = c("gear4:vs", "gear5:vs"), + Controls = c(2, 3, 7) ) ) ) }) + # Digits ------ + test_that("digits and ci_digits", { + mtcars$cyl <- as.factor(mtcars$cyl) + mtcars$gear <- as.factor(mtcars$gear) + model <- lm(mpg ~ hp + gear + vs + cyl + drat, data = mtcars) + expect_snapshot(model_parameters(model, digits = 4)) + expect_snapshot(model_parameters(model, digits = 4, ci_digits = 1)) + out <- model_parameters(model) + expect_snapshot(print(out, digits = 4)) + expect_snapshot(print(out, digits = 4, ci_digits = 1)) + }) + + # Table templates ------ test_that("select pattern", { mtcars$cyl <- as.factor(mtcars$cyl) @@ -66,27 +79,27 @@ withr::with_options( out <- model_parameters(model, drop = "^\\(Intercept") expect_snapshot( print(out, groups = list( - "Engine" = c("cyl6", "cyl8", "vs", "hp"), - "Interactions" = c("gear4:vs", "gear5:vs"), - "Controls" = c(2, 3, 7) + Engine = c("cyl6", "cyl8", "vs", "hp"), + Interactions = c("gear4:vs", "gear5:vs"), + Controls = c(2, 3, 7) )) ) expect_snapshot(print(out, select = "{coef} ({se})")) expect_snapshot(print(out, select = "{coef}{stars}|[{ci}]")) expect_snapshot( print(out, groups = list( - "Engine" = c("cyl6", "cyl8", "vs", "hp"), - "Interactions" = c("gear4:vs", "gear5:vs"), - "Controls" = c(2, 3, 7) + Engine = c("cyl6", "cyl8", "vs", "hp"), + Interactions = c("gear4:vs", "gear5:vs"), + Controls = c(2, 3, 7) ), select = "{coef}{stars}|[{ci}]") ) expect_snapshot( print(out, sep = " ", groups = list( - "Engine" = c("cyl6", "cyl8", "vs", "hp"), - "Interactions" = c("gear4:vs", "gear5:vs"), - "Controls" = c(2, 3, 7) + Engine = c("cyl6", "cyl8", "vs", "hp"), + Interactions = c("gear4:vs", "gear5:vs"), + Controls = c(2, 3, 7) ), select = "{coef}{stars}|[{ci}]" )