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 11 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] 9.264387 7.532259 8.321515 5.063982 10.915901 7.068204
[1] 4.3749946 6.6523166 8.1629323 3.7559417 0.7335339 7.0144202
iterations [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 13.508123 12.927211 7.084756 4.4389547 1.0645882 4.302487
[2,] 5.197941 10.263788 4.846419 4.6961940 -0.8791130 5.646958
[3,] 5.197980 8.387341 11.082761 11.2908558 -0.9776036 6.858847
[4,] 7.261789 4.234994 12.282606 6.5455962 -2.4357326 5.333933
[5,] 11.392526 11.063745 11.492085 10.0636099 11.1234929 10.903826
[6,] 15.417872 13.534648 12.431409 0.1939912 2.7519099 6.717547
iterations [,7] [,8]
[1,] 8.013038 8.496487
[2,] 7.149842 -6.239125
[3,] 14.152487 5.466075
[4,] 3.515676 -0.588059
[5,] 12.211917 11.563312
[6,] 6.500746 12.059947
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.438777 13.478654
[2,] 11.899997 13.305585
[3,] 6.643817 6.187893
[4,] 10.217506 5.426754
[5,] 2.984540 3.423543
[6,] 11.299232 11.178555
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% 50%
mu 8.08544776 0.32209123 5.6748607 -2.5918479 4.6318634 7.92964066
tau 7.01933763 0.18877506 6.1071511 0.2232951 2.5054145 5.55771763
eta[1] 0.41550731 0.01487660 0.9561075 -1.4997298 -0.2148951 0.44354619
eta[2] -0.01685718 0.02124064 0.8696249 -1.7926319 -0.5700728 -0.01339714
eta[3] -0.21022979 0.01609943 0.9029323 -1.9833995 -0.8059864 -0.22408078
eta[4] -0.03443285 0.01607047 0.8837582 -1.7100056 -0.6272921 -0.02900023
eta[5] -0.35470986 0.01711456 0.8787852 -2.0360756 -0.9386034 -0.36269568
eta[6] -0.24150626 0.01879604 0.9178001 -1.9974791 -0.8418028 -0.25375199
eta[7] 0.33844628 0.01753914 0.9071545 -1.4909339 -0.2298367 0.34134105
eta[8] 0.05865807 0.01348561 0.9075872 -1.7366427 -0.5438963 0.07013935
theta[1] 11.97894788 0.30032352 8.8087747 -1.8607081 6.1719763 10.49446476
theta[2] 7.81461589 0.09970294 6.1058058 -4.2250125 4.0124304 7.86519069
theta[3] 5.98916039 0.16433131 8.0652537 -11.7258489 1.6939847 6.58373839
theta[4] 7.63439616 0.10233882 6.5850975 -6.2514380 3.7814608 7.71982588
theta[5] 4.90246034 0.11275064 6.5092552 -9.5066672 1.1063243 5.44789796
theta[6] 5.97410715 0.10278816 6.8256699 -9.0398858 2.1317648 6.41394148
theta[7] 10.78762539 0.13435774 6.7442684 -0.9500700 6.1573886 10.31260163
theta[8] 8.72068717 0.30464080 8.2737452 -6.9337489 3.6205341 8.28882436
lp__ -39.48940697 0.07674344 2.6688863 -45.4680362 -41.1234562 -39.18738069
75% 97.5% n_eff Rhat
mu 11.2560414 19.476027 310.4221 1.0117287
tau 9.9308854 21.944336 1046.6178 1.0018334
eta[1] 1.0394846 2.286120 4130.5329 0.9995223
eta[2] 0.5396902 1.760146 1676.2105 1.0014133
eta[3] 0.3789947 1.612031 3145.4982 1.0001675
eta[4] 0.5376120 1.684640 3024.1941 1.0018620
eta[5] 0.2049529 1.452015 2636.5380 1.0034160
eta[6] 0.3716703 1.593875 2384.3150 1.0019915
eta[7] 0.9418666 2.056268 2675.1349 1.0002056
eta[8] 0.6818988 1.790606 4529.3455 1.0004780
theta[1] 16.3747148 34.356536 860.3048 1.0037847
theta[2] 11.6544904 20.472616 3750.3349 0.9995593
theta[3] 10.7337357 20.651680 2408.7704 1.0012638
theta[4] 11.6196924 20.624632 4140.4122 0.9994396
theta[5] 9.3813162 16.114073 3332.9172 1.0002509
theta[6] 10.4107358 18.316474 4409.6524 0.9998224
theta[7] 14.8294773 25.389841 2519.6736 1.0002245
theta[8] 13.0356805 27.964255 737.6123 1.0041103
lp__ -37.5448195 -35.007517 1209.4214 0.9998492
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.085448 0.3220912 5.674861 1.4406346 14.50235 310.4221 1.011729
tau 7.019338 0.1887751 6.107151 0.9605613 14.60252 1046.6178 1.001833
Since mu_tau_summary is a matrix we can pull out columns
using their names:
10% 90%
mu 1.4406346 14.50235
tau 0.9605613 14.60252
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.9014808 0.8052644 0.8094257 0.9168968
max_treedepth_by_chain <- sapply(sampler_params, function(x) max(x[, "treedepth__"]))
print(max_treedepth_by_chain)[1] 4 4 5 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] 1.139419
$tau
[1] 1.135551
$eta
[1] -0.2816226 0.8839261 0.1348173 1.1142563 0.5333998 -1.6239508 0.6269902
[8] 0.6529137
$theta
[1] 0.8196216 2.1431620 1.2925104 2.4047137 1.7451214 -0.7046610 1.8513981
[8] 1.8808355
The get_seed function returns the (P)RNG seed as an
integer:
[1] 757216720