Skip to content

Cmdstanr option #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5d708bd
add cmdstan backend
sbfnk Jan 30, 2024
76c267e
add cmdstanr model
sbfnk Jan 30, 2024
277921c
generalise extract functions
sbfnk Jan 31, 2024
f6eda7a
create general fit_model function
sbfnk Jan 31, 2024
547ea8a
initial values for cmdstanr
sbfnk Jan 31, 2024
4d5f088
move stan model creation to create func
sbfnk Jan 31, 2024
db81caf
make dist_fit cmdstanr ready
sbfnk Jan 31, 2024
32e2cc8
make estimate_infections cmdstanr ready
sbfnk Jan 31, 2024
19fadcc
make estimate_secondary cmdstanr ready
sbfnk Jan 31, 2024
4eb4726
make estimate_truncation cmdstanr ready
sbfnk Jan 31, 2024
311d433
make simulate_infections cmdstanr ready
sbfnk Jan 31, 2024
6569e68
gitignore for binaries
sbfnk Jan 31, 2024
4ae5948
add cmdstanr as suggest
sbfnk Jan 31, 2024
028b499
make forecast_secondary cmdstanr ready
sbfnk Feb 1, 2024
f5b03bd
update stanargs test
sbfnk Feb 1, 2024
1cca4ca
make simulations work with updated options
sbfnk Feb 1, 2024
c8b5e09
add globals
sbfnk Feb 1, 2024
d7064ab
tests for cmdstanr backend
sbfnk Feb 1, 2024
ccb004f
update actions
sbfnk Feb 2, 2024
a808431
updates in response to lintr
sbfnk Feb 2, 2024
02dc4bf
don't use future_lapply for cmdstanr
sbfnk Feb 4, 2024
7c7112b
backend-specific success criteria
sbfnk Feb 5, 2024
df76685
use epinowcast action for installing cmdstan
sbfnk Feb 6, 2024
0032367
improve .gitignore for compiled stan files
sbfnk Feb 6, 2024
91a6b8e
deactivate testing on windows for now
sbfnk Feb 12, 2024
cac3811
Revert "use epinowcast action for installing cmdstan"
sbfnk Feb 13, 2024
6593f5a
Apply suggestions from code review
sbfnk Feb 14, 2024
6b60299
match arguments in `stan_model`
sbfnk Feb 14, 2024
efba507
don't match args$method but explictly stop instead
sbfnk Feb 14, 2024
b594c3c
put choices in argument
sbfnk Feb 14, 2024
b1c54a4
render documentation for stan_model
sbfnk Feb 14, 2024
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
Prev Previous commit
Next Next commit
tests for cmdstanr backend
  • Loading branch information
sbfnk committed Feb 14, 2024
commit d7064ab20da526628926d449dae6cd0734db5e01
26 changes: 26 additions & 0 deletions tests/testthat/test-epinow.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ test_that("epinow produces expected output when run with default settings", {
expect_equal(names(out$plots), c("infections", "reports", "R", "growth_rate", "summary"))
})

test_that("epinow produces expected output when run with the
cmdstanr backend", {
output <- capture.output(suppressMessages(suppressWarnings(
out <- epinow(
reported_cases = reported_cases,
generation_time = generation_time_opts(example_generation_time),
delays = delay_opts(example_incubation_period + reporting_delay),
stan = stan_opts(
samples = 25, warmup = 25,
cores = 1, chains = 2,
control = list(adapt_delta = 0.8),
backend = "cmdstanr"
),
logs = NULL, verbose = FALSE
)
)))

expect_equal(names(out), expected_out)
df_non_zero(out$estimates$samples)
df_non_zero(out$estimates$summarised)
df_non_zero(out$estimated_reported_cases$samples)
df_non_zero(out$estimated_reported_cases$summarised)
df_non_zero(out$summary)
expect_equal(names(out$plots), c("infections", "reports", "R", "growth_rate", "summary"))
})

