# Checking an epidemic model
# https://tidyecology.com/posts/checking-an-epidemic-model/
#
# 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-11-15

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

b <- 0.002; gamma <- 0.1; beta_fd <- 0.4
r_obs <- 0.297
R0_from_r <- function(D) 1 + r_obs*D
set.seed(170)
kh <- rpois(5000, 5)
ko <- rnbinom(5000, mu = 5, size = 0.3)
eff <- function(k) mean(k^2)/mean(k)
ratio_h <- eff(kh)/mean(kh); ratio_o <- eff(ko)/mean(ko)
top20 <- function(k) sum(sort(k, decreasing = TRUE)[1:1000])/sum(k)

## ---- transmission ----
b <- 0.002; gamma <- 0.1; beta_fd <- 0.4
R0_density   <- function(N) b*N/gamma        # density-dependent: rises with N
R0_frequency <- function(N) beta_fd/gamma    # frequency-dependent: flat in N
rbind(density_dependent   = sapply(c(30, 60, 120), R0_density),
      frequency_dependent = sapply(c(30, 60, 120), R0_frequency))

## ---- fig-transmission ----
Ng <- seq(0, 130, 1)
dt <- rbind(data.frame(N = Ng, R0 = R0_density(Ng), type = "density-dependent"),
            data.frame(N = Ng, R0 = R0_frequency(Ng), type = "frequency-dependent"))
ggplot(dt, aes(N, R0, colour = type)) +
  geom_hline(yintercept = 1, colour = col_ref, linetype = "dotted") +
  geom_line(linewidth = 1) +
  scale_colour_manual(values = c("density-dependent" = col_a, "frequency-dependent" = col_b)) +
  labs(x = "Host density", y = "Basic reproduction number", colour = NULL,
       title = "The transmission assumption decides whether culling helps")

## ---- growth ----
r_obs <- 0.297                          # observed early exponential growth rate
R0_from_r <- function(D) 1 + r_obs*D    # D = assumed mean infectious period (days)
c(period_5 = R0_from_r(5), period_10 = R0_from_r(10), period_14 = R0_from_r(14))

## ---- fig-growth ----
Dg <- seq(3, 16, 0.5)
dg <- data.frame(D = Dg, R0 = R0_from_r(Dg))
ggplot(dg, aes(D, R0)) +
  geom_line(colour = col_a, linewidth = 1) +
  geom_point(data = data.frame(D = c(5, 10, 14), R0 = R0_from_r(c(5, 10, 14))),
             aes(D, R0), colour = col_b, size = 2.4) +
  labs(x = "Assumed mean infectious period (days)", y = "Inferred reproduction number",
       title = "Same growth rate, very different R0")

## ---- heterogeneity ----
set.seed(170)
kh <- rpois(5000, 5)                    # chance variation only: no superspreaders
ko <- rnbinom(5000, mu = 5, size = 0.3) # superspreading: same mean, heavy tail
eff <- function(k) mean(k^2)/mean(k)    # the contact number that actually drives R0
c(mean_contacts = mean(ko),
  effective_poisson = eff(kh),
  effective_superspreading = eff(ko))
