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 |
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.
With uniform habitat
Let'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 here before reading on.
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 state space: in uniform habitat this is simply a rectangle including the traps, and large enough so that any animals with an activity centre (AC) outside the state space have a negligible probability of being captured.
We assume there are more animals in the state space than we caught, and we want to know how many. For this we use data augmentation: to the capture histories we have, we add a large number of all-zero capture histories. Some of these represent real animals we didn't catch, while some are 'imaginary' animals. If we know how many real animals are in the state space, we can calculate the density. For more on data augmentation, see Royle & Dorazio (2012).
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
We use a half-normal detection function, and this calculation
is done in line 15. Probability of detection depends on the
The x and y coordinates of the animal's
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 habitat
The 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 habitat
In 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 code
We 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
You 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 OpenBUGS
Line 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
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 (not the size of the home range), so we are more certain about the locations for the black and green animals than for the red animal and especially the blue guy, which was only caught in the trap on the corner of the array.
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.
Royle, J A; R M Dorazio. 2012. Parameter-expanded data augmentation for Bayesian analysis of capture–recapture models. Journal of Ornithology 152:521-537.
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. Camera traps in animal ecology: methods and analyses. Springer, New York.
| Updated 5Nov 2013 by Mike Meredith|