# Allee effects and extinction risk
# https://tidyecology.com/posts/allee-effects-and-extinction-risk/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-07-01

## ---- setup ----
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
                      dev = "png", dpi = 120, fig.path = "figures/",
                      fig.width = 7, fig.height = 4.4)
library(ggplot2)
theme_te <- theme_minimal(base_size = 12) +
  theme(panel.background = element_rect(fill = "#f5f4ee", colour = NA),
        plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank(),
        plot.title = element_text(face = "bold", size = 12))
theme_set(theme_te)
col_allee <- "#b5651d"; col_no <- "#3a6b52"; col_ref <- "#555555"

r <- 0.5; K <- 100; A <- 30
step_pop <- function(N, allee) {
  g <- if (allee) r*(1 - N/K)*(N/A - 1) else r*(1 - N/K)
  rpois(1, N * exp(g))
}
sim_ext <- function(N0, allee, tmax = 100, reps = 600) {
  ext <- 0
  for (j in 1:reps) { N <- N0
    for (t in 1:tmax) { if (N < 1) { N <- 0; break }; N <- step_pop(N, allee); if (N < 1) { N <- 0; break } }
    if (N < 1) ext <- ext + 1 }
  ext/reps
}
sim_est <- function(N0, allee, tmax = 80, reps = 600) {
  est <- 0
  for (j in 1:reps) { N <- N0
    for (t in 1:tmax) { if (N < 1) { N <- 0; break }; N <- step_pop(N, allee); if (N < 1) { N <- 0; break } }
    if (N >= K/2) est <- est + 1 }
  est/reps
}
set.seed(401)
ext_grid <- data.frame(N0 = seq(30, 72, 3))
ext_grid$allee <- sapply(ext_grid$N0, sim_ext, allee = TRUE)
ext_grid$none  <- sapply(ext_grid$N0, sim_ext, allee = FALSE)
est_grid <- data.frame(N0 = seq(6, 60, 3))
est_grid$allee <- sapply(est_grid$N0, sim_est, allee = TRUE)
est_grid$none  <- sapply(est_grid$N0, sim_est, allee = FALSE)

## ---- sim ----
r <- 0.5; K <- 100; A <- 30
step_pop <- function(N, allee) {
  g <- if (allee) r*(1 - N/K)*(N/A - 1) else r*(1 - N/K)   # strong Allee, or plain logistic
  rpois(1, N * exp(g))                                      # demographic stochasticity
}
sim_ext <- function(N0, allee, tmax = 100, reps = 600) {     # extinct after tmax steps?
  ext <- 0
  for (j in 1:reps) { N <- N0
    for (t in 1:tmax) { if (N < 1) { N <- 0; break }; N <- step_pop(N, allee); if (N < 1) { N <- 0; break } }
    if (N < 1) ext <- ext + 1 }
  ext/reps
}
sim_est <- function(N0, allee, tmax = 80, reps = 600) {      # reached half of K?
  est <- 0
  for (j in 1:reps) { N <- N0
    for (t in 1:tmax) { if (N < 1) { N <- 0; break }; N <- step_pop(N, allee); if (N < 1) { N <- 0; break } }
    if (N >= K/2) est <- est + 1 }
  est/reps
}
set.seed(401)
ext36 <- c(allee_from_36 = sim_ext(36, TRUE), logistic_from_36 = sim_ext(36, FALSE))
ext36

## ---- fig-ext ----
el <- rbind(data.frame(N0 = ext_grid$N0, p = ext_grid$allee, type = "strong Allee"),
            data.frame(N0 = ext_grid$N0, p = ext_grid$none,  type = "no Allee"))
ggplot(el, aes(N0, p, colour = type)) +
  geom_vline(xintercept = A, colour = col_ref, linetype = "dashed") +
  geom_line(linewidth = 1) +
  annotate("text", x = A + 1, y = 0.6, label = "deterministic threshold", hjust = 0, colour = col_ref, size = 3.2) +
  scale_colour_manual(values = c("strong Allee" = col_allee, "no Allee" = col_no)) +
  labs(x = "Starting population size", y = "Extinction probability (100 steps)", colour = NULL,
       title = "The Allee effect kills populations the threshold calls safe")

## ---- establishment ----
set.seed(401)
est_out <- c(released_12 = sim_est(12, TRUE), released_42 = sim_est(42, TRUE),
             released_12_noallee = sim_est(12, FALSE))
est_out

## ---- fig-est ----
sl <- rbind(data.frame(N0 = est_grid$N0, p = est_grid$allee, type = "strong Allee"),
            data.frame(N0 = est_grid$N0, p = est_grid$none,  type = "no Allee"))
ggplot(sl, aes(N0, p, colour = type)) +
  geom_vline(xintercept = A, colour = col_ref, linetype = "dashed") +
  geom_line(linewidth = 1) +
  scale_colour_manual(values = c("strong Allee" = col_allee, "no Allee" = col_no)) +
  labs(x = "Number released", y = "Establishment probability", colour = NULL,
       title = "Propagule pressure has to clear the threshold")