test_that("epinow runs without error when saving to disk", {
expect_null(suppressWarnings(epinow(
reported_cases = reported_cases,
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-estimate_secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ inc <- estimate_secondary(cases[1:60],
verbose = FALSE
)

output <- capture.output(suppressMessages(suppressWarnings(
inc_cmdstanr <- estimate_secondary(cases[1:60],
obs = obs_opts(scale = list(mean = 0.2, sd = 0.2), week_effect = FALSE),
verbose = FALSE, stan = stan_opts(backend = "cmdstanr")
)
)))

# extract posterior variables of interest
params <- c(
"meanlog" = "delay_mean[1]", "sdlog" = "delay_sd[1]",
"scaling" = "frac_obs[1]"
)

inc_posterior <- inc$posterior[variable %in% params]
inc_posterior_cmdstanr <- inc_cmdstanr$posterior[variable %in% params]

#### Prevalence data example ####

Expand Down Expand Up @@ -92,6 +100,18 @@ test_that("estimate_secondary can recover simulated parameters", {
)
})

test_that("estimate_secondary can recover simulated parameters with the
cmdstanr backend", {
expect_equal(
inc_posterior_cmdstanr[, mean], c(1.8, 0.5, 0.4),
tolerance = 0.1
)
expect_equal(
inc_posterior_cmdstanr[, median], c(1.8, 0.5, 0.4),
tolerance = 0.1
)
})

test_that("forecast_secondary can return values from simulated data and plot
them", {
inc_preds <- forecast_secondary(inc, cases[seq(61, .N)][, value := primary])
Expand All @@ -100,6 +120,16 @@ test_that("forecast_secondary can return values from simulated data and plot
expect_error(plot(inc_preds, new_obs = cases, from = "2020-05-01"), NA)
})

test_that("forecast_secondary can return values from simulated data when using
the cmdstanr backend", {
capture.output(suppressMessages(suppressWarnings(
inc_preds <- forecast_secondary(
inc_cmdstanr, cases[seq(61, .N)][, value := primary], backend = "cmdstanr"
)
)))
expect_equal(names(inc_preds), c("samples", "forecast", "predictions"))
})

test_that("estimate_secondary works with weigh_delay_priors = TRUE", {
delays <- dist_spec(
mean = 2.5, mean_sd = 0.5, sd = 0.47, sd_sd = 0.25, max = 30
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-estimate_truncation.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ test_that("estimate_truncation can return values from simulated data and plot
expect_error(plot(est), NA)
})

test_that("estimate_truncation can return values from simulated data with the
cmdstanr backend", {
# fit model to example data
output <- capture.output(suppressMessages(suppressWarnings(
est <- estimate_truncation(example_data,
verbose = FALSE, chains = 2, iter = 1000, warmup = 250,
stan = stan_opts(backend = "cmdstanr")
))))
expect_equal(
names(est),
c("dist", "obs", "last_obs", "cmf", "data", "fit")
)
expect_s3_class(est$dist, "dist_spec")
expect_error(plot(est), NA)
})

test_that("deprecated arguments are recognised", {
options(warn = 2)
expect_error(estimate_truncation(example_data,
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-simulate_infections.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ test_that("forecast_infections works to simulate a passed in estimate_infections
expect_equal(names(sims), c("samples", "summarised", "observations"))
})

test_that("forecast_infections works to simulate a passed in estimate_infections
object when using the cmdstanr backend", {
output <- capture.output(suppressMessages(suppressWarnings(
sims <- forecast_infections(out, backend = "cmdstanr")
)))
expect_equal(names(sims), c("samples", "summarised", "observations"))
})

test_that("forecast_infections works to simulate a passed in estimate_infections object with an adjusted Rt", {
R <- c(rep(NA_real_, 40), rep(0.5, 17))
sims <- forecast_infections(out, R)
Expand Down