Below is a reproducible example for the multi-species
N-mixture model, and attached is the necessary input data, including
z-scored covariates. Any insights or suggestions to improving this model would be much appreciated, and so thank you for your time and assistance in advance.
Sincerely,
Andrew
###############################################################
library(nimble) ; library(coda)
win.data <- readRDS("win.data.rds") # load the input data
modelCode <- nimbleCode({
## Priors for community parameters
for(k in 1:nspec){
phi[k] ~ dunif(0, 1) # adjustment for zero-inflation
## Alpha parameters (detection)
alpha0[k] ~ dnorm(mu.alpha0, sd=sd.alpha0) # species-specific intercepts on detection
alpha1[k] ~ dnorm(mu.alpha1, sd=sd.alpha1)
# linear julian day on detection
alpha2[k] ~ dnorm(mu.alpha2, sd=sd.alpha2)
# quadratic julian day on detection
## Beta parameters (abundance)
for(y in 1:nyear){
beta0[k,y] ~ dnorm(mu.beta0[y], sd=sd.beta0[y])
# species- and year-specific intercepts on abundance
}
beta1[k] ~ dnorm(mu.beta1, sd=sd.beta1)
# cp type (dummy coded, 0/1) on abundance
beta2[k] ~ dnorm(mu.beta2, sd=sd.beta2)
# percent forb cover on abundance
beta3[k] ~ dnorm(mu.beta3, sd=sd.beta3)
# landscape metrics on abundance
}
tau.lam <- pow(sd.lam, -2)
sd.lam ~ dunif(0, 2)
# site heterogeneity in abundance
tau.p <- pow(sd.p, -2)
sd.p ~ dunif(0, 2)
# site heterogeneity in detection
## Priors for community hyperparameters
for(y in 1:nyear){
mu.beta0[y] ~ dnorm(0, 0.01)
sd.beta0[y] ~ dunif(0, 10)
}
# abundance model
mu.beta1 ~ dnorm(0, 0.01)
sd.beta1 ~ dunif(0, 10)
mu.beta2 ~ dnorm(0, 0.01)
sd.beta2 ~ dunif(0, 10)
mu.beta3 ~ dnorm(0, 0.01)
sd.beta3 ~ dunif(0, 10)
# detection model
mu.alpha0 ~ dnorm(mu.a0L, sd.alpha0)
sd.alpha0 ~ dunif(0, 1)
mu.a0 ~ dunif(0, 1)
mu.a0L <- logit(mu.a0)
mu.alpha1 ~ dnorm(0, 0.01)
sd.alpha1 ~ dunif(0, 10)
mu.alpha2 ~ dnorm(0, 0.01)
sd.alpha2 ~ dunif(0, 10)
## Ecological model for true abundance (process model)
for(k in 1:nspec){
for(i in 1:nsite){
eps.lam[i] ~ dnorm(0, tau.lam)
spatial random effects on abundance
a[i,k] ~ dbern(phi[k])
# adjustment for zero-inflation
N[i,k] ~ dpois(a[i,k] * lambda[i,k])
loglam[i,k] <- beta0[k, year[i]] + beta1[k] * CPType23[i] + beta2[k] * PercForb[i] + beta3[k] * OC50_1[i] + eps.lam[i]
loglam.lim[i,k] <- min(250, max(-250, loglam[i,k]))
# stabilize log
lambda[i,k] <- exp(loglam.lim[i,k])
## Compute presence/absence matrix z (for N > 0) from latent abundance
z[i,k] <- step(N[i,k]-1) # returns TRUE if N >= 0
}
}
## Observation model for replicated counts (detection model)
for(k in 1:nspec){
for(i in 1:nsite){
for(j in 1:nrep){
eps.p[i] ~ dnorm(0, tau.p)
# spatial random effects on detection
Yc[i,j,k] ~ dbin(p[i,j,k], N[i,k])
p[i,j,k] <- 1 / (1 + exp(-lp.lim[i,j,k]))
lp.lim[i,j,k] <- min(250, max(-250, mu[i,j,k]))
# stabilize logit
mu[i,j,k] <- alpha0[k] + alpha1[k] * JulianDay[i] + alpha2[k] * pow(JulianDay[i], 2) + eps.p[i]
}
}
}
for(k in 1:nspec){
for(y in 1:nyear){
mlambda[k,y] <- phi[k] * exp(beta0[k,y])
# expected abundance on natural scale
}
Nocc.fs[k] <- sum(z[1:nsite,k])
# number of occupied sites
logit(mp[k]) <- alpha0[k]
# mean detection on natural scale
}
for (i in 1:nsite) {
Nrich[i] <- sum(z[i,1:nspec])
# number of occurring species at each site
Nbirds[i] <- sum(N[i,1:nspec])
# number of total individuals at each site
}
})
Ycd <- win.data$Yc
nyear <- length(c(2005,2006))
nsite <- dim(Ycd)[1]
nrep <- (Ycd)[2]
nspec <- dim(Ycd)[3]
const <- list(year=c(rep(1,14),rep(2,14)), nyear=nyear, nsite=nsite, nrep=nrep, nspec=nspec)
## Prepare initial values
ast <- matrix(rep(1, dim(Ycd)[3] * dim(Ycd)[1]),
nrow = dim(Ycd)[1])
# all species will begin as present
some.more <- 5
Nst <- apply(Ycd, c(1,3), max, na.rm = T) + some.more
# initial abundance > obs abundance
initFunction <- function() list(
# starting values for latent N and its dependents
a = ast,
phi = runif(dim(Ycd)[3],0,1),
N = Nst,
# starting values for species-specific abundance
## starting values for community parameters
# detection process
alpha0 = rnorm(dim(Ycd)[3]),
# detection intercepts
alpha1 = rnorm(dim(Ycd)[3]),
# linear julian day on detection
alpha2 = rnorm(dim(Ycd)[3]),
# quadratic julian day on detection
# abundance process
beta0 = matrix(rnorm(dim(Ycd)[3]*nyear),
nrow = dim(Ycd)[3], ncol = nyear),
# abundance intercepts
beta1 = rnorm(dim(Ycd)[3]),
# cp type (dummy coded) on abundance
beta2 = rnorm(dim(Ycd)[3]),
# percent forb cover on abundance
beta3 = rnorm(dim(Ycd)[3]),
# landscape metrics on abundance
## starting values for community hyperparameters
# detection process
mu.alpha0 = rnorm(1, logit(runif(1,0,1)), runif(1,0,1)),
mu.a0 = runif(1,0,1),
mu.beta0 = rnorm(2),
sd.beta0 = runif(2,0,5),
sd.alpha0 = runif(1,0,1),
mu.alpha1 = rnorm(1),
sd.alpha1 = runif(1,0,5),
mu.alpha2 = rnorm(1),
sd.alpha2 = runif(1,0,5),
# abundance process
mu.beta1 = rnorm(1),
sd.beta1 = runif(1,0,5),
mu.beta2 = rnorm(1),
sd.beta2 = runif(1,0,5),
mu.beta3 = rnorm(1),
sd.beta3 = runif(1,0,5),
# spatial random effects on abundance/detection
sd.lam=runif(1, 0, 2), eps.lam=rnorm(nsite),
sd.p=runif(1, 0, 2), eps.p=rnorm(nsite))
params <- c("phi", "mlambda", "mp",
"beta0", "beta1", "beta2", "beta3",
"alpha0", "alpha1", "alpha2",
"N", "Nrich", "Nbirds")
model <- nimbleModel(code = modelCode, name = "modelCode",
constants = const, data = win.data,
inits = initFunction())
Cmodel <- compileNimble(model)
modelConf <- configureMCMC(model)
modelConf$setMonitors(params)
modelConf$removeSampler(c("alpha0","alpha1","alpha2","beta0","beta1","beta2","beta3","p"))
modelConf$addSampler(target = c("p[1:28,1:3,1:35]"), type = "RW_block")
modelConf$addSampler(target = c("beta0[1:35,1]"), type = "RW_block") #year 1 intercepts
modelConf$addSampler(target = c("beta0[1:35,2]"), type = "RW_block") #year 2 intercepts
modelConf$addSampler(target = c("beta1[1:35]","beta2[1:35]","beta3[1:35]"), type = "RW_block")
modelConf$addSampler(target = c("alpha1[1:35]","alpha2[1:35]"), type = "RW_block")
modelConf$addSampler(target = c("alpha0[1:35]"), type = "RW_block")
modelMCMC <- buildMCMC(modelConf)
CmodelMCMC <- compileNimble(modelMCMC, project = model)
ni <- 10000 ; nb <- 5000 ; nt <- 5 ; nc <- 3 ; set.seed <- 999
results <- runMCMC(CmodelMCMC, niter=ni, nburnin=nb, thin=nt, nchains=nc,
samples=TRUE, summary=TRUE, samplesAsCodaMCMC=TRUE, setSeed = seed)