Hi Hanah,
The specific issue is that you can't use arbitrary R code in defining your model (please see Section 5.2.5 of our manual). In particular, there's various R functions in these lines that won't work (rep, seq_along, as.numeric, tapply):
However, you can do this with some additional work. Your case is similar to
another discussion we had on the users group a while back. The basic idea is that you need to do the indexing as part of a nimbleFunction (because we don't support syntax like `sum(u[idx[1:4]])` directly in model code).
Outside of your model code, write a nimbleFunction that does the mean calculation:
mymean <- nimbleFunction(
run = function(lambda = double(1), nbrs = double(1), start = double(1), nb = double(1), i = double(0)) {
return(mean(lambda[nbrs[start[i]:(start[i]+nb[i]-1)]])
returnType(double(0))
}
)
Then in model code:
u[i] <- mymean(lambda, nbrs, start, nb, i)
You'd need to provide nbrs, idx and nb as constants. 'nbrs' should be vector containing the deltas, all strung together in a single vector. 'start' should be a vector indicating the first delta for each i. Suppose that the delta_1 is {1,3} and delta_2 is {1,7,11}, and delta_3 is {2, 4} and so forth. Then 'start' is {1,3,6,...}.
Alternatively, your spatial formulation is similar to a CAR model, so you may want to consider that instead. See Chapter 9 of our manual and
this example.
-chris