0
$\begingroup$

This is my first post,I hope this is the right forum for such a question and I formulate it correctly. I am working with a time series data set where the response y seems to follow a mixture of two normal distributions. i.e. hist (y) draws two 'bell shapes' with means m1 and m2 which seem to be partially overlapping.

The data set also comes with additional covariates x1 and x2.

x1 is a continuous variable variable with again displaying mixture models behaviour (two partially overlapping normal distributions), and x2 = 0 for dates 200x-xx and before and =1 for all later points in time. I suspect that x2 causes x1 and y to display mixture model distributions.

Ignoring the fact that y is probably a mixture model I have the following jags model:

   mod1_string = " model{
     for (i in 1:n){
      y[i]~dnorm(mu[i],prec)
      mu[i]=b[1]+b[2]*x1[i]+b[3]*x2[i]
     }
     for (j in 1:3){
        b[j] ~ dnorm(0.0,1.0e-6)
     }
     prec ~ dgamma(5.0/2.0,5.0*1.0/2.0)
    sig2 =1.0/prec
    sig =sqrt(sig2)
    }"

I run the above for n.iter=1e5 and traceplots look ok(mixed ok), gelman diagnostics also show no issues as far as I can see. Not sure about the autocorrelation as I guess y (and also x1) seemed autocorrelated in the first place, ideally I would need a jags model to also cater for AR(2) effects.

If I ignore x1 and x2 I have a mixture model such as below:

    mod2_string = " model {
     for (i in 1:length(y)) {
      y[i] ~ dnorm(mu[z[i]], prec)
      z[i] ~ dcat(omega)
     }

     mu[1] ~ dnorm(-1.0, 1.0/100.0)
     mu[2] ~ dnorm(1.0, 1.0/100.0) T(mu[1],) # ensures mu[1] < mu[2]
     prec ~ dgamma(1.0/2.0, 1.0*1.0/2.0)
     sig = sqrt(1.0/prec)

     omega ~ ddirich(c(0.0, 1.0))
    } "

Again traceplots and diagnostics look ok but how could I add x1 and x2 to model 2 ?

Some attempts I made were at embedding the categorical z in the mu[] calculation statement like below:

    mod3string = "model{
      omega ~ ddirich(c(1.0,1.0))
      for (i in 1:n){
        y[i]~dnorm(mu[z[i]],prec)
        z[i]~dcat(omega)
      }
      for (j in 1:3){
         b[j] ~ dnorm(0.0,1.0e-6)
      }
      mu[z[i]]=b[1]+b[2]*x1[i]+b[3]*x2[i]
      prec ~ dgamma(1.0/2.0,1.0*1.0/2.0)
      sig =sqrt(1.0/prec)
     }"

...but I get errors such as Error in jags.model(textConnection(mod2string), data = data2_jags, n.chains = 3) :

RUNTIME ERROR:
Compilation error on line 13.
Unknown variable z
Either supply values for this variable with the data or define it  on the 
left hand side of a relation.

Have I got the theory confused or can such a model be described and run with jags?

$\endgroup$
  • 1
    $\begingroup$ I think you missed a for loop on the last third line. $\endgroup$ – zick094 Jun 23 '18 at 11:21
0
$\begingroup$

I believe that you need to put the line:

mu[z[i]]=b[1]+b[2]*x1[i]+b[3]*x2[i]

inside the first for loop. That's why it doesn't know how to handle z, because it isn't established outside the loop.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.