Introduction to the bSims package

Peter Solymos


The bSims R package is a highly scientific and utterly addictive bird point count simulator. Highly scientific, because it implements a spatially explicit mechanistic simulation that is based on statistical models widely used in bird point count analysis (i.e. removal models, distance sampling), and utterly addictive because the implementation is designed to allow rapid interactive exploration (via shiny apps) and efficient simulation (supporting various parallel backends), thus elevating the user experience.

The goals of the package are to:

  1. allow easy testing of statistical assumptions and explore effects of violating these assumptions,
  2. aid survey design by comparing different options,
  3. and most importantly, to have fun while doing it via an intuitive and interactive user interface.

The simulation interface was designed with the following principles in mind:

  1. isolation: the spatial scale is small (local point count scale) so that we can treat individual landscapes as more or less homogeneous units (but see below how certain stratified designs and edge effects can be incorporated) and independent independent in space and time;
  2. realism: the implementation of biological mechanisms and observation processes are realistic, defaults are chosen to reflect common practice and assumptions;
  3. efficiency: implementation is computationally efficient utilizing parallel computing backends when available;
  4. extensibility: the package functionality is well documented and easily extensible.

This documents outlines the major functionality of the package. First we describe the motivation for the simulation and the details of the layers. Then we outline an interactive workflow to design simulation studies and describe how to run efficient simulation experiments. Finally we present some of the current limitations of the framework and how to extend the existing functionality of the package to incorporate more of the biological realism into the simulations.

Simulation layers

Introductory stats books begin with the coin flip to introduce the binomial distribution. In R we can easily simulate an outcome from such a random variable \(Y \sim Binomial(1, p)\) doing something like this:

p <- 0.5

Y <- rbinom(1, size = 1, prob = p)

But a coin flip in reality is a lot more complicated: we might consider the initial force, the height of the toss, the spin, and the weight of the coin.

Bird behavior combined with the observation process presents a more complicated system, that is often treated as a mixture of a count distribution and a detection/nondetection process, e.g.:

D <- 2 # individuals / unit area
A <- 1 # area
p <- 0.8 # probability of availability given presence
q <- 0.5 # probability of detection given availability

N <- rpois(1, lambda = A * D)
Y <- rbinom(1, size = N, prob = p * q)

This looks not too complicated, corresponding to the true abundance being a random variables \(N \sim Poisson(DA)\), while the observed count being \(Y \sim Binomial(N, pq)\). This is the exact simulation that we need when we want to make sure that an estimator is capable of estimating the model parameters (lambda and prob here). But such probabilistic simulations are not very useful when we are interested how well the model captures important aspects of reality.

Going back to the Poisson–Binomial example, N would be a result of all the factors influencing bird abundance, such as geographical location, season, habitat suitability, number of conspecifics, competitors, or predators. Y however would largely depend on how the birds behave depending on timing, or how an observer might detect or miss the different individuals, or count the same individual twice, etc.

Therefore the package has layers, that by default are conditionally independent of each other. This design decision is meant to facilitate the comparison of certain settings while keeping all the underlying realizations identical, thus helping to pinpoint effects without the extra variability introduced by all the other effects.

The conditionally independent layers of a bSims realization are the following, with the corresponding function:

  1. landscape (bsims_init),
  2. population (bsims_populate),
  3. behavior with movement and vocalization events (bsims_animate),
  4. the physical side of the observation process (bsims_detect), and
  5. the human aspect of the observation process (bsims_transcribe).

See this example as a sneak peek that we’ll explain in the following subsections:

#> Loading required package: intrval
#> Loading required package: mefa4
#> Loading required package: Matrix
#> mefa4 0.3-6   2019-06-20
#> Loading required package: MASS
#> Loading required package: deldir
#> deldir 0.1-23
#> bSims 0.2-1   2019-12-16      woonk-a-chunk

phi <- 0.5                 # singing rate
tau <- 1:3                 # detection distances by strata
tbr <- c(3, 5, 10)         # time intervals
rbr <- c(0.5, 1, 1.5, Inf) # count radii

