Chapter 15
Nonparametric models

Example 15.1.3

model{
  for (i in 1:n){
    W[i] ~ dcat(w[1:2])
     y[i] ~ dnorm(mu[W[i]],tau[W[i]]) 
}
w[1:2] ~ ddirch(alpha[1:2])
mu[1] ~ dnorm(0,0.0001)
mu[2] ~ dnorm(0,0.0001)I(mu[1],)
for (i in 1:2) {tau[i] ~ dgamma(0.001,0.001) }
}
list(w=c(0.3,0.7), mu=c(0,2))
list(n=100, alpha = c(1,1),
y = c(-1.902,-1.579,-1.368,-1.204,
-1.068,-0.949,-0.842,-0.744,
-0.652,-0.566,...,4.883,5.172))

Exercise 15.3

model{
 # Sampling model
 for(i in 1:n){ 
   W[i] ~ dcat(w[1:2])
   y[i] ~ dnorm(mu[W[i]],tau[W[i]]) 
 }
 # Prior
 w[1:2] ~ ddirch(alpha[1:2])
 mu[1] ~ dnorm(0,0.001)
 delta1 ~ dnorm(0,0.001)I(0,)
 mu[2] <- mu[1]+delta1
 for(i in 1:2){ tau[i] ~ dgamma(0.001,0.001) }
 # Density estimate
 for(i in 1:100){
   grid[i] <- -3.5+5*(i-1)/99
   f[i] <- 0.398942*w[1]*sqrt(tau[1])*exp(-0.5*pow(grid[i]-mu[1],2)*tau[1])
          +0.398942*w[2]*sqrt(tau[2])*exp(-0.5*pow(grid[i]-mu[2],2)*tau[2])
 } 
}
list(w=c(0.5,0.5), mu=c(-2,NA), delta1=2, tau=c(1,1)) # inits
list(n=66, alpha=c(1,1), 
      y = c(-2.3,0.66,-1.11,0.52,0.93,0.8,0.07,
                0.43,-2.66,0.46,-0.34,0.53,0.17,
               -0.04,0.35,-2.81,0.4,0.13,0.88,
                0.83, -2.3,-2.53,-1.35,-1.71,0.55,
              -3.22,0.13,0.6,-0.07,-1.56,1.02,
                0.61,0.52,0.03,0.6,-2.04,0.76,-1.39, 
                0.51,0.15,-0.26,0.98,0.8,
               0.24,0.12,0.67,-0.07,-0.31,0.41,-1.05,
               -1.35,-1.71,0.92,-1.2,-2.3,-1.27,0.49,-0.26,
               -2.81,-0.82,-0.73,-0.2,0.59,0.58,-1.77,-2.04))