1

I am trying to fit a space state model, and I am getting this error message:

Error in jags.model(file = model.file, data = data, inits = inits, n.chains = n.chains,  : 
  RUNTIME ERROR:
Possible directed cycle involving some or all
of the following nodes:
t[2]
t[3]
t[4]
t[5]
t[6]
t[7]
t[8]
t[9]
v[2]
v[3]
v[4]
v[5]
v[6]
v[7]
v[8]
v[9]


I`ve found here JAGS error - Possible directed cycle involving some or all of the following nodes that it might be due to the term t[i] which is in both sides of the equation but I have done this before in other space states models and I have no problem with this, any idea of what can be causing this problem??? Here is the model:

o<-c(22.77619, 19.07782, 22.08817, 16.32168, 32.57081, 10.48027, 15.93440, 27.54557, 33.39933)

cat(file="pop.din","
model {
  t[1] <- n0
  o[1] ~ dlnorm(log(t[1]),tau.obs)
  for (i in 2:9) {
  v[i] <- lambda*t[i] #valor esperado
  t[i] ~ dpois(v[i])  #t valor verdadero
  o[i] ~ dlnorm(log(t[i]),tau.obs)
  }
  lambda ~ dunif(0.1,0.00001)
  tau.obs ~ dgamma(1,10)
  n0 ~ dlnorm(1,0.0001)
}")

pop.din.data<-c("o")

#initial values for the parameters stored as a list
inits<-function()list(lambda=runif(0.01,1),tau.obs=rlnorm(1,1,1),n0=rlnorm(1,1,1))

params<- c("lambda","n0","tau.obs")

ni <- 10000
nt <- 1
nb <- 5000
nc <- 3


library(jagsUI)
j.model   <- jags (model.file = "pop.din", data = pop.din.data,parameters.to.save = params,
                   inits=inits, n.burnin=nb,n.chains = nc,n.iter = ni)


print(j.model)

Regards

MyName
  • 89
  • 9

1 Answers1

1

Here is the solution to my problem: the space state model relates the value of a variable with its value a time before, in this case, so the process model should be written:

cat(file="pop.din","
model{
  #### Data Model
  for(i in 1:n){
    o[i] ~ dlnorm(x[i],tau_obs)
  }

  #### Process Model
  for(i in 2:n){
    v[i]<-lambda*x[i-1] ###not x[i]
    x[i]~dpois(v[i])  
  }

  #### Priors
  x[1] ~ dlnorm(x_ic,tau_ic)
  lambda ~dunif(mu,tau_lambda)
  tau_obs ~ dgamma(1,1)
  }")

Regards

MyName
  • 89
  • 9
  • thanks for answering ... should `v[i]` be getting passed to `dpois`? – user20650 Dec 27 '19 at 23:43
  • Excuse me, do you mean that it should be: ``cat(file="pop.din"," model{ #### Data Model for(i in 1:n){ o[i] ~ dlnorm(x[i],tau_obs) } #### Process Model for(i in 2:n){ v[i]<-lambda*x[i-1] ###not x[i] x[i]~dpois(v[i-1]) } #### Priors x[1] ~ dlnorm(1,0.0001) o[1] ~ dgamma(1,10) lambda ~ dunif(0,100) tau_obs ~ dgamma(1,1) }") `` – MyName Dec 28 '19 at 15:35
  • I am getting another error message: `` RUNTIME ERROR: Compilation error on line 5. Attempt to redefine node o[1]`` – MyName Dec 28 '19 at 15:38
  • Hi, my comment was a question rather than a definite "*you have done something wrong*". I suppose my query was because you define `v` but don't use it in the model. From eyeballing your code, I thought it should be `v[i] <-lambda*x[i-1] ; x[i]~dpois(v[i])` . Re the code in your comment^^, I don't think you need a proir on `o[1]` as `o` is observed. – user20650 Dec 28 '19 at 16:34
  • I think you 're right. I'm a novice at state-space modeling. ``x[i]`` is a hidden variable, in my case population true density, and ``o[i]`` is observed density but since I don't know the true initial value, I thought that prior should be written to it. – MyName Dec 28 '19 at 20:21