View source: R/posterior_predict.R
posterior_predict | R Documentation |
Draw samples from the posterior predictive distribution of a fitted geostan
model.
posterior_predict(
object,
S,
summary = FALSE,
width = 0.95,
approx = TRUE,
K = 20,
preserve_order = FALSE,
seed
)
object |
A |
S |
Optional; number of samples to take from the posterior distribution. The default, and maximum, is the total number of samples stored in the model. |
summary |
Should the predictive distribution be summarized by its means and central quantile intervals? If |
width |
Only used if |
approx |
For SAR models only; |
K |
For SAR models only; number of matrix powers to for the matrix inverse approximation (used when |
preserve_order |
If |
seed |
A single integer value to be used in a call to |
This method returns samples from the posterior predictive distribution of the model (at the observed values of covariates, etc.). The predictions incorporate uncertainty of all parameter values (used to calculate the expected value of the model, for example) plus the error term (the model's description of the amount of variability of observations around the expected value). If the model includes measurement error in the covariates, this source of uncertainty (about X
) is passed into the posterior predictive distribution as well.
For SAR models (and all other models), the observed outcomes are not used to formulate the posterior predictive distribution. The posterior predictive distribution for the SLM (see stan_sar) is given by
(I - \rho W)^{-1} (\mu + \epsilon).
The SDLM is the same but includes spatially-lagged covariates in mu
. The approx = FALSE
method for SAR models requires a call to Matrix::solve(I - rho * W)
for each MCMC sample; the approx = TRUE
method uses an approximation based on matrix powers (LeSage and Pace 2009). The approximation will deteriorate if \rho^K
is not near zero, so use with care.
A matrix of size S x N containing samples from the posterior predictive distribution, where S is the number of samples drawn and N is the number of observations. If summary = TRUE
, a data.frame
with N rows and 3 columns is returned (with column names mu
, lwr
, and upr
).
LeSage, James, & Robert kelley Pace (2009). Introduction to Spatial Econometrics. Chapman and Hall/CRC.
Gelman, A., J. B.Carlin, H. S. Stern, D. B. Dunson, A. Vehtari, & D. B. Rubin, D. B. (2014). Bayesian data analysis (3rd ed.). CRC Press.
McElreath, Richard (2016). Statistical Rethinking: A Bayesian Course with Examples in R and Stan. CRC Press, Ch. 3.
E <- sentencing$expected_sents
sentencing$log_E <- log(E)
fit <- stan_glm(sents ~ offset(log_E),
re = ~ name,
data = sentencing,
family = poisson(),
chains = 2, iter = 600) # for speed only
yrep <- posterior_predict(fit, S = 65)
plot(density(yrep[1,] / E ))
for (i in 2:nrow(yrep)) lines(density(yrep[i,] / E), col = 'gray30')
lines(density(sentencing$sents / E), col = 'darkred', lwd = 2)
sars <- prep_sar_data2(row = 9, col = 9)
W <- sars$W
y <- sim_sar(rho = .9, w = W)
fit <- stan_sar(y ~ 1, data = data.frame(y=y), sar = sars,
iter = 650, quiet = TRUE)
yrep <- posterior_predict(fit, S = 15)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.