The $variables() method of a CmdStanModel object returns a
list, each element representing a Stan model block: data, parameters,
transformed_parameters and generated_quantities. The information
describes the program the executable was built from, captured when the
model object was created.
The standalone function variables_stan_file() returns the same list for a
Stan program as it is now, without creating a model object or compiling.
Each element in the returned object contains a list of variables, with each
variable represented as a list with information on its scalar type (real,
int or complex) and number of dimensions. A tuple's type is instead
a list with one such entry per tuple element, nested for nested tuples,
and its dimensions count only the array dimensions around the tuple.
The number of dimensions reported is the number of indexing dimensions in
the declared Stan variable, equivalently the number of indices needed to
access one scalar element. This means a scalar has 0 dimensions, a vector
or one-dimensional array has 1, and a matrix or two-dimensional array has
2. Array dimensions are added to any vector or matrix dimensions, so
array[J] matrix[N, K] has 3 dimensions. See Examples.
transformed data is not included, as variables in that block are not
part of the model's input or output.
Arguments
- stan_file
(string) The path to a Stan program.
- include_paths
(character vector) Paths to directories where Stan should look for files specified in
#includedirectives in the Stan program. Relative paths are resolved against the working directory when the model object is created and stored as absolute paths, so subsequent changes to the working directory do not affect them. When the program contains#includedirectives and no paths are given, the program's own directory is used.
See also
write_stan_json() for writing data for CmdStan.
Other CmdStanModel methods:
model-method-build_info,
model-method-check_syntax,
model-method-cmdstan_defaults,
model-method-diagnose,
model-method-expose_functions,
model-method-format,
model-method-generate-quantities,
model-method-is_current,
model-method-laplace,
model-method-model-info,
model-method-optimize,
model-method-pathfinder,
model-method-sample,
model-method-sample_mpi,
model-method-variational
Examples
# \dontrun{
stan_file <- write_stan_file("
data {
int N;
array[2, 3] int y;
}
parameters {
real alpha;
vector[N] beta;
array[2] matrix[3, 4] theta;
}
")
mod <- cmdstan_model(stan_file)
vars <- mod$variables()
str(vars)
#> List of 5
#> $ parameters :List of 3
#> ..$ alpha:List of 2
#> .. ..$ type : chr "real"
#> .. ..$ dimensions: int 0
#> ..$ beta :List of 2
#> .. ..$ type : chr "real"
#> .. ..$ dimensions: int 1
#> ..$ theta:List of 2
#> .. ..$ type : chr "real"
#> .. ..$ dimensions: int 3
#> $ included_files : list()
#> $ data :List of 2
#> ..$ N:List of 2
#> .. ..$ type : chr "int"
#> .. ..$ dimensions: int 0
#> ..$ y:List of 2
#> .. ..$ type : chr "int"
#> .. ..$ dimensions: int 2
#> $ transformed_parameters: Named list()
#> $ generated_quantities : Named list()
# }