nimp4 <- nimbleCode({
#Priors and constraints
survT ~ dunif(0,1)
mean.pNT ~ dunif(0,1)
mean.rNT ~ dunif(0,1)
mean.rT ~ dunif(0,1)
mean.pT ~ dunif(0,1)
for(u in 1:10){
survNT[u] ~ dunif(0,1)
gam[u] ~ dunif(0,1)
}
#Modelling territorial recapture: 0s for non-sampling intervals, constant probability for sampling intervals
pT[1] <- 0
pT[2] <- mean.pT
pT[3] <- 0
pT[4] <- mean.pT
pT[5] <- 0
pT[6] <- mean.pT
pT[7] <- 0
pT[8] <- mean.pT
pT[9] <- 0
pT[10] <- mean.pT
pT[11] <- 0
pT[12] <- mean.pT
pT[13] <- 0
pT[14] <- mean.pT
pT[15] <- 0
pT[16] <- mean.pT
pT[17] <- 0
pT[18] <- mean.pT
pT[19] <- 0
pT[20] <- mean.pT
pT[21] <- 0
pT[22] <- mean.pT
pT[23] <- 0
pT[24] <- mean.pT
#Define state-transition and observation matrices
#STATES:
#1: ALIVE, NON-TERRITORIAL
#2: ALIVE, TERRITORIAL
#3: DEAD, NON-TERRITORIAL
#4: DEAD, TERRITORIAL
#5: LONG-DEAD
#OBSERVATIONS:
#1: ALIVE, NT
#2: ALIVE, T
#3: DEAD, NT
#4: DEAD, T
#5: UNOBSERVED
for(i in 1:nind){
for(t in f[i]:(n.occasions-1)){ #f is the vector of first encounters
phiNT[i,t] <- survNT[ages_sex[i,t]]
gamma[i,t] <- gam[ages_sex[i,t]]
#Survival and recruitment are age-dependent
#Ages_sex is the sex-by-age matrix, with 1:5 for males and 6:10 for females.
#we changed NAs by 0s between first occasion and first encounter since we already had some issues with NAs in
#the state matrix initial values (z). The model seemed to accept it.
#State transition matrix
ps[1,1,i,t] <- phiNT[i,t]
ps[1,2,i,t] <- phiNT[i,t]*gamma[i,t]
ps[1,3,i,t] <- (1-phiNT[i,t])
ps[1,4,i,t] <- (1-phiNT[i,t])*gamma[i,t]
ps[1,5,i,t] <- (1-phiNT[i,t])
ps[2,1,i,t] <- 0
ps[2,2,i,t] <- survT
ps[2,3,i,t] <- 0
ps[2,4,i,t] <- (1-survT)
ps[2,5,i,t] <- (1-survT)
ps[3,1,i,t] <- 0
ps[3,2,i,t] <- 0
ps[3,3,i,t] <- 0
ps[3,4,i,t] <- 0
ps[3,5,i,t] <- 1
ps[4,1,i,t] <- 0
ps[4,2,i,t] <- 0
ps[4,3,i,t] <- 0
ps[4,4,i,t] <- 0
ps[4,5,i,t] <- 1
ps[5,1,i,t] <- 0
ps[5,2,i,t] <- 0
ps[5,3,i,t] <- 0
ps[5,4,i,t] <- 0
ps[5,5,i,t] <- 1
} #t
} #i
for(t in f[i]:(n.occasions-1)){
#Observation probabilities
po[1,1,t] <- mean.pNT
po[1,2,t] <- 0
po[1,3,t] <- 0
po[1,4,t] <- 0
po[1,5,t] <- (1-mean.pNT)
po[2,1,t] <- 0
po[2,2,t] <- pT[t]
po[2,3,t] <- 0
po[2,4,t] <- 0
po[2,5,t] <- (1-pT[t])
po[3,1,t] <- 0
po[3,2,t] <- 0
po[3,3,t] <- mean.rNT
po[3,4,t] <- 0
po[3,5,t] <- (1-mean.rNT)
po[4,1,t] <- 0
po[4,2,t] <- 0
po[4,3,t] <- 0
po[4,4,t] <- mean.rT
po[4,5,t] <- (1-mean.rT)
po[5,1,t] <- 0
po[5,2,t] <- 0
po[5,3,t] <- 0
po[5,4,t] <- 0
po[5,5,t] <- 1
}
# Likelihood
for (i in 1:nind){
# Define latent state at first capture
z[i,f[i]] <- y[i,f[i]]
for (t in (f[i]+1):n.occasions){
# State process: draw S(t) given S(t-1)
z[i,t] ~ dcat(ps[z[i,t-1], 1:5, i, t-1])
# Observation process: draw O(t) given S(t)
y[i,t] ~ dcat(po[z[i,t], 1:5, t])
} #t
} #i
})
ages_sex_nimble <- ages_sex[,1:24]
ages_sex_nimble[
is.na(ages_sex_nimble)] <- 0
my.data <- list(y=CMRmatrix)
my.constants <- list(n.occasions=dim(CMRmatrix)[2], nind=dim(CMRmatrix)[1], f=f, ages_sex = ages_sex_nimble)
z2 <- my.inits4(CMRmatrix, f, 5)
my.inits <- function(){list(survNT=runif(10,0,1),
mean.pNT=runif(1,0,1),
mean.pT=runif(1,0,1),
survT=runif(1,0,1),
gam=runif(10,0,1),
mean.rNT=runif(1,0,1),
mean.rT=runif(1,0,1),
z=z2)}