MyName
MyName

Reputation: 109

Possible directed cycle in JAGS

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

Upvotes: 1

Views: 518

Answers (1)

MyName
MyName

Reputation: 109

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

Upvotes: 1

Related Questions