Fit Bayesian models using Stan with checkpointing.
chkpt_stan(
model_code,
data,
iter_adaptation = 150,
iter_warmup = 1000,
iter_sampling = 1000,
iter_per_chkpt = 100,
parallel_chains = 4,
threads_per = 1,
chkpt_progress = TRUE,
control = NULL,
seed = 1,
stop_after = NULL,
reset = FALSE,
path,
...
)
Character string corresponding to the Stan model.
A named list of R objects (like for RStan). Further details can
be found in sample
.
(positive integer) The number of iterations in the initial warmup, which are used for the adaptation of the step size and inverse mass matrix. This is equivalent to the traditional warmup stage. Checkpointing will begin only after this stage is complete.
(positive integer) The number of warmup iterations to run per chain (defaults to 1000).
(positive integer) The number of post-warmup iterations to run per chain (defaults to 1000).
(positive integer). The number of iterations per
checkpoint. Note that iter_sampling
is divided by
iter_per_chkpt
to determine the number of checkpoints. This must
result in an integer (if not, there will be an error).
(positive integer) The maximum number of MCMC
chains to run in parallel. If parallel_chains is not specified then the
default is to look for the option mc.cores
, which can be set for an
entire R session by options(mc.cores=value)
. If the mc.cores
option has not been set then the default is 1
.
(positive integer) Number of threads to use in
within-chain parallelization (defaults to 1
).
logical. Should the chkptstanr
progress be
printed (defaults to TRUE
) ? If set to FALSE
, the standard
cmdstanr
progress bar is printed for each checkpoint (which does not
actually keep track of checkpointing progress)
A named list of parameters to control the sampler's behavior.
It defaults to NULL so all the default values are used. For a comprehensive
overview see stan
.
(positive integer). The seed for random number generation to make results reproducible.
(positive integer). The number of iterations to sample
before stopping. If NULL
, then all iterations are sampled (defaults
to NULL
). Note that sampling will stop at the end of the first
checkpoint which has an iteration number greater than or equal to
stop_after
.
logical. Should the checkpointing be reset? If TRUE
, then
the model will begin sampling from the beginning (defaults to
FALSE
). WARNING: This will remove all previous checkpointing
information (see reset_checkpoints()
). If the model is unchanged and previously
compiled, sampling will begin without recompiling the model.
Character string. The path to the folder, that is used for saving the checkpoints (see Details). You can provide either a relative path to the current working directory or a full path. You no longer need to create the folder, as this is done automatically.
Currently ignored.
An objet of class chkpt_stan
if (FALSE) {
stan_code <- make_stancode(
bf(
formula = count ~ zAge + zBase * Trt + (1 | patient),
family = poisson()
),
data = epilepsy
)
stan_data <- make_standata(
bf(
formula = count ~ zAge + zBase * Trt + (1 | patient),
family = poisson()
),
data = epilepsy
)
# "random" intercept
fit1 <- chkpt_stan(
model_code = stan_code,
data = stan_data,
iter_warmup = 1000,
iter_sampling = 1000,
iter_per_chkpt = 250,
path = "chkpt_folder_fit1"
)
draws <- combine_chkpt_draws(object = fit1)
posterior::summarise_draws(draws)
# eight schools example
stan_code <- "
data {
int<lower=0> n;
real y[n];
real<lower=0> sigma[n];
}
parameters {
real mu;
real<lower=0> tau;
vector[n] eta;
}
transformed parameters {
vector[n] theta;
theta = mu + tau * eta;
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
"
stan_data <- schools.data <- list(
n = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18)
)
fit2 <- chkpt_stan(
model_code = stan_code,
data = stan_data,
iter_warmup = 1000,
iter_sampling = 1000,
iter_per_chkpt = 250,
path = "chkpt_folder_fit2"
)
draws <- combine_chkpt_draws(object = fit2)
posterior::summarise_draws(draws)
}