generate_posterior_quantities: Generate posterior quantities following MCMC

View source: R/generate_posterior_quantities.R

generate_posterior_quantitiesR Documentation

Generate posterior quantities following MCMC

Description

generate_posterior_quantities automates the CmdStanR $generate_quantities method, which enables the "generated quantities" block of a Stan model to be sampled from after a model is fit. This vastly reduces memory requirements, particularly if per-sample posterior predictions are desired.

Usage

generate_posterior_quantities(
  fit_mcmc,
  data_list,
  out_dir = "outputs/cmdstan",
  save_model_as = "",
  return_type = c("paths", "draws_list"),
  par_chains = getOption("mc.cores", 4)
)

Arguments

fit_mcmc

CmdStanR model environment or a full path to a saved CmdStanR fit (as an .RDS file).

data_list

Raw data that fit was fit to; either an R object or full path to an .RDS file.

out_dir

Path to output directory (relative to working directory).

save_model_as

Name to give model; defaults to model name from the fit metadata.

return_type

Return something other than the fit environment? Options are "paths" (for .csv file paths), or "draws_list" which returns the posterior predictions as a posterior::draws_list.

par_chains

Maximum number of chains to compute in parallel; defaults to options(mc.cores) if this has been set, or 4 if not.

Value

Either file paths or a posterior::draws_list.

Examples

## Not run: 
data(example_data)

fit <- fit_learning_model(
  example_data$nd,
  model = "2a",
  vb = FALSE,
  exp_part = "training"
)

posterior_preds <- generate_posterior_quantities(
  fit_mcmc = fit,
  data_list = fit$stan_datalist,
  return_type = "draws_list"
)

## End(Not run)


qdercon/pstpipeline documentation built on June 1, 2025, 1:11 p.m.