l <- bsims_init(10, 0.5, 1)# landscape
p <- bsims_populate(l, 1)  # population
e <- bsims_animate(p,      # events
d <- bsims_detect(e,       # detections
x <- bsims_transcribe(d,   # transcription
  tint=tbr, rint=rbr)

get_table(x) # removal table
#>          0-3min 3-5min 5-10min
#> 0-50m         1      0       1
#> 50-100m       3      0       0
#> 100-150m      1      0       0
#> 150+m         8      2       0


The bsims_ini function sets up the geometry of a local landscape. The extent of the landscape determines the edge lengths of a square shaped area. With no argument values passed, the function assumes a homogeneous habitat (H) in a 10 units x 10 units landscape, 1 unit is 100 meters. Having units this way allows easier conversion to ha as area unit that is often used in the North American bird literature. As a result, our landscape has an area of 1 km\(^2\).

The road argument defines the half-width of the road that is placed in a vertical position. The edge argument defines the width of the edge stratum on both sides of the road. Habitat (H), edge (E), and road (R) defines the 3 strata that we refer to by their initials (H for no stratification, HER for all 3 strata present).

The origin of the Cartesian coordinate system inside the landscape is centered at the middle of the square. The offset argument allows the road and edge strata to be shifted to the left (negative values) or to the right (positive values) of the horizontal axis. This makes it possible to create landscapes with only two strata. The bsims_init function returns a landscape object (with class ‘bsims_landscape’).


The bsims_populate function populates the landscape we created by the bsims_init function, which is the first argument we have to pass to bsims_populate. The function returns a population object (with class ‘bsims_population’). The most important argument that controls how many individuals will inhabit our landscape is density that defines the expected value of individuals per unit area (1 ha). By default, density = 1 (\(D=1\)) and we have 100 ha in the landscape (\(A=100\)) which translates into 100 individuals on average (\(E[N]=\lambda=AD\)). The actual number of individuals in the landscape might deviate from this expectation, because \(N\) is a random variable (\(N \sim f(\lambda)\)). The abund_fun argument controls this relationship between the expected (\(\lambda\)) and realized abundance (\(N\)). The default is a Poisson distribution:

Changing abund_fun can be useful to make abundance constant or allow under or overdispersion, e.g.:

Once we determine how many individuals will populate the landscape, we have control over the spatial arrangement of the nest location for each individual. The default is a homogeneous Poisson point process (complete spatial randomness). Deviations from this can be controlled by the xy_fun. This function takes distance as its only argument and returns a numeric value between 0 and 1. A function function(d) reurn(1) would be equivalent with the Poisson process, meaning that every new random location is accepted with probability 1 irrespective of the distance between the new location and the previously generated point locations in the landscape. When this function varies with distance, it leads to a non-homogeneous point process via this accept-reject algorithm. The other arguments (margin, maxit, fail) are passed to the underlying accepreject function to remove edge effects and handle high rejection rates.

In the next example, we fix the abundance to be constant (i.e. not a random variable, \(N=\lambda\)) and different spatial point processes:

The get_nests function extracts the nest locations. get_abundance and get_density gives the total abundance (\(N\)) and density (\(D=N/A\), where \(A\) is extent^2) in the landscape, respectively.

If the landscape is stratified, that has no effect on density unless we specify different values through the density argument as a vector of length 3 referring to the HER strata:


The bsims_animate function animates the population created by the bsims_populate function. bsims_animate returns an events object (with class ‘bsims_events’). The most important arguments are governing the duration of the simulation in minutes, the vocalization (vocal_rate), and the movement (move_rate) rates as average number of events per minute.

We can describe these behavioral events using survival modeling terminology. Event time (\(T\)) is a continuous random variable. In the simplest case, its probability density function is the Exponential distribution: \(f(t)=\phi e^{-t\phi}\). The corresponding cumulative distribution function is: \(F(t)=\int_{0}^{t} f(t)dt=1-e^{-t\phi}=p_t\), giving the probability that the event has occurred by duration \(t\). The parameter \(\phi\) is the rate of the Exponential distribution with mean \(1/\phi\) and variance \(1/\phi^2\).

In survival models, the complement of \(F(t)\) is called the survival function (\(S(t)=1-F(t)\), \(S(0)=1\)), which gives the probability that the event has not occurred by duration \(t\). The hazard function (\(\lambda(t)=f(t)/S(t)\)) defines the instantaneous rate of occurrence of the event (risk, the density of events at \(t\) divided by the probability of surviving). The cumulative hazard (cumulative risk) is the sum of the risks between duration 0 and \(t\) (\(\Lambda(t)=\int_{0}^{t} \lambda(t)dt\)).

The simplest survival distribution assumes constant risk over time (\(\lambda(t)=\phi\)), which corresponds to the Exponential distribution. The Exponential distribution also happens to describe the lengths of the inter-event times in a homogeneous Poisson process (events are independent, it is a ‘memory-less’ process).

bsims_animate uses independent Exponential distributions with rates vocal_rate and move_rate to simulate vocalization and movement events, respectively. The get_events function extracts the events as a data frame with columns describing the location (x, y) and time (t) of the events (v is 1 for vocalizations and 0 otherwise) for each individual (i gives the individual identifier that links individuals to the nest locations)

There are no movement related events when move_rate = 0, the individuals are always located at the nest, i.e. there is no within territory movement. If we increase the movement rate, we also have to increase the value of movement, that is the standard deviation of bivariate Normal kernels centered around each nest location. This kernel is used to simulate new locations for the movement events.

Individuals in the landscape might have different vocalization rates depending on, e.g., breeding status. Such heterogeneity can be added to the simulations as a finite mixture: vocal_rate and move_rate can be supplied as a vector, each element giving the rate for the groups. The mixture argument is then used to specify the mixture proportions.

Vocal and movement rates (and corresponding kernel standard deviations) can be defined four different ways:

Strata based groups are tracked by column s, behavior based groups are tracked as the column g in the output of get_nests.

Here is how different territory sizes can be achieved in a two-habitat landscape:

Stratum related behavior groups depend on the nest location. Sometimes it makes sense to restrict movement even further, i.e. individuals do not land in certain strata (but can cross a stratum if movement is large enough). For example we can restrict movement into the road stratum (this requires density to be 0 in that stratum):