From 22566e07f97920ce1e2a3aba00b12d1f0f35e844 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 16 Sep 2024 21:24:58 +0200 Subject: [PATCH] add test --- R/p_rope.R | 2 +- R/p_significance.R | 14 ++++++++++---- man/p_significance.Rd | 4 ++-- tests/testthat/test-p_significance.R | 13 ++++++++++++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/R/p_rope.R b/R/p_rope.R index 3a8ae940f..d3531186e 100644 --- a/R/p_rope.R +++ b/R/p_rope.R @@ -229,7 +229,7 @@ p_rope.mcmc.list <- p_rope.mcmc #' @keywords internal .p_rope <- function(rope_rez) { cols <- c("Parameter", "ROPE_low", "ROPE_high", "ROPE_Percentage", "Effects", "Component") - out <- as.data.frame(rope_rez[cols[cols %in% names(rope_rez)]]) + out <- as.data.frame(rope_rez)[cols[cols %in% names(rope_rez)]] names(out)[names(out) == "ROPE_Percentage"] <- "p_ROPE" class(out) <- c("p_rope", "see_p_rope", "data.frame") diff --git a/R/p_significance.R b/R/p_significance.R index e9027d420..c00917612 100644 --- a/R/p_significance.R +++ b/R/p_significance.R @@ -54,8 +54,8 @@ #' chains = 2, refresh = 0 #' ) #' p_significance(model) -#' # multiple thresholds -#' p_significance(model, threshold = list(c(-10, 5), c(-0.2, 0.2), "default")) +#' # multiple thresholds - asymmetric, symmetric, default +#' p_significance(model, threshold = list(c(-10, 5), 0.2, "default")) #' } #' @export p_significance <- function(x, ...) { @@ -142,7 +142,7 @@ p_significance.data.frame <- function(x, threshold = "default", rvar_col = NULL, } # check if list of values contains only valid values checks <- vapply(threshold, function(r) { - !all(r == "default") || !all(is.numeric(r)) || length(r) != 2 + !all(r == "default") || !all(is.numeric(r)) || length(r) > 2 }, logical(1)) if (!all(checks)) { insight::format_error("`threshold` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).") @@ -391,7 +391,13 @@ as.double.p_significance <- as.numeric.p_significance #' @keywords internal .select_threshold_ps <- function(model = NULL, threshold = "default", verbose = TRUE) { if (is.list(threshold)) { - lapply(threshold, .select_threshold_list, model = model, verbose = verbose) + lapply(threshold, function(i) { + out <- .select_threshold_list(model = model, threshold = i, verbose = verbose) + if (length(out) == 1) { + out <- c(-1 * abs(out), abs(out)) + } + out + }) } else { .select_threshold_list(model = model, threshold = threshold, verbose = verbose) } diff --git a/man/p_significance.Rd b/man/p_significance.Rd index 1e4ad290c..78e10864c 100644 --- a/man/p_significance.Rd +++ b/man/p_significance.Rd @@ -132,8 +132,8 @@ model <- rstanarm::stan_glm(mpg ~ wt + cyl, chains = 2, refresh = 0 ) p_significance(model) -# multiple thresholds -p_significance(model, threshold = list(c(-10, 5), c(-0.2, 0.2), "default")) +# multiple thresholds - asymmetric, symmetric, default +p_significance(model, threshold = list(c(-10, 5), 0.2, "default")) } \dontshow{\}) # examplesIf} } diff --git a/tests/testthat/test-p_significance.R b/tests/testthat/test-p_significance.R index 6f542f3c0..cd4ccc95f 100644 --- a/tests/testthat/test-p_significance.R +++ b/tests/testthat/test-p_significance.R @@ -49,7 +49,7 @@ test_that("stanreg", { test_that("brms", { skip_if_offline() - skip_if_not_or_load_if_installed("rstanarm") + skip_if_not_or_load_if_installed("brms") m2 <- insight::download_model("brms_1") @@ -58,4 +58,15 @@ test_that("brms", { c(1.0000, 0.9985, 0.9785), tolerance = 0.01 ) + + expect_equal( + p_significance(m2, threshold = list(1, "default", 2), effects = "all")$ps, + c(1.00000, 0.99850, 0.12275), + tolerance = 0.01 + ) + expect_equal( + attributes(out)$threshold, + list(c(-1, 1), c(-0.60269480520891, 0.60269480520891), c(-2, 2)), + tolerance = 1e-4 + ) })