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:
Warning: There were 1 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
Warning: Examine the pairs() plot to diagnose sampling problems
[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.447580 9.418657 15.031435 6.649415 5.107273 6.397837
[1] 11.417342 8.720120 19.049094 7.807759 5.871701 5.689897
iterations [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 29.102315 -6.8352772 1.23356314 8.976819 -2.8839518 1.865264
[2,] 17.078375 8.6445358 -3.23158464 15.926286 4.6678079 18.680604
[3,] 27.852589 -10.4130976 -7.61875190 8.524452 -0.1367373 4.948742
[4,] 12.382813 16.6561818 9.54715182 9.853305 4.8246508 11.056800
[5,] 5.845018 5.8791792 -3.83363100 3.290390 5.0624421 -3.871691
[6,] 7.617909 0.3362016 0.04841452 7.687758 9.0632974 3.331856
iterations [,7] [,8]
[1,] 23.561906 9.542693
[2,] 5.875668 14.785823
[3,] 18.845638 26.807357
[4,] 18.420371 5.660821
[5,] 19.235571 5.490098
[6,] 6.831372 18.526726
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,] 6.396282 6.354773
[2,] 7.662356 7.568286
[3,] 5.021843 4.966370
[4,] 9.521256 9.710008
[5,] 7.401199 5.294640
[6,] 5.616735 5.596601
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 8.190493020 0.10094285 5.0348597 -1.3684385 4.8798182
tau 6.285897792 0.13946786 5.2226912 0.1791174 2.3471887
eta[1] 0.389103775 0.01389046 0.9405289 -1.5505938 -0.2054586
eta[2] 0.003440379 0.01394031 0.8728298 -1.7514717 -0.5826758
eta[3] -0.169140344 0.01414622 0.9374580 -1.9415179 -0.8225351
eta[4] -0.068875026 0.01369500 0.9015124 -1.8099156 -0.6674928
eta[5] -0.338904184 0.01530117 0.8845929 -2.0306160 -0.9335687
eta[6] -0.206333574 0.01406427 0.9140670 -1.9834252 -0.8101284
eta[7] 0.315543688 0.01418088 0.8983463 -1.5092330 -0.2846886
eta[8] 0.043423175 0.01437340 0.9330859 -1.7953530 -0.5579193
theta[1] 11.266248104 0.16214590 7.8788866 -2.1082879 6.1615017
theta[2] 8.079353246 0.08886517 6.2005540 -4.3028936 4.2950385
theta[3] 6.526529901 0.11547740 7.4404528 -9.5282541 2.6028216
theta[4] 7.623234850 0.09258236 6.4698649 -5.6261091 3.6433675
theta[5] 5.431031450 0.10679264 6.3159180 -8.1187846 1.7478471
theta[6] 6.351951997 0.10774051 6.8250987 -8.4484326 2.6317519
theta[7] 10.548337152 0.10476329 6.5956453 -1.0543288 5.9443021
theta[8] 8.547837597 0.14417203 7.7267428 -6.7253719 3.9902413
lp__ -39.659671566 0.07993048 2.6468184 -45.4316415 -41.2455374
50% 75% 97.5% n_eff Rhat
mu 8.07115584 11.2487511 18.520587 2487.847 1.0001926
tau 5.01773253 8.8707562 19.998788 1402.298 1.0049815
eta[1] 0.41760281 1.0085123 2.205133 4584.704 0.9991427
eta[2] -0.01420980 0.6019420 1.705852 3920.256 0.9991851
eta[3] -0.17329615 0.4738874 1.641107 4391.601 1.0000856
eta[4] -0.08897772 0.5046882 1.792725 4333.305 0.9998628
eta[5] -0.35930807 0.2478095 1.456381 3342.240 0.9997310
eta[6] -0.23573517 0.3955700 1.654451 4223.979 0.9994896
eta[7] 0.32238654 0.9253870 2.046694 4013.111 0.9992923
eta[8] 0.05363753 0.6605782 1.855477 4214.286 0.9995499
theta[1] 10.23376978 15.4109486 30.143158 2361.118 1.0010100
theta[2] 8.05420013 11.8427249 20.318133 4868.530 0.9997838
theta[3] 7.01749339 11.0652904 20.460197 4151.497 1.0003605
theta[4] 7.66750650 11.4617651 20.774681 4883.530 0.9996923
theta[5] 5.79340833 9.5746062 16.998168 3497.762 1.0004061
theta[6] 6.71726075 10.5998817 19.127611 4012.914 0.9994485
theta[7] 9.98507347 14.5519171 24.896658 3963.660 1.0000578
theta[8] 8.27251798 12.7664163 25.330121 2872.307 0.9999784
lp__ -39.48362883 -37.7752727 -35.141510 1096.537 1.0033910
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 8.190493 0.1009429 5.034860 2.063252 14.60106 2487.847 1.000193
tau 6.285898 0.1394679 5.222691 0.885754 13.26074 1402.298 1.004981
Since mu_tau_summary
is a matrix we can pull out columns using their names:
10% 90%
mu 2.063252 14.60106
tau 0.885754 13.26074
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.9335954 0.8281076 0.9218277 0.8700841
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.8768214
$tau
[1] 5.6757
$eta
[1] 1.6005474 1.3092637 0.8760021 1.1847391 0.6396777 -1.0579591 -1.9268414
[8] 1.8571407
$theta
[1] 8.207405 6.554166 4.095104 5.847402 2.753797 -6.881480 -11.812995
[8] 9.663752
The get_seed
function returns the (P)RNG seed as an integer:
[1] 542726306
The get_elapsed_time
function returns a matrix with the warmup and sampling times for each chain:
warmup sample
chain:1 0.034 0.036
chain:2 0.033 0.027
chain:3 0.033 0.036
chain:4 0.031 0.033