Chapter 3
Adam Branscum
1-14-2010

Section 3.2:  WinBUGS I:  Getting Started

Simple Binomial

model{
 y ~ dbin(theta,10)
 theta ~ dbeta(1,1) 
}
list(y=7)
list(theta=.5)

	 node	 mean	 sd	     MC error	2.5%	  median	97.5%	 start	sample
	  theta	0.6677	0.1309	0.00153	   0.3952	0.6754	   0.8914	1001	10000

Simulate normals

model{ 
m1 ~ dnorm(0,1)
m2 ~ dnorm(0,1)
m3 ~ dnorm(0,1) 
}

Z~N(0,1); Pr[Z>1]

model{ 
 theta ~ dnorm(0,1)
 w <- step(theta-1)
}

Section 3.3:  Method of Composition

Simulate from p(y,mu,tau)

model{
 tau ~ dgamma(5,10)
 mu ~ dnorm(3,tau)
 y ~ dnorm(mu,tau)
}

 node	 mean	 sd	 MC error	2.5%	median	97.5%	start	sample
	mu	2.978	1.581	0.01646	-0.1694	2.982	6.066	1	10000
	tau	0.4995	0.224	0.002344	0.1613	0.4672	1.031	1	10000
	y	2.966	2.237	0.0239	-1.522	2.964	7.41	1	10000

Exercise 3.6

model{ 
 y ~ dbin(theta, n)  # Model the data
 ytilde ~ dbin(theta, m) # Prediction of future binomial
 theta ~ dbeta(a, b) # The prior
 prob <- step(ytilde - 20) # Pred prob that ytilde >= 20
} 
list(n = 100, m = 100, y = 10, a = 1, b = 1) # The data
list(theta = 0.5, ytilde =10) # Starting/initial values

Exercise 3.7

model{ 
 y1 ~ dbin(theta1, n1) # Modeling the data as
 y2 ~ dbin(theta2, n2) # independent binomials
 theta1 ~ dbeta(1, 1)  # Specifying the priors
 theta2 ~ dbeta(1, 1) 
 prob1 <- step(theta1-theta2) # Pr(theta1 >= theta2|data)
 y1tilde ~ dbin(theta1, m1) # Get predictive densities
 y2tilde ~ dbin(theta2, m2)
 prob2 <- step(y1tilde - y2tilde -11) 
}
list(y1=25, y2=10, n1=100, n2=100, m1=100, m2=100) # The data
list(theta1=.5, theta2=.5, y1tilde=20, y2tilde=20) #Starting values