diff --git a/R/biokinetics.R b/R/biokinetics.R index e56c8f3..a4731a2 100644 --- a/R/biokinetics.R +++ b/R/biokinetics.R @@ -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: #' @param ... Named arguments to the `sample()` method of CmdStan model. diff --git a/man/biokinetics.Rd b/man/biokinetics.Rd index 4e2b85f..77620d2 100644 --- a/man/biokinetics.Rd +++ b/man/biokinetics.Rd @@ -11,6 +11,7 @@ fit it to a dataset. \subsection{Public methods}{ \itemize{ \item \href{#method-biokinetics-new}{\code{biokinetics$new()}} +\item \href{#method-biokinetics-get_stan_data}{\code{biokinetics$get_stan_data()}} \item \href{#method-biokinetics-fit}{\code{biokinetics$fit()}} \item \href{#method-biokinetics-extract_population_parameters}{\code{biokinetics$extract_population_parameters()}} \item \href{#method-biokinetics-extract_individual_parameters}{\code{biokinetics$extract_individual_parameters()}} @@ -58,6 +59,19 @@ An epikinetics::biokinetics object. } } \if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-biokinetics-get_stan_data}{}}} +\subsection{Method \code{get_stan_data()}}{ +View the data that is passed to the stan model, for debugging purposes. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{biokinetics$get_stan_data()}\if{html}{\out{
}} +} + +\subsection{Returns}{ +A list of arguments that will be passed to the stan model. +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-biokinetics-fit}{}}} \subsection{Method \code{fit()}}{ diff --git a/tests/testthat/test-data.R b/tests/testthat/test-data.R index f06fc91..e141cb7 100644 --- a/tests/testthat/test-data.R +++ b/tests/testthat/test-data.R @@ -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) @@ -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) })