Learn R Programming

rstan (version 2.8.2)

stanc: Translate Stan model specification to C++ code

Description

Translate Stan model specification to C++ code, which can then be compiled and loaded for sampling.

Usage

stanc(file, model_code = '', model_name = "anon_model", verbose = FALSE,
        obfuscate_model_name = TRUE) 
  stanc_builder(file, isystem = dirname(file), 
                verbose = FALSE, obfuscate_model_name = FALSE)

Arguments

file
A character string or a connection that Rsupports specifying the Stan model specification in Stan's modeling language.
model_code
A character string either containing a Stan model specification or the name of a character string object in the workspace. This parameter is used only if parameter file is not specified, so it defaults to empty string.
model_name
A character string naming the model. The default is "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
verbose
TRUE print out more intermediate information during the translation procedure; FALSE otherwise. The default is FALSE.
obfuscate_model_name
A logical scalar indicating whether to use a randomly-generated character string for the name of the C++ class. Setting this to TRUE prevents name clashes when compiling multiple models in the same R session.
isystem
A character vector of length one naming a path to look for file paths in file that are to be included within the Stan program named by file. See details.

Value

  • A list with named entries:
    1. model_nameCharacter string for the model name.
    2. model_codeCharacter string for the model's Stan specification.
    3. cppcodeCharacter string for the model's C++ code.
    4. statusLogical indicating success/failure (TRUE/FALSE) of translating the Stan code.

Details

The stanc_builder function supports the standard C++ convention of specifying something like #include "my_includes.txt" on an entire line within the file named by the file argument. In other words, stanc_builder would look for "my_includes.txt" in (or under) the directory named by the isystem argument and insert its contents verbatim at that position before calling stanc on the resulting model_code. This mechanism reduces the need to copy common chunks of code across Stan programs. Note that line numbers referred to in parser warnings or errors refer to the postprocessed Stan program rather than file. In the case of a parser error, the postprocessed Stan program will be printed after the error message. Line numbers referred to in messages while Stan is executing also refer to the postprocessed Stan program which can be obtained by calling get_stancode.

References

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

The Stan Development Team CmdStan Interface User's Guide. http://mc-stan.org.

See Also

stan_model and stan

Examples

Run this code
stanmodelcode <- "data {
  int<lower=0> N;
  real y[N];
} 

parameters {
  real mu;
} 

model {
  mu ~ normal(0, 10);
  y ~ normal(mu, 1); 
} 
"

r <- stanc(model_code = stanmodelcode, model_name = "normal1") 
str(r)

Run the code above in your browser using DataLab