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!
If you just wanted to mix these two models you could do something like this:
That way, in each step of the model it either chooses to use the lognormal or the normal model. If you track
indexit 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).