## SECR in BUGS/JAGS with patchy habitat |
|||

Back to home page |
Analysis of spatially explicit capture-recapture (SECR) data can
be done in a maximum likelihood (ML) or a Bayesian framework. Program
DENSITY and the `secr` package take care of the former. Bayesian
analysis with the usual workhorses, WinBUGS, OpenBUGS and JAGS,
is straightforward if the traps are laid out in a large area of
homogenous habitat.Faced with patches of suitable habitat surrounded by inhospitable terrain, or a large extent of habitat punctuated with patches of non-habitat, we had the choice of ML methods or one of the packages designed specifically for Bayesian SECR analysis, such as SPACECAP or SCRbayes. But then we are limited to the range of models provided by package authors: we don't have the flexibility to specify our own models that comes with WinBUGS, OpenBUGS or JAGS. Here I present a way to incorporate patchy habitat into a BUGS/JAGS model specification. ## With uniform habitatLet's review how a JAGS analysis would work with a large area
of uniformly good habitat. (The analysis in WinBUGS or OpenBUGS
would be very similar, but the code may need to be modified
slightly.) If you aren't familiar with the principles of SECR,
take a look The data consist of capture histories for a number of animals, recording when and where each was captured. Our example will use camera-trap data, so an animal could be caught more than once in a single occasion (eg, one night), and the captures could be in different locations, so we will use a Poisson encounter model (Royle & Gardner 2011). We also know the locations of all the traps, including those that caught nothing. We designate a We assume there are more animals in the state space than we
caught, and we want to know how many. For this we use A typical JAGS model specification for the analysis is shown below (adapted from WinBUGS code in Royle & Gardner, 2011 p181).
Let's start with the data, and see how the model says they were generated. (Skip ahead to the next section if you are familiar with this kind of model.) In line 17, the data are The We use a half-normal detection function, and this calculation
is done in line 15. Probability of detection depends on the
parameter The x and y coordinates of the animal's
AC are Lines 9 and 10 encode the priors for the x and y
coordinates of the AC. The variables In lines 2 to 5 we provide minimally-informative priors for
the remaining parameters. In lines 20 and 21 we calculate the
total number of real animals (ie, those with ## With patchy habitatThe model above allows ACs to be located anywhere in the entire state space. If part of the space is unsuitable as habitat for the target species, or if the area of interest is a patch of suitable habitat in a larger area of inhospitable country (see diagram right), this will not work. We need to provide information on which parts of the state space are suitable habitat, and insert two lines of code to ensure that ACs are only located in suitable habitat. ## Specifying suitable habitatIn the diagram right we have an isolated patch of habitat delineated by the black line. This is overlaid with a grid of squares (pixels), in this case 50 pixels east-west and 46 north-south. We create a matrix with 50 rows and 46 columns, where cell
[x, y] = 1 if the corresponding pixel is good habitat, 0
otherwise. This matrix is To streamline the JAGS estimation, we work in units of pixels instead of metres (or kilometres or whatever were the original units), with the SW corner of the state space having coordinates (0, 0). In particular, we convert the trap coordinates to the new units, so that x coordinates are between 0 and 50, and y coordinates between 0 and 46. For example, if the original trap location was (10750, 20750) metres, this is converted to (8.287463, 15.99673) pixels. ## Modifying the JAGS codeWe allow JAGS to draw AC locations from the uniform
distribution as before: lines 9 and 10 in the code block above
don't change, except that now
In line 11, we see which pixel the proposed AC location falls into, and get the
habitat value, 1 or 0, from the habitat matrix described in the previous
section. To get the indices into the matrix, we round up the x and y coordinates
of the AC; JAGS has no round-up function, so we add one and round down with Now we indulge in a bit of trickery: it's referred to as the "ones trick" in the WinBUGS documentation.
We treat the value pulled out of the habitat matrix as the "probability" that
the AC is in good habitat (I called it
In line 12, we compare them: if AC ## Starting valuesYou will need to provide initial values for the AC locations. If you leave this to JAGS, it will just draw random values from the priors in lines 9 and 10, many ACs will be in impossible locations, and it will throw an error. A clever way to do this would be to use the average of the
capture locations for the animals actually captured, and give
random locations in the good-habitat area for the rest. The
A lazy but effective way is just to choose a location somewhere in the good habitat and use this as the starting value for all the ACs. ## Using WinBUGS or OpenBUGSLine 11 in the code above will throw an error at the
syntax-checking stage in WinBUGS or
OpenBUGS: "logical function not allowed in integer expression".
They don't like having
I
tried this code with WinBUGS via the ## Does it work?
UPDATED:
The plot below left shows the simulated ACs for the four animals
which were captured. The spread of the points reflects
uncertainty about the location of the AC ( The right hand plot shows simulated locations for real animals that were not caught. These probably aren't inside the array of traps, but could be anywhere else in the habitat patch. (The plot I first posted included phantom animals too, and they can't be caught even if they have ACs inside the trap array!) I have now run thousands of simulations with known true densities to investigate the effectiveness of different study designs to detect trends in density, and code similar to that shown here gives good results. ## Example codeClick here to download a ZIP file with example R code. ## ReferencesRoyle, J A; R M Dorazio. 2012. Parameter-expanded data
augmentation for Bayesian analysis of capture–recapture models.
Royle, J A; B Gardner. 2011. Hierarchical spatial
capture–recapture models for estimating density from trapping
arrays. 163–190 in O'Connell, A F, J D Nichols, and K U Karanth,
editors. | ||

Updated 5Nov 2013 by Mike Meredith |