0

I'm new to JAGS and I'm running a model in R via R2jags package. The model code is based on a code taken from Kéry & Schaub 2012 ('Bayesian Population Analysis using WinBUGS"), pg 399.

Chi-square discrepancy measure is computed

model { 
....
for(g in 1:G) {
  for (t in 1:T) {
    ...
    E[g,t] <- pow((y[g,t] - eval[g,t]),2) / eval[g,t]
    ...
  }#t
}#g

fit <- sum(E[,])

}#model

where g and t are site and time indices and G and T are then the number of sites and the number of years

I get an error though

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
RUNTIME ERROR:
Compilation error on line 140.
Cannot evaluate subset expression for fit

Is it caused by different syntax used by JAGS relative to WinBUGS? The code is the same used in the book, except for I have 2 dimensions instead of three as in the book example.

Quechua
  • 124
  • 10

1 Answers1

0

To answer the last part of your question, no that error isn't caused by different syntax in JAGS (although the error message might look different in BUGS).

In fact I can't see anything wrong with the code snippet that you have posted, and the following reproducible example shows that it works at least when y and eval are given in data:

m <- 'model { 
for(g in 1:G) {
  for (t in 1:T) {
    E[g,t] <- pow((y[g,t] - eval[g,t]),2) / eval[g,t]
  }#t
}#g

fit <- sum(E[,])

#data# G, T, y, eval
#monitor# fit

}#model
'

library('runjags')

G=T <- 10
y <- matrix(rnorm(100), nrow=G, ncol=T)
eval <- matrix(rnorm(100), nrow=G, ncol=T)

results <- run.jags(m)

Have you verified what line 140 refers to? Either line 140 is something that you haven't shown, or maybe you have specified either fit or E somewhere else in the model with a different number of dimensions?

If this isn't the case and you still get an error then please add a minimal reproducible example to your question that shows the problem (preferably underneath an ---EDIT--- line below what you have already written) and we can try to help with that.

Matt

Matt Denwood
  • 2,537
  • 1
  • 12
  • 16
  • Thank you Matt, the line 140 is the `fit <- sum(E[,])` shown in the code, I didn't specified it sorry! I'll try with a minimum reproducible example then – Quechua Mar 27 '17 at 20:53