Skip to content

Commit

Permalink
add test for median(Prior)
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc committed Jun 28, 2024
1 parent dc53a1f commit 709161b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/Prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ prior_invgamma <- function(alpha, beta) {
#' @export
median.Prior <- function(x, na.rm, ...) {
vals <- replicate(
n = 250,
n = 500,
initialValues(x),
simplify = FALSE
) |>
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-Prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,30 @@ test_that("Limits work as expected", {
x <- set_limits(x, upper = 0)
expect_error(initialValues(x), regex = "Unable to generate")
})



test_that("median(Prior) works as expected", {
set.seed(2410)

# Unrestricted
p1 <- prior_normal(-200, 400)
expect_equal(
median(p1),
-200,
tolerance = 0.15
)


# Constrained
p2 <- set_limits(p1, lower = 0)

actual <- rnorm(6000, -200, 400) * 0.5 + -200 * 0.5
actual_red <- actual[actual >= 0]

expect_equal(
median(p2),
median(actual_red),
tolerance = 0.15
)
})

0 comments on commit 709161b

Please sign in to comment.