Chapter 4 Multiparameter Models
4.1 Normal Data with Both Parameters Unknown
Illustrates exact posterior sampling of (\(\mu, \sigma^2\)) for normal sampling with a noninformative prior.
library(LearnBayes)
mycontour(normchi2post,
c(220, 330, 500, 9000),
$time,
marathontimesxlab="mean", ylab="variance")
<- with(marathontimes,
S sum((time - mean(time))^2))
<- length(marathontimes$time)
n <- S / rchisq(1000, n - 1)
sigma2 <- rnorm(1000, mean = mean(marathontimes$time),
mu sd = sqrt(sigma2) / sqrt(n))
mycontour(normchi2post,
c(220, 330, 500, 9000),
$time,
marathontimesxlab="mean", ylab="variance")
points(mu, sigma2)
quantile(mu, c(0.025, 0.975))
## 2.5% 97.5%
## 255.8784 300.8766
quantile(sqrt(sigma2), c(0.025, 0.975))
## 2.5% 97.5%
## 38.16360 72.06066
4.2 A Multinomial Model
Multinomial data and a uniform prior placed on the proportions. Sampling from the Dirichlet posterior distribution.
<- c(728, 584, 138)
alpha <- rdirichlet(1000, alpha)
theta hist(theta[, 1] - theta[, 2], main="")
Considers posterior distribution of Obama electoral votes for the 2008 presidential election.
<- function(j){
prob.Obama <- with(election.2008,
p rdirichlet(5000,
500 * c(M.pct[j], O.pct[j],
100 - M.pct[j] - O.pct[j]) / 100 + 1))
mean(p[, 2] > p[, 1])
}<- sapply(1 : 51, prob.Obama) Obama.win.probs
<- function(){
sim.election <- rbinom(51, 1,
winner
Obama.win.probs) sum(election.2008$EV * winner)
}
<- replicate(1000, sim.election()) sim.EV
hist(sim.EV, min(sim.EV) : max(sim.EV), col="blue")
abline(v=365, lwd=3) # Obama received 365 votes
text(375, 30, "Actual \n Obama \n total")
4.3 A Bioassay Experiment
Bayesian fitting of a logistic model using data from a dose-response experiment.
<- c(-0.86, -0.3, -0.05, 0.73)
x <- c(5, 5, 5, 5)
n <- c(0, 1, 3, 5)
y <- cbind(x, n, y) data
Traditional logistic model fit.
<- cbind(y, n - y)
glmdata <- glm(glmdata ~ x, family = binomial)
results summary(results)
##
## Call:
## glm(formula = glmdata ~ x, family = binomial)
##
## Deviance Residuals:
## 1 2 3 4
## -0.17236 0.08133 -0.05869 0.12237
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.8466 1.0191 0.831 0.406
## x 7.7488 4.8728 1.590 0.112
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 15.791412 on 3 degrees of freedom
## Residual deviance: 0.054742 on 2 degrees of freedom
## AIC: 7.9648
##
## Number of Fisher Scoring iterations: 7
Illustration of a conditional means prior. When x = -.7, median and 90th percentile of p are (.2,.4). When x = +.6, median and 90th percentile of p are (.8, .95)
<- beta.select(list(p=.5, x=.2),
a1.b1 list(p=.9, x=.5))
<- beta.select(list(p=.5, x=.8),
a2.b2 list(p=.9, x=.98))
<- rbind(c(-0.7, 4.68, 1.12),
prior c(0.6, 2.10, 0.74))
<- rbind(data, prior) data.new
Plot prior.
plot(c(-1,1), c(0, 1), type="n",
xlab="Dose", ylab="Prob(death)")
lines(-0.7 * c(1, 1), qbeta(c(.25, .75),
1], a1.b1[2]), lwd=4)
a1.b1[lines(0.6 * c(1, 1), qbeta(c(.25, .75),
1], a2.b2[2]), lwd=4)
a2.b2[points(c(-0.7, 0.6), qbeta(.5, c(a1.b1[1],
1]), c(a1.b1[2], a2.b2[2])),
a2.b2[pch=19, cex=2)
text(-0.3, .2, "Beta(1.12, 3.56)")
text(.2, .8, "Beta(2.10, 0.74)")
<- rbind(a1.b1, a2.b2)
response <- c(-0.7, 0.6)
x <- glm(response ~ x, family = binomial) fit
## Warning in eval(family$initialize): non-integer counts in a binomial glm!
curve(exp(fit$coef[1] + fit$coef[2] * x) /
1 + exp(fit$coef[1] + fit$coef[2] * x)),
(add=T)
Posterior of regression coefficients.
mycontour(logisticpost, c(-3, 3, -1, 9), data.new,
xlab="beta0", ylab="beta1")
mycontour(logisticpost, c(-3, 3, -1, 9), data.new,
xlab="beta0", ylab="beta1")
<- simcontour(logisticpost, c(-2, 3, -1, 11),
s 1000)
data.new, points(s)
plot(density(s$y), xlab="beta1")
Estimation of LD50 parameter.
<- -s$x / s$y
theta hist(theta, xlab="LD-50", breaks=20,
xlim = c(-1, 1.5))
quantile(theta, c(.025, .975))
## 2.5% 97.5%
## -0.3248469 0.4748061
4.4 Comparing Two Proportions
Using Howard’s dependent prior for two proportions. Graph of the prior.
<- c(2, 1, .5, .25)
sigma <- .0001; phi <- .9999
plo par(mfrow=c(2, 2))
for (i in 1:4){
mycontour(howardprior,
c(plo, phi, plo, phi),
c(1, 1, 1, 1, sigma[i]),
main=paste("sigma=", as.character(sigma[i])),
xlab="p1", ylab="p2")
}
Graphs of the posterior.
<- c(2, 1, .5, .25)
sigma par(mfrow=c(2, 2))
for (i in 1:4){
mycontour(howardprior,
c(plo, phi, plo, phi),
c(1 + 3, 1 + 15, 1 + 7, 1 + 5, sigma[i]),
main=paste("sigma=", as.character(sigma[i])),
xlab="p1", ylab="p2")
lines(c(0, 1), c(0, 1))
}
<- simcontour(howardprior, c(plo, phi, plo, phi),
s c(1 + 3, 1 + 15, 1 + 7, 1 + 5, 2), 1000)
sum(s$x > s$y) / 1000
## [1] 0.012