I am trying to fit a CJS model in JAGS using jagsUI package.
I have several models that I would like to compare with WAIC.
For that, I am trying to calculate a log-likelihood inside the model and take it out as a parameter, so I can use the loo package to have a WAIC value.
However, I encounter with this error for my log likelihood calculation: "Unable to resolve the following parameters:
Below is my full JAGS code, I got lost in the many forums to find a solution however still couldn't manage the code to work. Does someone have any idea on what am I doing wrong or what is missing?
I'd appreciate any help!
Thank you so much in advance.
Dilsad
------------------ JAGS CODE------------------ J
cat(file = "p1global.txt","
model {
## Priors and constraints
#########################
for (i in 1:n.individuals){
for (t in f[i]:(n.occasions-1)){
logit(phi[i,t]) <- alphaphi +
betaphi1[sex[i]] + betaphi2[agemat[i,t]] + betaphi3[season[t]] +
betaphi4[sex[i],agemat[i,t]] + betaphi5[sex[i],season[t]] + betaphi6[agemat[i,t],season[t]]
logit(p[i,t]) <- alphap +
betap1[sex[i]] + betap2[agemat[i,t]] + betap3[season[t]] +
betap4[sex[i],agemat[i,t]] + betap5[sex[i],season[t]] + betap6[agemat[i,t],season[t]] +
epsilonp[year[t]]
} # t
} # i
### Intercepts ###
alphaphi <- logit(mean.phi)
mean.phi ~ dunif(0, 1) # Prior for mean survival
alphap <- logit(mean.p)
mean.p ~ dunif(0, 1) # Prior for mean recapture
### Slopes ###
for (b in 1:2){
# Prior for the slope of each sex class, F=1 , M=2
betaphi1[b] ~ dunif(0,1)
betap1[b] ~ dunif(0,1)
# Prior for the slope of each age class, J=1, A=2
betaphi2[b] ~ dunif(0,1)
betap2[b] ~ dunif(0,1)
# Prior for the slope of each season, D=1, W=2
betaphi3[b] ~ dunif(0,1)
betap3[b] ~ dunif(0,1)
}
# Prior for the slope of the interaction effects
# order for betas always: 1,1; 1,2; 2,1; 2,2
for (i in 1:2){
for (t in 1:2) {
# sex:age will have total 4, FJ, FA, MJ, MA
betaphi4[i,t] ~ dunif(0,1)
betap4[i,t] ~ dunif(0,1)
# sex:season will have total 4, FD, FW, MD, MW
betaphi5[i,t] ~ dunif(0,1)
betap5[i,t] ~ dunif(0,1)
# age:season will have total 4, JD, JW, AD, AW
betaphi6[i,t] ~ dunif(0,1)
betap6[i,t] ~ dunif(0,1)
}
}
## Random effect ##
for (t in 1:(n.years)){
epsilonp[t] ~ dnorm(0, taup)
}
sigmap ~ dunif(0, 10) # Prior for standard deviation
taup <- pow(sigmap, -2)
sigma2p <- pow(sigmap, 2) # Temporal variance
## Likelihood
#########################
for (i in 1:n.individuals){
# Define latent state at first capture
z[i,f[i]] <- 1
for (t in (f[i]+1):n.occasions){
# State process
z[i,t] ~ dbern(mu1[i,t])
mu1[i,t] <- phi[i,t-1] * z[i,t-1]
# Observation process
y[i,t] ~ dbern(mu2[i,t])
mu2[i,t] <- p[i,t-1] * z[i,t]
} # t
} # i
## Log-Likelihood for WAIC ##
for (i in 1:n.individuals){
for (t in (f[i]+1):n.occasions){
# estimate the cell log-likelihood, all distributions in JAGS have a log density function associated with them
log_lik0[i,t] <- logdensity.bern(y[i,t],p[i,t-1] * z[i,t])
# get the row log-likelihood
log_lik[i] <- sum(log_lik0[i,])
} # t
} # i
}")
Later I want to use log_lik with the loo package
library(loo)
waic(p1global_out$sims.list$log_lik)