For models fit using MCMC (algorithm="sampling"
), the posterior sample
---the post-warmup draws from the posterior distribution--- can be extracted
from a fitted model object as a matrix, data frame, or array. The
as.matrix
and as.data.frame
methods merge all chains together,
whereas the as.array
method keeps the chains separate. For models fit
using optimization ("optimizing"
) or variational inference
("meanfield"
or "fullrank"
), there is no posterior sample but
rather a matrix (or data frame) of 1000 draws from either the asymptotic
multivariate Gaussian sampling distribution of the parameters or the
variational approximation to the posterior distribution.
# S3 method for stanreg as.matrix(x, ..., pars = NULL, regex_pars = NULL) # S3 method for stanreg as.array(x, ..., pars = NULL, regex_pars = NULL) # S3 method for stanreg as.data.frame(x, ..., pars = NULL, regex_pars = NULL)
x | A fitted model object returned by one of the
rstanarm modeling functions. See |
---|---|
... | Ignored. |
pars | An optional character vector of parameter names. |
regex_pars | An optional character vector of regular
expressions to use for parameter selection. |
A matrix, data.frame, or array, the dimensions of which depend on
pars
and regex_pars
, as well as the model and estimation
algorithm (see the Description section above).
#> #> exmpl_> example_model <- #> exmpl_+ stan_glmer(cbind(incidence, size - incidence) ~ size + period + (1|herd), #> exmpl_+ data = lme4::cbpp, family = binomial, QR = TRUE, #> exmpl_+ # this next line is only to keep the example small in size! #> exmpl_+ chains = 2, cores = 1, seed = 12345, iter = 1000, refresh = 0) #> #> exmpl_> example_model #> stan_glmer #> family: binomial [logit] #> formula: cbind(incidence, size - incidence) ~ size + period + (1 | herd) #> observations: 56 #> ------ #> Median MAD_SD #> (Intercept) -1.5 0.6 #> size 0.0 0.0 #> period2 -1.0 0.3 #> period3 -1.1 0.3 #> period4 -1.5 0.5 #> #> Error terms: #> Groups Name Std.Dev. #> herd (Intercept) 0.77 #> Num. levels: herd 15 #> #> ------ #> * For help interpreting the printed output see ?print.stanreg #> * For info on the priors used see ?prior_summary.stanreg#> [1] 1000 21# For example, we can see that the median of the draws for the intercept # is the same as the point estimate rstanarm uses print(median(draws[, "(Intercept)"]))#> [1] -1.48726#> [1] -1.48726# The as.array method keeps the chains separate draws_array <- as.array(example_model) print(dim(draws_array)) # iterations x chains x parameters#> [1] 500 2 21# Extract draws from asymptotic Gaussian sampling distribution # after optimization fit <- stan_glm(mpg ~ wt, data = mtcars, algorithm = "optimizing") draws <- as.data.frame(fit) print(colnames(draws))#> [1] "(Intercept)" "wt" "sigma"#> [1] 1000# Extract draws from variational approximation to the posterior distribution fit2 <- update(fit, algorithm = "meanfield")#> Chain 1: ------------------------------------------------------------ #> Chain 1: EXPERIMENTAL ALGORITHM: #> Chain 1: This procedure has not been thoroughly tested and may be unstable #> Chain 1: or buggy. The interface is subject to change. #> Chain 1: ------------------------------------------------------------ #> Chain 1: #> Chain 1: #> Chain 1: #> Chain 1: Gradient evaluation took 2.2e-05 seconds #> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.22 seconds. #> Chain 1: Adjust your expectations accordingly! #> Chain 1: #> Chain 1: #> Chain 1: Begin eta adaptation. #> Chain 1: Iteration: 1 / 250 [ 0%] (Adaptation) #> Chain 1: Iteration: 50 / 250 [ 20%] (Adaptation) #> Chain 1: Iteration: 100 / 250 [ 40%] (Adaptation) #> Chain 1: Iteration: 150 / 250 [ 60%] (Adaptation) #> Chain 1: Iteration: 200 / 250 [ 80%] (Adaptation) #> Chain 1: Success! Found best value [eta = 1] earlier than expected. #> Chain 1: #> Chain 1: Begin stochastic gradient ascent. #> Chain 1: iter ELBO delta_ELBO_mean delta_ELBO_med notes #> Chain 1: 100 -161.572 1.000 1.000 #> Chain 1: 200 -144.252 0.560 1.000 #> Chain 1: 300 -134.024 0.399 0.120 #> Chain 1: 400 -125.750 0.316 0.120 #> Chain 1: 500 -113.285 0.274 0.110 #> Chain 1: 600 -98.533 0.254 0.120 #> Chain 1: 700 -90.331 0.230 0.110 #> Chain 1: 800 -89.678 0.203 0.110 #> Chain 1: 900 -89.555 0.180 0.091 #> Chain 1: 1000 -89.145 0.163 0.091 #> Chain 1: 1100 -89.921 0.063 0.076 #> Chain 1: 1200 -89.202 0.052 0.066 #> Chain 1: 1300 -89.039 0.045 0.009 #> Chain 1: 1400 -89.352 0.039 0.008 #> Chain 1: 1500 -89.057 0.028 0.007 #> Chain 1: 1600 -89.417 0.013 0.005 #> Chain 1: 1700 -89.327 0.004 0.004 #> Chain 1: 1800 -89.653 0.004 0.004 #> Chain 1: 1900 -89.299 0.004 0.004 #> Chain 1: 2000 -88.874 0.004 0.004 #> Chain 1: 2100 -89.270 0.004 0.004 #> Chain 1: 2200 -89.304 0.003 0.004 #> Chain 1: 2300 -89.431 0.003 0.004 #> Chain 1: 2400 -89.393 0.003 0.004 #> Chain 1: 2500 -89.279 0.003 0.004 #> Chain 1: 2600 -89.137 0.002 0.002 #> Chain 1: 2700 -89.333 0.002 0.002 #> Chain 1: 2800 -89.726 0.002 0.002 #> Chain 1: 2900 -89.222 0.003 0.002 #> Chain 1: 3000 -89.288 0.002 0.002 #> Chain 1: 3100 -89.225 0.002 0.001 #> Chain 1: 3200 -89.567 0.002 0.002 #> Chain 1: 3300 -89.237 0.002 0.002 #> Chain 1: 3400 -89.108 0.003 0.002 #> Chain 1: 3500 -89.303 0.003 0.002 #> Chain 1: 3600 -89.145 0.003 0.002 #> Chain 1: 3700 -89.309 0.003 0.002 #> Chain 1: 3800 -89.198 0.002 0.002 #> Chain 1: 3900 -89.193 0.002 0.002 #> Chain 1: 4000 -89.329 0.002 0.002 #> Chain 1: 4100 -89.606 0.002 0.002 #> Chain 1: 4200 -89.123 0.002 0.002 #> Chain 1: 4300 -89.069 0.002 0.002 #> Chain 1: 4400 -89.129 0.002 0.002 #> Chain 1: 4500 -89.123 0.002 0.002 #> Chain 1: 4600 -89.437 0.002 0.002 #> Chain 1: 4700 -89.894 0.002 0.002 #> Chain 1: 4800 -89.679 0.002 0.002 #> Chain 1: 4900 -89.283 0.003 0.003 #> Chain 1: 5000 -88.948 0.003 0.004 #> Chain 1: 5100 -89.017 0.003 0.004 #> Chain 1: 5200 -89.056 0.002 0.002 #> Chain 1: 5300 -89.227 0.002 0.002 #> Chain 1: 5400 -89.055 0.002 0.002 #> Chain 1: 5500 -89.075 0.002 0.002 #> Chain 1: 5600 -89.282 0.002 0.002 #> Chain 1: 5700 -89.063 0.002 0.002 #> Chain 1: 5800 -89.219 0.002 0.002 #> Chain 1: 5900 -89.499 0.002 0.002 #> Chain 1: 6000 -89.686 0.002 0.002 #> Chain 1: 6100 -89.091 0.002 0.002 #> Chain 1: 6200 -89.066 0.002 0.002 #> Chain 1: 6300 -89.143 0.002 0.002 #> Chain 1: 6400 -89.043 0.002 0.002 #> Chain 1: 6500 -88.950 0.002 0.002 #> Chain 1: 6600 -88.988 0.002 0.002 #> Chain 1: 6700 -89.083 0.002 0.001 #> Chain 1: 6800 -89.328 0.002 0.001 #> Chain 1: 6900 -89.002 0.002 0.001 #> Chain 1: 7000 -89.481 0.002 0.001 #> Chain 1: 7100 -89.321 0.002 0.001 #> Chain 1: 7200 -89.337 0.002 0.001 #> Chain 1: 7300 -89.547 0.002 0.002 #> Chain 1: 7400 -89.506 0.002 0.002 #> Chain 1: 7500 -89.160 0.002 0.002 #> Chain 1: 7600 -89.079 0.002 0.002 #> Chain 1: 7700 -88.969 0.002 0.002 #> Chain 1: 7800 -89.337 0.002 0.002 #> Chain 1: 7900 -89.181 0.002 0.002 #> Chain 1: 8000 -88.942 0.002 0.002 #> Chain 1: 8100 -89.011 0.002 0.002 #> Chain 1: 8200 -89.574 0.002 0.002 #> Chain 1: 8300 -88.728 0.003 0.003 #> Chain 1: 8400 -89.325 0.004 0.004 #> Chain 1: 8500 -89.601 0.004 0.003 #> Chain 1: 8600 -88.828 0.004 0.004 #> Chain 1: 8700 -89.140 0.005 0.004 #> Chain 1: 8800 -89.195 0.004 0.004 #> Chain 1: 8900 -88.970 0.004 0.004 #> Chain 1: 9000 -89.349 0.005 0.004 #> Chain 1: 9100 -89.734 0.005 0.004 #> Chain 1: 9200 -89.110 0.005 0.004 #> Chain 1: 9300 -89.341 0.004 0.004 #> Chain 1: 9400 -89.194 0.004 0.004 #> Chain 1: 9500 -89.307 0.004 0.004 #> Chain 1: 9600 -89.221 0.003 0.003 #> Chain 1: 9700 -89.214 0.003 0.003 #> Chain 1: 9800 -89.147 0.003 0.003 #> Chain 1: 9900 -89.304 0.002 0.002 #> Chain 1: 10000 -88.740 0.003 0.002 #> Chain 1: Informational Message: The maximum number of iterations is reached! The algorithm may not have converged. #> Chain 1: This variational approximation is not guaranteed to be meaningful. #> Chain 1: #> Chain 1: Drawing a sample of size 1000 from the approximate posterior... #> Chain 1: COMPLETED.#>#> [1] "wt"#> [1] 1000# }