R/model.R
modelmethodgeneratequantities.Rd
The $generate_quantities()
method of a CmdStanModel
object
runs Stan's standalone generated quantities to obtain generated quantities
based on previously fitted parameters.
generate_quantities( fitted_params, data = NULL, seed = NULL, output_dir = NULL, output_basename = NULL, sig_figs = NULL, parallel_chains = getOption("mc.cores", 1), threads_per_chain = NULL, opencl_ids = NULL )
fitted_params  (multiple options) The parameter draws to use. One of the following:
NOTE: if you plan on making many calls to 

data  (multiple options) The data to use for the variables specified in the data block of the Stan program. One of the following:

seed  (positive integer(s)) A seed for the (P)RNG to pass to CmdStan.
In the case of multichain sampling the single 
output_dir  (string) A path to a directory where CmdStan should write
its output CSV files. For interactive use this can typically be left at

output_basename  (string) A string to use as a prefix for the names of
the output CSV files of CmdStan. If 
sig_figs  (positive integer) The number of significant figures used
when storing the output values. By default, CmdStan represent the output
values with 6 significant figures. The upper limit for 
parallel_chains  (positive integer) The maximum number of MCMC chains
to run in parallel. If 
threads_per_chain  (positive integer) If the model was
compiled with threading support, the number of
threads to use in parallelized sections within an MCMC chain (e.g., when
using the Stan functions 
opencl_ids  (integer vector of length 2) The platform and
device IDs of the OpenCL device to use for fitting. The model must
be compiled with 
A CmdStanGQ
object.
The CmdStanR website (mcstan.org/cmdstanr) for online documentation and tutorials.
The Stan and CmdStan documentation:
Stan documentation: mcstan.org/users/documentation
CmdStan User’s Guide: mcstan.org/docs/cmdstanguide
Other CmdStanModel methods:
modelmethodcheck_syntax
,
modelmethodcompile
,
modelmethoddiagnose
,
modelmethodformat
,
modelmethodoptimize
,
modelmethodsample_mpi
,
modelmethodsample
,
modelmethodvariables
,
modelmethodvariational
# \dontrun{ # first fit a model using MCMC mcmc_program < write_stan_file( "data { int<lower=0> N; int<lower=0,upper=1> y[N]; } parameters { real<lower=0,upper=1> theta; } model { y ~ bernoulli(theta); }" ) mod_mcmc < cmdstan_model(mcmc_program) data < list(N = 10, y = c(1,1,0,0,0,1,0,1,0,0)) fit_mcmc < mod_mcmc$sample(data = data, seed = 123, refresh = 0)#> Running MCMC with 4 sequential chains... #> #> Chain 1 finished in 0.0 seconds. #> Chain 2 finished in 0.0 seconds. #> Chain 3 finished in 0.0 seconds. #> Chain 4 finished in 0.0 seconds. #> #> All 4 chains finished successfully. #> Mean chain execution time: 0.0 seconds. #> Total execution time: 0.6 seconds. #># stan program for standalone generated quantities # (could keep model block, but not necessary so removing it) gq_program < write_stan_file( "data { int<lower=0> N; int<lower=0,upper=1> y[N]; } parameters { real<lower=0,upper=1> theta; } generated quantities { int y_rep[N] = bernoulli_rng(rep_vector(theta, N)); }" ) mod_gq < cmdstan_model(gq_program) fit_gq < mod_gq$generate_quantities(fit_mcmc, data = data, seed = 123)#> Running standalone generated quantities after 4 MCMC chains, 1 chain at a time ... #> #> Chain 1 finished in 0.0 seconds. #> Chain 2 finished in 0.0 seconds. #> Chain 3 finished in 0.0 seconds. #> Chain 4 finished in 0.0 seconds. #> #> All 4 chains finished successfully. #> Mean chain execution time: 0.0 seconds. #> Total execution time: 0.5 seconds.#> 'draws_array' int [1:1000, 1:4, 1:10] 0 0 0 0 1 0 1 0 0 0 ... #>  attr(*, "dimnames")=List of 3 #> ..$ iteration: chr [1:1000] "1" "2" "3" "4" ... #> ..$ chain : chr [1:4] "1" "2" "3" "4" #> ..$ variable : chr [1:10] "y_rep[1]" "y_rep[2]" "y_rep[3]" "y_rep[4]" ...#> # A draws_df: 1000 iterations, 4 chains, and 10 variables #> y_rep[1] y_rep[2] y_rep[3] y_rep[4] y_rep[5] y_rep[6] y_rep[7] y_rep[8] #> 1 0 0 0 0 0 0 0 0 #> 2 0 1 1 1 1 1 1 1 #> 3 0 0 0 0 0 1 1 1 #> 4 0 0 0 0 0 1 0 1 #> 5 1 1 1 1 1 0 1 0 #> 6 0 0 0 0 1 0 1 1 #> 7 1 0 0 0 1 1 1 0 #> 8 0 1 0 0 0 1 0 1 #> 9 0 1 0 1 1 1 1 1 #> 10 0 1 0 1 0 0 0 0 #> # ... with 3990 more draws, and 2 more variables #> # ... hidden reserved variables {'.chain', '.iteration', '.draw'}# }