I have a multi year model of count data and wrote code to condition on the data if there are NA's present or data and how the transitions occur. I wrote a model without conditionals for this same year with for loops in sequence and Nimble performed wonderfully. However I would prefer not to write unique coding for each year and this code should parse each year correctly. The issue is when I run this I get this specific error:
Test_Code <- nimbleCode({
n[1] <- z[1]
if(
is.na(y[1])){
y[1] <- n[1]
}else{
y[1] ~ dbin(p,n[1])
}
for(i in 2:Days){
if(
IS.NA[i]==1 &
IS.NA[i-1]==1){
n[i] <- z[i]
y[i] <- n[i]
}else if (
IS.NA[i] != 1 &
IS.NA[i-1] == 1) {
n[i] <- z[i]
y[i] ~ dbin(p,n[i])
}else if(
IS.NA[i] != 1 &
IS.NA[i-1] != 1 ) {
n[i] <- n[i-1]-y[i-1]+z[i]
y[i] ~ dbin(p,n[i])
}else {
n[i] <- n[i-1]-y[i-1]+z[i]
y[i] <- n[i]
}
}
for(i in 1:Days){
z[i] ~ dnegbin(pz[i],r)
pz[i] <- r/(r+mu[i])
mu[i] <- alpha * exp((m-i)/beta)*exp(-exp((m-i)/beta)) / beta
}
alpha <- exp(lnalpha)
lnalpha ~ dnorm(0,sd=32)
beta ~ dunif( 1,12 )
m ~ dunif( 5,35 )
r ~ dexp(0.001)
p ~ dbeta(167,182)
esc <- sum(y[1:48])
})
y = c(NA,NA,NA,1,1,0,1,14,10,16,
117,28,35,71,362,679,464,NA,NA,NA,
NA,NA,NA,NA,725,754,NA,NA,NA,379,
300,341,483,350,250, 357,139,53,54,96,
142,NA,NA,NA,NA,54,34,29)
y
IS.NA <-
is.na(y)*1.0
Test_Inits <- list(p=0.5,lnalpha=9.8, m=20.7 ,beta=7.3, r = 0.1,
z=c(1,1,1,2,1, 0,1,27,6,22,
140,17,42,107,653, 996,249,536,1000,1000,
1000,1000,1000,1000,1450, 783,246,1000,1000,758,
221,382,625,217,150, 226,66,60,55,138,
188,58,200,200,200, 108,14,24)
)
constants <- list(Days = length(y),
IS.NA =
is.na(y)*1.0)
Test_Model <- nimbleModel(Test_Code, inits = Test_Inits, data = list( y = y))