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 7 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
Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess
[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] -3.420055 -7.636769 6.752753 10.241792 14.633111 13.282258
[1] 31.5377854 11.9595697 0.7064215 4.9441182 6.3539497 9.2448610
iterations [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 23.328668 4.908283 -1.988749 -4.974494 -2.885298 4.495949
[2,] 5.783575 9.028135 -4.898691 6.076346 -6.322634 -11.944873
[3,] 8.342899 5.188406 6.570914 7.838929 5.130391 6.293183
[4,] 9.797854 12.061193 6.698647 10.992656 5.958245 19.001250
[5,] 14.327440 12.281963 14.135591 7.381758 15.616034 11.046860
[6,] 29.072284 3.530285 21.954457 4.450225 7.875213 5.000080
iterations [,7] [,8]
[1,] 11.061977 39.356458
[2,] 10.386931 -4.978543
[3,] 7.492634 6.343046
[4,] 11.230626 21.430579
[5,] 35.511911 6.945644
[6,] 12.552719 17.143696
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,] 10.90457 13.46021
[2,] 11.27033 12.69268
[3,] 10.71249 26.53400
[4,] 11.02373 19.36241
[5,] 10.88506 13.85685
[6,] 11.74784 21.83248
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.030603344 0.26542383 5.6742704 -2.1830705 4.5565216
tau 7.213479506 0.44059676 6.6965866 0.3014454 2.6621664
eta[1] 0.394916678 0.01765875 0.9482628 -1.5994426 -0.2218139
eta[2] -0.004027778 0.01616322 0.8641011 -1.6820239 -0.5755269
eta[3] -0.195393647 0.01637530 0.9081897 -1.9404889 -0.7908092
eta[4] -0.036321208 0.01553075 0.8782501 -1.7822270 -0.6323282
eta[5] -0.376984343 0.01505687 0.8459914 -2.0225525 -0.9178973
eta[6] -0.204365038 0.01561132 0.8785185 -1.9125689 -0.7889179
eta[7] 0.359734190 0.01448114 0.9101862 -1.5167255 -0.2108986
eta[8] 0.040737550 0.01525718 0.9538197 -1.8756141 -0.5516992
theta[1] 11.548788861 0.18551933 8.6439463 -2.1351265 5.7653851
theta[2] 7.961949390 0.10111970 6.3739496 -4.4012686 3.8571044
theta[3] 5.908690714 0.14937414 7.7232425 -11.1936806 1.7873279
theta[4] 7.470044009 0.12267794 6.5328834 -6.2710086 3.5390086
theta[5] 4.896064527 0.13008943 6.3010054 -8.2358431 0.9229122
theta[6] 6.073576106 0.12062448 6.6664081 -8.3197327 2.2985435
theta[7] 10.911262420 0.16142578 7.3033667 -1.8074265 6.1478477
theta[8] 8.181137220 0.13729735 8.0323654 -7.3066911 3.5856147
lp__ -39.443754541 0.07541723 2.6699949 -45.2890695 -41.0630521
50% 75% 97.5% n_eff Rhat
mu 7.82567126 11.1189662 20.317214 457.0253 1.0026645
tau 5.62607668 9.7404773 24.091436 231.0066 1.0181085
eta[1] 0.44080660 1.0314630 2.198051 2883.6159 1.0002245
eta[2] -0.01271811 0.5693325 1.714931 2858.0726 0.9997304
eta[3] -0.20716612 0.3862526 1.628383 3075.9180 1.0007039
eta[4] -0.04032776 0.5630892 1.689534 3197.8020 1.0002339
eta[5] -0.39894749 0.1500136 1.339926 3156.9139 0.9996941
eta[6] -0.22223886 0.3723464 1.556807 3166.8131 1.0012703
eta[7] 0.38450099 0.9534831 2.076774 3950.5251 1.0003008
eta[8] 0.03486754 0.6659002 1.972341 3908.2667 1.0001888
theta[1] 10.26541805 15.8761013 32.540459 2170.9299 1.0031981
theta[2] 7.91262545 11.8549060 21.214381 3973.2481 0.9994947
theta[3] 6.36520775 10.6887695 20.265882 2673.3050 1.0024376
theta[4] 7.62832952 11.7781310 19.764174 2835.8083 0.9996138
theta[5] 5.20764105 9.1252087 16.159653 2346.0415 1.0000503
theta[6] 6.40440583 10.2681486 18.875826 3054.3085 1.0015452
theta[7] 10.17755410 14.9615229 28.295741 2046.9180 1.0012093
theta[8] 7.88597197 12.5045578 25.832510 3422.6508 0.9996538
lp__ -39.18186756 -37.5475367 -34.941370 1253.3711 1.0028665
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.030603 0.2654238 5.674270 1.469753 14.57340 457.0253 1.002664
tau 7.213480 0.4405968 6.696587 1.044260 14.62933 231.0066 1.018109
Since mu_tau_summary is a matrix we can pull out columns
using their names:
10% 90%
mu 1.469753 14.57340
tau 1.044260 14.62933
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.8193160 0.8812611 0.8866066 0.7414120
max_treedepth_by_chain <- sapply(sampler_params, function(x) max(x[, "treedepth__"]))
print(max_treedepth_by_chain)[1] 4 4 4 5
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 array[J] real y; // estimated treatment effects\n array[J] real<lower=0> sigma; // 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
array[J] real y; // estimated treatment effects
array[J] real<lower=0> sigma; // 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.5414148
$tau
[1] 5.644424
$eta
[1] 1.3262079 -0.4528914 0.5457449 -1.1831928 -1.5947169 0.8760721 0.6773341
[8] -0.1397527
$theta
[1] 6.944265 -3.097726 2.539001 -7.219857 -9.542674 4.403508 3.281746
[8] -1.330238
The get_seed function returns the (P)RNG seed as an
integer:
[1] 2000439405