Bayes Book
Chapter 4:  Fundamental Ideas II

Exercise 4.1

model{   
 y ~ dpois(theta)  
 z ~ dbern(q0)  
 theta0 <- 1  
 theta1 ~ dexp(1)  
 theta <- z*theta0 + (1-z)* theta1 
} 
list(y=3, q0=0.5)
list(theta1=1,z=1)

Exercise 4.2

 model{ 
  for(i in 1:2){ 
    y[i] ~ dexp(theta[i])
    ttheta[i] ~ dexp(1)      # priors on distinct thetas
    theta[i] <- z*mu + (1-z)*ttheta[i]   # z indicates common versus different rates
  }      
  z ~ dbern(q0)   
  mu ~ dexp(1)  # mu is the common rate
 }
 list(y=c(500,2), q0=0.5) 
 list(z=1,ttheta=c(1,1),mu=1)   

Example 4.8.1

model{  
 for(i in 1:n){
   y[i] ~ dgamma(alpha,lambda)
   v[i] <-  -(log(theta0) + theta0*theta1*log(theta1)) +
               y[i]*(theta1 -1/theta0) - (theta0*theta1 -1)*log(y[i]) +
               loggam(theta0*theta1)  # WinBUGS loggamma function     
 }
 alpha <- theta0*theta1   
 lambda <- theta1     
 theta0 ~ dgamma(a,b)     
 theta1  ~ dgamma(c,d)     
 BF   <-   exp(sum(v[1:n])) 
}
list(n=19,c=1,d=1,a=1,b=1,
      y=c(0.05129329, 0.10536052, 0.16251893,  0.22314355,
             0.28768207, 0.35667494,  0.43078292 ,0.51082562,
             0.59783700 ,0.69314718, 0.79850770, 0.91629073,
             1.04982212, 1.20397280, 1.38629436, 1.60943791,
             1.89711998, 2.30258509, 2.99573227))

Exercise 4.23

model{ 
 for(i in 1:10){ y[i] ~ dlnorm(mu,tau) } 
 tau ~ dgamma(0.001,0.001) 
 mu ~ dflat()
}
list(y=c(1.287, 3.961, 0.538, 1.281, 0.482,  
             1.929, 1.951, 0.825, 0.801, 4.114))
list(mu=0,tau=1)

Exercise 4.27

model{ 
 for(i in 1:k){
   y[i] ~ dbin(theta[i],n[i])     
   theta[i] ~ dbeta(alpha1,alpha2)           
 }
 mu  ~ dbeta(a,b)     
 psi ~ dgamma(c,d)     
 alpha1 <- mu*psi   
 alpha2 <- (1-mu)*psi     
 prev ~ dbeta(alpha1,alpha2)  # Gets estimate of the distribution of prevalences     
 prob <-  step(prev - 0.5)  # Pr(prev > 0.5|y)       
}