Description Usage Arguments Value Examples
fit_models()
fits a stan model across multiple datasets,
collates, and returns summary information and data for all fitted models as
a stansim_simulation
object. All fitted models have basic
reproducibility information recorded; such as parameter inits and seeds,
along with parameter estimates, and simulation information such as time and
date ran.
Raw stan posterior samples are not returned, rather the user specifies the estimates they wish to record (e.g. posterior percentiles, Rhat, etc.) and the parameters for which they wish to record these estimates. All data is collated into a single, tidy dataframe for further analysis.
By default the function caches completed runs as it progresses, so that progress is not lost in the case of function failure. By simply running the function with the same calls in the same working directory it will pick up where it left off. When the function terminates as expected this cache is removed.
1 2 3 4 5 6 | fit_models(sim_name = paste0("Stansim_", Sys.time()), sim_data = NULL,
stan_args = list(), calc_loo = FALSE, use_cores = 1L,
parameters = "all", probs = c(0.025, 0.25, 0.5, 0.75, 0.975),
estimates = c("mean", "se_mean", "sd", "n_eff", "Rhat"),
stan_warnings = "catch", cache = TRUE, seed = floor(stats::runif(1, 1,
1e+05)))
|
sim_name |
A name attached to the |
sim_data |
Either an object of class |
stan_args |
A list of function arguments to be used by the internal
|
calc_loo |
If |
use_cores |
Number of cores to use when running in parallel. Each stan model is fitted serially regardless of the number of chains ran, as parallelisation across models is more flexible than within. |
parameters |
A character vector indicating which parameters should have
estimates returned and stored from the fitted models. By default all
parameters are returned, for non-scalar parameters you cannot select
subsets of the parameter (e.g. must request |
probs |
A numeric vector of values between 0 and 1. Corresponding quantiles will be estimated and returned for all fitted models. |
estimates |
A character vector of non-quantile estimates to be returned for each model parameter. Argument must be some subset of the default character vector. |
stan_warnings |
How warnings returned by individual |
cache |
If |
seed |
Set a seed for the function. |
An S3 object of class stansim_simulation
recording relevant
simulation data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ## Not run:
# specify arguments for stan
StanArgs <- list(file = '8schools.stan',
iter = 1000, chains = 4)
# get number of cores
core_num <- parallel::detectCores()
# get the list of data file locations
datasets <- dir("data/repo", full.names = TRUE)
# fit the model to all datasets using specified stan arguments
# store the specified estimates for all parameters
simulation <- fit_models(
sim_name = "stansim simulation",
sim_data = datasets,
stan_args = StanArgs,
calc_loo = T,
use_cores = core_num,
probs = c(.025, .5, .975),
estimates = c("mean", "n_eff", "Rhat")
)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.