Chapter 6 Model Checking

Prior Sensitity Analysis

For the normal hierarchical model yiindN(θi,s2i)θiindN(μ,τ2) which results in the posterior distribution for τ of p(τy)p(τ)V1/2μIi=1(s2i+τ2)1/2exp((yiˆμ)22(s2i+τ2))

As an attempt to be non-informative, we can consider an IG(ϵ,ϵ) prior for τ2 or τU(0,C), τCa+(0,C). C can be chosen relatively large for the particular problem.

library(tidyverse)
library(reshape2)

d = data.frame(y = c(28,  8, -3,  7, -1,  1, 18, 12),
               s = c(15, 10, 16, 11,  9, 11, 10, 18))
t(d) %>% pander::pander()
y 28 8 -3 7 -1 1 18 12
s 15 10 16 11 9 11 10 18
dinvgamma = function(x, a, b) dgamma(1/x,a,b)/x^2
dsqrtinvgamma = function(x, a, b) dinvgamma(x^2, a, b)*2*x

tau_log_posterior = function(tau, y, si2) 
{
  spt = si2+tau^2
  Vmu = 1/sum(1/spt)
  mu = sum(y/spt)*Vmu
  0.5*log(Vmu)+sum(-0.5*log(spt)-(y-mu)^2/(2*spt))
}

V_tau_log_posterior = Vectorize(tau_log_posterior,"tau")

h = 0.001
e = 0.001

df = data.frame(x = seq(0,20-h, by=h)+h/2) %>%
  dplyr::mutate(log_like = V_tau_log_posterior(x, d$y, d$s^2),
         'IG(1,1)'    = log(dsqrtinvgamma(x,1,1)),
         'IG(e,e)'    = log(dsqrtinvgamma(x,e,e)),
         'Unif(0,20)' = 0,
         'Ca^*(0,5)'  = dcauchy(x, 0, 10, log=TRUE)) %>%
  melt(id.var=c('x','log_like'), 
       variable.name = 'Distribution', 
       value.name = 'log_prior') %>%
  dplyr::mutate(log_posterior = log_like + log_prior) %>%
  group_by(Distribution) %>% 
  dplyr::mutate(prior = exp(log_prior-max(log_prior)),
         prior = prior/sum(prior)/h,
         posterior = exp(log_posterior-max(log_posterior)),
         posterior = posterior/sum(posterior)/h) %>%
  melt(measure.vars = c('prior','posterior'), 
       value.name = 'density')
  
ggplot(df, aes(x, density, color=variable, linetype=variable, group=variable)) +
  geom_line() + 
  facet_wrap(~Distribution, scales='free') +
  theme_bw() + 
  theme(legend.position="bottom")

Posterior Predictive Checks

Let yrep be a replication of y, then p(yrep|y)=p(yrep|θ,y)p(θ|y)dθ=p(yrep|θ)p(θ|y)dθ. where y is the observed data and θ are the model parameters.

To simulate a full replication:

  1. Simulate θ(j)p(θy) and
  2. Simulate yrep,jp(yθ(j))

To assess model adequacy:

  • Compare plots of replicated data to the observed data.
  • Calculate posterior predictive p-values.
    • Define a test statistic T(y,θ)
    • Define the posterior predictive p-value: pB=P(T(yrep,θ))T(y,θ)y)
    • Small or large p-values are (possible) cause for concern