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
deactivate testing on windows for now
  • Loading branch information
sbfnk committed Feb 14, 2024
commit 91a6b8e9243572bf959ef923cb5da40344ec1016
2 changes: 2 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ jobs:
with:
dependencies: NA
extra-packages: |
dplyr
rmarkdown
rcmdcheck
stan-dev/cmdstanr
testthat

- name: Install cmdstan
if: runner.os != 'Windows'
uses: epinowcast/actions/install-cmdstan@v1
with:
cmdstan-version: 'latest'
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-epinow.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test_that("epinow produces expected output when run with default settings", {

test_that("epinow produces expected output when run with the
cmdstanr backend", {
skip_on_os("windows")
output <- capture.output(suppressMessages(suppressWarnings(
out <- epinow(
reported_cases = reported_cases,
Expand Down
50 changes: 27 additions & 23 deletions tests/testthat/test-estimate_secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,50 @@ skip_on_cran()
# make some example secondary incidence data
cases <- example_confirmed
cases <- as.data.table(cases)[, primary := confirm]

inc_cases <- copy(cases)
# Assume that only 40 percent of cases are reported
cases[, scaling := 0.4]
inc_cases[, scaling := 0.4]
# Parameters of the assumed log normal delay distribution
cases[, meanlog := 1.8][, sdlog := 0.5]
inc_cases[, meanlog := 1.8][, sdlog := 0.5]

# Simulate secondary cases
cases <- simulate_secondary(cases, type = "incidence")
cases[
inc_cases <- simulate_secondary(inc_cases, type = "incidence")
inc_cases[
,
c("confirm", "scaling", "meanlog", "sdlog", "index", "scaled", "conv") :=
NULL
]
#
# fit model to example data specifying a weak prior for fraction reported
# with a secondary case
inc <- estimate_secondary(cases[1:60],
inc <- estimate_secondary(inc_cases[1:60],
obs = obs_opts(scale = list(mean = 0.2, sd = 0.2), week_effect = FALSE),
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 ####

# make some example prevalence data
cases <- example_confirmed
cases <- as.data.table(cases)[, primary := confirm]
prev_cases <- copy(cases)
# Assume that only 30 percent of cases are reported
cases[, scaling := 0.3]
prev_cases[, scaling := 0.3]
# Parameters of the assumed log normal delay distribution
cases[, meanlog := 1.6][, sdlog := 0.8]
prev_cases[, meanlog := 1.6][, sdlog := 0.8]

# Simulate secondary cases
cases <- simulate_secondary(cases, type = "prevalence")
prev_cases <- simulate_secondary(prev_cases, type = "prevalence")

# fit model to example prevalence data
prev <- estimate_secondary(cases[1:100],
prev <- estimate_secondary(prev_cases[1:100],
secondary = secondary_opts(type = "prevalence"),
obs = obs_opts(
week_effect = FALSE,
Expand Down Expand Up @@ -102,6 +95,14 @@ test_that("estimate_secondary can recover simulated parameters", {

test_that("estimate_secondary can recover simulated parameters with the
cmdstanr backend", {
skip_on_os("windows")
output <- capture.output(suppressMessages(suppressWarnings(
inc_cmdstanr <- estimate_secondary(inc_cases[1:60],
obs = obs_opts(scale = list(mean = 0.2, sd = 0.2), week_effect = FALSE),
verbose = FALSE, stan = stan_opts(backend = "cmdstanr")
)
)))
inc_posterior_cmdstanr <- inc_cmdstanr$posterior[variable %in% params]
expect_equal(
inc_posterior_cmdstanr[, mean], c(1.8, 0.5, 0.4),
tolerance = 0.1
Expand All @@ -114,17 +115,20 @@ test_that("estimate_secondary can recover simulated parameters with the

test_that("forecast_secondary can return values from simulated data and plot
them", {
inc_preds <- forecast_secondary(inc, cases[seq(61, .N)][, value := primary])
inc_preds <- forecast_secondary(
inc, inc_cases[seq(61, .N)][, value := primary]
)
expect_equal(names(inc_preds), c("samples", "forecast", "predictions"))
# validation plot of observations vs estimates
expect_error(plot(inc_preds, new_obs = cases, from = "2020-05-01"), NA)
expect_error(plot(inc_preds, new_obs = inc_cases, from = "2020-05-01"), NA)
})

test_that("forecast_secondary can return values from simulated data when using
the cmdstanr backend", {
skip_on_os("windows")
capture.output(suppressMessages(suppressWarnings(
inc_preds <- forecast_secondary(
inc_cmdstanr, cases[seq(61, .N)][, value := primary], backend = "cmdstanr"
inc, inc_cases[seq(61, .N)][, value := primary], backend = "cmdstanr"
)
)))
expect_equal(names(inc_preds), c("samples", "forecast", "predictions"))
Expand All @@ -135,7 +139,7 @@ test_that("estimate_secondary works with weigh_delay_priors = TRUE", {
mean = 2.5, mean_sd = 0.5, sd = 0.47, sd_sd = 0.25, max = 30
)
inc_weigh <- estimate_secondary(
cases[1:60], delays = delay_opts(delays),
inc_cases[1:60], delays = delay_opts(delays),
obs = obs_opts(scale = list(mean = 0.2, sd = 0.2), week_effect = FALSE),
weigh_delay_priors = TRUE, verbose = FALSE
)
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-estimate_truncation.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ test_that("estimate_truncation can return values from simulated data and plot
test_that("estimate_truncation can return values from simulated data with the
cmdstanr backend", {
# fit model to example data
skip_on_os("windows")
output <- capture.output(suppressMessages(suppressWarnings(
est <- estimate_truncation(example_data,
verbose = FALSE, chains = 2, iter = 1000, warmup = 250,
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-simulate_infections.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ test_that("forecast_infections works to simulate a passed in estimate_infections

test_that("forecast_infections works to simulate a passed in estimate_infections
object when using the cmdstanr backend", {
skip_on_os("windows")
output <- capture.output(suppressMessages(suppressWarnings(
sims <- forecast_infections(out, backend = "cmdstanr")
)))
Expand Down