# JAGS, cannot evaluate upper index of counter

I asked this question at the JAGS sourceforge help forum but didn't get response there. I have the following JAGS model:

model {
x ~ dbin(0.5, 10)
# length of y is larger than 11
for(i in 1:x) {
y[i] ~ dnorm(0, 1)
}
for(i in (x+1):length(y)) {
y[i] ~ dnorm(2,1)
}
}


And here is the R code and simulated data to work with it:

library(rjags)
y <- c(dnorm(4,0,1), dnorm(12,2,1))
data <- list(y = y)
jm <- jags.model('test.model', data = data)


However, I got an error message:

Error in jags.model("test.model", data = data) : RUNTIME ERROR:
Compilation error on line 3.
Cannot evaluate upper index of counter i


I wonder if it's because jags doesn't support variable index in for loops, but I didn't find any reference in the manual and google search. In this case, I don't know where is the data partitioned into two different likelihoods and I am trying to do inference on that. If jags doesn't support variable index in for loops, what alternatives can I try in such situation?

Thanks a lot for the help!

• What is the length of y? Could it possibly be > dpois(10) + 1 ..? – Tim Jan 31 '15 at 21:41
• @Tim, the length of y shall be well larger than 11. I can change the distribution of x to be binomial so that it's bounded, but still, I get the error. – qkhhly Jan 31 '15 at 21:46
• Maximal value of Poisson distribution with mean = 10 is much larger than 11! Check in R: max(rpois(1e8, 10)). – Tim Jan 31 '15 at 21:49
• As I said, I can change it to x ~ dbin(0.5, 10), so that maximal of x is 10, but there is still the error. So I don't think the range of x is the problem. – qkhhly Jan 31 '15 at 21:55

## 1 Answer

Actually the answer is pretty simple: for-loops in BUGS/JAGS are not for-loops. Those are declarative language and loops are just a way to declare "apply to all".

Excerpt from JAGS paper (Plummer, 2003):

The existence of “for” loops in the BUGS language is somewhat incongruous, since it is a declarative language, not a procedural one. For loops can be thought of as a kind of macro that succinctly describes repetitive structures. For example, the loop:

for (i in 1:2) {
mu[i] <- alpha + beta*(x[i] - x.bar);
Y[i] ~ dnorm(mu[i], tau);
}


can be thought of as expanding to

mu[1] <- alpha + beta*(x[1] - x.bar);
Y[1] ~ dnorm(mu[1], tau);
mu[2] <- alpha + beta*(x[2] - x.bar);
Y[2] ~ dnorm(mu[2], tau);


Your model declaration is simply not possible in BUGS/JAGS.

Since loops in BUGS/JAGS declare "repetitive structures" you should think of defining such a general structure that will be flexible enough to define both cases in a single loop. What may work is defining a dummy variable with values from 1 to $N$:

x ~ dbin(0.5, 10)
for (i in 1:N) {
mu[i] <- ifelse(dummy[i] < x, 0, 2)
y[i] ~ dnorm(mu[i], 1)
}


You could also try Stan instead since it is more flexible and enables you to do actual programming (rather than work-around tricks).

• Unfortunately, Stan doesn't support discrete parameter. So it's not easy to do inference on discrete parameter in Stan. Could this be done in jags? – qkhhly Jan 31 '15 at 22:18