This post is intended to provide a simple example of how to construct
and make inferences on a multi-species multi-year occupancy model using
R, JAGS, and the ‘rjags’ package. This is not intended to be a
standalone tutorial on dynamic community occupancy modeling. Useful
primary literature references include MacKenzie et al. (2002), Kery and
Royle (2007), Royle and Kery (2007), Russell et al. (2009), and Dorazio
et al. (2010). Royle and Dorazio’s Heirarchichal Modeling and
Inference in Ecology also provides a clear explanation of simple one
species occupancy models, multispecies occupancy models, and dynamic
(multiyear) occupancy models, among other things. There’s also a wealth
of code provided
here by
Elise Zipkin, J. Andrew Royle, and others.
Before getting started, we can define two convenience functions:
Then, initializing the number of sites, species, years, and repeat surveys (i.e. surveys within years, where the occupancy status of a site
is assumed to be constant),
1234
nsite <-150nspec <-6nyear <-4nrep <-3
we can begin to consider occupancy. We’re interested in making
inferences about the rates of colonization and population persistence
for each species in a community, while estimating and accounting for
imperfect detection.
Occupancy status at site , by species , in year is
represented by . For occupied sites ; for
unoccupied sites . However, is incompletely observed: it
is possible that a species is present at a site in some year
() but species was never
seen at at site in year across all repeat
surveys because of imperfect detection. These observations
are represented by . Here we assume that there are no
“false positive” observations. In other words, if
, then . If a site is
occupied, the probability that is represented as
a Bernoulli trial with probability of
detection , such that
The occupancy status of species at site in year
is modeled as a Markov Bernoulli trial. In other words whether a species
is present at a site in year is influenced by whether it was
present at year .
where for
and in year one
where the occupancy status in year 0,
, and
. and are
parameters that control the probabilities of colonization and
persistence. If a site was unoccupied by species in a previous
year , then the probability of colonization is
given by the antilogit of . If a site was previously
occupied , the probability of population
persistence is given by the anitlogit of . We
assume that the distributions of species specific parameters are
defined by community level hyperparameters such that
and
. We can generate
occupancy data as follows:
# community level hyperparametersp_beta =0.7mubeta <- logit(p_beta)sdbeta <-2p_rho <-0.8murho <- logit(p_rho)sdrho <-1# species specific random effectsset.seed(1)# for reproducibilitybeta <- rnorm(nspec, mubeta, sdbeta)set.seed(1008)rho <- rnorm(nspec, murho, sdrho)# initial occupancy statesset.seed(237)rho0 <- runif(nspec,0,1)z0 <- array(dim = c(nsite, nspec))for(i in1:nspec){ z0[, i]<- rbinom(nsite,1, rho0[i])}# subsequent occupancyz <- array(dim = c(nsite, nspec, nyear))lpsi <- array(dim = c(nsite, nspec, nyear))psi <- array(dim = c(nsite, nspec, nyear))for(j in1:nsite){for(i in1:nspec){for(t in1:nyear){if(t ==1){ lpsi[j, i, t]<- beta[i]+ rho[i]* z0[j, i] psi[j, i, t]<- antilogit(lpsi[j, i, t]) z[j, i, t]<- rbinom(1,1, psi[j, i, t])}else{ lpsi[j, i, t]<- beta[i]+ rho[i]* z[j, i, t -1] psi[j, i, t]<- antilogit(lpsi[j, i, t]) z[j, i, t]<- rbinom(1,1, psi[j, i, t])}}}}
For simplicity, we’ll assume that there are no differences in species detectability among sites, years, or repeat surveys, but that detectability varies among species. We’ll again use hyperparameters to specify a distribution of detection probabilities in our community, such that $logit(p_i) \sim Normal(\mu_p, \sigma_p^2)$.
We can now generate our observations based on occupancy states and
detection probabilities. Although this could be vectorized for speed,
let’s stick with nested for loops in the interest of clarity.
12345678910
x <- array(dim = c(nsite, nspec, nyear, nrep))for(j in1:nsite){for(i in1:nspec){for(t in1:nyear){for(k in1:nrep){ x[j, i, t, k]<- rbinom(1,1, p[i]* z[j, i, t])}}}}
Now that we’ve collected some data, we can specify our model:
As a side note, it is helpful in JAGS to provide initial values for the incompletely observed occupancy state $z$ that are consistent with observed presences, as provided in this example with zinit. In other words if $x(j, i, t, k)=1$, provide an intial value of $1$ for $z(j, i, t)$. Unlike WinBUGS and OpenBUGS, if you do not do this, you’ll often (but not always) encounter an error message such as:
123
# Error in jags.model(file = 'com_occ.txt', data = data, n.chains = 3) :# Error in node x[1,1,2,3] Observed node inconsistent with unobserved# parents at initialization
Now we’re ready to monitor and make inferences about some parameters of interest using JAGS.
At this point, you’ll want to run through the usual MCMC diagnostics to
check for convergence and adjust the burn-in or number of iterations
accordingly. Once satisfied, we can check to see how well our model
performed based on our known parameter values.