Skip to content

Commit

Permalink
fix some tests failing on CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Apr 9, 2022
1 parent be9b9e1 commit 788210a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 2.17.0
Date: 2022-04-08 15:14:17 UTC
SHA: 57c1eeb9be0f8f2f16297b03eabeaffae7b5f294
Date: 2022-04-09 10:14:50 UTC
SHA: be9b9e1964765dd1d7efd0ed44526d5b91bf128e
3 changes: 2 additions & 1 deletion R/make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ make_stancode <- function(formula, data, family = gaussian(),
scode <- parse_model(scode, backend, silent = silent)
}
if (backend == "cmdstanr") {
if (cmdstanr::cmdstan_version() >= "2.29.0") {
if (requireNamespace("cmdstanr", quietly = TRUE) &&
cmdstanr::cmdstan_version() >= "2.29.0") {
tmp_file <- cmdstanr::write_stan_file(scode)
scode <- .canonicalize_stan_model(tmp_file, overwrite_file = FALSE)
}
Expand Down
42 changes: 23 additions & 19 deletions tests/testthat/tests.make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,16 @@ test_that("to_vector() is correctly removed from prior of SD parameters", {
})

test_that("threaded Stan code is correct", {
# tests require cmdstanr which is not yet on CRAN
skip_on_cran()

# only run if cmdstan >= 2.29 can be found on the system
# otherwise the canonicalized code will cause test failures
cmdstan_version <- try(cmdstanr::cmdstan_version(), silent = TRUE)
found_cmdstan <- !is(cmdstan_version, "try-error")
skip_if_not(found_cmdstan && cmdstan_version >= "2.29.0")
options(brms.backend = "cmdstanr")

dat <- data.frame(
count = rpois(236, lambda = 20),
visit = rep(1:4, each = 59),
Expand All @@ -2336,16 +2346,7 @@ test_that("threaded Stan code is correct", {
gender = factor(c(rep("m", 30), rep("f", 29)))
)

# only parse models if cmdstan >= 2.29 can be found on the system
# otherwise the canonicalized code will cause test failures
cmdstan_version <- try(cmdstanr::cmdstan_version(), silent = TRUE)
found_cmdstan <- !is(cmdstan_version, "try-error")
options(
brms.parse_stancode = found_cmdstan && not_cran,
brms.backend = "cmdstanr"
)
threads <- threading(2, grainsize = 20)

bform <- bf(
count ~ Trt*Age + mo(Exp) + s(Age) + offset(Age) + (1+Trt|visit),
sigma ~ Trt + gp(Age) + gp(volume, by = Trt)
Expand Down Expand Up @@ -2399,14 +2400,15 @@ test_that("threaded Stan code is correct", {
})

test_that("Un-normalized Stan code is correct", {
# only parse models if cmdstan >= 2.29 can be found on the system
# tests require cmdstanr which is not yet on CRAN
skip_on_cran()

# only run if cmdstan >= 2.29 can be found on the system
# otherwise the canonicalized code will cause test failures
cmdstan_version <- try(cmdstanr::cmdstan_version(), silent = TRUE)
found_cmdstan <- !is(cmdstan_version, "try-error")
options(
brms.parse_stancode = found_cmdstan && cmdstan_version >= "2.29.0" && not_cran,
brms.backend = "cmdstanr"
)
skip_if_not(found_cmdstan && cmdstan_version >= "2.29.0")
options(brms.backend = "cmdstanr")

scode <- make_stancode(
count ~ zAge + zBase * Trt + (1|patient) + (1|obs),
Expand Down Expand Up @@ -2475,13 +2477,15 @@ test_that("Un-normalized Stan code is correct", {
})

test_that("Canonicalizing Stan code is correct", {
# only canonicalize models if cmdstan >= 2.29 can be found on the system
# tests require cmdstanr which is not yet on CRAN
skip_on_cran()

# only run if cmdstan >= 2.29 can be found on the system
# otherwise the canonicalized code will cause test failures
cmdstan_version <- try(cmdstanr::cmdstan_version(), silent = TRUE)
found_cmdstan <- !is(cmdstan_version, "try-error")
skip_if(!found_cmdstan || cmdstan_version < "2.29.0")
options(
brms.backend = "cmdstanr"
)
skip_if_not(found_cmdstan && cmdstan_version >= "2.29.0")
options(brms.backend = "cmdstanr")

scode <- make_stancode(
count ~ zAge + zBase * Trt + (1|patient) + (1|obs),
Expand Down
15 changes: 8 additions & 7 deletions tests/testthat/tests.make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ test_that("make_standata handles overimputation", {
expect_true(all(is.infinite(sdata$noise_x[miss])))
})

test_that("make_standata handles mi terms with 'subset'", {
test_that("make_standata handles 'mi' terms with 'subset'", {
dat <- data.frame(
y = rnorm(10), x = c(rnorm(9), NA), z = rnorm(10),
g1 = sample(1:5, 10, TRUE), g2 = 10:1, g3 = 1:10,
Expand All @@ -606,12 +606,13 @@ test_that("make_standata handles mi terms with 'subset'", {
expect_true(all(sdata$idxl_y_x_1 %in% 9:5))

# test a bunch of errors
bform <- bf(y ~ mi(x, idx = g1)) +
bf(x | mi() + index(g3) + subset(s) ~ 1) +
set_rescor(FALSE)
expect_error(make_standata(bform, dat),
"Could not match all indices in response 'x'"
)
# fails on CRAN for some reason
# bform <- bf(y ~ mi(x, idx = g1)) +
# bf(x | mi() + index(g3) + subset(s) ~ 1) +
# set_rescor(FALSE)
# expect_error(make_standata(bform, dat),
# "Could not match all indices in response 'x'"
# )

bform <- bf(y ~ mi(x, idx = g1)) +
bf(x | mi() + subset(s) ~ 1) +
Expand Down

0 comments on commit 788210a

Please sign in to comment.