# Checking a source-sink analysis
# https://tidyecology.com/posts/checking-a-source-sink-analysis/
#
# 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-10-07

## ---- 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_source <- "#3a6b52"; col_sink <- "#b5651d"; col_ref <- "#555555"

l_local <- 0.84; imm <- 48
n_sink <- imm/(1 - l_local)
app_lambda <- (n_sink*l_local + imm)/n_sink      # apparent growth from counts at equilibrium

## ---- counts ----
source_hab <- c(density = 120, births = 0.9, deaths = 0.5)   # births > deaths: a source
sink_hab   <- c(density = 120, births = 0.4, deaths = 0.8)   # deaths > births: a sink
rbind(source = source_hab, sink = sink_hab)

## ---- fig-counts ----
d <- data.frame(habitat = c("source", "sink"), density = c(120, 120), kind = c("source", "sink"))
ggplot(d, aes(habitat, density, fill = kind)) +
  geom_col(width = 0.55) +
  scale_fill_manual(values = c("source" = col_source, "sink" = col_sink), guide = "none") +
  labs(x = NULL, y = "Density (identical)", title = "Same count, opposite demography")

## ---- apparent ----
l_local <- 0.84; imm <- 48                 # true local growth rate, and annual immigrants
n_sink  <- imm/(1 - l_local)               # equilibrium sink size
apparent <- (n_sink*l_local + imm)/n_sink  # growth inferred from total counts
c(true_local_lambda = l_local, apparent_lambda_from_counts = apparent, sink_size = n_sink)

## ---- fig-apparent ----
N0 <- 300
di <- data.frame(imm = seq(0, 120, 2))
di$apparent <- (N0*l_local + di$imm)/N0
ggplot(di, aes(imm, apparent)) +
  geom_hline(yintercept = 1, colour = col_ref, linetype = "dotted") +
  geom_hline(yintercept = l_local, colour = col_sink, linetype = "dashed") +
  geom_line(colour = col_source, linewidth = 1) +
  annotate("text", x = 2, y = l_local - 0.03, label = "true local rate", hjust = 0, colour = col_sink, size = 3.4) +
  labs(x = "Immigrants per year", y = "Growth rate inferred from counts",
       title = "Immigration inflates the apparent local growth")

## ---- contribution ----
# two sources, both self-sufficient (lambda > 1), very different net contributions
sA <- c(lambda = 1.20, size = 40);  sB <- c(lambda = 1.04, size = 400)
c(A_emigrants = unname(sA["size"]*(sA["lambda"]-1)), B_emigrants = unname(sB["size"]*(sB["lambda"]-1)))
