Search code examples
bayesianjags

Compilation Error in simple JAGS model


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!


Solution

  • 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)