sim.spatialDS {AHMbook}R Documentation

Simulates data for a basic spatial distance sampling model


Generates data with the following steps:

1. Simulate a spatially correlated habitat covariate (x) over a grid of pixels covering a square.

2. Distribute the population of N individuals over the square with probability of location in a pixel related to the covariate.

3. Decide which individuals are detected using a distance sampling model with an observer at the center of the square, with either a half normal or a logit detection function. (Note that all the individuals in the square can be detected.)

4. If keep.all = FALSE, return the locations of only the individuals detected.

To recreate the data sets used in the book with R 3.6.0 or later, include sample.kind="Rounding" in the call to set.seed. This should only be used for reproduction of old results.


sim.spatialDS(N = 1000, beta = 1, sigma = 1, keep.all = FALSE, B = 3,
  model=c("logit", "halfnorm"), lambda = B/3, useHabitat, show.plot=TRUE)



total population size in the square


coefficient for the effect of spatial covariate x on the distribution of individuals


scale parameter of detection function


if TRUE, the data for all individuals are returned; if FALSE, only for individuals detected.


distance from the observer to the side of the square. This is usually set so that the probability of detection of individuals outside the square is negligable, eg, B = 3*sigma.


The detection function used, can be "logit" or "halfnorm": see Details.


The scale parameter for the spatially autocorrelated Habitat covariate.


If the output from a previous simulation is provided, the same Habitat covariate will be used (and lambda will be ignored).


choose whether to show plots or not. Set to FALSE when using function in simulations.


The "logit" detection function is 2*plogis(-d^2/(2*sigma^2)), which corresponds to the detection model implemented in unmarked::pcount.spHDS.


A list with the values of the input arguments and the following additional elements:


x coordinate of each animal


y coordinate of each animal


distance of each animal from the center of the circle

the pixel in which each animal is located, the row number in grid


indicator of detection of each animal, a vector of length N


the number of animals inside the circle of radius B


Value of the spatially correlated habitat covariate, a 900 x 1 matrix


Coordinates of the center of each pixel, a dataframe with 900 rows and 2 columns

If keep.all = FALSE (the default), only the animals detected are included in u1, u2, d,


Kéry & Royle (2016, p.535 and discussion p.540) and earlier versions of AHMbook included a hazard rate detection function. This is problematic because the detection probability at distance zero is less than 1 (p(0) < 1) and should not be used. It is replaced here with the logit detection function, which does have p(0) = 1.


Marc Kéry & Andy Royle


Kéry, M. & Royle, J.A. (2016) Applied Hierarchical Modeling in Ecology AHM2 - 9.8.3.


# Generate data with the default arguments and look at the structure:
tmp <- sim.spatialDS()

# Generate date with model = "logit" and analyse the data with unmarked::pcount.spatialHDS
# RNGkind(sample.kind = "Rounding") # run this for R >= 3.6.0
tmp <- sim.spatialDS(model="logit")
# Plot shows a large area of good habitat west of the observer with many animals detected
str(tmp)  # 272 animals detected out of 850 inside the circle (N.real)

# Fit some models with unmarked
if(require(unmarked)) {
  # Get the count of animals detected in each pixel
  pixel.count <- tabulate(tmp$, nbins=nrow(tmp$grid))
  # Centre the Habitat covariate
  Habitat <- tmp$Habitat - mean(tmp$Habitat)
  # Create a detection covariate: distance between observer and pixel center
  dist <- with(tmp, sqrt((grid[,1]-B)^2 + (grid[,2]-B)^2))
  # Construct an unmarkedFrame
  umf <- unmarkedFramePCount(y=cbind(pixel.count),
     siteCovs=data.frame(dist=dist, Habitat=Habitat))

  (fm0 <- pcount.spHDS(~ -1 + I(dist^2) ~ 1, umf, K=20))
  (fm1 <- pcount.spHDS(~ -1 + I(dist^2) ~ Habitat, umf, K=20))
  # The model with Habitat has much lower AIC
  # Get an estimate of the total population in the square (true is N = 1000)
  sum(predict(fm1, type='state')[, 1])

[Package AHMbook version 0.2.3 Index]