0Pricing
R Academy · Lesson

Posterior Predictive Checks

Validate model fit by comparing simulated vs observed data distributions.

What Are Posterior Predictive Checks?

After fitting a Bayesian model you need to ask: does this model generate data that looks like the data you observed? Posterior Predictive Checks (PPCs) answer this by simulating replicated datasets yrep from the posterior and comparing them graphically to the observed y.

Extracting Posterior Samples

extract(fit, pars='mu') returns a named list; $mu is a numeric vector of all post-warmup samples for that parameter. With 4 chains x 1000 post-warmup iterations you get 4000 samples.

# library(rstan)
# mu_samples    <- extract(fit, pars = 'mu')$mu
# sigma_samples <- extract(fit, pars = 'sigma')$sigma
#
# cat('Samples drawn:', length(mu_samples), '
')
# cat('Posterior mean of mu:', mean(mu_samples), '
')
# cat('90% CI:', quantile(mu_samples, c(0.05, 0.95)), '
')

All lessons in this course

  1. Introduction to Bayesian Thinking
  2. Writing Stan Models in R
  3. MCMC Sampling and Diagnostics
  4. Posterior Predictive Checks
← Back to R Academy