0
votes

I have read other questions on the topic, but all of the models on those questions are far more complicated than mine and are not helping me find my answer (very new to JAGS).

When I run the following:

x <- c(1,0,4,1,4,2,5,3,0,3,1,2,2,4,1)
Data <- as.list(x=x, nx=length(x))

model <- function() {
## Likelihood
for (i in 1:nx) {
x[i] ~ dpois(mu[i])
}
## Prior
mu[i] ~ dexp(1)
}
fit <- jags(Data, param=c("mu"), model=model, n.chains=1, n.iter=10000,   
n.burn=0, n.thin=1, DIC=FALSE) 

I get the error: Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, : RUNTIME ERROR: Compilation error on line 3. Cannot evaluate upper index of counter i

Other solutions mention things being in the loops that shouldn't be in the loops, but I don't think I have any problems with my loop? I'm not sure. Thank you!

1
Welcome to CV! Questions which just relate to debugging code aren't considered on-topic here, but you can check out our list of support resources. - Silverfish
For one thing, mu[i] ~ dexp(1) needs to be in your i loop. If that doesn't solve your problem, we'll need to see the structure of your Data. - jbaums
@src471 did my answer below resolve your question? - rackhamup

1 Answers

1
votes

I believe your issue is that your data list isn't in the right format. Rather than use as.list just use list(). Also, like jbaums mentioned you need to move mu[i] inside the loop. Try this:

x <- c(1,0,4,1,4,2,5,3,0,3,1,2,2,4,1)
Data <- list(x=x, nx=length(x))

model <- function() {
  ## Likelihood
  for (i in 1:nx) {
    x[i] ~ dpois(mu[i])
    ## Prior
    mu[i] ~ dexp(1)
  }

}
fit <- jags(Data, param=c("mu"), model=model, n.chains=1, n.iter=10000,   
            n.burn=0, n.thin=1, DIC=FALSE)