Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/rcmdcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
TESTTHAT_CPUS: 4

steps:
- uses: actions/checkout@v7
Expand Down
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Imports:
parallel,
matrixStats
Suggests:
testthat (>= 2.1.0),
testthat (>= 3.0.0),
caret (>= 6.0-84),
gbm (>= 2.1.8),
randomForest (>= 4.6.14),
Expand All @@ -58,6 +58,9 @@ Encoding: UTF-8
LazyData: false
URL: https://mc-stan.org/posterior/, https://discourse.mc-stan.org/
BugReports: https://github.com/stan-dev/posterior/issues
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/testthat/start-first: summarise_draws-multicore-many-variables, summarise_draws-multicore-two-variables, summarise_draws-multicore-external-function, summarise_draws-multicore-one-variable, rstar
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Config/roxygen2/version: 8.1.0
23 changes: 13 additions & 10 deletions tests/testthat/test-as_draws.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ test_that("transformations to and from draws_df objects work", {
expect_equal(draws_df, draws_df2)

# test that a single draw does not lead to a drop of dimensions
old_option <- getOption("posterior.warn_on_merge_chains")
on.exit(options(posterior.warn_on_merge_chains = old_option), add = TRUE)
options(posterior.warn_on_merge_chains = FALSE)
draws_rvars2 <- subset_draws(draws_rvars, draw = 1)
draws_df2 <- as_draws_df(draws_rvars2)
expect_equal(subset_draws(draws_df, draw = 1), draws_df2)
Expand Down Expand Up @@ -109,7 +112,7 @@ test_that("matrices can be transformed to draws_matrix objects", {
dimnames(x) <- list(NULL, paste0("theta", 1:5))

y <- as_draws(x)
expect_is(y, "draws_matrix")
expect_s3_class(y, "draws_matrix")
expect_equal(variables(y), colnames(x))
expect_equal(niterations(y), 40)
expect_equal(nchains(y), 1)
Expand All @@ -121,7 +124,7 @@ test_that("arrays can be transformed to draws_array objects", {
dimnames(x) <- list(NULL, NULL, paste0("theta", 1:5))

y <- as_draws(x)
expect_is(y, "draws_array")
expect_s3_class(y, "draws_array")
expect_equal(variables(y), dimnames(x)[[3]])
expect_equal(niterations(y), 10)
expect_equal(nchains(y), 4)
Expand All @@ -135,15 +138,15 @@ test_that("lists of matrices can be transformed to draws_array objects", {
# one chain
z1 <- list(x)
y <- as_draws(z1)
expect_is(y, "draws_array")
expect_s3_class(y, "draws_array")
expect_equal(variables(y), colnames(z1[[1]]))
expect_equal(niterations(y), 50)
expect_equal(nchains(y), 1)

# multiple chains
z3 <- list(x, x, x)
y <- as_draws(z3)
expect_is(y, "draws_array")
expect_s3_class(y, "draws_array")
expect_equal(variables(y), colnames(z3[[1]]))
expect_equal(niterations(y), 50)
expect_equal(nchains(y), 3)
Expand All @@ -155,7 +158,7 @@ test_that("data.frames can be transformed to draws_df objects", {
v2 = rnorm(100)
)
y <- as_draws(x)
expect_is(y, "draws_df")
expect_s3_class(y, "draws_df")
expect_equal(variables(y), names(x))
expect_equal(niterations(y), 100)
expect_equal(nchains(y), 1)
Expand All @@ -165,7 +168,7 @@ test_that("data.frames can be transformed to draws_df objects", {
x2$.iteration <- 1:100
x2$.chain <- rep(1:4, each = 25)
y2 <- as_draws(x2)
expect_is(y2, "draws_df")
expect_s3_class(y2, "draws_df")
expect_equal(variables(y2), names(x))
expect_equal(niterations(y2), 25)
expect_equal(nchains(y2), 4)
Expand All @@ -183,7 +186,7 @@ test_that("lists can be transformed to draws_list objects", {
)
)
y <- as_draws(x)
expect_is(y, "draws_list")
expect_s3_class(y, "draws_list")
expect_equal(variables(y), names(x[[1]]))
expect_equal(ndraws(y), 100)
expect_equal(nchains(y), 2)
Expand All @@ -192,7 +195,7 @@ test_that("lists can be transformed to draws_list objects", {
test_that("numeric vectors can be transformed to draws_matrix objects", {
draws_matrix <- draws_matrix(a = 1:10, b = 11:20, c = 1)
draws_matrix2 <- as_draws_matrix(cbind(1:10, 11:20, 1))
expect_equivalent(draws_matrix, draws_matrix2)
expect_equal(draws_matrix, draws_matrix2, ignore_attr = "dimnames")
})

test_that("numeric vectors can be transformed to draws_array objects", {
Expand Down Expand Up @@ -486,14 +489,14 @@ test_that("1-length rvars can be cast to other formats", {
# scalars should stay as scalars...
x <- rvar(1:10)
expect_equal(as_draws_array(x), draws_array(x = 1:10))
expect_equal(as_draws_df(x), draws_df(x = 1:10))
expect_equal(as.data.frame(as_draws_df(x)), as.data.frame(draws_df(x = 1:10)))
expect_equal(as_draws_list(x), draws_list(x = 1:10))
expect_equal(as_draws_matrix(x), draws_matrix(x = 1:10))

# ... but extra dims should not be dropped
x <- rvar(array(1:10, dim = c(10, 1, 1)))
expect_equal(as_draws_array(x), draws_array(`x[1,1]` = 1:10))
expect_equal(as_draws_df(x), draws_df(`x[1,1]` = 1:10))
expect_equal(as.data.frame(as_draws_df(x)), as.data.frame(draws_df(`x[1,1]` = 1:10)))
expect_equal(as_draws_list(x), draws_list(`x[1,1]` = 1:10))
expect_equal(as_draws_matrix(x), draws_matrix(`x[1,1]` = 1:10))
})
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-convergence.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ test_that("mcse diagnostics return reasonable values", {

# check if issue #252 remains fixed
se <- mcse_quantile(tau, c(0.999999, 1))
expect_equivalent(se[1], se[2])
expect_equal(se[1], se[2], ignore_attr = "names")

se <- mcse_quantile(tau, c(0.000001, 0))
expect_equivalent(se[1], se[2])
expect_equal(se[1], se[2], ignore_attr = "names")
})

test_that("convergence diagnostics accept vectors as input", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-extract_variable_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ test_that("extract_variable_matrix default method works", {
# it should convert matrix to draws object
x <- matrix(1:20, nrow = 10, ncol = 2)
colnames(x) <- c("A", "B")
expect_equivalent(extract_variable_matrix(x, "A"), x[, 1, drop=FALSE])
expect_equivalent(extract_variable_matrix(x, "B"), x[, 2, drop=FALSE])
expect_equal(extract_variable_matrix(x, "A"), x[, 1, drop=FALSE], ignore_attr = "dimnames")
expect_equal(extract_variable_matrix(x, "B"), x[, 2, drop=FALSE], ignore_attr = "dimnames")
})
9 changes: 4 additions & 5 deletions tests/testthat/test-mutate_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,24 @@ test_that("mutate_variables works correctly for draws_list objects", {
test_that("mutate_variables works correctly for draws_matrix objects", {
x <- as_draws_matrix(example_draws())
x <- mutate_variables(x, tau2 = tau^2)
expect_equal(x[, "tau2"], x[, "tau"]^2, check.attributes = FALSE)
expect_equal(x[, "tau2"], x[, "tau"]^2, ignore_attr = "dimnames")
})

test_that("mutate_variables works correctly for draws_array objects", {
x <- as_draws_array(example_draws())
x <- mutate_variables(x, tau2 = tau^2)
expect_equal(x[, , "tau2"], x[, , "tau"]^2, check.attributes = FALSE)
expect_equal(x[, , "tau2"], x[, , "tau"]^2, ignore_attr = "dimnames")
})

test_that("mutate_variables works correctly for draws_rvars objects", {
x <- as_draws_rvars(example_draws())
x <- mutate_variables(x, tau_sq = tau^2, theta_2_sq = theta[[2]]^2)
expect_equal(x$tau_sq, x$tau^2, check.attributes = FALSE)
expect_equal(x$theta_2_sq, x$theta[[2]]^2, check.attributes = FALSE)
expect_equal(x$tau_sq, x$tau^2, ignore_attr = "cache")
expect_equal(x$theta_2_sq, x$theta[[2]]^2, ignore_attr = "cache")
})

test_that("mutate_variables drops variables on assigning NULL", {
x <- example_draws()
x <- mutate_variables(x, mu = NULL, tau = NULL)
expect_equal(variables(x), paste0("theta[", 1:8, "]"))
})

36 changes: 17 additions & 19 deletions tests/testthat/test-pareto_smooth.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("pareto_khat handles both tails being constant correctly", {

# left and right tails are constant, so khat should be NA in all cases
x <- c(rep(-100, 10), sort(rnorm(100)), rep(100, 10))

expect_true(is.na(pareto_khat(x, tail = "left", ndraws_tail = 10)))
expect_true(is.na(pareto_khat(x, tail = "right", ndraws_tail = 10)))
expect_true(is.na(pareto_khat(x, tail = "both", ndraws_tail = 10)))
Expand Down Expand Up @@ -64,13 +64,13 @@ test_that("pareto_khat handles ndraws_tail argument", {

expect_warning(pareto_khat(tau, tail = "both", ndraws_tail = 4),
"Number of tail draws cannot be less than 5. Changing to 5.")

expect_warning(pareto_khat(tau, tail = "right", ndraws_tail = 4),
"Number of tail draws cannot be less than 5. Changing to 5.")

expect_warning(pareto_khat(tau, tail = "left", ndraws_tail = 4),
"Number of tail draws cannot be less than 5. Changing to 5.")

expect_warning(pareto_khat(tau, tail = "right", ndraws_tail = length(tau)),
"Tail draws (400) not strictly less than total draws (400). Fitting of generalized Pareto distribution not performed.",
fixed = TRUE)
Expand Down Expand Up @@ -173,11 +173,10 @@ test_that("pareto_khat functions work with matrix with chains", {
expect_equal(pareto_khat(tau_chains, ndraws_tail = 20),
pareto_khat(tau_nochains, ndraws_tail = 20))

ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE)
ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE)
ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE)
ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE)

expect_equal(as.numeric(ps_chains$x), as.numeric(ps_nochains$x))

})

test_that("pareto_khat functions work with rvars with and without chains", {
Expand All @@ -201,22 +200,21 @@ test_that("pareto_khat functions work with rvars with and without chains", {
expect_equal(pareto_diags(tau_rvar_chains, ndraws_tail = 20),
pareto_diags(tau_rvar_nochains, ndraws_tail = 20))

ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE)
ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, return_k = TRUE)
ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE)
ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE)

ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE)
ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, return_k = TRUE)
ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE)
ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, return_k = TRUE, verbose = FALSE)

expect_equal(ps_rvar_chains$x, rvar(ps_chains$x, with_chains = TRUE))

expect_equal(ps_rvar_nochains$x, rvar(ps_nochains$x))

ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE)
ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE)

ps_chains <- pareto_smooth(tau_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE)
ps_rvar_chains <- pareto_smooth(tau_rvar_chains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE)

ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE)
ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE)
ps_nochains <- pareto_smooth(tau_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE)
ps_rvar_nochains <- pareto_smooth(tau_rvar_nochains, ndraws_tail = 20, extra_diags = TRUE, return_k = TRUE, verbose = FALSE)

expect_equal(ps_rvar_chains$x, rvar(ps_chains$x, with_chains = TRUE))

Expand All @@ -227,11 +225,11 @@ test_that("pareto_khat functions work with rvars with and without chains", {
test_that("pareto_smooth returns x with smoothed tail(s)", {
mu <- extract_variable_matrix(example_draws(), "mu")

mu_smoothed_right <- pareto_smooth(mu, ndraws_tail = 10, tail = "right", return_k = TRUE)$x
mu_smoothed_right <- pareto_smooth(mu, ndraws_tail = 10, tail = "right", return_k = TRUE, verbose = FALSE)$x

mu_smoothed_left <- pareto_smooth(mu, ndraws_tail = 10, tail = "left", return_k = TRUE)$x
mu_smoothed_left <- pareto_smooth(mu, ndraws_tail = 10, tail = "left", return_k = TRUE, verbose = FALSE)$x

mu_smoothed_both <- pareto_smooth(mu, ndraws_tail = 10, tail = "both", return_k = TRUE)$x
mu_smoothed_both <- pareto_smooth(mu, ndraws_tail = 10, tail = "both", return_k = TRUE, verbose = FALSE)$x

expect_equal(sort(mu)[1:390], sort(mu_smoothed_right)[1:390])
expect_equal(sort(mu_smoothed_both)[11:400], sort(mu_smoothed_right)[11:400])
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-repair_draws.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ test_that("repair_draws works correctly on draws_matrix objects", {
x <- x[10:6, ]

x_rep <- repair_draws(x, order = TRUE)
expect_equal(x[5:1, ], x_rep, check.attributes = FALSE)
expect_equal(x[5:1, ], x_rep, ignore_attr = "dimnames")
expect_equal(rownames(x_rep), as.character(1:5))

x_rep <- repair_draws(x, order = FALSE)
expect_equal(x, x_rep, check.attributes = FALSE)
expect_equal(x, x_rep, ignore_attr = "dimnames")
expect_equal(rownames(x_rep), as.character(1:5))
})

Expand All @@ -16,12 +16,12 @@ test_that("repair_draws works correctly on draws_array objects", {
x <- x[10:6, c(4, 1), ]

x_rep <- repair_draws(x, order = TRUE)
expect_equal(x[5:1, 2:1, ], x_rep, check.attributes = FALSE)
expect_equal(x[5:1, 2:1, ], x_rep, ignore_attr = "dimnames")
expect_equal(rownames(x_rep), as.character(1:5))
expect_equal(colnames(x_rep), as.character(1:2))

x_rep <- repair_draws(x, order = FALSE)
expect_equal(x, x_rep, check.attributes = FALSE)
expect_equal(x, x_rep, ignore_attr = "dimnames")
expect_equal(rownames(x_rep), as.character(1:5))
expect_equal(colnames(x_rep), as.character(1:2))
})
Expand All @@ -37,7 +37,7 @@ test_that("repair_draws works correctly on draws_df objects", {
expect_equal(x_rep$.chain, 1:4)

x_rep <- repair_draws(x, order = FALSE)
expect_equal(x$mu, x_rep$mu, check.attributes = FALSE)
expect_equal(x$mu, x_rep$mu, ignore_attr = "cache")
expect_equal(x_rep$.iteration, rep(1, 4))
expect_equal(x_rep$.chain, 4:1)
})
Expand All @@ -47,11 +47,11 @@ test_that("repair_draws works correctly on draws_list objects", {
x <- x[c(4, 2)]

x_rep <- repair_draws(x, order = TRUE)
expect_equal(x[2], x_rep[1], check.attributes = FALSE)
expect_equal(x[2], x_rep[1], ignore_attr = "names")
expect_equal(names(x_rep), as.character(1:2))

x_rep <- repair_draws(x, order = FALSE)
expect_equal(x, x_rep, check.attributes = FALSE)
expect_equal(x, x_rep, ignore_attr = "names")
expect_equal(names(x_rep), as.character(1:2))
})

Expand All @@ -65,25 +65,25 @@ test_that("repair_draws works correctly on draws_rvars objects", {
expect_equal(
draws_of(x_rep$mu),
draws_of(x$mu)[order(as.integer(rownames(draws_of(x$mu)))),],
check.attributes = FALSE
ignore_attr = c("dim", "dimnames", "names")
)
expect_equal(niterations(x_rep), 4)
expect_equal(nchains(x_rep), 1)

x_rep <- repair_draws(x, order = FALSE)
expect_equal(draws_of(x$mu), draws_of(x_rep$mu), check.attributes = FALSE)
expect_equal(draws_of(x$mu), draws_of(x_rep$mu), ignore_attr = "dimnames")
expect_equal(niterations(x_rep), 4)
expect_equal(nchains(x_rep), 1)

x_ord <- order_draws(x)
expect_equal(
draws_of(x_ord$mu),
draws_of(x$mu)[order(as.integer(rownames(draws_of(x$mu)))),],
check.attributes = FALSE
ignore_attr = c("dim", "dimnames", "names")
)
expect_equal(
draws_of(x_ord$theta),
draws_of(x$theta)[order(as.integer(rownames(draws_of(x$theta)))),],
check.attributes = FALSE
ignore_attr = "cache"
)
})
13 changes: 13 additions & 0 deletions tests/testthat/test-resample_draws.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
test_that("resample_draws returns expected format", {

# Store and restore option to ensure test isolation
old_option <- getOption("posterior.warn_on_merge_chains")
on.exit(options(posterior.warn_on_merge_chains = old_option))

options(posterior.warn_on_merge_chains = FALSE)

x <- example_draws()
w <- runif(ndraws(x), 0, 10)

Expand Down Expand Up @@ -54,6 +61,12 @@ test_that("Resampling algorithms return the correct result in expectation", {
})

test_that("resample_draws uses stored weights when available", {
# Store and restore option to ensure test isolation
old_option <- getOption("posterior.warn_on_merge_chains")
on.exit(options(posterior.warn_on_merge_chains = old_option))

options(posterior.warn_on_merge_chains = FALSE)

x <- example_draws()

x_rs <- resample_draws(x)
Expand Down
Loading
Loading