This vignette demonstrates how to
access most of data stored in a stanfit object. A stanfit object (an
object of class "stanfit"
) contains the output derived from
fitting a Stan model using Markov chain Monte Carlo or one of Stan’s
variational approximations (meanfield or full-rank). Throughout the
document we’ll use the stanfit object obtained from fitting the Eight
Schools example model:
Error in stanc(file = file, model_code = model_code, model_name = model_name, : 0
Syntax error in 'string', line 3, column 9 to column 10, parsing error:
-------------------------------------------------
1: data {
2: int<lower=0> J; // number of schools
3: real y[J]; // estimated treatment effects
^
4: real<lower=0> sigma[J]; // s.e. of effect estimates
5: }
-------------------------------------------------
";" expected after variable declaration.
It looks like you are trying to use the old array syntax.
Please use the new syntax:
array[J] real y;
Error: object 'fit' not found
There are several functions that can be used to access the draws from
the posterior distribution stored in a stanfit object. These are
extract
, as.matrix
,
as.data.frame
, and as.array
, each of which
returns the draws in a different format.
The extract
function (with its default arguments)
returns a list with named components corresponding to the model
parameters.
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'extract': object 'fit' not found
Error: object 'list_of_draws' not found
In this model the parameters mu
and tau
are
scalars and theta
is a vector with eight elements. This
means that the draws for mu
and tau
will be
vectors (with length equal to the number of post-warmup iterations times
the number of chains) and the draws for theta
will be a
matrix, with each column corresponding to one of the eight
components:
Error: object 'list_of_draws' not found
Error: object 'list_of_draws' not found
Error: object 'list_of_draws' not found
The as.matrix
, as.data.frame
, and
as.array
functions can also be used to retrieve the
posterior draws from a stanfit object:
Error: object 'fit' not found
Error: object 'matrix_of_draws' not found
Error: object 'fit' not found
Error: object 'df_of_draws' not found
Error: object 'fit' not found
Error: object 'array_of_draws' not found
The as.matrix
and as.data.frame
methods
essentially return the same thing except in matrix and data frame form,
respectively. The as.array
method returns the draws from
each chain separately and so has an additional dimension:
Error: object 'matrix_of_draws' not found
Error: object 'df_of_draws' not found
Error: object 'array_of_draws' not found
By default all of the functions for retrieving the posterior draws
return the draws for all parameters (and generated quantities).
The optional argument pars
(a character vector) can be used
if only a subset of the parameters is desired, for example:
Error: object 'fit' not found
Error: object 'mu_and_theta1' not found
Summary statistics are obtained using the summary
function. The object returned is a list with two components:
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'fit' not found
Error: object 'fit_summary' not found
In fit_summary$summary
all chains are merged whereas
fit_summary$c_summary
contains summaries for each chain
individually. Typically we want the summary for all chains merged, which
is what we’ll focus on here.
The summary is a matrix with rows corresponding to parameters and
columns to the various summary quantities. These include the posterior
mean, the posterior standard deviation, and various quantiles computed
from the draws. The probs
argument can be used to specify
which quantiles to compute and pars
can be used to specify
a subset of parameters to include in the summary.
For models fit using MCMC, also included in the summary are the Monte
Carlo standard error (se_mean
), the effective sample size
(n_eff
), and the R-hat statistic (Rhat
).
Error: object 'fit_summary' not found
If, for example, we wanted the only quantiles included to be 10% and
90%, and for only the parameters included to be mu
and
tau
, we would specify that like this:
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'fit' not found
Error: object 'mu_tau_summary' not found
Since mu_tau_summary
is a matrix we can pull out columns
using their names:
Error: object 'mu_tau_summary' not found
Error: object 'mu_tau_80pct' not found
For models fit using MCMC the stanfit object will also contain the
values of parameters used for the sampler. The
get_sampler_params
function can be used to access this
information.
The object returned by get_sampler_params
is a list with
one component (a matrix) per chain. Each of the matrices has number of
columns corresponding to the number of sampler parameters and the column
names provide the parameter names. The optional argument inc_warmup
(defaulting to TRUE
) indicates whether to include the
warmup period.
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'get_sampler_params': object 'fit' not found
NULL
To do things like calculate the average value of
accept_stat__
for each chain (or the maximum value of
treedepth__
for each chain if using the NUTS algorithm,
etc.) the sapply
function is useful as it will apply the
same function to each component of sampler_params
:
Error in x[, "accept_stat__"]: incorrect number of dimensions
Error: object 'mean_accept_stat_by_chain' not found
Error in x[, "treedepth__"]: incorrect number of dimensions
Error: object 'max_treedepth_by_chain' not found
The Stan program itself is also stored in the stanfit object and can
be accessed using get_stancode
:
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'get_stancode': object 'fit' not found
The object code
is a single string and is not very
intelligible when printed:
Error: object 'code' not found
A readable version can be printed using cat
:
Error: object 'code' not found
The get_inits
function returns initial values as a list
with one component per chain. Each component is itself a (named) list
containing the initial values for each parameter for the corresponding
chain:
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'get_inits': object 'fit' not found
Error: object 'inits' not found
Error: object 'inits_chain1' not found
The get_seed
function returns the (P)RNG seed as an
integer:
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'get_seed': object 'fit' not found
The get_elapsed_time
function returns a matrix with the
warmup and sampling times for each chain:
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'get_elapsed_time': object 'fit' not found