diff --git a/.github/workflows/rcmdcheck.yml b/.github/workflows/rcmdcheck.yml index 6ae98c8b..bd4abc2d 100644 --- a/.github/workflows/rcmdcheck.yml +++ b/.github/workflows/rcmdcheck.yml @@ -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 diff --git a/DESCRIPTION b/DESCRIPTION index 7e057d45..f9898b96 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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), @@ -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 diff --git a/tests/testthat/test-as_draws.R b/tests/testthat/test-as_draws.R index b5d55791..ca13c58f 100644 --- a/tests/testthat/test-as_draws.R +++ b/tests/testthat/test-as_draws.R @@ -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) @@ -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) @@ -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) @@ -135,7 +138,7 @@ 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) @@ -143,7 +146,7 @@ test_that("lists of matrices can be transformed to draws_array objects", { # 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) @@ -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) @@ -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) @@ -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) @@ -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", { @@ -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)) }) diff --git a/tests/testthat/test-convergence.R b/tests/testthat/test-convergence.R index e6d806c8..f484c2b2 100644 --- a/tests/testthat/test-convergence.R +++ b/tests/testthat/test-convergence.R @@ -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", { diff --git a/tests/testthat/test-extract_variable_matrix.R b/tests/testthat/test-extract_variable_matrix.R index be7f6bbe..9a075322 100644 --- a/tests/testthat/test-extract_variable_matrix.R +++ b/tests/testthat/test-extract_variable_matrix.R @@ -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") }) diff --git a/tests/testthat/test-mutate_variables.R b/tests/testthat/test-mutate_variables.R index a6a57997..dcb2c5b1 100644 --- a/tests/testthat/test-mutate_variables.R +++ b/tests/testthat/test-mutate_variables.R @@ -46,20 +46,20 @@ 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", { @@ -67,4 +67,3 @@ test_that("mutate_variables drops variables on assigning NULL", { x <- mutate_variables(x, mu = NULL, tau = NULL) expect_equal(variables(x), paste0("theta[", 1:8, "]")) }) - diff --git a/tests/testthat/test-pareto_smooth.R b/tests/testthat/test-pareto_smooth.R index 78cb3500..5a42f8a2 100644 --- a/tests/testthat/test-pareto_smooth.R +++ b/tests/testthat/test-pareto_smooth.R @@ -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))) @@ -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) @@ -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", { @@ -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)) @@ -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]) diff --git a/tests/testthat/test-repair_draws.R b/tests/testthat/test-repair_draws.R index d35046e1..d2367aa8 100644 --- a/tests/testthat/test-repair_draws.R +++ b/tests/testthat/test-repair_draws.R @@ -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)) }) @@ -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)) }) @@ -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) }) @@ -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)) }) @@ -65,13 +65,13 @@ 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) @@ -79,11 +79,11 @@ test_that("repair_draws works correctly on draws_rvars objects", { 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" ) }) diff --git a/tests/testthat/test-resample_draws.R b/tests/testthat/test-resample_draws.R index bea49809..553555cb 100644 --- a/tests/testthat/test-resample_draws.R +++ b/tests/testthat/test-resample_draws.R @@ -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) @@ -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) diff --git a/tests/testthat/test-rvar-.R b/tests/testthat/test-rvar-.R index 2d674b41..20ac27a6 100755 --- a/tests/testthat/test-rvar-.R +++ b/tests/testthat/test-rvar-.R @@ -159,13 +159,15 @@ test_that("rvars work in tibbles", { df = tibble::tibble(x, y = x + 1) expect_equal(df$x, x) - expect_equal(df$y, rvar(x_array + 1)) + expect_equal(df$y, rvar(x_array + 1), ignore_attr = "cache") expect_equal(dplyr::mutate(df, z = x)$z, x) - expect_equal(dplyr::mutate(df, z = x * 2)$z, rvar(x_array * 2)) + expect_equal(dplyr::mutate(df, z = x * 2)$z, rvar(x_array * 2), + ignore_attr = "cache") expect_equal( dplyr::mutate(dplyr::group_by(df, 1:4), z = x * 2)$z, - rvar(x_array * 2) + rvar(x_array * 2), + ignore_attr = "cache" ) df = tibble::tibble(g = letters[1:4], x) @@ -175,7 +177,8 @@ test_that("rvars work in tibbles", { c = rvar(x_array[,3, drop = FALSE]), d = rvar(x_array[,4, drop = FALSE]) ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref, + ignore_attr = "cache") expect_equal(tidyr::pivot_longer(ref, a:d, names_to = "g", values_to = "x"), df) df$y = df$x + 1 @@ -186,13 +189,15 @@ test_that("rvars work in tibbles", { c = c(rvar(NA), NA, df$x[[3]], NA), d = c(rvar(NA), NA, NA, df$x[[4]]), ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2, + ignore_attr = "cache") expect_equal(dplyr::first(df$x), x[1]) z = cbind(a = x, b = x + 1) df$z = z - expect_equal(dplyr::mutate(dplyr::rowwise(df), w = z[,"b"])$w, z[,"b"]) + expect_equal(dplyr::mutate(dplyr::rowwise(df), w = z[,"b"])$w, z[,"b"], + ignore_attr = "cache") }) @@ -240,7 +245,7 @@ test_that("warnings for unequal draws/chains are correct", { # 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 = TRUE) expect_warning( expect_equal(rvar(1:10) + rvar(1:10, nchains = 2), rvar(1:10 + 1:10)), diff --git a/tests/testthat/test-rvar-bind.R b/tests/testthat/test-rvar-bind.R index 6f580ed5..30ebac6b 100755 --- a/tests/testthat/test-rvar-bind.R +++ b/tests/testthat/test-rvar-bind.R @@ -30,7 +30,7 @@ test_that("c works on rvar", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x) + expect_equal(c(x_col), x, ignore_attr = "cache") expect_equal(c(x_col, y), x_y) }) @@ -77,7 +77,7 @@ test_that("c works on rvar_factor", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x) + expect_equal(c(x_col), x, ignore_attr = "cache") expect_equal(c(x_col, y), x_y) }) @@ -139,7 +139,7 @@ test_that("c works on rvar_ordered", { x_col <- x dim(x_col) <- c(3,1) - expect_equal(c(x_col), x) + expect_equal(c(x_col), x, ignore_attr = "cache") expect_equal(c(x_col, y), x_y) }) diff --git a/tests/testthat/test-rvar-cast.R b/tests/testthat/test-rvar-cast.R index 384cb05e..971875cc 100755 --- a/tests/testthat/test-rvar-cast.R +++ b/tests/testthat/test-rvar-cast.R @@ -343,9 +343,9 @@ test_that("proxy restore works", { dimnames = list(A = paste0("a", 1:2), B = paste0("b", 1:2), C = paste0("c", 1:4)) ) - expect_equal(vec_restore(vec_proxy(unname(x1)), rvar()), unname(x1)) - expect_equal(vec_restore(vec_proxy(unname(x2)), rvar()), unname(x2)) - expect_equal(vec_restore(vec_proxy(unname(x3)), rvar()), unname(x3)) + expect_equal(vec_restore(vec_proxy(unname(x1)), rvar()), unname(x1), ignore_attr = "cache") + expect_equal(vec_restore(vec_proxy(unname(x2)), rvar()), unname(x2), ignore_attr = "cache") + expect_equal(vec_restore(vec_proxy(unname(x3)), rvar()), unname(x3), ignore_attr = "cache") expect_equal(vec_restore(vec_proxy(x1), rvar()), x1) expect_equal(vec_restore(vec_proxy(x2), rvar()), x2) expect_equal(vec_restore(vec_proxy(x3), rvar()), x3) diff --git a/tests/testthat/test-rvar-factor.R b/tests/testthat/test-rvar-factor.R index 055e3e13..1eb016fe 100644 --- a/tests/testthat/test-rvar-factor.R +++ b/tests/testthat/test-rvar-factor.R @@ -55,13 +55,15 @@ test_that("rvar_factors work in tibbles", { df = tibble::tibble(x, y = as_rvar_ordered(x)) expect_equal(df$x, x) - expect_equal(df$y, rvar(x_array_ordered)) + expect_equal(df$y, rvar(x_array_ordered), ignore_attr = "cache") expect_equal(dplyr::mutate(df, z = x)$z, x) - expect_equal(dplyr::mutate(df, z = as_rvar_ordered(x))$z, rvar(x_array_ordered)) + expect_equal(dplyr::mutate(df, z = as_rvar_ordered(x))$z, rvar(x_array_ordered), + ignore_attr = "cache") expect_equal( dplyr::group_by(dplyr::mutate(df, z = as_rvar_ordered(x)), 1:4)$z, - rvar(x_array_ordered) + rvar(x_array_ordered), + ignore_attr = "cache" ) df = tibble::tibble(g = letters[1:4], x) @@ -71,7 +73,8 @@ test_that("rvar_factors work in tibbles", { c = rvar(x_array[,3, drop = FALSE]), d = rvar(x_array[,4, drop = FALSE]) ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref, + ignore_attr = "cache") expect_equal(tidyr::pivot_longer(ref, a:d, names_to = "g", values_to = "x"), df) df$y = as_rvar_ordered(df$x) @@ -82,7 +85,8 @@ test_that("rvar_factors work in tibbles", { c = c(rvar(factor(NA)), NA, df$x[[3]], NA), d = c(rvar(factor(NA)), NA, NA, df$x[[4]]), ) - expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2) + expect_equal(tidyr::pivot_wider(df, names_from = g, values_from = x), ref2, + ignore_attr = "cache") }) # rep --------------------------------------------------------------------- diff --git a/tests/testthat/test-rvar-print.R b/tests/testthat/test-rvar-print.R index 32cc66bb..cee717a5 100755 --- a/tests/testthat/test-rvar-print.R +++ b/tests/testthat/test-rvar-print.R @@ -212,7 +212,7 @@ test_that("glimpse on rvar works", { expect_equal( format_glimpse(x_vec), c("3.5 ± 1.9", "9.5 ± 1.9", "15.5 ± 1.9", "21.5 ± 1.9"), - check.attributes = FALSE + ignore_attr = c("dim", "dimnames") ) expect_equal(format_glimpse(x_matrix), "") }) diff --git a/tests/testthat/test-rvar-summaries-within-draws.R b/tests/testthat/test-rvar-summaries-within-draws.R index 50ad4faf..3a26e26e 100755 --- a/tests/testthat/test-rvar-summaries-within-draws.R +++ b/tests/testthat/test-rvar-summaries-within-draws.R @@ -7,12 +7,12 @@ test_that("numeric summary functions work", { x_ord <- rvar_ordered(x_letters, levels = letters) x_fct <- rvar_factor(x_letters, levels = letters) - expect_equal(draws_of(rvar_mean(x)), apply(x_array, 1, mean), check.attributes = FALSE) - expect_equal(draws_of(rvar_median(x)), apply(x_array, 1, median), check.attributes = FALSE) - expect_equal(draws_of(rvar_sum(x)), apply(x_array, 1, sum), check.attributes = FALSE) - expect_equal(draws_of(rvar_prod(x)), apply(x_array, 1, prod), check.attributes = FALSE) - expect_equal(draws_of(rvar_min(x)), apply(x_array, 1, min), check.attributes = FALSE) - expect_equal(draws_of(rvar_max(x)), apply(x_array, 1, max), check.attributes = FALSE) + expect_equal(draws_of(rvar_mean(x)), apply(x_array, 1, mean), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_median(x)), apply(x_array, 1, median), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_sum(x)), apply(x_array, 1, sum), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_prod(x)), apply(x_array, 1, prod), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_min(x)), apply(x_array, 1, min), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_max(x)), apply(x_array, 1, max), ignore_attr = c("dim", "dimnames")) expect_error(rvar_mean(x_ord)) expect_equal(rvar_median(x_ord), rvar_ordered(letters[apply(x_array, 1, median)], levels = letters)) @@ -41,7 +41,7 @@ test_that("numeric summary functions work", { expect_equal( draws_of(rvar_mean(x, na.rm = TRUE)), apply(draws_of(x), 1, function(x) mean(x, na.rm = TRUE)), - check.attributes = FALSE + ignore_attr = c("dim", "dimnames", "names") ) }) @@ -55,10 +55,10 @@ test_that("spread summary functions work", { x_ord <- rvar_ordered(x_letters, levels = letters) x_fct <- rvar_factor(x_letters, levels = letters) - expect_equal(draws_of(rvar_sd(x)), apply(x_array, 1, sd), check.attributes = FALSE) - expect_equal(draws_of(rvar_var(x)), apply(x_array, 1, function(x) var(as.vector(x))), check.attributes = FALSE) - expect_equal(draws_of(rvar_mad(x)), apply(x_array, 1, mad), check.attributes = FALSE) - expect_equal(draws_of(rvar_mad(x, constant = 1)), apply(x_array, 1, mad, constant = 1), check.attributes = FALSE) + expect_equal(draws_of(rvar_sd(x)), apply(x_array, 1, sd), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_var(x)), apply(x_array, 1, function(x) var(as.vector(x))), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_mad(x)), apply(x_array, 1, mad), ignore_attr = c("dim", "dimnames")) + expect_equal(draws_of(rvar_mad(x, constant = 1)), apply(x_array, 1, mad, constant = 1), ignore_attr = c("dim", "dimnames")) expect_error(rvar_sd(x_ord)) expect_error(rvar_var(x_ord)) @@ -78,7 +78,7 @@ test_that("spread summary functions work", { expect_equal( draws_of(rvar_var(x, na.rm = TRUE)), apply(draws_of(x), 1, function(x) var(as.vector(x), na.rm = TRUE)), - check.attributes = FALSE + ignore_attr = c("dim", "dimnames", "names") ) }) @@ -91,7 +91,7 @@ test_that("rvar_range works", { x_letters <- array(letters[1:24], dim = c(4,2,3)) x_ord <- rvar_ordered(x_letters, levels = letters) - expect_equal(draws_of(rvar_range(x)), t(apply(x_array, 1, range)), check.attributes = FALSE) + expect_equal(draws_of(rvar_range(x)), t(apply(x_array, 1, range)), ignore_attr = "dimnames") expect_equal(rvar_range(x_ord), rvar_ordered(array(letters[t(apply(x_array, 1, range))], dim = c(4, 2)), levels = letters)) expect_error(rvar_range(rvar_factor("a"))) @@ -137,8 +137,8 @@ test_that("logical summaries work", { x_array <- array(1:24, dim = c(4,2,3)) x <- new_rvar(x_array) - expect_equal(draws_of(rvar_all(x > 6)), as.matrix(apply(x_array > 6, 1, all)), check.attributes = FALSE) - expect_equal(draws_of(rvar_any(x > 6)), as.matrix(apply(x_array > 6, 1, any)), check.attributes = FALSE) + expect_equal(draws_of(rvar_all(x > 6)), as.matrix(apply(x_array > 6, 1, all)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_any(x > 6)), as.matrix(apply(x_array > 6, 1, any)), ignore_attr = "dimnames") expect_error(rvar_all(rvar("a"))) expect_error(rvar_any(rvar("a"))) @@ -158,18 +158,18 @@ test_that("special value predicates work", { x_ord <- rvar_ordered(x_letters) x_fct <- rvar_factor(x_letters) - expect_equal(draws_of(rvar_is_finite(x)), as.matrix(is.finite(x_array)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_infinite(x)), as.matrix(is.infinite(x_array)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_nan(x)), as.matrix(is.nan(x_array)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_na(x)), as.matrix(is.na(x_array)), check.attributes = FALSE) + expect_equal(draws_of(rvar_is_finite(x)), as.matrix(is.finite(x_array)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_infinite(x)), as.matrix(is.infinite(x_array)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_nan(x)), as.matrix(is.nan(x_array)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_na(x)), as.matrix(is.na(x_array)), ignore_attr = "dimnames") - expect_equal(draws_of(rvar_is_finite(x_ord)), as.matrix(is.finite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_infinite(x_ord)), as.matrix(is.infinite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_nan(x_ord)), as.matrix(is.nan(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_na(x_ord)), as.matrix(is.na(x_letters)), check.attributes = FALSE) + expect_equal(draws_of(rvar_is_finite(x_ord)), as.matrix(is.finite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_infinite(x_ord)), as.matrix(is.infinite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_nan(x_ord)), as.matrix(is.nan(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_na(x_ord)), as.matrix(is.na(x_letters)), ignore_attr = "dimnames") - expect_equal(draws_of(rvar_is_finite(x_fct)), as.matrix(is.finite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_infinite(x_fct)), as.matrix(is.infinite(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_nan(x_fct)), as.matrix(is.nan(x_letters)), check.attributes = FALSE) - expect_equal(draws_of(rvar_is_na(x_fct)), as.matrix(is.na(x_letters)), check.attributes = FALSE) + expect_equal(draws_of(rvar_is_finite(x_fct)), as.matrix(is.finite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_infinite(x_fct)), as.matrix(is.infinite(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_nan(x_fct)), as.matrix(is.nan(x_letters)), ignore_attr = "dimnames") + expect_equal(draws_of(rvar_is_na(x_fct)), as.matrix(is.na(x_letters)), ignore_attr = "dimnames") }) diff --git a/tests/testthat/test-subset_draws.R b/tests/testthat/test-subset_draws.R index fa2d5235..b409fb57 100644 --- a/tests/testthat/test-subset_draws.R +++ b/tests/testthat/test-subset_draws.R @@ -1,14 +1,20 @@ test_that("subset_draws works correctly for draws_matrix objects", { + # 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 <- as_draws_matrix(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10) x_sub2 <- x[c(5:10, 105:110, 205:210, 305:310), c("mu", "tau")] - expect_equal(x_sub, x_sub2, check.attributes = FALSE) + expect_equal(x_sub, x_sub2, ignore_attr = c("dimnames", "nchains")) expect_equal(iteration_ids(x_sub), 1:6) x_sub <- subset_draws(x, draw = c(2, 2, 4, 4), unique = FALSE) expect_equal(niterations(x_sub), 4) - expect_equivalent(x_sub[1, ], x_sub[2, ]) + expect_equal(x_sub[1, ], x_sub[2, ], ignore_attr = "dimnames") x <- weight_draws(x, rep(1, ndraws(x))) x_sub <- subset_draws(x, variable = "mu") @@ -35,21 +41,21 @@ test_that("subset_draws works correctly for draws_array objects", { 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 <- as_draws_array(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10, chain = 3:4) - expect_equal(x[5:10, 3:4, c("mu", "tau")], x_sub, check.attributes = FALSE) + expect_equal(x[5:10, 3:4, c("mu", "tau")], x_sub, ignore_attr = "dimnames") expect_equal(iteration_ids(x_sub), 1:6) expect_equal(chain_ids(x_sub), 1:2) x_sub <- subset_draws(x, chain = c(1, 1), unique = FALSE) expect_equal(nchains(x_sub), 2) - expect_equivalent(x_sub[, 1, ], x_sub[, 2, ]) + expect_equal(x_sub[, 1, ], x_sub[, 2, ], ignore_attr = "dimnames") - expect_message( - x_sub <- subset_draws(x, draw = c(1, 200, 10)), - "Merging chains in order to subset via 'draw'" - ) + + x_sub <- subset_draws(x, draw = c(1, 200, 10)) expect_equal(niterations(x_sub), 3) x <- weight_draws(x, rep(1, ndraws(x))) @@ -89,6 +95,12 @@ test_that("subset_draws preserves weighted draws_df when selecting all variables }) test_that("subset_draws works correctly for draws_df objects", { + # 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 <- as_draws_df(example_draws()) x_sub <- subset_draws(x, variable = c("mu", "tau"), iteration = 5:10, chain = 3:4) expect_equal(x$mu[x$.iteration %in% 5:10 & x$.chain %in% 3:4], x_sub$mu) @@ -119,6 +131,8 @@ test_that("subset_draws works correctly for draws_list objects", { 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 <- as_draws_list(example_draws()) x_sub <- subset_draws(x, variable = c("theta[1]"), iteration = 5:10, chain = 3:4) expect_equal(variables(x_sub), "theta[1]") @@ -129,10 +143,7 @@ test_that("subset_draws works correctly for draws_list objects", { expect_equal(niterations(x_sub), 3) expect_equal(x_sub[[1]]$mu[1], x_sub[[1]]$mu[2]) - expect_message( - x_sub <- subset_draws(x, draw = c(1, 200, 10)), - "Merging chains in order to subset via 'draw'" - ) + x_sub <- subset_draws(x, draw = c(1, 200, 10)) expect_equal(niterations(x_sub), 3) x <- weight_draws(x, rep(1, ndraws(x))) @@ -153,8 +164,8 @@ test_that("subset_draws works correctly for draws_list objects", { test_that("subset_draws works correctly for draws_rvars objects", { # 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)) - + on.exit(options(posterior.warn_on_merge_chains = old_option), add = TRUE) + options(posterior.warn_on_merge_chains = TRUE) x <- as_draws_rvars(example_draws()) x_sub <- subset_draws(x, variable = c("mu"), iteration = 5:10, chain = 3:4) expect_equal(variables(x_sub), "mu") @@ -171,6 +182,8 @@ test_that("subset_draws works correctly for draws_rvars objects", { ) expect_equal(niterations(x_sub), 3) + options(posterior.warn_on_merge_chains = FALSE) + x <- weight_draws(x, rep(1, ndraws(x))) x_sub <- subset_draws(x, variable = "mu") expect_equal(variables(x_sub, reserved = TRUE), c("mu", ".log_weight")) @@ -206,6 +219,8 @@ test_that("subset_draws works correctly for rvar objects", { ) expect_equal(niterations(x_sub), 3) + options(posterior.warn_on_merge_chains = FALSE) + x_sub <- subset_draws(x, iteration = c(1, 2), chain = c(1, 2, 3), exclude = TRUE) expect_equal(niterations(x_sub), niterations(x) - 2) expect_equal(nchains(x_sub), 1) @@ -215,7 +230,6 @@ test_that("subset_draws works correctly for rvar objects", { x_sub <- subset_draws(x, iteration = c(1, 2, 3), exclude = TRUE) expect_equal(ndraws(x) - 3 * nchains(x), ndraws(x_sub)) - }) test_that("variables can be subsetted via regular expressions", { @@ -367,7 +381,7 @@ test_that("subset_draws respects posterior.warn_on_merge_chains option", { x_sub <- subset_draws(x, draw = c(1, 200, 10)) ) expect_equal(niterations(x_sub), 3) - + # Test with default behavior (should be TRUE, showing message) options(posterior.warn_on_merge_chains = NULL) x <- as_draws_array(example_draws()) diff --git a/tests/testthat/test-summarise_draws-multicore-external-function.R b/tests/testthat/test-summarise_draws-multicore-external-function.R new file mode 100644 index 00000000..affd386a --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-external-function.R @@ -0,0 +1,15 @@ +test_that("multicore summarise_draws finds externally defined summary functions", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 20 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + dimnames(x)$variable[2] <- reserved_variables()[1] + + mean2 <- function(x) sum(x) / length(x) + sum_x <- summarise_draws(x, mean2) + parsum_x <- summarise_draws(x, mean2, .cores = cores) + expect_identical(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws-multicore-many-variables.R b/tests/testthat/test-summarise_draws-multicore-many-variables.R new file mode 100644 index 00000000..b43a43f5 --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-many-variables.R @@ -0,0 +1,17 @@ +test_that("multicore summarise_draws is identical to single-core summarise_draws", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 20 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_equal(sum_x, parsum_x) + + dimnames(x)$variable[2] <- reserved_variables()[1] + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_equal(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws-multicore-one-variable.R b/tests/testthat/test-summarise_draws-multicore-one-variable.R new file mode 100644 index 00000000..30c02938 --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-one-variable.R @@ -0,0 +1,17 @@ +test_that("multicore summarise_draws is identical to single-core summarise_draws with 1 variable", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 1 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_identical(sum_x, parsum_x) + + dimnames(x)$variable[1] <- reserved_variables()[1] + suppressWarnings(sum_x <- summarise_draws(x)) + suppressWarnings(parsum_x <- summarise_draws(x, .cores = cores)) + expect_identical(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws-multicore-two-variables.R b/tests/testthat/test-summarise_draws-multicore-two-variables.R new file mode 100644 index 00000000..be8ae54b --- /dev/null +++ b/tests/testthat/test-summarise_draws-multicore-two-variables.R @@ -0,0 +1,17 @@ +test_that("multicore summarise_draws is identical to single-core summarise_draws with 2 variables", { + set.seed(1) + cores <- 2 + nc <- 4 + + n <- 2 + test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) + x <- as_draws_array(test_array) + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_identical(sum_x, parsum_x) + + dimnames(x)$variable[2] <- reserved_variables()[1] + sum_x <- summarise_draws(x) + parsum_x <- summarise_draws(x, .cores = cores) + expect_identical(sum_x, parsum_x) +}) diff --git a/tests/testthat/test-summarise_draws.R b/tests/testthat/test-summarise_draws.R index 80ed971e..27d1b6ed 100644 --- a/tests/testthat/test-summarise_draws.R +++ b/tests/testthat/test-summarise_draws.R @@ -69,57 +69,6 @@ test_that("summarise_draws warns if all variable names are reserved", { expect_warning(summarize_draws(x), "no variables with unreserved names") }) -test_that(paste( - "multicore summarise_draws is identical to single-core summarise_draws", - "including if some chunks contain no variables" -), { - set.seed(1) - cores <- 2 - nc <- 4 - - n <- 20 - test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) - x <- as_draws_array(test_array) - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_equal(sum_x, parsum_x) - - dimnames(x)$variable[2] <- reserved_variables()[1] - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_equal(sum_x, parsum_x) - - # test that externally defined summary functions can be found - mean2 <- function(x) sum(x) / length(x) - sum_x <- summarise_draws(x, mean2) - parsum_x <- summarise_draws(x, mean2, .cores = cores) - expect_identical(sum_x, parsum_x) - - n <- 2 - test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) - x <- as_draws_array(test_array) - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_identical(sum_x, parsum_x) - - dimnames(x)$variable[2] <- reserved_variables()[1] - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_identical(sum_x, parsum_x) - - n <- 1 - test_array <- array(data = rnorm(1000*nc*n), dim = c(1000,nc,n)) - x <- as_draws_array(test_array) - sum_x <- summarise_draws(x) - parsum_x <- summarise_draws(x, .cores = cores) - expect_identical(sum_x, parsum_x) - - dimnames(x)$variable[1] <- reserved_variables()[1] - suppressWarnings(sum_x <- summarise_draws(x)) - suppressWarnings(parsum_x <- summarise_draws(x, .cores = cores)) - expect_identical(sum_x, parsum_x) -}) - test_that("summarise_draws supports tibble::set_num_opts correctly", { x <- example_draws() expect_output( diff --git a/tests/testthat/test-thin_draws.R b/tests/testthat/test-thin_draws.R index 52def0b1..add05072 100644 --- a/tests/testthat/test-thin_draws.R +++ b/tests/testthat/test-thin_draws.R @@ -27,7 +27,10 @@ test_that("automatic thinning works as expected", { thin_by <- niterations(mu) / mean( c(ess_mu_1, ess_mu_2, ess_mu_3, ess_mu_4)) - - expect_equal(thin_draws(mu), thin_draws(mu, thin = thin_by)) + expect_message( + thinned <- thin_draws(mu), + "Automatically thinned by 1.6 based on ESS" + ) + expect_equal(thinned, thin_draws(mu, thin = thin_by)) }) diff --git a/tests/testthat/test-weight_draws.R b/tests/testthat/test-weight_draws.R index d947ae9e..ef8f0341 100644 --- a/tests/testthat/test-weight_draws.R +++ b/tests/testthat/test-weight_draws.R @@ -176,7 +176,7 @@ test_that("conversion between formats preserves weights", { for (type in names(draws)) { expect_equal(as_draws_matrix(draws[[!!type]]), draws$matrix) expect_equal(as_draws_array(draws[[!!type]]), draws$array) - expect_equal(as_draws_df(draws[[!!type]]), draws$df) + expect_equal(as.data.frame(as_draws_df(draws[[!!type]])), as.data.frame(draws$df)) expect_equal(as_draws_list(draws[[!!type]]), draws$list) expect_equal(as_draws_rvars(draws[[!!type]]), draws$rvars) } @@ -188,6 +188,9 @@ test_that("pareto smoothing smooths weights in weight_draws", { x <- example_draws() lw <- sort(log(abs(rt(ndraws(x), 1)))) weighted <- weight_draws(x, lw, pareto_smooth = FALSE, log = TRUE) - smoothed <- weight_draws(x, lw, pareto_smooth = TRUE, log = TRUE) + expect_message( + smoothed <- weight_draws(x, lw, pareto_smooth = TRUE, log = TRUE), + "Pareto khat for weights is high" + ) expect_false(all(weights(weighted) == weights(smoothed))) })