0

I am now working on using a mixture model including two components: normal and lognormal to fit a vector. I tried using JAGS, here is the code:

model {
   for(i in 1:N) {
     y[i] <- latent[i,index[i]+1]
     index[i] ~dbern(pi)
     latent[i,1]~ dlnorm(mu1,tau1)
     latent[i,2]~ dnorm(mu2,tau2)}
     pi ~ dbeta(0.5,0.5)
     mu1 ~ dnorm(0.4,0.000001)
     tau1~ dgamma(0.001,0.001)
     mu2 ~ dnorm(4,0.000001)
     tau2~ dgamma(0.001,0.001)
}

However, it does not work with an error message "y[1] is a logical node and cannot be observed". I also tried

y[i] <- pi*z1+(1-pi)*z2

z1 ~ dnorm(mu1,tau1)

z2 ~ dlnorm(mu2,tau2)

...

But it gave the same error message. It seems I have to assign a distribution to y[i]. Could anyone help to overcome this problem? or other approaches for solving such a mixture model would be appreciated too!

hveiga
  • 6,725
  • 7
  • 54
  • 78

1 Answers1

1

If you just wanted to mix these two models you could do something like this:

model {
   for(i in 1:N) {
     index[i] ~dbern(pi)
     latent[i]~ (dlnorm(mu1,tau1)*(1-index[i]))+(dnorm(mu2,tau2)*index[i]) 
}
     pi ~ dbeta(0.5,0.5)
     mu1 ~ dnorm(0.4,0.000001)
     tau1~ dgamma(0.001,0.001)
     mu2 ~ dnorm(4,0.000001)
     tau2~ dgamma(0.001,0.001)
}

That way, in each step of the model it either chooses to use the lognormal or the normal model. If you track index it will tell you which distribution is chosen at each step in the MCMC chain (index = 1 = normal, index = 0 = lognormal). Furthermore, you can take the sum of index and divide by the number of steps in your MCMC chain to get the proportion of times that 1 was chosen (normal).

mfidino
  • 3,030
  • 1
  • 9
  • 13
  • Hello, Thank you for your reply! I tried your code but the error message is "Error parsing model file: syntax error on line 4 near "(" " – Rosemary Chen Sep 24 '16 at 15:07
  • Ah. Change `latent[i]~ (dlnorm(mu1,tau1)*(1-index[i]))+(dnorm(mu2,tau2)*index[i]) ` to `y[i]~ (dlnorm(mu1,tau1)*(1-index[i]))+(dnorm(mu2,tau2)*index[i])` and see if that works. If it does I will edit the answer. – mfidino Sep 26 '16 at 16:21