2

I'm having quite a hard time in figuring out where the error should be in this simple Rjags model:

  dt=
[1] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 2.304157e-06 1.014568e-05
  [8] 1.798720e-05 2.582872e-05 3.367025e-05 4.151177e-05 4.935329e-05 5.719481e-05 6.503634e-05
 [15] 7.287786e-05 8.071938e-05 8.856090e-05 9.640242e-05 1.042439e-04 1.120855e-04 1.200225e-04
 [22] 1.274917e-04 1.332419e-04 1.426773e-04 1.682728e-04 2.018543e-04 2.302676e-04 2.810248e-04
 [29] 3.408895e-04 3.942806e-04 4.818033e-04 5.682188e-04 6.298504e-04 7.320266e-04 9.604787e-04
 [36] 1.315336e-03 1.695445e-03 1.945434e-03 2.047065e-03 2.192386e-03 2.404184e-03 2.629989e-03
 [43] 2.931693e-03 3.414607e-03 4.085636e-03 5.009995e-03 5.897174e-03 6.532098e-03 7.205807e-03
 [50] 8.125645e-03 9.097041e-03 9.857559e-03 1.021277e-02 1.026859e-02 1.053671e-02 1.125948e-02
 [57] 1.207536e-02 1.250217e-02 1.297049e-02 1.372405e-02 1.463063e-02 1.552144e-02 1.604347e-02
 [64] 1.628257e-02 1.701820e-02 1.843134e-02 2.045894e-02 2.294199e-02 2.517307e-02 2.666305e-02
 [71] 2.762321e-02 2.890740e-02 3.095278e-02 3.290344e-02 3.429784e-02 3.502680e-02 3.525143e-02
 [78] 3.534536e-02 3.508330e-02 3.431497e-02 3.302332e-02 3.136818e-02 2.979437e-02 2.850674e-02
 [85] 2.713331e-02 2.539821e-02 2.301579e-02 2.002319e-02 1.702893e-02 1.447840e-02 1.217091e-02
 [92] 1.006765e-02 8.119336e-03 6.326128e-03 4.835775e-03 3.611105e-03 2.514688e-03 1.618690e-03
 [99] 1.006864e-03 6.681004e-04 4.693963e-04 3.256632e-04

data = list('dt')

library(rjags);library(foreign);library(coda)
cat('model{ 
for(k in 1:102){ dt[k] ~ dgamma(a, 0.5)}
a=ax+ay

X  ~ dgamma(ax, 0.5)
Y  ~ dgamma(ay, 0.5) 
ax ~ dgamma(3, 1/10)
ay ~ dgamma(3, 1/10)
    }',
file='model1.bug')

inits1 = list('ax'=15, 'ay'=15)    
jags_mod = jags.model('model1.bug', data=data, inits=inits1, n.chains=1, n.adapt=2000)    

update(jags_mod, n.iter=2000)
posts=coda.samples(model=jags_mod, variable.names=c('ax','ay','a', 'X', 'Y'), n.iter=1000000, thin=2000)

The main idea should be that variable dt is a Gamma, sum of two Gamma random variables with the same rate parameter. I have made sure that there can't be negative values for the parameters, by assigning also Gamma priors on the two scale parameters, so I don't understand why Rjags is complaining here. I would like to infer on both parameters ax, ay (and secondarily on a).

Thanks for all the help you will provide! Best, Emanuele

LZG
  • 59
  • 1
  • 7
  • Not sure which node is inconsistent with the parents, but the assignment operator in `jags` is `<-`, not `=`. – mfidino Oct 06 '16 at 17:38
  • I'm sorry, the inconsistent signaled node is `dt[1]`. – LZG Oct 06 '16 at 17:53
  • The gamma distribution sometimes has some difficulties with numbers at zero, of which you have. Try adding at least a small number to all your data in `dt` (like 0.001) and see if the model runs. Of course, this is not 100% a work around, but could indicate that your choice in priors are not good given your data. – mfidino Oct 06 '16 at 20:58

1 Answers1

8

The gamma distribution is strictly positive, so cannot be zero (or negative). The first 5 observations of your data are therefore not consistent with your model, hence the error.

Possible solutions are to model this as censoring, a mixture model with a mass at zero, or possibly to simply add a constant to your data, but without knowing what your data represents it isn't possible to say which is best.

Matt Denwood
  • 2,537
  • 1
  • 12
  • 16