@article{kery2008estimating,
title={Estimating abundance from bird counts: binomial mixture models uncover complex covariate relationships},
author={K{\'e}ry, M.},
journal={The Auk},
volume={125},
number={2},
pages={336--345},
year={2008},
publisher={BioOne}
}
link to published article:
# Define function for generating binomial-mix model data
data.fn <- function(R = 200, T = 3, xmin = -1, xmax = 1, alpha0 = 1, alpha1 = 3, beta0 = 0, beta1 = -5){
# R: number of sites at which counts were made (= number of spatial reps)
# T: number of times that counts were made at each site
# (= number of temporal reps)
# xmin, xmax: define range of the covariate X
# alpha0 and alpha1: intercept and slope of log-linear regression
# relating abundance to the site covariate A
# beta0 and beta1: intercept and slope of logistic-linear regression
# of detection probability on A
y <- array(dim = c(R, T)) # Array for counts
# Ecological process
# Covariate values: sort for ease of presentation
X <- sort(runif(n = R, min = xmin, max = xmax))
# Relationship expected abundance – covariate
lam <- exp(alpha0 + alpha1 * X)
# Add Poisson noise: draw N from Poisson(lambda)
N <- rpois(n = R, lambda = lam)
table(N) # Distribution of abundances across sites
sum(N > 0) / R # Empirical occupancy
totalN <- sum(N) ; totalN
# Observation process
# Relationship detection prob – covariate
p <- plogis(beta0 + beta1 * X)
# Make a ‘census’ (i.e., go out and count things)
for (i in 1:T){
y[,i] <- rbinom(n = R, size = N, prob = p)
}
# Naïve regression
naive.pred <- exp(predict(glm(apply(y, 1, max) ~ X + I(X^2), family = poisson)))
# Plot features of the simulated system
par(mfrow = c(2, 2))
plot(X, lam, main = "Expected abundance", xlab = "Covariate", ylab = "lambda", las = 1, type = "l", col = "red", lwd = 3, frame.plot = FALSE)
plot(X, N, main = "Realised abundance", xlab = "Covariate", ylab = "N", las = 1, frame.plot = FALSE, col = "red", cex = 1.2)
plot(X, p, ylim = c(0, 1), main = "Detection probability", xlab = "Covariate", ylab = "p", type = "l", col = "red", lwd = 3, las = 1, frame.plot = FALSE)
plot(X, naive.pred, main = "Actual counts \n and naïve regression", xlab = "Covariate", ylab = "Relative abundance", ylim = c(min(y), max(y)), type = "l", lty = 2, lwd = 4, col = "blue", las = 1, frame.plot = FALSE)
points(rep(X, T), y, col = "black", cex = 1.2)
# Return stuff
return(list(R = R, T = T, X = X, alpha0 = alpha0, alpha1 = alpha1, beta0 = beta0, beta1 = beta1, lam = lam, N = N, totalN = totalN, p = p, y = y))
}
To execute, do this:
data <- data.fn()