INTRODUCTION TO BAYESIAN ANALYSIS FOR EPIDEMIOLOGISTS

What is Bayes?

Acknowledgements

Jim Albert
David Spiegelhalter and Nicky Best
Andrew Gelman

THE BAYESIAN WAY

Why Bayes?

Basics of Bayes

There is no free lunch

Contrast with frequentist approaches

Bayes Theorem

HIV, ELISA, Western Blot

Western Blot + Western Blot -
ELISA + 498 4
ELSIA - 10 488

different population, different prevalence, different prior

Western Blot + Western Blot -
ELISA + 1960 9944
ELSIA - 40 990056

Example: Student Sleep Habits

R Code for Student Sleep Example

1
2
3
4
5
6
data<-c(11, 16)  # number successes and failures

theta<-seq(0.05, 0.95, by = 0.1)
weights<-c(0, 1, 2, 3, 4, 2, 1, 0, 0,0)
prior<-weights/sum(weights)
plot(theta, prior, type="h", ylab="Prior Probability")
1
2
post<-pdisc(theta, prior, data) 
round(cbind(theta, prior, post), 2) 
1
2
3
4
5
6
7
library(lattice) 
PRIOR=data.frame("prior",theta,prior) 
POST=data.frame("posterior",theta,post) 
names(PRIOR)=c("Type","Theta","Probability") 
names(POST)=c("Type","Theta","Probability") 
data=rbind(PRIOR,POST) 
xyplot(Probability~Theta|Type,data=data,layout=c(1,2), type="h",lwd=3,col="black")

PROBABILITY DISTRIBUTIONS USED IN BAYESIAN ANALYSIS

The Normal Distribution

\[ \sim Nl(\mu, \sigma^2) \]

The Normal Distribution

The Normal Distribution

The Uniform Distribution

\[ \sim Unif(a,b), \\ \mu = \frac{(a+b)}{2}, \\ \sigma^2 = \frac{(b-a)^2}{12} \]

The Uniform Distribution

The Uniform Distribution

The Exponential Distribution

\[ \sim Exp(\lambda),\\ mu=1/\lambda,\\ \sigma^2=1/\lambda^2 \]

The Exponential Distribution

The Exponential Distribution

The Gamma Distribution

\[ \sim \Gamma(\alpha, \beta),\\ \mu=\frac{\alpha}{\beta} \\ \sigma^2=\frac{\alpha}{\beta^2} \]

The Gamma Distribution

The Gamma Distribution

The Beta Distribution

\[ \sim Beta(\alpha,\beta)\\ \mu=\frac{\alpha}{\alpha+\beta}\\ \sigma^2=\frac{\alpha \beta}{(\alpha \beta)^2 (\alpha + \beta) + 1} \]

The Beta Distribution

The Beta Distribution

The Dirichlet Distribution

\[ \sim Dirichlet(\alpha_1, ... \alpha_k) \]

The Dirichlet Distribution

The Dirichlet Distribution

CONJUGATE ANALYSIS

What is Conjugate Analysis?

Likelihood Prior Posterior
Normal Normal Normal
Binomial Beta Beta
Poisson Gamma Gamma

Conjugate Analysis of Proportions

Drug Response Example: No Data

1
2
3
4
5
6

Model{
y~dbin(theta , 20)         #sampling distribution
theta ~ dbeta (9.2, 13.8)  #parameter from sampling distribution
p.crit <- step(y-14.5)     # indicator variable, 1 if y>=15, 0 otherwise
}
1
2
3
4
5
6
7
8
9
N=1000
theta<-rbeta(N,9.2,13.8)
x<-rbinom(N,20, theta)
y<-0
accept<-ifelse(x>14.5, y+1, y+0)
prob<-sum(accept)/N
prob
library(coda)
densityplot(accept)

Drug Response Example: Data

MCMC of Drug Example

1
2
3
4
5
6
7
8
9
cat(
"model
{
y~dbin(theta, m) #likelihood
theta~dbeta(a,b) # prior
y.pred~dbin(theta, n)  #posterior, used as predictive distribution
p.crit<- step(y.pred-ncrit+0.5) # if y.pred>=ncrit, returns 1, otherwise 0  
}"
,file="m1.jag")
1
2
3
4
5
6
m1.dat<-list(a=9.2,
b=13.8,
y=15,
m=20,
n=40,
ncrit=25)
1
m1.inits<-list(theta=0.1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
library(R2jags)

m1<-jags(data=m1.dat, n.chains=1,
    inits=m1.inits, 
    parameters.to.save=c("theta", "p.crit"), 
    model.file="m1.jag")

class(m1)
print(m1)
plot(m1)
traceplot(m1)

m1.mcmc<-as.mcmc(m1)
plot(m1.mcmc)

Beta Parameters from mean and standard deviation

1
2
3
4
5
6
7
estBetaParams <- function(mu, var) {
  alpha <- ((1 - mu) / var - 1 / mu) * mu ^ 2
  beta <- alpha * (1 / mu - 1)
  return(params = list(alpha = alpha, beta = beta))
}

estBetaParams(.4,(.1)^2)

Conjugate Analysis of Normally Distributed Data

Conjugate Binomial Analysis

Conjugate Analysis of Count Data

London Bombings During WWII

Hits (x) 0 1 2 3 4 7
Areas (n) 229 211 93 35 7 1

Heart Transplant Mortality

1
2
3
4
5
6
7
8
9
10
11
12
13
y_A<-1
n_A<-66
prop.test(y_A, n_A)


y_B<-4
n_B<-1767
prop.test(y_B, n_B)


y_T<-16
n_T<-15174
prop.test(y_T, n_T)
1
2
lambda_A<-rgamma(1000, shape=y_T+y_A, rate=n_T+n_A)
lambda_B<-rgamma(1000, shape=y_T+y_B, rate=n_T+n_B)
1
2
3
4
5
6
7
8
9
10
11
12
summary(lambda_A)
summary(lambda_B)

t.test(lambda_A, lambda_B)

par(mfrow = c(2, 1)) 
plot(density(lambda_A), main="HOSPITAL A", xlab="lambda_A", lwd=3) 
curve(dgamma(x, shape = y_T, rate = n_T), add=TRUE) 
legend("topright",legend=c("prior","posterior"),lwd=c(1,3)) 
plot(density(lambda_B), main="HOSPITAL B", xlab="lambda_B", lwd=3) 
curve(dgamma(x, shape = y_T, rate = n_T), add=TRUE) 
legend("topright",legend=c("prior","posterior"),lwd=c(1,3))