Skip to content

Commit

Permalink
expose stan data
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Oct 3, 2024
1 parent cf94cd1 commit 863de20
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
5 changes: 5 additions & 0 deletions R/biokinetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ biokinetics <- R6::R6Class(
package = "epikinetics"
)
},
#' @description View the data that is passed to the stan model, for debugging purposes.
#' @return A list of arguments that will be passed to the stan model.
get_stan_data = function() {
private$stan_input_data
},
#' @description Fit the model and return CmdStanMCMC fitted model object.
#' @return A CmdStanMCMC fitted model object: <https://mc-stan.org/cmdstanr/reference/CmdStanMCMC.html>
#' @param ... Named arguments to the `sample()` method of CmdStan model.
Expand Down
14 changes: 14 additions & 0 deletions man/biokinetics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions tests/testthat/test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ local_mocked_bindings(
test_that("Can construct stan data", {
dt <- data.table::fread(system.file("delta_full.rds", package = "epikinetics"))
mod <- biokinetics$new(data = dt,
priors = biokinetics_priors(),
covariate_formula = ~0 + infection_history,
preds_sd = 0.25,
time_type = "relative")
priors = biokinetics_priors(),
covariate_formula = ~0 + infection_history,
preds_sd = 0.25,
time_type = "relative")
# the fit function has been mocked above to return the stan inputs
stan_dt <- mod$fit()
expect_equal(stan_dt$N_events, 335)
Expand All @@ -21,11 +21,30 @@ test_that("Can construct stan data", {

test_that("Can initialise file path data", {
expect_true(inherits(biokinetics$new(file_path = system.file("delta_full.rds", package = "epikinetics"),
priors = biokinetics_priors()), "biokinetics"))
priors = biokinetics_priors()), "biokinetics"))
})

test_that("Can provide data directly", {
dat <- data.table::fread(system.file("delta_full.rds", package = "epikinetics"))
expect_true(inherits(biokinetics$new(data = dat,
priors = biokinetics_priors()), "biokinetics"))
priors = biokinetics_priors()), "biokinetics"))
})

test_that("Can get stan data", {
dat <- data.table::fread(system.file("delta_full.rds", package = "epikinetics"))
priors <- biokinetics_priors(mu_values = c(1, 2, 3, 4, 5, 6),
sigma_values = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6))
mod <- biokinetics$new(data = dat, priors = priors)
stan_data <- mod$get_stan_data()
expect_true(is.list(stan_data))
expect_equal(names(stan_data), c("N", "N_events", "id", "value", "censored",
"titre_type", "preds_sd", "K", "N_uncens", "N_lo",
"N_me", "N_hi", "uncens_idx", "cens_lo_idx", "cens_me_idx",
"cens_hi_idx", "t", "X", "P", "mu_t0",
"mu_tp", "mu_ts", "mu_m1", "mu_m2", "mu_m3",
"sigma_t0", "sigma_tp", "sigma_ts", "sigma_m1", "sigma_m2",
"sigma_m3"))
expect_equal(stan_data$mu_t0, priors$mu_t0)
expect_equal(stan_data$sigma_t0, priors$sigma_t0)
expect_equal(stan_data$id, dat$pid)
})

0 comments on commit 863de20

Please sign in to comment.