Chapter 8 Model Comparison
8.1 A One-Sided Test of a Normal Mean
Bayesian testing of \(\mu \le \mu_0\) against \(\mu > \mu_0\).
library(LearnBayes)
<- 170
pmean <- 25
pvar <- pnorm(175, pmean, sqrt(pvar))
probH <- 1 - probH
probA <- probH / probA
prior.odds prior.odds
## [1] 5.302974
<- c(182, 172, 173, 176, 176, 180,
weights 173, 174, 179, 175)
<- mean(weights)
xbar <- 3 ^ 2 / length(weights) sigma2
<- 1 / sigma2 + 1 / pvar
post.precision <- 1 / post.precision post.var
<- (xbar / sigma2 + pmean / pvar) /
post.mean
post.precisionc(post.mean, sqrt(post.var))
## [1] 175.7915058 0.9320546
<- pnorm(175, post.mean,
post.odds sqrt(post.var)) /
1 - pnorm(175, post.mean,
(sqrt(post.var)))
post.odds
## [1] 0.2467017
<- post.odds / prior.odds
BF BF
## [1] 0.04652139
<- probH * BF / (probH * BF + probA)
postH postH
## [1] 0.1978835
Contrast with a frequentist p-value calculation.
<- sqrt(length(weights)) *
z mean(weights) - 175) / 3
(1 - pnorm(z)
## [1] 0.1459203
<- c(182, 172, 173, 176, 176, 180,
weights 173, 174, 179, 175)
<- c(mean(weights), length(weights), 3)
data <- c(170, 1000)
prior.par mnormt.onesided(175, prior.par, data)
## $BF
## [1] 0.1694947
##
## $prior.odds
## [1] 1.008011
##
## $post.odds
## [1] 0.1708525
##
## $postH
## [1] 0.1459215
8.2 A Two-Sided Test of a Normal Mean
Bayesian testing of \(\mu = \mu_0\) against \(\mu \neq \mu_0\).
<- c(182, 172, 173, 176, 176, 180,
weights 173, 174, 179, 175)
<- c(mean(weights), length(weights), 3)
data <- c(.5, 1, 2, 4, 8)
t mnormt.twosided(170, .5, t, data)
## $bf
## [1] 1.462146e-02 3.897038e-05 1.894326e-07 2.591162e-08 2.309739e-08
##
## $post
## [1] 1.441076e-02 3.896887e-05 1.894325e-07 2.591162e-08 2.309739e-08
8.3 Models for Soccer Goals
Illustrates the use of the marginal likelihood to compare several Bayesian models for soccer goals.
<- list(data=soccergoals$goals,
datapar par=c(4.57, 1.43))
<- laplace(logpoissgamma, .5, datapar)
fit1 <- list(data=soccergoals$goals,
datapar par=c(1, .5))
<- laplace(logpoissnormal, .5, datapar)
fit2 <- list(data=soccergoals$goals,
datapar par=c(2, .5))
<- laplace(logpoissnormal, .5, datapar)
fit3 <- list(data=soccergoals$goals,
datapar par=c(1, 2))
<- laplace(logpoissnormal, .5, datapar) fit4
<- c(fit1$mode, fit2$mode, fit3$mode,
postmode $mode)
fit4<- sqrt(c(fit1$var, fit2$var, fit3$var,
postsd $var))
fit4<- c(fit1$int, fit2$int, fit3$int,
logmarg $int)
fit4cbind(postmode,postsd,logmarg)
## postmode postsd logmarg
## [1,] 0.5248047 0.1274414 -1.502977
## [2,] 0.5207825 0.1260712 -1.255171
## [3,] 0.5825195 0.1224723 -5.076316
## [4,] 0.4899414 0.1320165 -2.137216
8.4 Is a Baseball Hitter Really Streaky?
Defines a family of streaky models to measure the level of support for streakiness by a Bayes factor.
<- cbind(jeter2004$H, jeter2004$AB)
data <- regroup(data, 5) data1
<- function(logK){
log.marg laplace(bfexch, 0,
list(data=data1, K=exp(logK)))$int
}
<- seq(2, 6)
log.K <- exp(log.K)
K <- sapply(log.K, log.marg)
log.BF <- exp(log.BF)
BF round(data.frame(log.K, K, log.BF, BF), 2)
## log.K K log.BF BF
## 1 2 7.39 -4.04 0.02
## 2 3 20.09 0.17 1.19
## 3 4 54.60 0.92 2.51
## 4 5 148.41 0.57 1.78
## 5 6 403.43 0.26 1.29
8.5 A Test of Independence in a Two-Way Contingency Table
Constructs several Bayes factor statistics for two-way contingency tables.
<- matrix(c(11, 9, 68, 23, 3, 5),
data c(2, 3))
data
## [,1] [,2] [,3]
## [1,] 11 68 3
## [2,] 9 23 5
Traditional chi-square test of independence.
chisq.test(data)
## Warning in chisq.test(data): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: data
## X-squared = 6.9264, df = 2, p-value = 0.03133
Bayes factor against independence using uniform priors.
=matrix(rep(1, 6), c(2, 3))
a a
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 1 1
ctable(data, a)
## [1] 1.662173
Consider Bayes factors against independence for alternatives close to independence.
<- seq(2,7)
log.K <- function(log.K){
compute.log.BF log(bfindep(data, exp(log.K), 100000)$bf)
}<- sapply(log.K, compute.log.BF)
log.BF <- exp(log.BF) BF
round(data.frame(log.K, log.BF, BF), 2)
## log.K log.BF BF
## 1 2 -1.51 0.22
## 2 3 0.13 1.14
## 3 4 0.78 2.17
## 4 5 0.73 2.09
## 5 6 0.44 1.55
## 6 7 0.20 1.22