I'm trying to get an N-mixture model to run in JAGS for a stats class that I'm currently in. However, I keep getting the error "Failed to set trace monitor for deviance There are no observed stochastic nodes" whenever I try to run the model. At the moment I'm just trying to get a very basic model with no covariates to run to make sure I have everything formatted correctly but am still unable to get the model to run. Any advice would be very much appreciated as I've been struggling with this for a while now and can't figure out where I've gone wrong.
Here's a reproducible example of the issue:
library(R2jags)
# Create example dataframe
years <- c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)
sites <- c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3)
months <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
# Detection data
day1 <- floor(runif(18,0,7))
day2 <- floor(runif(18,0,7))
day3 <- floor(runif(18,0,7))
day4 <- floor(runif(18,0,7))
day5 <- floor(runif(18,0,7))
df <- as.data.frame(cbind(years, sites, months, day1, day2, day3, day4, day5))
# Put data into array
y <- array(NA,dim=c(2,3,3,5))
for(m in 1:2){
for(k in 1:3){
sel.rows <- df$years == m &
df$months==k
y[m,k,,] <- as.matrix(df)[sel.rows,4:8]
}
}
# JAGS model
sink("model1.txt")
cat("
model {
# PRIORS
lambda ~ dunif(0,10)
p ~ dunif(0,1)
# LIKELIHOOD
# ECOLOGICAL MODEL FOR TRUE ABUNDANCE
for (m in 1:2) { # Loop over years (1-2)
for (k in 1:3) { # Loop over months (1-3)
for (i in 1:3) { # Loop over sites (1-3)
N[m,k,i] ~ dpois(lambda)
for (j in 1:5) { # Loop over days (1-5)
y[m,k,i,j] ~ dbin(p, N[m,k,i])
}#j
}#i
}#k
}#m
}#END", fill=TRUE)
sink()
win.data <- list(y <- y)
Nst <- apply(y,c(1,2,3),max)+1
inits <- function()list(N = Nst)
params <- c("lambda",
"N")
nc <- 3
nt <- 1
ni <- 5000
nb <- 500
out <- jags(win.data, inits, params, "model1.txt",
n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb,
working.directory = getwd())
Inside of
list
objects you cannot use the "<-
" assignment operator to create a named list, which is what you need to provide toJAGS
. Instead, you need to use=
.So if you change this line in your code:
to:
the model compiles.