Skip to content

Commit

Permalink
test of dev of bayestestR (#1016)
Browse files Browse the repository at this point in the history
* test of dev of bayestestR

* fix?

* test

* fix

* fix
  • Loading branch information
strengejacke authored Sep 17, 2024
1 parent 29bc324 commit 86597e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
12 changes: 10 additions & 2 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,16 @@ p_significance.lm <- function(x, threshold = "default", ci = 0.95, verbose = TRU
out <- result$out
posterior <- result$posterior

# calculate the ROPE range
if (all(threshold == "default")) {
# calculate the ROPE range - for multiple thresholds, we have to check
# each list element for "default", to replace it with the appropriate range
if (is.list(threshold)) {
threshold <- lapply(threshold, function(i) {
if (all(i == "default")) {
i <- bayestestR::rope_range(x, verbose = verbose)
}
i
})
} else if (all(threshold == "default")) {
threshold <- bayestestR::rope_range(x, verbose = verbose)
}

Expand Down
53 changes: 29 additions & 24 deletions tests/testthat/test-p_significance.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
skip_on_cran()
skip_if_not_installed("bayestestR", minimum_version = "0.14.1")
skip_if_not_installed("bayestestR", minimum_version = "0.14.0.10")
skip_if_not_installed("distributional")
skip_if_not_installed("withr")

test_that("p_significance", {
data(mtcars)
m <- lm(mpg ~ gear + wt + cyl + hp, data = mtcars)
set.seed(123)
x <- p_significance(m)
expect_identical(c(nrow(x), ncol(x)), c(5L, 5L))
expect_named(x, c("Parameter", "CI", "CI_low", "CI_high", "ps"))
expect_snapshot(print(x))
withr::with_environment(
new.env(),
test_that("p_significance", {
data(mtcars)
m <<- lm(mpg ~ gear + wt + cyl + hp, data = mtcars)
set.seed(123)
x <- p_significance(m)
expect_identical(c(nrow(x), ncol(x)), c(5L, 5L))
expect_named(x, c("Parameter", "CI", "CI_low", "CI_high", "ps"))
expect_snapshot(print(x))

mp <- model_parameters(m)
set.seed(123)
x2 <- p_significance(mp)
expect_equal(x$ps, x2$ps, tolerance = 1e-4)
mp <- model_parameters(m)
set.seed(123)
x2 <- p_significance(mp)
expect_equal(x$ps, x2$ps, tolerance = 1e-4)

set.seed(123)
x <- p_significance(m, ci = 0.8)
expect_equal(x$ps, c(1, 0.3983, 0.9959, 0.6188, 0), tolerance = 1e-3)
set.seed(123)
x <- p_significance(m, ci = 0.8)
expect_equal(x$ps, c(1, 0.3983, 0.9959, 0.6188, 0), tolerance = 1e-3)

set.seed(123)
x <- p_significance(m, threshold = 0.5)
expect_equal(x$ps, c(1, 0.4393, 0.9969, 0.6803, 0), tolerance = 1e-4)
set.seed(123)
x <- p_significance(m, threshold = 0.5)
expect_equal(x$ps, c(1, 0.4393, 0.9969, 0.6803, 0), tolerance = 1e-4)

set.seed(123)
# Test p_significance with custom thresholds for specific parameters
x <- p_significance(m, threshold = list(cyl = 0.5, wt = 0.7))
expect_equal(x$ps, c(1, 0.6002, 0.995, 0.6805, 0), tolerance = 1e-4)
})
set.seed(123)
# Test p_significance with custom thresholds for specific parameters
x <- p_significance(m, threshold = list(cyl = 0.5, wt = 0.7))
expect_equal(x$ps, c(1, 0.5982, 0.9955, 0.6803, 1e-04), tolerance = 1e-4)
})
)

test_that("p_significance, glmmTMB", {
skip_if_not_installed("glmmTMB")
Expand Down

0 comments on commit 86597e2

Please sign in to comment.