Chapter 6 Model Checking
Prior Sensitity Analysis
For the normal hierarchical model yiind∼N(θi,s2i)θiind∼N(μ,τ2) which results in the posterior distribution for τ of p(τ∣y)∝p(τ)V1/2μI∏i=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)
= data.frame(y = c(28, 8, -3, 7, -1, 1, 18, 12),
d 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 |
= function(x, a, b) dgamma(1/x,a,b)/x^2
dinvgamma = function(x, a, b) dinvgamma(x^2, a, b)*2*x
dsqrtinvgamma
= function(tau, y, si2)
tau_log_posterior
{= si2+tau^2
spt = 1/sum(1/spt)
Vmu = sum(y/spt)*Vmu
mu 0.5*log(Vmu)+sum(-0.5*log(spt)-(y-mu)^2/(2*spt))
}
= Vectorize(tau_log_posterior,"tau")
V_tau_log_posterior
= 0.001
h = 0.001
e
= data.frame(x = seq(0,20-h, by=h)+h/2) %>%
df ::mutate(log_like = V_tau_log_posterior(x, d$y, d$s^2),
dplyr'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') %>%
::mutate(log_posterior = log_like + log_prior) %>%
dplyrgroup_by(Distribution) %>%
::mutate(prior = exp(log_prior-max(log_prior)),
dplyrprior = 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:
- Simulate θ(j)∼p(θ∣y) and
- Simulate yrep,j∼p(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