Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 16, 2024
1 parent ca77ee8 commit 22566e0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion R/p_rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 10 additions & 4 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down Expand Up @@ -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)).")
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions man/p_significance.Rd

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

13 changes: 12 additions & 1 deletion tests/testthat/test-p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
)
})

0 comments on commit 22566e0

Please sign in to comment.