Run Bayesian model
Usage
run_model(
model_data,
refresh = 100,
chains = 4,
parallel_chains = 4,
iter_warmup = 1000,
iter_sampling = 1000,
adapt_delta = 0.95,
max_treedepth = 14,
k = NULL,
output_basename = NULL,
output_dir = ".",
save_model = TRUE,
overwrite = FALSE,
set_seed = NULL,
quiet = FALSE,
jags_data,
inits,
parameters_to_save,
track_n,
n_adapt,
n_burnin,
n_thin,
n_chains,
n_saved_steps,
n_iter,
modules,
parallel,
n_cores,
...
)
Arguments
- model_data
List. Model data generated by
prepare_model()
.- refresh
Numeric. Passed to
cmdstanr::sample()
. Number of iterations between screen updates. If 0, only errors are shown.- chains
Numeric. Passed to
cmdstanr::sample()
. Number of Markov chains to run.- parallel_chains
Numeric. Passed to
cmdstanr::sample()
. Maximum number of chains to run in parallel.- iter_warmup
Numeric. Passed to
cmdstanr::sample()
. Number of warmup iterations per chain.- iter_sampling
Numeric. Passed to
cmdstanr::sample()
. Number of sampling (post-warmup) iterations per chain.- adapt_delta
Numeric. Passed to
cmdstanr::sample()
. The adaptation target acceptance statistic.- max_treedepth
Numeric. Passed to
cmdstanr::sample()
. The maximum allowed tree depth for the NUTS engine. See?cmdstanr::sample
.- k
Numeric. The K-fold group to run for cross-validation. Only relevant if folds defined by
prepare_model(calculate_cv = TRUE)
or custom definition. See?prepare_model
or the models article for more details.- output_basename
Character. Name of the files created as part of the Stan model run and the final model output RDS file if
save_model = TRUE
.- output_dir
Character. Directory in which all model files will be created.
- save_model
Logical. Whether or not to save the model output to file as an RDS object with all required data. Defaults to
TRUE
.- overwrite
Logical. Whether to overwrite an existing model output file when saving
- set_seed
Numeric. If
NULL
(default) no seed is set. Otherwise an integer number to be used withwithr::with_seed()
internally to ensure reproducibility.- quiet
Logical. Suppress progress messages? Default
FALSE
.- jags_data
Defunct.
- inits
Defunct.
- parameters_to_save
Defunct.
- track_n
Defunct.
- n_adapt
Defunct.
- n_burnin
Defunct.
- n_thin
Defunct.
- n_chains
Defunct.
- n_saved_steps
Defunct.
- n_iter
Defunct.
- modules
Defunct.
- parallel
Defunct.
- n_cores
Defunct.
- ...
Other arguments passed on to
cmdstanr::sample()
Value
A list containing the model output (model_fit
), meta data for the
analysis (meta_data
), meta data for the strata (meta_strata
) and
prepared data counts from prepare_data()
(raw_data
).
Details
The model is set up in prepare_model()
. The run_model()
function
does the final (and often long-running) step of actually running the model.
Here is where you can tweak how the model will be run (iterations etc.).
See the models article for more advanced examples and explanations.
Examples
s <- stratify(by = "bbs_cws", sample_data = TRUE)
#> Using 'bbs_cws' (standard) stratification
#> Using sample BBS data...
#> Using species Pacific Wren (sample data)
#> Stratifying data...
#> Combining BCR 7 and NS and PEI...
#> Renaming routes...
p <- prepare_data(s)
pm <- prepare_model(p, model = "first_diff", model_variant = "hier")
# Run model (quick and dirty)
m <- run_model(pm, iter_warmup = 20, iter_sampling = 20, chains = 2)
#> One set of `init` values supplied, duplicating for each chain.
#> Running MCMC with 2 chains, at most 4 in parallel...
#>
#> Chain 1 WARNING: There aren't enough warmup iterations to fit the
#> Chain 1 three stages of adaptation as currently configured.
#> Chain 1 Reducing each adaptation stage to 15%/75%/10% of
#> Chain 1 the given number of warmup iterations:
#> Chain 1 init_buffer = 3
#> Chain 1 adapt_window = 15
#> Chain 1 term_buffer = 2
#> Chain 1 Iteration: 1 / 40 [ 2%] (Warmup)
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: neg_binomial_2_log_lpmf: Precision parameter is 0, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: neg_binomial_2_log_lpmf: Precision parameter is 0, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: neg_binomial_2_log_lpmf: Precision parameter is 0, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: neg_binomial_2_log_lpmf: Precision parameter is inf, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 2 WARNING: There aren't enough warmup iterations to fit the
#> Chain 2 three stages of adaptation as currently configured.
#> Chain 2 Reducing each adaptation stage to 15%/75%/10% of
#> Chain 2 the given number of warmup iterations:
#> Chain 2 init_buffer = 3
#> Chain 2 adapt_window = 15
#> Chain 2 term_buffer = 2
#> Chain 2 Iteration: 1 / 40 [ 2%] (Warmup)
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: neg_binomial_2_log_lpmf: Precision parameter is 0, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: neg_binomial_2_log_lpmf: Precision parameter is 0, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: neg_binomial_2_log_lpmf: Precision parameter is 0, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: neg_binomial_2_log_lpmf: Precision parameter is inf, but must be positive finite! (in '/tmp/RtmpDv9MRA/model-24731b8f724d.stan', line 227, column 3 to column 40)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 1 Iteration: 21 / 40 [ 52%] (Sampling)
#> Chain 2 Iteration: 21 / 40 [ 52%] (Sampling)
#> Chain 1 Iteration: 40 / 40 [100%] (Sampling)
#> Chain 1 finished in 136.0 seconds.
#> Chain 2 Iteration: 40 / 40 [100%] (Sampling)
#> Chain 2 finished in 145.1 seconds.
#>
#> Both chains finished successfully.
#> Mean chain execution time: 140.6 seconds.
#> Total execution time: 145.3 seconds.
#>
#> Saving model output to /home/runner/work/bbsBayes/bbsBayes/docs/reference/BBS_STAN_first_diff_hier_2023-01-17.rds
# Clean up (remove model files)
unlink(list.files(pattern = paste0("BBS_STAN_first_diff_hier_", Sys.Date())))