Hello all,
I have hit a wall here, might be a simple error, but i can't seem to find it, maybe someone can help.
I am attempting to run an n-mixture on model on camera trap data. the data was collected at a different study area each year, so I want to loop over years, then sites/reps for each year.
Another small wrinkle is that I am using a long form (vector) for the detection data, and then supplying indexing variables as constants. I have removed NAs from the data set so the capture histories have uneven numbers of replicates by site (dealt with by the indexing variables).
the model includes some covariates on detection (days since lure applied, Julian date, julian date sq), I get the same error below if I remove the covs from the model.
I have fairly meticulously checked the indexing vars, this is the messiest part of the model, but seems to be correct as far as i can tell.
I can create and configure the model, but when running the samples I get the following error:
running chain 1...
Error in if (nrow > rows) { : missing value where TRUE/FALSE needed
In addition: Warning message:
In container$resize(as.integer(k)) :
NAs introduced by coercion to integer range
Thank you in advance, I would be grateful for any suggestions
I can post the data if it would be helpful
Model:
fm.site.Nmix <- nimbleCode({
# Priors
alpha0 ~ dnorm(0, 0.1)
# alphas - det covs
for(l in 1: n.alpha){
alpha[l] ~ dnorm(0, 0.1)
}
for(k in 1:n_years){ ## loop over years
lambda[k] ~ dgamma(0.001, 0.001)
for (i in 1:n.sites.by.year.index[k]) { # Loop over sites (for year K )
N[site.by.year.index[k,i]] ~ dpois(lambda[k])
for (j in 1:n.reps.by.year.site.index[k,i]) { # Loop over replicates (by site and year)
y[rep.by.year.site.index[k,i,j]] ~ dbin(p[rep.by.year.site.index[k,i,j]] , N[site.by.year.index[k,i]])
logit(p[rep.by.year.site.index[k,i,j]]) <- alpha0 +
(alpha[1:n.alpha] %*% det.covs.on.reps[rep.by.year.site.index[k,i,j], 1:n.alpha])[1,1]
}
}
}
})
indexing vars:
n.sites.by.year.index[k]: vector of number of sites by year
site.by.year.index[k,i]: ragged matrix of individual sites by year
n.reps.by.year.site.index[k,i]: ragged matrix of number of reps for each site by year
rep.by.year.site.index[k,i,j]: 3d array of individual reps by site and year
Data lists:
dat<-list(y = y,
det.covs.on.reps = rep.cov)
const<-list(
n_years = 5,
n.sites.by.year.index = index$yr.nsite,
rep.by.year.site.index = index$yr.site.rep,
n.reps.by.year.site.index = index$yr.nreps,
site.by.year.index = index$yr.site,
n.alpha = ncol(rep.cov)
)
init<- list( N = n.start, #total detections by site plus 1 (from ahm -> nimble on git)
lambda = rep(.5, 5),
alpha = rep(0,ncol(rep.cov)),
p = runif(length(y), 0, 1)
)
params <- c('N', 'alpha0', 'alpha', 'lambda', 'p')
model call:
run.model<-nimbleModel(
code = fm.site.Nmix ,
constants = const,
data = dat,
inits = init
)
model.config<-configureMCMC(run.model, monitors = params, onlySlice = F)
mcmc.model<-buildMCMC(model.config, enableWAIC = T)
samples<-runMCMC(
mcmc.model,
niter = 100,
nburnin = 0,
thin = 0,
summary = T,
WAIC = T)