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:
[1] "stanfit"
attr(,"package")
[1] "rstan"
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.
[1] "mu" "tau" "eta" "theta" "lp__"
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:
[1] 2.717288 6.126390 9.269763 5.466024 12.371736 12.728201
[1] 8.127311 6.099894 8.431778 20.337739 16.045552 0.629017
iterations [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 17.37919 6.324138 5.510211 0.7914177 2.904062 6.276932
[2,] 11.34469 -5.869765 5.637176 8.5654652 4.030264 16.738744
[3,] 18.70269 11.845173 2.530775 7.8741726 1.411100 -5.637879
[4,] 30.95059 -10.294568 1.955394 7.5121802 -10.532689 -5.168254
[5,] 21.34431 3.995896 8.344221 2.2093363 -9.902931 -2.376102
[6,] 12.73459 12.518335 13.578908 13.0927392 13.360413 13.179632
iterations [,7] [,8]
[1,] 5.839641 2.403244
[2,] 6.168454 4.611533
[3,] 9.336305 9.989825
[4,] 18.488237 21.460631
[5,] 15.664796 10.110411
[6,] 12.256780 12.888348
The as.matrix
, as.data.frame
, and
as.array
functions can also be used to retrieve the
posterior draws from a stanfit object:
[1] "mu" "tau" "eta[1]" "eta[2]" "eta[3]" "eta[4]"
[7] "eta[5]" "eta[6]" "eta[7]" "eta[8]" "theta[1]" "theta[2]"
[13] "theta[3]" "theta[4]" "theta[5]" "theta[6]" "theta[7]" "theta[8]"
[19] "lp__"
[1] "mu" "tau" "eta[1]" "eta[2]" "eta[3]" "eta[4]"
[7] "eta[5]" "eta[6]" "eta[7]" "eta[8]" "theta[1]" "theta[2]"
[13] "theta[3]" "theta[4]" "theta[5]" "theta[6]" "theta[7]" "theta[8]"
[19] "lp__"
$iterations
NULL
$chains
[1] "chain:1" "chain:2" "chain:3" "chain:4"
$parameters
[1] "mu" "tau" "eta[1]" "eta[2]" "eta[3]" "eta[4]"
[7] "eta[5]" "eta[6]" "eta[7]" "eta[8]" "theta[1]" "theta[2]"
[13] "theta[3]" "theta[4]" "theta[5]" "theta[6]" "theta[7]" "theta[8]"
[19] "lp__"
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:
[1] 4000 19
[1] 4000 19
[1] 1000 4 19
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:
parameters
iterations mu theta[1]
[1,] 9.227737 8.299509
[2,] 8.082361 5.790565
[3,] 8.722221 9.479570
[4,] 8.315916 15.959164
[5,] 3.037107 -1.764284
[6,] 10.983375 22.235240
Summary statistics are obtained using the summary
function. The object returned is a list with two components:
[1] "summary" "c_summary"
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
).
mean se_mean sd 2.5% 25%
mu 7.791069797 0.11047203 5.0284092 -1.823628 4.5833891
tau 6.621189538 0.15192887 5.7078160 0.213518 2.3989686
eta[1] 0.375167501 0.01413084 0.9043180 -1.440227 -0.2153100
eta[2] 0.006079915 0.01432982 0.8725014 -1.753491 -0.5402504
eta[3] -0.206966717 0.01363669 0.9154714 -1.952194 -0.8283019
eta[4] -0.036063071 0.01393823 0.8892260 -1.788453 -0.6291556
eta[5] -0.332470034 0.01392901 0.8866139 -2.005504 -0.9088546
eta[6] -0.201343116 0.01341847 0.8775020 -1.907306 -0.7899503
eta[7] 0.344761555 0.01401385 0.9037140 -1.473784 -0.2554285
eta[8] 0.053791608 0.01466782 0.9319365 -1.781152 -0.5517679
theta[1] 11.128155093 0.14970977 8.0661605 -1.681819 5.8217849
theta[2] 7.687423157 0.09307702 6.2900071 -4.955894 3.7368645
theta[3] 6.023535979 0.12609781 7.5978651 -11.143391 1.8603687
theta[4] 7.339617186 0.09576759 6.5351425 -6.595344 3.3172772
theta[5] 5.166822458 0.10259851 6.3686654 -9.257621 1.3233358
theta[6] 6.198681519 0.10924196 6.6624817 -8.143649 2.3000815
theta[7] 10.570322104 0.11143987 6.8693376 -1.487116 5.9502232
theta[8] 8.351327207 0.13041952 7.8078084 -6.844088 3.7857475
lp__ -39.530966428 0.07606357 2.6295864 -45.376958 -41.0873111
50% 75% 97.5% n_eff Rhat
mu 7.713022157 11.0793992 17.715051 2071.842 0.9999567
tau 5.270943971 9.2272232 21.452016 1411.430 1.0003046
eta[1] 0.379224322 1.0015137 2.168445 4095.497 0.9991383
eta[2] 0.002921243 0.5609610 1.756944 3707.242 0.9993053
eta[3] -0.215252842 0.3908249 1.642870 4506.834 0.9996002
eta[4] -0.059869812 0.5115277 1.770089 4070.135 0.9997199
eta[5] -0.375113690 0.2036155 1.498410 4051.617 0.9993853
eta[6] -0.219602807 0.3599869 1.570985 4276.520 0.9999683
eta[7] 0.360984365 0.9562135 2.123045 4158.599 0.9993518
eta[8] 0.048763639 0.6783403 1.880174 4036.841 0.9994810
theta[1] 10.132380305 15.3222404 30.952250 2902.909 0.9998498
theta[2] 7.747102020 11.5978132 20.200951 4566.856 0.9994868
theta[3] 6.512108981 10.6715201 20.028821 3630.513 0.9992852
theta[4] 7.438175396 11.5210522 20.094142 4656.644 0.9997170
theta[5] 5.584636110 9.4153610 16.423056 3853.140 0.9992456
theta[6] 6.503581442 10.4355377 18.885154 3719.573 0.9998555
theta[7] 9.974653355 14.5531686 25.620589 3799.693 0.9995101
theta[8] 8.016680253 12.4698016 25.353115 3584.042 0.9996276
lp__ -39.342765161 -37.7184744 -34.893597 1195.147 1.0000859
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:
mu_tau_summary <- summary(fit, pars = c("mu", "tau"), probs = c(0.1, 0.9))$summary
print(mu_tau_summary)
mean se_mean sd 10% 90% n_eff Rhat
mu 7.79107 0.1104720 5.028409 1.4410093 13.96417 2071.842 0.9999567
tau 6.62119 0.1519289 5.707816 0.9939053 13.94651 1411.430 1.0003046
Since mu_tau_summary
is a matrix we can pull out columns
using their names:
10% 90%
mu 1.4410093 13.96417
tau 0.9939053 13.94651
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.
sampler_params <- get_sampler_params(fit, inc_warmup = FALSE)
sampler_params_chain1 <- sampler_params[[1]]
colnames(sampler_params_chain1)
[1] "accept_stat__" "stepsize__" "treedepth__" "n_leapfrog__"
[5] "divergent__" "energy__"
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
:
mean_accept_stat_by_chain <- sapply(sampler_params, function(x) mean(x[, "accept_stat__"]))
print(mean_accept_stat_by_chain)
[1] 0.9224838 0.8551551 0.8899882 0.8782613
max_treedepth_by_chain <- sapply(sampler_params, function(x) max(x[, "treedepth__"]))
print(max_treedepth_by_chain)
[1] 4 4 4 4
The Stan program itself is also stored in the stanfit object and can
be accessed using get_stancode
:
The object code
is a single string and is not very
intelligible when printed:
[1] "data {\n int<lower=0> J; // number of schools\n real y[J]; // estimated treatment effects\n real<lower=0> sigma[J]; // s.e. of effect estimates\n}\nparameters {\n real mu;\n real<lower=0> tau;\n vector[J] eta;\n}\ntransformed parameters {\n vector[J] theta;\n theta = mu + tau * eta;\n}\nmodel {\n target += normal_lpdf(eta | 0, 1);\n target += normal_lpdf(y | theta, sigma);\n}"
attr(,"model_name2")
[1] "schools"
A readable version can be printed using cat
:
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
vector[J] theta;
theta = mu + tau * eta;
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
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:
$mu
[1] 0.8020384
$tau
[1] 2.83205
$eta
[1] -0.7138932 1.5828580 -1.7463003 0.5080906 0.9833842 -1.2372880 0.8546742
[8] -0.5458396
$theta
[1] -1.2197427 5.2847710 -4.1435708 2.2409763 3.5870313 -2.7020228 3.2225183
[8] -0.7438065
The get_seed
function returns the (P)RNG seed as an
integer:
[1] 806056744