Skip to content
Draft
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
44 changes: 44 additions & 0 deletions tests/testthat/_snaps/pareto_smooth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# pareto_khat diagnostics messages are as expected

Code
pareto_k_diagmsg(diags)
Message
Pareto k-hat = 0.5.
Code
diags$khat <- 0.6
pareto_k_diagmsg(diags)
Message
Pareto k-hat = 0.6. Sample size is too small, for given Pareto k-hat. Sample size larger than 10 is needed for reliable results.

Code
diags$khat <- 0.71
diags$khat_threshold <- 0.8
pareto_k_diagmsg(diags)
Message
Pareto k-hat = 0.71. Bias dominates when k-hat > 0.7, making empirical mean estimate of the Pareto-smoothed draws unreliable.

Code
diags$khat_threshold <- 0.55
pareto_k_diagmsg(diags)
Message
Pareto k-hat = 0.71. Sample size is too small, for given Pareto k-hat. Sample size larger than 10 is needed for reliable results.
Bias dominates when k-hat > 0.7, making empirical mean estimate of the Pareto-smoothed draws unreliable.

Code
diags$khat <- 1.1
pareto_k_diagmsg(diags)
Message
Pareto k-hat = 1.1. Mean does not exist, making empirical mean estimate of the draws not applicable.
Code
diags$khat <- 0.8
pareto_k_diagmsg(diags, are_weights = TRUE)
Message
Pareto k-hat = 0.8. Pareto khat for weights is high (0.8). This indicates a single or few weights dominate.
Inference based on weighted draws will be unreliable.

Code
diags$khat <- 0.5
pareto_k_diagmsg(diags, are_weights = TRUE)
Message
Pareto k-hat = 0.5.

97 changes: 97 additions & 0 deletions tests/testthat/_snaps/rvar-print.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# print(<rvar>) works

Code
print(rvar())
Output
rvar<1>[0] mean ± sd:
rvar()
Code
print(x)
Output
rvar<2>[2,3] mean ± sd:
[,1] [,2] [,3]
[1,] 1.5 ± 0.71 5.5 ± 0.71 9.5 ± 0.71
[2,] 3.5 ± 0.71 7.5 ± 0.71 11.5 ± 0.71
Code
print(x_with_chains)
Output
rvar<1,2>[2,3] mean ± sd:
[,1] [,2] [,3]
[1,] 1.5 ± 0.71 5.5 ± 0.71 9.5 ± 0.71
[2,] 3.5 ± 0.71 7.5 ± 0.71 11.5 ± 0.71
Code
print(x, summary = "median_mad")
Output
rvar<2>[2,3] median ± mad:
[,1] [,2] [,3]
[1,] 1.5 ± 0.74 5.5 ± 0.74 9.5 ± 0.74
[2,] 3.5 ± 0.74 7.5 ± 0.74 11.5 ± 0.74

# print(<rvar_factor>) works

Code
print(x)
Output
rvar_factor<1,2>[2,3] mode <entropy>:
[,1] [,2] [,3]
[1,] a <0.28> e <0.28> i <0.28>
[2,] c <0.28> g <0.28> k <0.28>
12 levels: a b c d e f g h i j k l

# print(<rvar_ordered>) works

Code
print(x)
Output
rvar_ordered<1,2>[2,3] mode <dissent>:
[,1] [,2] [,3]
[1,] a <0.067> e <0.067> i <0.067>
[2,] c <0.067> g <0.067> k <0.067>
12 levels: a < b < c < d < e < f < g < h < i < j < k < l

# str(<rvar>) works

Code
str(rvar())
Output
rvar<1>[0]
Code
str(rvar(1:3))
Output
rvar<3>[1] 2 ± 1
Code
str(x, vec.len = 5)
Output
rvar<2>[3,4] 1.5 ± 0.71 3.5 ± 0.71 5.5 ± 0.71 7.5 ± 0.71 9.5 ± 0.71 ...
Code
str(x_with_attrs, vec.len = 5)
Output
rvar<2>[3,4] 1.5 ± 0.71 3.5 ± 0.71 5.5 ± 0.71 7.5 ± 0.71 9.5 ± 0.71 ...
- dimnames(*)=List of 2
..$ : chr [1:3] "a" "b" "c"
..$ : NULL
- attr(draws_of(*), "foo")=List of 2
..$ : num 1
..$ : num 2
- attr(*, "bar")=List of 2
..$ : num 1
..$ : num 2

# str(<rvar_factor>) works

Code
str(x, vec.len = 5)
Output
rvar_factor<1,2>[3,4] a <0.00> c <0.22> e <0.22> g <0.22> i <0.22> ...
- 24 levels: a b c d ... x
- attr(*, "foo")= chr "bar"

# str(<rvar_ordered>) works

Code
str(x, vec.len = 5)
Output
rvar_ordered<1,2>[3,4] a <0.000> c <0.032> e <0.032> g <0.032> i <0.032> ...
- 24 levels: a < b < c < d < ... < x
- attr(*, "foo")= chr "bar"

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, "]"))
})

Loading
Loading