Learn R Programming

rstan (version 2.8.2)

stan_model: Construct a Stan model

Description

Construct an instance of S4 class stanmodel from a model specified in Stan's modeling language. A stanmodel can be used to draw samples from the model. If the model is supplied in the Stan modeling language, it is first translated to C++ code. The C++ code for the model plus other auxiliary code is compiled into a dynamic shared object (DSO) and then loaded. The loaded DSO for the model can be executed to draw samples, allowing inference to be performed for the model and data.

Usage

stan_model(file, model_name = "anon_model", 
             model_code = "", stanc_ret = NULL, 
             boost_lib = NULL, eigen_lib = NULL, 
             save_dso = TRUE, verbose = FALSE, 
             auto_write = rstan_options("auto_write"), 
             obfuscate_model_name = TRUE)

Arguments

file
A character string or a connection that Rsupports specifying the Stan model specification in Stan's modeling language.
model_name
A character string naming the model; defaults to "anon_model". However, the model name would be derived from file or model_code (if model_code is the name of a character string object) if <
model_code
A character string either containing the model specification or the name of a character string object in the workspace; an alternative is to specify the model with parameters file or stanc_ret.
stanc_ret
A named list returned from a previous call to function stanc. The list can be used to specify the model instead of using parameter file or model_code.
boost_lib
The path to a version of the Boost C++ library to use instead of the one in the BH package.
eigen_lib
The path to a version of the Eigen C++ library to use instead of the one in the RcppEigen package.
save_dso
Logical with the default of TRUE: indication of whether the dynamic shared object (DSO) compiled from the C++ code for the model will be saved or not. If TRUE, we can draw samples from the same model in another Rses
verbose
TRUE or FALSE: indication of whether to report intermediate output to the console, which might be helpful for debugging.
auto_write
TRUE or FALSE: indication of whether to write the object to the hard disk using saveRDS. By default, this argument is FALSE, but we recommend to always avoid t
obfuscate_model_name
A logical scalar that is TRUE by default and passed to stanc

Value

  • An instance of S4 class stanmodel, which can be used later for drawing samples by calling its sampling function.

Details

If a previously compiled stan_model exists on the hard drive, its validity is checked and then returned without recompiling. To avoid reading previously compiled stan_models from the hard drive, supply the stanc_ret argument rather than the file or model_code arguments. More details of Stan, including the full user's guide and reference manual can be found at http://mc-stan.org/.

There are three ways to specify the model's code for stan_model.

  1. parametermodel_code, containing character string to whose value is the Stan model specification,
  2. parameterfile, indicating a file (or a connection) from which to read the Stan model specification, or
  3. parameterstanc_ret, indicating the re-use of a model generated in a previous call tostanc.

References

The Stan Development Team Stan Modeling Language User's Guide and Reference Manual. http://mc-stan.org/.

See Also

stanmodel and stan

Examples

Run this code
stan_model(model_code = 'parameters {real y;} model {y ~ normal(0,1);}')

Run the code above in your browser using DataLab