Accessing the contents of a stanfit object

Stan Development Team

2022-09-07

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:

library(rstan)
fit <- stan_demo("eight_schools", refresh = 0)
Warning: There were 2 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
class(fit)
[1] "stanfit"
attr(,"package")
[1] "rstan"

Posterior draws

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.


extract()

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]  7.369312  4.738791 12.186492 13.760985 17.112781  1.973161
[1]  7.978171  1.089728  4.125323  5.601477 14.917425  6.435649
          
iterations      [,1]      [,2]        [,3]      [,4]      [,5]      [,6]
      [1,] 20.771861  3.149816  6.84929626 17.202170 -1.491638 10.045561
      [2,]  4.235975  5.702351  5.46369584  4.647449  3.069896  4.836850
      [3,] 17.183456  8.058508 18.94757288 13.566894 11.428894 10.165681
      [4,]  9.680666  5.659564 14.75438928  9.747233  2.466835 18.354298
      [5,] 30.503290 14.097343  4.04705241 13.009119 13.570706  5.266705
      [6,]  6.010498 10.157953 -0.04061875  9.479760  4.169078  5.132693
          
iterations      [,7]      [,8]
      [1,]  5.098211 -1.811097
      [2,]  5.265172  5.240903
      [3,] 15.043956 12.889617
      [4,]  8.417942  9.487148
      [5,] 29.271447  8.762275
      [6,] 14.529463 13.972027


as.matrix(), as.data.frame(), as.array()

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,]  7.704581  7.310924
      [2,] 12.866489 21.274948
      [3,]  4.309980  4.978919
      [4,] 10.220344 13.042346
      [5,]  5.926049 11.841887
      [6,]  8.239194 12.268610


Posterior summary statistics and convergence diagnostics

Summary statistics are obtained using the summary function. The object returned is a list with two components:

fit_summary <- summary(fit)
print(names(fit_summary))
[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).

print(fit_summary$summary)
                  mean    se_mean        sd        2.5%         25%
mu         7.990476551 0.13638519 5.1607569  -1.8706588   4.6142060
tau        6.762924820 0.13661467 5.4197384   0.3484975   2.6328173
eta[1]     0.393503508 0.01539995 0.9361409  -1.5431178  -0.2108019
eta[2]     0.005466482 0.01500633 0.8610708  -1.7042092  -0.5641638
eta[3]    -0.206593495 0.01482551 0.9200078  -2.0183803  -0.8318383
eta[4]    -0.034381263 0.01478586 0.8765419  -1.7340930  -0.6170551
eta[5]    -0.368965166 0.01601678 0.8459114  -1.9996973  -0.9223153
eta[6]    -0.213887751 0.01593222 0.8658979  -1.8989164  -0.7896156
eta[7]     0.346944146 0.01477211 0.8930023  -1.5434491  -0.2129022
eta[8]     0.060572448 0.01556258 0.9238299  -1.7814491  -0.5605957
theta[1]  11.492856487 0.17703365 8.5085143  -2.3852731   5.8827308
theta[2]   7.884966432 0.10176589 6.2150845  -4.7042486   4.0611059
theta[3]   6.055576626 0.14408116 7.8464805 -12.1349414   2.1179796
theta[4]   7.634462419 0.10896845 6.6941906  -6.1531274   3.5807990
theta[5]   5.113826435 0.10114357 6.2508747  -8.5167406   1.4106055
theta[6]   6.078937379 0.11529981 6.6690209  -7.8732431   2.3053587
theta[7]  10.887935413 0.12535706 6.8580137  -0.9037645   6.2793789
theta[8]   8.571310149 0.14189270 7.9603919  -7.6927459   3.9250264
lp__     -39.379967490 0.07099875 2.5284606 -44.8113092 -41.0042906
                   50%         75%      97.5%    n_eff      Rhat
mu        7.863686e+00  11.2037379  18.480531 1431.833 1.0011095
tau       5.476201e+00   9.4529232  20.659966 1573.845 0.9999193
eta[1]    4.169561e-01   1.0184377   2.181095 3695.251 1.0001351
eta[2]    1.429393e-04   0.5854696   1.714123 3292.521 1.0003660
eta[3]   -2.215231e-01   0.4264461   1.627305 3850.912 1.0021652
eta[4]   -4.863008e-02   0.5340422   1.688879 3514.406 0.9998232
eta[5]   -3.795160e-01   0.1678531   1.343664 2789.325 0.9994925
eta[6]   -2.298394e-01   0.3336712   1.510450 2953.796 1.0009000
eta[7]    3.840260e-01   0.9205111   2.057795 3654.432 0.9995959
eta[8]    7.830821e-02   0.6840637   1.873693 3523.876 0.9995864
theta[1]  1.032506e+01  15.9741691  31.922405 2309.914 0.9995204
theta[2]  7.887766e+00  11.7688342  20.417707 3729.835 1.0004593
theta[3]  6.549406e+00  10.8138304  20.722800 2965.756 1.0009363
theta[4]  7.645091e+00  11.7227322  20.953045 3773.936 1.0003973
theta[5]  5.424388e+00   9.2542199  16.548798 3819.487 0.9997135
theta[6]  6.461682e+00  10.3308785  18.448228 3345.546 0.9999136
theta[7]  1.016331e+01  14.8221071  26.725688 2992.948 0.9994907
theta[8]  8.234690e+00  12.8046889  26.076766 3147.377 0.9995999
lp__     -3.917174e+01 -37.6267299 -34.995963 1268.268 1.0016461

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.990477 0.1363852 5.160757 1.662045 14.57341 1431.833 1.0011095
tau 6.762925 0.1366147 5.419738 1.075565 14.21973 1573.845 0.9999193

Since mu_tau_summary is a matrix we can pull out columns using their names:

mu_tau_80pct <- mu_tau_summary[, c("10%", "90%")]
print(mu_tau_80pct)
         10%      90%
mu  1.662045 14.57341
tau 1.075565 14.21973


Sampler diagnostics

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.7625438 0.7829519 0.8700801 0.8734198
max_treedepth_by_chain <- sapply(sampler_params, function(x) max(x[, "treedepth__"]))
print(max_treedepth_by_chain)
[1] 4 5 4 4


Model code

The Stan program itself is also stored in the stanfit object and can be accessed using get_stancode:

code <- get_stancode(fit)

The object code is a single string and is not very intelligible when printed:

print(code)
[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:

cat(code)
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);
}


Initial values

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:

inits <- get_inits(fit)
inits_chain1 <- inits[[1]]
print(inits_chain1)
$mu
[1] 1.083532

$tau
[1] 6.267349

$eta
[1] -0.5314910 -0.6480376  0.5977603 -0.6206335  0.5769578 -1.7231283 -1.0818183
[8]  1.9134495

$theta
[1] -2.247508 -2.977946  4.829904 -2.806195  4.699528 -9.715915 -5.696601
[8] 13.075788


(P)RNG seed

The get_seed function returns the (P)RNG seed as an integer:

print(get_seed(fit))
[1] 1730641227


Warmup and sampling times

The get_elapsed_time function returns a matrix with the warmup and sampling times for each chain:

print(get_elapsed_time(fit))
          warmup   sample
chain:1 0.036806 0.022088
chain:2 0.045345 0.032371
chain:3 0.033602 0.032454
chain:4 0.032642 0.032313