# Dynamic community occupancy modeling with R and JAGS

**Ecology in silico**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

1 2 3 4 5 6 7 | logit <- function(x) { log(x/(1 - x)) } antilogit <- function(x) { exp(x)/(1 + exp(x)) } |

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),

1 2 3 4 | nsite <- 150 nspec <- 6 nyear <- 4 nrep <- 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 $j$, by species $i$, in year $t$ is represented by $z(j,i,t)$. For occupied sites $z=1$; for unoccupied sites $z=0$. However, $Z$ is incompletely observed: it is possible that a species $i$ is present at a site $j$ in some year $t$ ($z(j,i,t)=1$) but species $i$ was never seen at at site $j$ in year $t$ across all $k$ repeat surveys because of imperfect detection. These observations are represented by $x(j,i,t,k)$. Here we assume that there are no “false positive” observations. In other words, if $\sum_{1}^{k}x(j,i,t,k)>0$ , then $z(j,i,t)=1$. If a site is occupied, the probability that $x(j,i,t,k)=1$ is represented as a Bernoulli trial with probability of detection $p(j,i,t,k)$, such that

The occupancy status $z$ of species $i$ at site $j$ in year $t$ is modeled as a Markov Bernoulli trial. In other words whether a species is present at a site in year $t$ is influenced by whether it was present at year $t−1$. $$ z(j,i,t) \sim Bernoulli(ψj,i,t) $$

where for $t>1$

and in year one $(t=1)$

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # community level hyperparameters p_beta = 0.7 mubeta <- logit(p_beta) sdbeta <- 2 p_rho <- 0.8 murho <- logit(p_rho) sdrho <- 1 # species specific random effects set.seed(1) # for reproducibility beta <- rnorm(nspec, mubeta, sdbeta) set.seed(1008) rho <- rnorm(nspec, murho, sdrho) # initial occupancy states set.seed(237) rho0 <- runif(nspec, 0, 1) z0 <- array(dim = c(nsite, nspec)) for (i in 1:nspec) { z0[, i] <- rbinom(nsite, 1, rho0[i]) } # subsequent occupancy z <- array(dim = c(nsite, nspec, nyear)) lpsi <- array(dim = c(nsite, nspec, nyear)) psi <- array(dim = c(nsite, nspec, nyear)) for (j in 1:nsite) { for (i in 1:nspec) { for (t in 1: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 .

1 2 3 4 5 6 | p_p <- 0.7 mup <- logit(p_p) sdp <- 1.5 set.seed(222) lp <- rnorm(nspec, mup, sdp) p <- antilogit(lp) |

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.

1 2 3 4 5 6 7 8 9 10 | x <- array(dim = c(nsite, nspec, nyear, nrep)) for (j in 1:nsite) { for (i in 1:nspec) { for (t in 1:nyear) { for (k in 1: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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | cat(" model{ #### priors # beta hyperparameters p_beta ~ dbeta(1, 1) mubeta <- log(p_beta / (1 - p_beta)) sigmabeta ~ dunif(0, 10) taubeta <- (1 / (sigmabeta * sigmabeta)) # rho hyperparameters p_rho ~ dbeta(1, 1) murho <- log(p_rho / (1 - p_rho)) sigmarho~dunif(0,10) taurho<-1/(sigmarho*sigmarho) # p hyperparameters p_p ~ dbeta(1, 1) mup <- log(p_p / (1 - p_p)) sigmap ~ dunif(0,10) taup <- (1 / (sigmap * sigmap)) #### occupancy model # species specific random effects for (i in 1:(nspec)) { rho0[i] ~ dbeta(1, 1) beta[i] ~ dnorm(mubeta, taubeta) rho[i] ~ dnorm(murho, taurho) } # occupancy states for (j in 1:nsite) { for (i in 1:nspec) { z0[j, i] ~ dbern(rho0[i]) logit(psi[j, i, 1]) <- beta[i] + rho[i] * z0[j, i] z[j, i, 1] ~ dbern(psi[j, i, 1]) for (t in 2:nyear) { logit(psi[j, i, t]) <- beta[i] + rho[i] * z[j, i, t-1] z[j, i, t] ~ dbern(psi[j, i, t]) } } } #### detection model for(i in 1:nspec){ lp[i] ~ dnorm(mup, taup) p[i] <- (exp(lp[i])) / (1 + exp(lp[i])) } #### observation model for (j in 1:nsite){ for (i in 1:nspec){ for (t in 1:nyear){ mu[j, i, t] <- z[j, i, t] * p[i] for (k in 1:nrep){ x[j, i, t, k] ~ dbern(mu[j, i, t]) } } } } } ", fill=TRUE, file="com_occ.txt") |

Next, bundle up the data.

1 | data <- list(x = x, nrep = nrep, nsite = nsite, nspec = nspec, nyear = nyear) |

Provide initial values.

1 2 3 4 5 6 7 8 9 10 11 12 13 | zinit <- array(dim = c(nsite, nspec, nyear)) for (j in 1:nsite) { for (i in 1:nspec) { for (t in 1:nyear) { zinit[j, i, t] <- max(x[j, i, t, ]) } } } inits <- function() { list(p_beta = runif(1, 0, 1), p_rho = runif(1, 0, 1), sigmarho = runif(1, 0, 1), sigmap = runif(1, 0, 10), sigmabeta = runif(1, 0, 10), z = zinit) } |

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:

1 2 3 | # 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.

1 2 3 4 5 6 7 8 | params <- c("lp", "beta", "rho") require(rjags) ocmod <- jags.model(file = "com_occ.txt", inits = inits, data = data, n.chains = 3) nburn <- 2000 update(ocmod, n.iter = nburn) out <- coda.samples(ocmod, n.iter = 17000, variable.names = params) summary(out) plot(out) |

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.

1 2 3 | require(mcmcplots) caterplot(out, "beta", style = "plain") caterpoints(beta) |

1 2 | caterplot(out, "lp", style = "plain") caterpoints(lp) |

1 2 | caterplot(out, "rho", style = "plain") caterpoints(rho) |

**leave a comment**for the author, please follow the link and comment on their blog:

**Ecology in silico**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.