These simulations go with the slides at thevazquezlab.com/talks. Every chunk runs in base R; copy and paste the code to try other values.
Our clinic example: \(n = 4\) patients, and each one tests positive with probability \(p = 0.25\).
n <- 4
p <- 0.25
dbinom(0:n, size = n, prob = p) # P(X = k) for k = 0, ..., 4
#> [1] 0.31640625 0.42187500 0.21093750 0.04687500 0.00390625
choose(4, 1) # number of ways to get exactly one positive
#> [1] 4
pbinom(1, size = n, prob = p) # P(X <= 1)
#> [1] 0.7382812
c(mean = n * p, variance = n * p * (1 - p))
#> mean variance
#> 1.00 0.75
barplot(dbinom(0:n, n, p), names.arg = 0:n, col = accent, border = NA,
xlab = "Number of positive tests, k", ylab = "P(X = k)",
main = "Binomial(n = 4, p = 0.25)")
My jar holds \(N = 126\) objects. After the first toss I swapped in \(M = 21\) blue cubes. On the second toss \(n = 21\) objects land inside the circle, and each one is a cube with probability \(p = M/N\). The number of cubes in the circle, \(X\), is binomial, and the Lincoln–Petersen estimator is \(\hat N = Mn/k\).
set.seed(2026)
N <- 126 # true total (unknown in practice)
M <- 21 # cubes swapped in
n <- 21 # objects in the circle on the second toss
k <- rbinom(1, size = n, prob = M / N) # cubes in the circle
k
#> [1] 4
M * n / k # one estimate of N
#> [1] 110.25
Now repeat the experiment 1,000 times.
R <- 1000
k <- rbinom(R, size = n, prob = M / N)
N_hat <- ifelse(k > 0, M * n / k, NA)
summary(N_hat)
#> Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
#> 44.1 88.2 147.0 164.3 220.5 441.0 21
quantile(N_hat, c(0.05, 0.95), na.rm = TRUE) # middle 90% of the estimates
#> 5% 95%
#> 73.5 441.0
mean(k == 0) # share of jars with no cubes in the circle
#> [1] 0.021
hist(N_hat, breaks = seq(0, 1500, by = 10), xlim = c(40, 470),
col = accent, border = "white", main = "1,000 simulated jars",
xlab = expression(hat(N) == Mn/k), ylab = "Number of jars")
abline(v = N, col = hl, lwd = 3, lty = 2)
text(N + 10, par("usr")[4] * 0.9, "true N = 126", col = hl, adj = 0)
The estimates pile up near 126 but have a long right tail: tosses with few cubes in the circle give very large estimates. Because \(k\) is a whole number, \(\hat N = 441/k\) takes only a few values, and a toss with \(k = 0\) gives no estimate at all.
I ran the experiment 28 times with the real jar (\(N = 126\), \(M = 21\)). For each toss I counted the pebbles and the cubes inside the circle.
pebbles <- c(20, 6, 18, 16, 15, 12, 15, 18, 11, 21, 11, 16, 13, 17,
13, 23, 16, 12, 15, 14, 15, 16, 16, 24, 17, 18, 8, 17)
cubes <- c(2, 3, 3, 4, 1, 2, 3, 3, 2, 3, 1, 0, 2, 3,
3, 2, 3, 4, 3, 1, 1, 3, 3, 4, 3, 2, 1, 3)
n_home <- pebbles + cubes # all objects in the circle
N_home <- ifelse(cubes > 0, 21 * n_home / cubes, NA)
median(N_home, na.rm = TRUE) # median estimate
#> [1] 147
21 * sum(n_home) / sum(cubes) # pooled estimate
#> [1] 154.7206
sum(cubes) / sum(n_home) # share of cubes in the circle (jar: 21/126 = 0.167)
#> [1] 0.1357285
The real estimates run a little high: cubes were 14% of the objects in the circle but 17% of the jar, so cubes were somewhat less likely than pebbles to land in the circle.
sim_jar <- function(n, R = 1000) {
k <- rbinom(R, size = n, prob = M / N)
ifelse(k > 0, M * n / k, NA)
}
est_21 <- N_hat # the 1,000 jars from section 2
est_60 <- sim_jar(60)
rbind("n = 21" = quantile(est_21, c(0.05, 0.95), na.rm = TRUE),
"n = 60" = quantile(est_60, c(0.05, 0.95), na.rm = TRUE))
#> 5% 95%
#> n = 21 73.5 441
#> n = 60 84.0 210
c("n = 21" = sum(is.na(est_21)), "n = 60" = sum(is.na(est_60))) # jars with no cubes in the circle
#> n = 21 n = 60
#> 21 0
par(mfrow = c(1, 2))
for (v in list(list(est_21, "Second toss n = 21"), list(est_60, "Second toss n = 60"))) {
hist(v[[1]], breaks = seq(0, 1500, by = 10), xlim = c(40, 470), col = accent,
border = "white", main = v[[2]], xlab = expression(hat(N)), ylab = "Number of jars")
abline(v = N, col = hl, lwd = 3, lty = 2)
}
A bigger second toss narrows the spread of the estimates around the true total.
Now the “jar” is a population of \(N = 180\) people. Clinic A’s list plays the role of the cubes (\(M\)), clinic B’s list plays the role of the second toss (\(n\)), and people on both lists are the cubes in the circle (\(k\)). The function below simulates both lists under four scenarios.
sim_clinics <- function(scenario, N = 180, R = 1000) {
est <- numeric(R)
for (r in 1:R) {
if (scenario == "Independent lists") {
a <- runif(N) < 1/3
b <- runif(N) < 1/4
} else if (scenario == "Referral") {
# people on A's list are often referred to B
a <- runif(N) < 1/3
b <- ifelse(a, runif(N) < 0.5, runif(N) < 0.167)
} else if (scenario == "Hard-to-reach group") {
# 60 people rarely reach either clinic
hard <- seq_len(N) <= 60
a <- runif(N) < ifelse(hard, 0.10, 0.43)
b <- runif(N) < ifelse(hard, 0.10, 0.33)
} else if (scenario == "Separate catchments") {
# half live near clinic B, half near clinic A
near_b <- seq_len(N) <= N / 2
a <- runif(N) < ifelse(near_b, 0.10, 0.55)
b <- runif(N) < ifelse(near_b, 0.50, 0.10)
}
k <- sum(a & b)
est[r] <- if (k > 0) sum(a) * sum(b) / k else NA
}
est
}
set.seed(2026)
scenarios <- c("Independent lists", "Referral", "Hard-to-reach group", "Separate catchments")
res <- lapply(scenarios, sim_clinics)
names(res) <- scenarios
data.frame(median = sapply(res, median, na.rm = TRUE),
share_below_180 = sapply(res, function(v) mean(v < 180, na.rm = TRUE)))
#> median share_below_180
#> Independent lists 180.000 0.496
#> Referral 100.000 1.000
#> Hard-to-reach group 148.775 0.856
#> Separate catchments 340.100 0.001
par(mfrow = c(1, 4))
for (s in scenarios) {
hist(pmin(res[[s]], 610), breaks = seq(40, 620, by = 20), col = accent,
border = "white", main = s, xlab = expression(hat(N)), ylab = "Number of simulations")
abline(v = 180, col = hl, lwd = 3, lty = 2)
}
When the two lists are independent, the estimates center on the true total of 180. Referral and a hard-to-reach group make the lists overlap too much, so the estimator falls short; separate catchments make the lists overlap too little, so the estimator overshoots.