Chapter 7 Hierarchical Modeling
7.1 Introduction to Hierarchical Modeling
library(LearnBayes)
library(lattice)
Fit logistic model for home run data for a particular player
<- function(player){
logistic.fit <- subset(sluggerdata, Player==player)
d <- d$Age
x <- d$Age^2
x2 <- cbind(d$HR, d$AB - d$HR)
response list(Age=x,
p=glm(response ~ x + x2,
family=binomial)$fitted)
}
<- unique(sluggerdata$Player)
names <- NULL
newdata for (j in 1:9){
<-logistic.fit(as.character(names[j]))
fit <- rbind(newdata,
newdata data.frame(as.character(names[j]),
$Age, fit$p))
fit
}names(newdata) <- c("Player", "Age", "Fitted")
xyplot(Fitted ~ Age | Player,
data=newdata,
type="l", lwd=3, col="black")
7.2 Individual or Combined Estimates
with(hearttransplants,
plot(log(e), y / e, xlim=c(6, 9.7),
xlab="log(e)", ylab="y/e"))
with(hearttransplants,
text(log(e), y / e,
labels=as.character(y), pos=4))
7.3 Equal Mortality Rates?
Using posterior predictive checks to see if equal mortality rate model is appropriate.
with(hearttransplants, sum(y))
## [1] 277
with(hearttransplants, sum(e))
## [1] 294681
<- rgamma(1000, shape=277, rate=294681)
lambda <- with(hearttransplants,
ys94 rpois(1000, e[94] * lambda))
hist(ys94, breaks=seq(0.5, max(ys94) + 0.5))
with(hearttransplants,
lines(c(y[94], y[94]), c(0, 120), lwd=3))
Find posterior predictive distribution of each observation with its posterior predictive distribution.
<- rgamma(1000, shape=277, rate=294681)
lambda <- function(i){
prob.out <- with(hearttransplants,
ysi rpois(1000, e[i] * lambda))
<- with(hearttransplants,
pleft sum(ysi <= y[i]) / 1000)
<- with(hearttransplants,
pright sum(ysi >= y[i]) / 1000)
min(pleft, pright)
}<- sapply(1:94, prob.out) pout
with(hearttransplants,
plot(log(e), pout, ylab="Prob(extreme)"))
7.4 Modeling a Prior Belief of Exchangeability
Graph of two-stage prior to model a belief in exchangeability of the Poisson rates.
<- function(lambda, pars){
pgexchprior <- pars[1]
alpha <- pars[2]
a <- pars[3]
b - 1) * log(prod(lambda)) -
(alpha 2 * alpha + a) * log(alpha * sum(lambda) + b)
( }
<- c(5, 20, 80, 400)
alpha par(mfrow=c(2, 2))
for (j in 1:4){
mycontour(pgexchprior,
c(.001, 5, .001, 5),
c(alpha[j], 10, 10),
main=paste("ALPHA = ",alpha[j]),
xlab="LAMBDA 1", ylab="LAMBDA 2")
}
7.5 Simulating from the Posterior
Representing posterior as [\(\mu, \alpha\)] [\(\{\lambda_j\} | \mu, \alpha\)].
Focus on posterior of [\(\mu, \alpha\)]:
<- list(data = hearttransplants, z0 = 0.53)
datapar <- c(2, -7)
start <- laplace(poissgamexch, start, datapar)
fit fit
## $mode
## [1] 1.883954 -6.955446
##
## $var
## [,1] [,2]
## [1,] 0.233694921 -0.003086655
## [2,] -0.003086655 0.005866020
##
## $int
## [1] -2208.503
##
## $converge
## [1] TRUE
par(mfrow = c(1, 1))
mycontour(poissgamexch, c(0, 8, -7.3, -6.6),
datapar,xlab="log alpha", ylab="log mu")
<- c(4, -7)
start <- gibbs(poissgamexch,
fitgibbs 1000,
start, c(1, .15), datapar)
$accept fitgibbs
## [,1] [,2]
## [1,] 0.52 0.483
mycontour(poissgamexch,
c(0, 8, -7.3, -6.6),
datapar,xlab="log alpha", ylab="log mu")
points(fitgibbs$par[, 1], fitgibbs$par[, 2])
plot(density(fitgibbs$par[, 1], bw = 0.2))
Posterior of rates:
<- exp(fitgibbs$par[, 1])
alpha <- exp(fitgibbs$par[, 2])
mu <- rgamma(1000, y[1] + alpha,
lam1 $e[1] + alpha / mu)
hearttransplants<- exp(fitgibbs$par[, 1])
alpha <- exp(fitgibbs$par[, 2]) mu
with(hearttransplants,
plot(log(e), y/e, pch = as.character(y)))
for (i in 1:94) {
<- with(hearttransplants,
lami rgamma(1000, y[i] + alpha,
+ alpha/mu))
e[i] <- quantile(lami, c(0.05, 0.95))
probint with(hearttransplants,
lines(log(e[i]) * c(1, 1), probint))
}
7.6 Posterior Inferences
<- list(data = hearttransplants, z0 = 0.53)
datapar <- c(2, -7)
start <- laplace(poissgamexch, start, datapar)
fit fit
## $mode
## [1] 1.883954 -6.955446
##
## $var
## [,1] [,2]
## [1,] 0.233694921 -0.003086655
## [2,] -0.003086655 0.005866020
##
## $int
## [1] -2208.503
##
## $converge
## [1] TRUE
par(mfrow = c(1, 1))
mycontour(poissgamexch,
c(0, 8, -7.3, -6.6), datapar,
xlab="log alpha",ylab="log mu")
<- c(4, -7)
start <- gibbs(poissgamexch,
fitgibbs 1000,
start, c(1,.15), datapar)
<- exp(fitgibbs$par[, 1])
alpha <- exp(fitgibbs$par[, 2]) mu
Look at posteriors of shrinkages.
<-function(i)
shrink with(hearttransplants,
mean(alpha / (alpha + e[i] * mu)))
=sapply(1:94, shrink) shrinkage
with(hearttransplants,
plot(log(e), shrinkage))
Comparing hospitals.
<- function(i){
mrate with(hearttransplants,
mean(rgamma(1000, y[i] + alpha,
+ alpha/mu)))
e[i]
}<- 1:94
hospital <- sapply(hospital,mrate)
meanrate == min(meanrate)] hospital[meanrate
## [1] 85
<- function(i) {
sim.lambda with(hearttransplants,
rgamma(1000, y[i] + alpha,
+ alpha / mu))
e[i]
}<- sapply(1:94, sim.lambda) LAM
<- function(x) {
compare.rates <- NCOL(x)
nc <- as.matrix(expand.grid(1:nc, 1:nc))
ij <- as.matrix(x[,ij[,1]] > x[,ij[,2]])
m matrix(colMeans(m), nc, nc, byrow = TRUE)
}
<- compare.rates(LAM) better
1:24, 85] better[
## [1] 0.174 0.190 0.086 0.144 0.124 0.227 0.203 0.166 0.067 0.255 0.196 0.176
## [13] 0.202 0.097 0.070 0.231 0.239 0.088 0.258 0.185 0.166 0.152 0.057 0.087
7.7 Bayesian Sensitivity Analysis
Explore sensitivity of inference with respect to the choice of \(z_0\) in prior.
<- list(data = hearttransplants,
datapar z0 = 0.53)
<- c(4, -7)
start <-gibbs(poissgamexch,
fitgibbs 1000,
start, c(1,.15), datapar)
<- function(theta, prior, prior.new){
sir.old.new <- log(prior(theta))
log.g <- log(prior.new(theta))
log.g.new <- exp(log.g.new - log.g -
wt max(log.g.new - log.g))
<- wt / sum(wt)
probs <- length(probs)
n <- sample(1:n, size=n,
indices prob=probs, replace=TRUE)
theta[indices] }
<- function(theta){
prior 0.53 * exp(theta) / (exp(theta) + 0.53) ^ 2
}<- function(theta){
prior.new 5 * exp(theta) / (exp(theta) + 5) ^ 2
}
<- fitgibbs$par[, 1]
log.alpha <- sir.old.new(log.alpha,
log.alpha.new prior, prior.new)
library(lattice)
<- function(){
draw.graph <- data.frame("prior", log.alpha)
LOG.ALPHA names(LOG.ALPHA) <- c("Prior", "log.alpha")
<- data.frame("new.prior",
LOG.ALPHA.NEW
log.alpha.new)names(LOG.ALPHA.NEW) <- c("Prior","log.alpha")
<- densityplot(~ log.alpha,
D group=Prior,
data = rbind(LOG.ALPHA, LOG.ALPHA.NEW),
plot.points=FALSE,
main="Original Prior and Posterior (solid), \nNew Prior and Posterior (dashed)",
lwd=4, adjust=2, lty=c(1,2),
xlab="log alpha",xlim=c(-3,5),col="black")
update(D, panel=function(...){
panel.curve(prior(x), lty=1, lwd=2,
col="black")
panel.curve(prior.new(x), lty=2, lwd=2,
col="black")
panel.densityplot(...)
})}
draw.graph()
7.8 Posterior Predictive Model Checking
Study predictive distributions of observations.
<- list(data = hearttransplants, z0 = 0.53) datapar
<- c(4, -7)
start <- gibbs(poissgamexch,
fitgibbs 1000, c(1,.15),
start, datapar)
<- with(hearttransplants,
lam94 rgamma(1000, y[94] + alpha,
94] + alpha / mu)) e[
<- with(hearttransplants,
ys94 rpois(1000, e[94] * lam94))
hist(ys94, breaks=seq(-0.5, max(ys94) + 0.5))
lines(y[94] * c(1, 1), c(0, 100), lwd=3)
Explore the probabilities that the predictive distribution of each observation is at least as large as observed \(y_i\).
<- function(i){
prob.out <- with(hearttransplants,
lami rgamma(1000, y[i] + alpha,
+ alpha / mu))
e[i] <- with(hearttransplants,
ysi rpois(1000, e[i] * lami))
<- with(hearttransplants,
pleft sum(ysi <= y[i]) / 1000)
<- with(hearttransplants,
pright sum(ysi >= y[i]) / 1000)
min(pleft, pright)
}<- sapply(1:94, prob.out) pout.exchange
plot(pout, pout.exchange,
xlab="P(extreme), equal means",
ylab="P(extreme), exchangeable")
abline(0,1)