Model library

118 compartmental models with generated R / deSolve code← Back to editor

Parameter values are typical published or textbook values for illustration. Check them against the cited source and your own setting before use.

Tip: has:I finds models with a compartment named I, -word excludes, "exact phrase" matches a phrase.

SI

Basic
SI

Infection with no recovery (e.g. untreated chronic infection) in a closed population.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, ch. 2

Show R code
## SI
## Infection with no recovery (e.g. untreated chronic infection) in a closed population.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, ch. 2
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I
## Flows (time unit: days):
##   S -> I  : lam * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.2  # transmission rate per day
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 999,  # Susceptible
  I = 1     # Infected
)

# ---- Model equations -------------------------------------------------
si_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- -lam * S
    dI <- lam * S

    list(c(dS, dI), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = si_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SI")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SI: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIS

Basic
SI

Infection without lasting immunity (e.g. many bacterial STIs): recovered people become susceptible again.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.4

Show R code
## SIS
## Infection without lasting immunity (e.g. many bacterial STIs): recovered people become susceptible again.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.4
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I
## Flows (time unit: days):
##   S -> I  : lam * S
##   I -> S  : gamma * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.3,     # transmission rate per day
  gamma = 1 / 10  # recovery rate = 1/(infectious period)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 999,  # Susceptible
  I = 1     # Infectious
)

# ---- Model equations -------------------------------------------------
sis_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Rates of change
    dS <- -lam * S + gamma * I
    dI <- lam * S - gamma * I

    list(c(dS, dI))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sis_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIS")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIR

Basic
SIR

Basic SIR epidemic in a closed population.

Kermack & McKendrick (1927); Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.1

Show R code
## SIR
## Basic SIR epidemic in a closed population.
## Reference: Kermack & McKendrick (1927); Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.1
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R
## Flows (time unit: days):
##   S -> I  : lam * S
##   I -> R  : gamma * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 160, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.3,     # transmission rate per day
  gamma = 1 / 10  # recovery rate = 1/(infectious period)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 990,  # Susceptible
  I = 10,   # Infectious
  R = 0     # Recovered
)

# ---- Model equations -------------------------------------------------
sir_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- -lam * S
    dI <- lam * S - gamma * I
    dR <- gamma * I

    list(c(dS, dI, dR), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIR")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SIR: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIR with births and deaths

Basic
SIR

Endemic SIR with demography; damped oscillations to an endemic equilibrium (measles-like parameters).

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.1.2 (Program 2.2)

Show R code
## SIR with births and deaths
## Endemic SIR with demography; damped oscillations to an endemic equilibrium (measles-like parameters).
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.1.2 (Program 2.2)
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R
## Flows (time unit: years):
##   (outside) -> S  : mu * N
##   S -> I          : lam * S
##   I -> R          : gamma * I
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 60, by = 0.02)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 520,       # transmission rate per year
  gamma = 365 / 7,  # recovery rate per year (7-day infectious period)
  mu = 1 / 70       # birth = death rate per year (70-year life expectancy)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 10000,  # Susceptible
  I = 10,     # Infectious
  R = 89990   # Recovered
)

# ---- Model equations -------------------------------------------------
sir_vital_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- mu * N - lam * S - mu * S
    dI <- lam * S - gamma * I - mu * I
    dR <- gamma * I - mu * R

    list(c(dS, dI, dR), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_vital_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SIR with births and deaths")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "SIR with births and deaths: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIRS

Basic
SIR

SIR with waning immunity: recovered individuals return to the susceptible class.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.4

Show R code
## SIRS
## SIR with waning immunity: recovered individuals return to the susceptible class.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.4
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R
## Flows (time unit: days):
##   S -> I  : lam * S
##   I -> R  : gamma * I
##   R -> S  : omega * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 1000, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.3,      # transmission rate per day
  gamma = 1 / 10,  # recovery rate
  omega = 1 / 180  # rate of loss of immunity = 1/(duration of immunity)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 990,  # Susceptible
  I = 10,   # Infectious
  R = 0     # Recovered
)

# ---- Model equations -------------------------------------------------
sirs_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Rates of change
    dS <- -lam * S + omega * R
    dI <- lam * S - gamma * I
    dR <- gamma * I - omega * R

    list(c(dS, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sirs_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIRS")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SEIR

Basic
SEIR

Epidemic with a latent (infected, not yet infectious) period, closed population.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5

Show R code
## SEIR
## Epidemic with a latent (infected, not yet infectious) period, closed population.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R
## Flows (time unit: days):
##   S -> E  : lam * S
##   E -> I  : sigma * E
##   I -> R  : gamma * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.5,     # transmission rate per day
  sigma = 1 / 5,  # rate of becoming infectious = 1/(latent period)
  gamma = 1 / 7   # recovery rate = 1/(infectious period)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9999,  # Susceptible
  E = 1,     # Exposed (latent)
  I = 0,     # Infectious
  R = 0      # Recovered
)

# ---- Model equations -------------------------------------------------
seir_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    onsets <- sigma * E

    # Rates of change
    dS <- -lam * S
    dE <- lam * S - sigma * E
    dI <- sigma * E - gamma * I
    dR <- gamma * I

    list(c(dS, dE, dI, dR), c(onsets = onsets))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seir_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEIR")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("onsets")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SEIR: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEIR with births and deaths

Basic
SEIR

Endemic SEIR parameterised by R0 (measles-like): transmission rate derived from R0.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5 (Program 2.6)

Show R code
## SEIR with births and deaths
## Endemic SEIR parameterised by R0 (measles-like): transmission rate derived from R0.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5 (Program 2.6)
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R
## Flows (time unit: years):
##   (outside) -> S  : mu * N
##   S -> E          : lam * S
##   E -> I          : sigma * E
##   I -> R          : gamma * I
##   S -> (outside)  : mu * S
##   E -> (outside)  : mu * E
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.01)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0 = 13,          # basic reproduction number
  sigma = 365 / 8,  # rate of becoming infectious per year (8-day latent period)
  gamma = 365 / 5,  # recovery rate per year (5-day infectious period)
  mu = 1 / 70       # birth = death rate per year
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 6000,  # Susceptible
  E = 10,    # Exposed
  I = 10,    # Infectious
  R = 93980  # Recovered
)

# ---- Model equations -------------------------------------------------
seir_vital_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + R

    # Derived quantities
    beta <- R0 * (gamma + mu) * (sigma + mu) / sigma  # transmission rate implied by R0
    lam <- beta * I / N                               # force of infection

    # Flows recorded as outputs
    incidence <- sigma * E

    # Rates of change
    dS <- mu * N - lam * S - mu * S
    dE <- lam * S - sigma * E - mu * E
    dI <- sigma * E - gamma * I - mu * I
    dR <- gamma * I - mu * R

    list(c(dS, dE, dI, dR), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seir_vital_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SEIR with births and deaths")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "SEIR with births and deaths: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEIRS

Basic
SEIR

SEIR with waning immunity, giving recurrent epidemics.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5

Show R code
## SEIRS
## SEIR with waning immunity, giving recurrent epidemics.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R
## Flows (time unit: days):
##   S -> E  : lam * S
##   E -> I  : sigma * E
##   I -> R  : gamma * I
##   R -> S  : omega * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 1500, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.5,      # transmission rate per day
  sigma = 1 / 5,   # 1/(latent period)
  gamma = 1 / 7,   # 1/(infectious period)
  omega = 1 / 365  # 1/(duration of immunity)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9999,  # Susceptible
  E = 1,     # Exposed
  I = 0,     # Infectious
  R = 0      # Recovered
)

# ---- Model equations -------------------------------------------------
seirs_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Rates of change
    dS <- -lam * S + omega * R
    dE <- lam * S - sigma * E
    dI <- sigma * E - gamma * I
    dR <- gamma * I - omega * R

    list(c(dS, dE, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seirs_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEIRS")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIRD

Basic
SIRD

SIR with disease-induced deaths. D is excluded from the living population N.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.2

Show R code
## SIRD
## SIR with disease-induced deaths. D is excluded from the living population N.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.2
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, D
## Flows (time unit: days):
##   S -> I  : lam * S
##   I -> R  : gamma * (1 - cfr) * I
##   I -> D  : gamma * cfr * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.3,      # transmission rate per day
  gamma = 1 / 10,  # rate of leaving the infectious state
  cfr = 0.02       # case fatality ratio
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # Susceptible
  I = 10,    # Infectious
  R = 0,     # Recovered
  D = 0      # Dead
)

# ---- Model equations -------------------------------------------------
sird_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    deaths <- gamma * cfr * I

    # Rates of change
    dS <- -lam * S
    dI <- lam * S - gamma * (1 - cfr) * I - gamma * cfr * I
    dR <- gamma * (1 - cfr) * I
    dD <- gamma * cfr * I

    list(c(dS, dI, dR, dD), c(deaths = deaths))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sird_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIRD")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("deaths")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SIRD: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEIRD

Basic
SEIRD

SEIR with disease-induced deaths.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5

Show R code
## SEIRD
## SEIR with disease-induced deaths.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §2.5
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R, D
## Flows (time unit: days):
##   S -> E  : lam * S
##   E -> I  : sigma * E
##   I -> R  : gamma * (1 - cfr) * I
##   I -> D  : gamma * cfr * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 250, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.4,     # transmission rate per day
  sigma = 1 / 5,  # 1/(latent period)
  gamma = 1 / 8,  # rate of leaving the infectious state
  cfr = 0.01      # case fatality ratio
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 99990,  # Susceptible
  E = 10,     # Exposed
  I = 0,      # Infectious
  R = 0,      # Recovered
  D = 0       # Dead
)

# ---- Model equations -------------------------------------------------
seird_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    incidence <- sigma * E
    deaths <- gamma * cfr * I

    # Rates of change
    dS <- -lam * S
    dE <- lam * S - sigma * E
    dI <- sigma * E - gamma * (1 - cfr) * I - gamma * cfr * I
    dR <- gamma * (1 - cfr) * I
    dD <- gamma * cfr * I

    list(c(dS, dE, dI, dR, dD), c(incidence = incidence, deaths = deaths))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seird_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEIRD")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence", "deaths")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SEIRD: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIR with newborn vaccination

Interventions
SIRV

A proportion p of newborns is vaccinated (enters V). Elimination requires p > 1 - 1/R0.

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §8.1; Anderson & May (1991), Infectious Diseases of Humans, ch. 5

Show R code
## SIR with newborn vaccination
## A proportion p of newborns is vaccinated (enters V). Elimination requires p > 1 - 1/R0.
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §8.1; Anderson & May (1991), Infectious Diseases of Humans, ch. 5
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, V
## Flows (time unit: years):
##   (outside) -> S  : (1 - p) * mu * N
##   (outside) -> V  : p * mu * N
##   S -> I          : lam * S
##   I -> R          : gamma * I
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R
##   V -> (outside)  : mu * V

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 80, by = 0.02)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0 = 15,          # basic reproduction number
  gamma = 365 / 7,  # recovery rate per year
  mu = 1 / 70,      # birth = death rate per year
  p = 0.8           # proportion of newborns vaccinated
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 10000,  # Susceptible
  I = 10,     # Infectious
  R = 89990,  # Recovered
  V = 0       # Vaccinated
)

# ---- Model equations -------------------------------------------------
sir_newborn_vacc_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R + V

    # Derived quantities
    beta <- R0 * (gamma + mu)  # transmission rate implied by R0
    lam <- beta * I / N        # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- (1 - p) * mu * N - lam * S - mu * S
    dI <- lam * S - gamma * I - mu * I
    dR <- gamma * I - mu * R
    dV <- p * mu * N - mu * V

    list(c(dS, dI, dR, dV), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_newborn_vacc_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SIR with newborn vaccination")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "SIR with newborn vaccination: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIR with a vaccination campaign

Interventions
SIRV

Mass vaccination of susceptibles at rate v starting on day t_vac (assumes a perfect vaccine).

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §8.1

Show R code
## SIR with a vaccination campaign
## Mass vaccination of susceptibles at rate v starting on day t_vac (assumes a perfect vaccine).
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §8.1
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, V
## Flows (time unit: days):
##   S -> I  : lam * S
##   I -> R  : gamma * I
##   S -> V  : vacc * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.3,      # transmission rate per day
  gamma = 1 / 10,  # recovery rate
  v = 0.02,        # vaccination rate per day once the campaign starts
  t_vac = 30       # day the campaign starts
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # Susceptible
  I = 10,    # Infectious
  R = 0,     # Recovered
  V = 0      # Vaccinated
)

# ---- Model equations -------------------------------------------------
sirv_campaign_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R + V

    # Derived quantities
    lam <- beta * I / N       # force of infection
    vacc <- v * (t >= t_vac)  # vaccination rate at time t

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- -lam * S - vacc * S
    dI <- lam * S - gamma * I
    dR <- gamma * I
    dV <- vacc * S

    list(c(dS, dI, dR, dV), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sirv_campaign_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIR with a vaccination campaign")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SIR with a vaccination campaign: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SVIR with a leaky vaccine

Interventions
SVIR

Vaccinated individuals are infected at a reduced rate (1 - eff) * lam.

Vynnycky & White (2010), An Introduction to Infectious Disease Modelling, ch. 7

Show R code
## SVIR with a leaky vaccine
## Vaccinated individuals are infected at a reduced rate (1 - eff) * lam.
## Reference: Vynnycky & White (2010), An Introduction to Infectious Disease Modelling, ch. 7
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, V, I, R
## Flows (time unit: days):
##   S -> I  : lam * S
##   V -> I  : (1 - eff) * lam * V
##   I -> R  : gamma * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 300, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.4,     # transmission rate per day
  gamma = 1 / 7,  # recovery rate
  eff = 0.6       # vaccine efficacy against infection
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 3999,  # Susceptible
  V = 6000,  # Vaccinated
  I = 1,     # Infectious
  R = 0      # Recovered
)

# ---- Model equations -------------------------------------------------
leaky_vaccine_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + V + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    breakthrough <- (1 - eff) * lam * V

    # Rates of change
    dS <- -lam * S
    dV <- -(1 - eff) * lam * V
    dI <- lam * S + (1 - eff) * lam * V - gamma * I
    dR <- gamma * I

    list(c(dS, dV, dI, dR), c(breakthrough = breakthrough))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = leaky_vaccine_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", V = "#2457C5", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SVIR with a leaky vaccine")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("breakthrough")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SVIR with a leaky vaccine: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIQR (isolation)

Interventions
SIQR

Infectious individuals are detected and isolated (Q) at rate q and no longer transmit.

Hethcote, Zhien & Shengbing (2002), Math. Biosci. 180:141-160

Show R code
## SIQR (isolation)
## Infectious individuals are detected and isolated (Q) at rate q and no longer transmit.
## Reference: Hethcote, Zhien & Shengbing (2002), Math. Biosci. 180:141-160
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, Q, R
## Flows (time unit: days):
##   S -> I  : lam * S
##   I -> R  : gamma * I
##   I -> Q  : q * I
##   Q -> R  : gamma * Q

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.4,     # transmission rate per day
  gamma = 1 / 7,  # recovery rate
  q = 0.1         # rate of detection and isolation
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # Susceptible
  I = 10,    # Infectious
  Q = 0,     # Isolated
  R = 0      # Recovered
)

# ---- Model equations -------------------------------------------------
siqr_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + Q + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Rates of change
    dS <- -lam * S
    dI <- lam * S - gamma * I - q * I
    dQ <- q * I - gamma * Q
    dR <- gamma * I + gamma * Q

    list(c(dS, dI, dQ, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = siqr_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", Q = "#00838F", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIQR (isolation)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SEIR with a temporary lockdown

Interventions
SEIR

Contacts fall by a proportion eff between days t_on and t_off.

Flaxman et al. (2020), Nature 584:257-261 (concept)

Show R code
## SEIR with a temporary lockdown
## Contacts fall by a proportion eff between days t_on and t_off.
## Reference: Flaxman et al. (2020), Nature 584:257-261 (concept)
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R
## Flows (time unit: days):
##   S -> E  : lam * S
##   E -> I  : sigma * E
##   I -> R  : gamma * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0 = 2.5,       # basic reproduction number
  sigma = 1 / 5,  # 1/(latent period)
  gamma = 1 / 5,  # 1/(infectious period)
  eff = 0.6,      # proportional reduction in contacts during lockdown
  t_on = 50,      # day lockdown starts
  t_off = 120     # day lockdown ends
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 999990,  # Susceptible
  E = 10,      # Exposed
  I = 0,       # Infectious
  R = 0        # Recovered
)

# ---- Model equations -------------------------------------------------
seir_lockdown_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + R

    # Derived quantities
    contact <- 1 - eff * (t >= t_on & t < t_off)  # relative contact rate
    lam <- contact * R0 * gamma * I / N           # force of infection
    Rt <- contact * R0 * S / N                    # effective reproduction number

    # Flows recorded as outputs
    incidence <- sigma * E

    # Rates of change
    dS <- -lam * S
    dE <- lam * S - sigma * E
    dI <- sigma * E - gamma * I
    dR <- gamma * I

    list(c(dS, dE, dI, dR), c(Rt = Rt, incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seir_lockdown_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEIR with a temporary lockdown")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("Rt", "incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SEIR with a temporary lockdown: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Hepatitis E with latrines

Interventions
SEIR

Hepatitis E outbreak with a latrine intervention (MMID practical, day 4).

MMID practical: modelling interventions in R

Show R code
## Hepatitis E with latrines
## Hepatitis E outbreak with a latrine intervention (MMID practical, day 4).
## Reference: MMID practical: modelling interventions in R
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R
## Flows (time unit: weeks):
##   (outside) -> S  : mui * P
##   S -> (outside)  : muo * S
##   S -> E          : lam * S
##   E -> (outside)  : muo * E
##   E -> I          : gamma * E
##   I -> (outside)  : muo * I
##   I -> R          : nui * I
##   R -> (outside)  : muo * R
##   R -> S          : omega * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(38, 73, by = 1 / 7)  # weeks

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mui = 1 / (50 * 52),    # birth
  muo = 1 / (50 * 52),    # death
  R0 = 6.6,               # basic reproduction number
  omega = 1 / (10 * 52),  # rate of loss of immunity
  gamma = 1 / 2,          # latent -> infectious = 1/(latent period)
  nui = 1 / 4,            # recovery = 1/(duration of infection)
  report = 1 / 7,         # proportion of infections reported
  amp = 0,                # relative amplitude of seasonal forcing
  phi = 0,                # week of peak in seasonal forcing
  week_start = 38,        # first week of the simulation
  week_interv = 15,       # weeks after first case when intervention starts
  lat_cov_i = 0.24,       # intervention coverage of latrines
  lat_eff = 0.7           # efficacy of latrines in reducing transmission
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  E <- 1                 # Exposed
  I <- 0                 # Infectious
  R <- 0                 # Immune
  S <- 3700 - E - I - R  # Susceptible (non-immune)
  c(S = S, E = E, I = I, R = R)
})

# ---- Model equations -------------------------------------------------
hepE_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    P <- S + E + I + R

    # Derived quantities
    seas <- 1 + amp * cos(2 * pi * (t - phi) / 52)            # seasonal forcing
    beta <- R0 * (muo + nui) * (gamma + muo) / gamma          # transmission rate from R0
    lat_cov <- (t >= (week_start + week_interv)) * lat_cov_i  # latrine coverage (0 before intervention)
    latrine <- 1 - lat_eff * lat_cov                          # relative transmission with latrines
    lam <- latrine * beta * seas * I / P                      # force of infection
    inc <- report * gamma * E                                 # weekly reported incidence

    # Rates of change
    dS <- mui * P - muo * S - lam * S + omega * R
    dE <- lam * S - muo * E - gamma * E
    dI <- gamma * E - muo * I - nui * I
    dR <- nui * I - muo * R - omega * R

    list(c(dS, dE, dI, dR), c(inc = inc))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hepE_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (weeks)", ylab = "Number of individuals",
        main = "Hepatitis E with latrines")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("inc")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (weeks)", ylab = "Value",
        main = "Hepatitis E with latrines: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

MSIR (maternal immunity)

Natural history
MSIR

Newborns are protected by maternal antibodies (M) before becoming susceptible.

Hethcote (2000), SIAM Review 42:599-653

Show R code
## MSIR (maternal immunity)
## Newborns are protected by maternal antibodies (M) before becoming susceptible.
## Reference: Hethcote (2000), SIAM Review 42:599-653
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: M, S, I, R
## Flows (time unit: years):
##   (outside) -> M  : mu * N
##   M -> S          : delta * M
##   S -> I          : lam * S
##   I -> R          : gamma * I
##   M -> (outside)  : mu * M
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 60, by = 0.02)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 700,       # transmission rate per year
  delta = 2,        # rate of losing maternal immunity per year (6 months)
  gamma = 365 / 7,  # recovery rate per year
  mu = 1 / 70       # birth = death rate per year
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  M = 500,   # Maternally immune
  S = 9500,  # Susceptible
  I = 10,    # Infectious
  R = 89990  # Recovered
)

# ---- Model equations -------------------------------------------------
msir_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- M + S + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dM <- mu * N - delta * M - mu * M
    dS <- delta * M - lam * S - mu * S
    dI <- lam * S - gamma * I - mu * I
    dR <- gamma * I - mu * R

    list(c(dM, dS, dI, dR), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = msir_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(M = "#F3C6D3", S = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "MSIR (maternal immunity)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "MSIR (maternal immunity): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEIAR (asymptomatic infection)

Natural history
SEIAR

A proportion of infections is asymptomatic (A) and less infectious.

Arino et al. (2008), J. R. Soc. Interface 5(Suppl 2)

Show R code
## SEIAR (asymptomatic infection)
## A proportion of infections is asymptomatic (A) and less infectious.
## Reference: Arino et al. (2008), J. R. Soc. Interface 5(Suppl 2)
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, A, R
## Flows (time unit: days):
##   S -> E  : lam * S
##   E -> I  : sigma * ps * E
##   E -> A  : sigma * (1 - ps) * E
##   I -> R  : gamma * I
##   A -> R  : gamma * A

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.5,     # transmission rate per day
  rA = 0.5,       # relative infectiousness of asymptomatics
  sigma = 1 / 4,  # 1/(latent period)
  ps = 0.6,       # proportion of infections that are symptomatic
  gamma = 1 / 6   # recovery rate
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 99990,  # Susceptible
  E = 10,     # Exposed
  I = 0,      # Symptomatic
  A = 0,      # Asymptomatic
  R = 0       # Recovered
)

# ---- Model equations -------------------------------------------------
seiar_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + A + R

    # Derived quantities
    lam <- beta * (I + rA * A) / N  # force of infection

    # Flows recorded as outputs
    cases <- sigma * ps * E

    # Rates of change
    dS <- -lam * S
    dE <- lam * S - sigma * ps * E - sigma * (1 - ps) * E
    dI <- sigma * ps * E - gamma * I
    dA <- sigma * (1 - ps) * E - gamma * A
    dR <- gamma * I + gamma * A

    list(c(dS, dE, dI, dA, dR), c(cases = cases))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seiar_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", A = "#E57373", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEIAR (asymptomatic infection)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("cases")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SEIAR (asymptomatic infection): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

COVID-19 style SEPIR

Natural history
SEPIsIaR

Latent (E), pre-symptomatic infectious (P), then symptomatic (Is) or asymptomatic (Ia) infection.

Davies et al. (2020), Nature Medicine 26:1205-1211 (structure)

Show R code
## COVID-19 style SEPIR
## Latent (E), pre-symptomatic infectious (P), then symptomatic (Is) or asymptomatic (Ia) infection.
## Reference: Davies et al. (2020), Nature Medicine 26:1205-1211 (structure)
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, P, Is, Ia, R
## Flows (time unit: days):
##   S -> E   : lam * S
##   E -> P   : sigma * E
##   P -> Is  : delta * ps * P
##   P -> Ia  : delta * (1 - ps) * P
##   Is -> R  : gamma * Is
##   Ia -> R  : gamma * Ia

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 300, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0 = 2.5,       # basic reproduction number
  sigma = 1 / 3,  # 1/(latent period)
  delta = 1 / 2,  # 1/(pre-symptomatic period)
  gamma = 1 / 5,  # 1/(infectious period after the pre-symptomatic stage)
  ps = 0.6,       # proportion symptomatic
  rP = 1,         # relative infectiousness, pre-symptomatic
  rA = 0.5        # relative infectiousness, asymptomatic
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 999990,  # Susceptible
  E = 10,      # Latent
  P = 0,       # Pre-symptomatic
  Is = 0,      # Symptomatic
  Ia = 0,      # Asymptomatic
  R = 0        # Recovered
)

# ---- Model equations -------------------------------------------------
covid_sepiar_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + P + Is + Ia + R

    # Derived quantities
    beta <- R0 / (rP / delta + (ps + (1 - ps) * rA) / gamma)  # transmission rate implied by R0
    lam <- beta * (rP * P + Is + rA * Ia) / N                 # force of infection

    # Flows recorded as outputs
    onsets <- delta * ps * P

    # Rates of change
    dS <- -lam * S
    dE <- lam * S - sigma * E
    dP <- sigma * E - delta * ps * P - delta * (1 - ps) * P
    dIs <- delta * ps * P - gamma * Is
    dIa <- delta * (1 - ps) * P - gamma * Ia
    dR <- gamma * Is + gamma * Ia

    list(c(dS, dE, dP, dIs, dIa, dR), c(onsets = onsets))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = covid_sepiar_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", P = "#F0A35E", Is = "#B71C1C", Ia = "#E57373", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "COVID-19 style SEPIR")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("onsets")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "COVID-19 style SEPIR: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEIHRD (hospitalisation)

Natural history
SEIRHD

A proportion of cases is hospitalised; deaths occur in hospital.

Vynnycky & White (2010), An Introduction to Infectious Disease Modelling, ch. 4 (structure)

Show R code
## SEIHRD (hospitalisation)
## A proportion of cases is hospitalised; deaths occur in hospital.
## Reference: Vynnycky & White (2010), An Introduction to Infectious Disease Modelling, ch. 4 (structure)
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R, H, D
## Flows (time unit: days):
##   S -> E  : lam * S
##   E -> I  : sigma * E
##   I -> R  : gamma * (1 - h) * I
##   I -> H  : gamma * h * I
##   H -> R  : tauH * (1 - m) * H
##   H -> D  : tauH * m * H

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 300, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.45,    # transmission rate per day
  sigma = 1 / 5,  # 1/(latent period)
  gamma = 1 / 7,  # rate of leaving the infectious state
  h = 0.05,       # proportion hospitalised
  tauH = 1 / 10,  # rate of leaving hospital
  m = 0.2         # in-hospital fatality
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 99990,  # Susceptible
  E = 10,     # Exposed
  I = 0,      # Infectious
  R = 0,      # Recovered
  H = 0,      # Hospitalised
  D = 0       # Dead
)

# ---- Model equations -------------------------------------------------
seihrd_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + R + H

    # Derived quantities
    lam <- beta * I / N  # force of infection (hospitalised patients do not transmit)

    # Flows recorded as outputs
    admissions <- gamma * h * I
    deaths <- tauH * m * H

    # Rates of change
    dS <- -lam * S
    dE <- lam * S - sigma * E
    dI <- sigma * E - gamma * (1 - h) * I - gamma * h * I
    dR <- gamma * (1 - h) * I + tauH * (1 - m) * H
    dH <- gamma * h * I - tauH * (1 - m) * H - tauH * m * H
    dD <- tauH * m * H

    list(c(dS, dE, dI, dR, dH, dD), c(admissions = admissions, deaths = deaths))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seihrd_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E", H = "#6A2C9C", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEIHRD (hospitalisation)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("admissions", "deaths")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SEIHRD (hospitalisation): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEACHR with re-exposure

Natural history
SEACRERHD

Asymptomatic (A) and clinical (C) infection, hospitalisation (H), death (D), waning immunity and re-exposure of recovereds (ER). Parameter values are illustrative; the force of infection is an assumption (the diagram does not define it).

Custom diagram

Show R code
## SEACHR with re-exposure
## Asymptomatic (A) and clinical (C) infection, hospitalisation (H), death (D),
## waning immunity and re-exposure of recovereds (ER). Parameter values are illustrative;
## the force of infection is an assumption (the diagram does not define it).
## Reference: Custom diagram
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, A, C, R, ER, H, D
## Flows (time unit: days):
##   S -> E   : lam * S
##   E -> A   : nu * (1 - gamma) * E
##   E -> C   : nu * gamma * E
##   A -> R   : tauA * A
##   C -> R   : tauC * (1 - theta) * C
##   C -> H   : tauC * theta * C
##   H -> R   : tauH * (1 - omega) * H
##   H -> D   : tauH * omega * H
##   R -> ER  : sigma * lam * R
##   ER -> A  : nu * (1 - gamma / 2) * ER
##   ER -> C  : nu * gamma / 2 * ER
##   R -> S   : alpha * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.35,     # transmission rate per day
  rA = 0.5,        # relative infectiousness of asymptomatics
  nu = 1 / 9,      # 1/(latent period)
  gamma = 0.7,     # proportion of infections that are clinical
  tauA = 1 / 7,    # recovery rate of asymptomatics
  tauC = 1 / 5,    # rate of leaving the clinical stage
  theta = 0.3,     # proportion of clinical cases hospitalised
  tauH = 1 / 10,   # rate of leaving hospital
  omega = 0.4,     # case fatality among hospitalised
  sigma = 0.2,     # relative susceptibility of recovereds
  alpha = 1 / 365  # rate of waning immunity
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  E <- 10          # Exposed
  S <- 100000 - E  # Susceptible
  A <- 0           # Asymptomatic
  C <- 0           # Clinical
  R <- 0           # Recovered
  ER <- 0          # Exposed recovered
  H <- 0           # Hospitalised
  D <- 0           # Dead
  c(S = S, E = E, A = A, C = C, R = R, ER = ER, H = H, D = D)
})

# ---- Model equations -------------------------------------------------
seachr_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + A + C + R + ER + H

    # Derived quantities
    lam <- beta * (rA * A + C) / N  # force of infection (assumed)

    # Flows recorded as outputs
    admissions <- tauC * theta * C
    deaths <- tauH * omega * H

    # Rates of change
    dS <- -lam * S + alpha * R
    dE <- lam * S - nu * (1 - gamma) * E - nu * gamma * E
    dA <- nu * (1 - gamma) * E - tauA * A + nu * (1 - gamma / 2) * ER
    dC <- nu * gamma * E - tauC * (1 - theta) * C - tauC * theta * C + nu * gamma / 2 * ER
    dR <- tauA * A + tauC * (1 - theta) * C + tauH * (1 - omega) * H - sigma * lam * R - alpha * R
    dER <- sigma * lam * R - nu * (1 - gamma / 2) * ER - nu * gamma / 2 * ER
    dH <- tauC * theta * C - tauH * (1 - omega) * H - tauH * omega * H
    dD <- tauH * omega * H

    list(c(dS, dE, dA, dC, dR, dER, dH, dD), c(admissions = admissions, deaths = deaths))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seachr_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", A = "#C62828", C = "#8E0E0E", R = "#4E8A3E", ER = "#D9A441", H = "#6A2C9C", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEACHR with re-exposure")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("admissions", "deaths")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SEACHR with re-exposure: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEEIIR (gamma-distributed periods)

Natural history
SE1E2I1I2R

Two latent and two infectious stages give gamma-distributed (Erlang k = 2) latent and infectious periods with the same means as the SEIR model ("linear chain trick").

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §3.3

Show R code
## SEEIIR (gamma-distributed periods)
## Two latent and two infectious stages give gamma-distributed (Erlang k = 2) latent and infectious
## periods with the same means as the SEIR model ("linear chain trick").
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §3.3
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E1, E2, I1, I2, R
## Flows (time unit: days):
##   S -> E1   : lam * S
##   E1 -> E2  : 2 * sigma * E1
##   E2 -> I1  : 2 * sigma * E2
##   I1 -> I2  : 2 * gamma * I1
##   I2 -> R   : 2 * gamma * I2

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.5,     # transmission rate per day
  sigma = 1 / 5,  # 1/(mean latent period)
  gamma = 1 / 7   # 1/(mean infectious period)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9999,  # Susceptible
  E1 = 1,    # Latent, stage 1
  E2 = 0,    # Latent, stage 2
  I1 = 0,    # Infectious, stage 1
  I2 = 0,    # Infectious, stage 2
  R = 0      # Recovered
)

# ---- Model equations -------------------------------------------------
seeiir_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E1 + E2 + I1 + I2 + R

    # Derived quantities
    lam <- beta * (I1 + I2) / N  # force of infection
    prevalence <- I1 + I2        # number infectious

    # Flows recorded as outputs
    onsets <- 2 * sigma * E2

    # Rates of change
    dS <- -lam * S
    dE1 <- lam * S - 2 * sigma * E1
    dE2 <- 2 * sigma * E1 - 2 * sigma * E2
    dI1 <- 2 * sigma * E2 - 2 * gamma * I1
    dI2 <- 2 * gamma * I1 - 2 * gamma * I2
    dR <- 2 * gamma * I2

    list(c(dS, dE1, dE2, dI1, dI2, dR), c(prevalence = prevalence, onsets = onsets))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seeiir_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E1 = "#E8C468", E2 = "#E8C468", I1 = "#B71C1C", I2 = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEEIIR (gamma-distributed periods)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("prevalence", "onsets")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "SEEIIR (gamma-distributed periods): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIRWS (waning and boosting)

Natural history
SIRW

Immunity wanes through a W stage; re-exposure while in W boosts immunity back to R (pertussis-like).

Lavine, King & Bjornstad (2011), PNAS 108:7259-7264

Show R code
## SIRWS (waning and boosting)
## Immunity wanes through a W stage; re-exposure while in W boosts immunity back to R (pertussis-like).
## Reference: Lavine, King & Bjornstad (2011), PNAS 108:7259-7264
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, W
## Flows (time unit: years):
##   (outside) -> S  : mu * N
##   S -> I          : lam * S
##   I -> R          : gamma * I
##   R -> W          : 2 * kappa * R
##   W -> S          : 2 * kappa * W
##   W -> R          : boost * lam * W
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R
##   W -> (outside)  : mu * W

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.02)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 260,        # transmission rate per year
  gamma = 365 / 21,  # recovery rate per year (21-day infectious period)
  kappa = 1 / 10,    # rate of waning per year (two stages, so mean 2 / (2 kappa) = 10 years)
  boost = 20,        # boosting factor relative to the force of infection
  mu = 1 / 70        # birth = death rate per year
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 5000,   # Susceptible
  I = 10,     # Infectious
  R = 70000,  # Immune
  W = 24990   # Waning immunity
)

# ---- Model equations -------------------------------------------------
sirws_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R + W

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- mu * N - lam * S + 2 * kappa * W - mu * S
    dI <- lam * S - gamma * I - mu * I
    dR <- gamma * I - 2 * kappa * R + boost * lam * W - mu * R
    dW <- 2 * kappa * R - 2 * kappa * W - boost * lam * W - mu * W

    list(c(dS, dI, dR, dW), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sirws_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", W = "#A5D6A7")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SIRWS (waning and boosting)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "SIRWS (waning and boosting): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SICR (chronic carriers)

Natural history
SICR

A proportion q of acute infections become long-term carriers (hepatitis B / typhoid-like).

Anderson & May (1991), Infectious Diseases of Humans, ch. 6; Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §3.2

Show R code
## SICR (chronic carriers)
## A proportion q of acute infections become long-term carriers (hepatitis B / typhoid-like).
## Reference: Anderson & May (1991), Infectious Diseases of Humans, ch. 6; Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §3.2
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, C, R
## Flows (time unit: years):
##   (outside) -> S  : mu * N
##   S -> I          : lam * S
##   I -> R          : gamma * (1 - q) * I
##   I -> C          : gamma * q * I
##   C -> R          : gammaC * C
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   C -> (outside)  : mu * C
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 150, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 4,        # transmission rate per year
  eps = 0.1,       # relative infectiousness of carriers
  gamma = 6,       # rate of leaving acute infection per year (2 months)
  q = 0.1,         # proportion becoming carriers
  gammaC = 0.025,  # rate of carriers clearing infection per year
  mu = 1 / 70      # birth = death rate per year
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 99000,  # Susceptible
  I = 10,     # Acute infection
  C = 990,    # Chronic carrier
  R = 0       # Recovered
)

# ---- Model equations -------------------------------------------------
carrier_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + C + R

    # Derived quantities
    lam <- beta * (I + eps * C) / N  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- mu * N - lam * S - mu * S
    dI <- lam * S - gamma * (1 - q) * I - gamma * q * I - mu * I
    dC <- gamma * q * I - gammaC * C - mu * C
    dR <- gamma * (1 - q) * I + gammaC * C - mu * R

    list(c(dS, dI, dC, dR), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = carrier_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", C = "#8E0E0E", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SICR (chronic carriers)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "SICR (chronic carriers): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Tuberculosis (fast and slow progression)

Natural history
SLIR

New infections progress rapidly to disease with probability p, otherwise enter long-term latency (L) with slow reactivation; treated/self-cured cases can relapse.

Vynnycky & White (2010), An Introduction to Infectious Disease Modelling, ch. 3; Blower et al. (1995), Nature Medicine 1:815-821

Show R code
## Tuberculosis (fast and slow progression)
## New infections progress rapidly to disease with probability p, otherwise enter long-term latency (L)
## with slow reactivation; treated/self-cured cases can relapse.
## Reference: Vynnycky & White (2010), An Introduction to Infectious Disease Modelling, ch. 3; Blower et al. (1995), Nature Medicine 1:815-821
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, L, I, R
## Flows (time unit: years):
##   (outside) -> S  : (mu * N + muTB * I)
##   S -> I          : p * lam * S
##   S -> L          : (1 - p) * lam * S
##   L -> I          : v * L
##   I -> R          : r * I
##   R -> I          : relapse * R
##   S -> (outside)  : mu * S
##   L -> (outside)  : mu * L
##   I -> (outside)  : mu * I
##   I -> (outside)  : muTB * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 10,        # effective contacts per case per year
  p = 0.1,          # proportion progressing rapidly to disease
  v = 0.001,        # reactivation rate per year
  r = 0.5,          # rate of cure / self-cure per year
  relapse = 0.002,  # relapse rate per year
  mu = 1 / 70,      # background death rate per year
  muTB = 0.2        # TB mortality rate per year
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 99990,  # Susceptible
  L = 0,      # Latent infection
  I = 10,     # Active TB
  R = 0       # Recovered
)

# ---- Model equations -------------------------------------------------
tb_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + L + I + R

    # Derived quantities
    lam <- beta * I / N  # force of infection

    # Flows recorded as outputs
    fast_cases <- p * lam * S

    # Rates of change
    dS <- (mu * N + muTB * I) - p * lam * S - (1 - p) * lam * S - mu * S
    dL <- (1 - p) * lam * S - v * L - mu * L
    dI <- p * lam * S + v * L - r * I + relapse * R - mu * I - muTB * I
    dR <- r * I - relapse * R - mu * R

    list(c(dS, dL, dI, dR), c(fast_cases = fast_cases))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = tb_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", L = "#E8C468", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Tuberculosis (fast and slow progression)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("fast_cases")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "Tuberculosis (fast and slow progression): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

HIV with disease stages

Natural history
SI1I2AD

Acute (I1), chronic (I2) and AIDS (A) stages with stage-specific infectiousness; AIDS deaths leave the population.

Anderson & May (1991), Infectious Diseases of Humans, ch. 11

Show R code
## HIV with disease stages
## Acute (I1), chronic (I2) and AIDS (A) stages with stage-specific infectiousness; AIDS deaths leave the population.
## Reference: Anderson & May (1991), Infectious Diseases of Humans, ch. 11
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I1, I2, A, D
## Flows (time unit: years):
##   (outside) -> S   : mu * N
##   S -> I1          : lam * S
##   I1 -> I2         : k1 * I1
##   I2 -> A          : k2 * I2
##   A -> D           : alpha * A
##   S -> (outside)   : mu * S
##   I1 -> (outside)  : mu * I1
##   I2 -> (outside)  : mu * I2
##   A -> (outside)   : mu * A

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp = 2,       # partner change rate per year
  b1 = 0.1,     # transmission probability per partnership, acute
  b2 = 0.02,    # transmission probability per partnership, chronic
  b3 = 0.05,    # transmission probability per partnership, AIDS
  k1 = 4,       # rate of leaving acute stage per year (3 months)
  k2 = 1 / 9,   # rate of progression to AIDS per year
  alpha = 0.5,  # AIDS death rate per year
  mu = 1 / 35   # rate of entering/leaving the sexually active population
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 99900,  # Susceptible
  I1 = 100,   # Acute HIV
  I2 = 0,     # Chronic HIV
  A = 0,      # AIDS
  D = 0       # AIDS deaths
)

# ---- Model equations -------------------------------------------------
hiv_stages_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I1 + I2 + A

    # Derived quantities
    lam <- cp * (b1 * I1 + b2 * I2 + b3 * A) / N  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S
    aids_deaths <- alpha * A

    # Rates of change
    dS <- mu * N - lam * S - mu * S
    dI1 <- lam * S - k1 * I1 - mu * I1
    dI2 <- k1 * I1 - k2 * I2 - mu * I2
    dA <- k2 * I2 - alpha * A - mu * A
    dD <- alpha * A

    list(c(dS, dI1, dI2, dA, dD), c(incidence = incidence, aids_deaths = aids_deaths))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hiv_stages_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I1 = "#B71C1C", I2 = "#B71C1C", A = "#E57373", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "HIV with disease stages")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence", "aids_deaths")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "HIV with disease stages: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Ebola (community, hospital, funeral)

Natural history
SEIHFuR

Transmission in the community (I), in hospital (H) and at funerals (F); structure after Legrand et al. Parameter values are illustrative.

Legrand et al. (2007), Epidemiology and Infection 135:610-621

Show R code
## Ebola (community, hospital, funeral)
## Transmission in the community (I), in hospital (H) and at funerals (F); structure after Legrand et al.
## Parameter values are illustrative.
## Reference: Legrand et al. (2007), Epidemiology and Infection 135:610-621
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, H, Fu, R
## Flows (time unit: days):
##   S -> E           : lam * S
##   E -> I           : alpha * E
##   I -> H           : gammaH * theta * I
##   I -> Fu          : gammaD * (1 - theta) * delta * I
##   I -> R           : gammaI * (1 - theta) * (1 - delta) * I
##   H -> Fu          : gammaDH * delta * H
##   H -> R           : gammaIH * (1 - delta) * H
##   Fu -> (outside)  : gammaF * Fu

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 300, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  betaI = 0.3,        # transmission rate in the community
  betaH = 0.05,       # transmission rate in hospital
  betaF = 0.4,        # transmission rate at funerals
  alpha = 1 / 7,      # 1/(incubation period)
  theta = 0.5,        # proportion of cases hospitalised
  delta = 0.7,        # case fatality ratio
  gammaH = 1 / 5,     # 1/(onset to hospitalisation)
  gammaD = 1 / 9.6,   # 1/(onset to death)
  gammaI = 1 / 10,    # 1/(onset to recovery)
  gammaDH = 1 / 4.6,  # 1/(hospitalisation to death)
  gammaIH = 1 / 5,    # 1/(hospitalisation to recovery)
  gammaF = 1 / 2      # 1/(death to burial)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 99999,  # Susceptible
  E = 1,      # Exposed
  I = 0,      # Infectious (community)
  H = 0,      # Hospitalised
  Fu = 0,     # Dead, not yet buried (funeral)
  R = 0       # Removed
)

# ---- Model equations -------------------------------------------------
ebola_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + H + Fu + R

    # Derived quantities
    lam <- (betaI * I + betaH * H + betaF * Fu) / N  # force of infection

    # Flows recorded as outputs
    onsets <- alpha * E
    burials <- gammaF * Fu

    # Rates of change
    dS <- -lam * S
    dE <- lam * S - alpha * E
    dI <- alpha * E - gammaH * theta * I - gammaD * (1 - theta) * delta * I - gammaI * (1 - theta) * (1 - delta) * I
    dH <- gammaH * theta * I - gammaDH * delta * H - gammaIH * (1 - delta) * H
    dFu <- gammaD * (1 - theta) * delta * I + gammaDH * delta * H - gammaF * Fu
    dR <- gammaI * (1 - theta) * (1 - delta) * I + gammaIH * (1 - delta) * H

    list(c(dS, dE, dI, dH, dFu, dR), c(onsets = onsets, burials = burials))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = ebola_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", H = "#6A2C9C", Fu = "#5D4037", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Ebola (community, hospital, funeral)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("onsets", "burials")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "Ebola (community, hospital, funeral): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Cholera SIWR (water reservoir)

Transmission routes
SIRW

Direct transmission plus indirect transmission through a contaminated water compartment W (W is a normalised pathogen concentration, so it is excluded from N).

Tien & Earn (2010), Bull. Math. Biol. 72:1506-1533

Show R code
## Cholera SIWR (water reservoir)
## Direct transmission plus indirect transmission through a contaminated water compartment W
## (W is a normalised pathogen concentration, so it is excluded from N).
## Reference: Tien & Earn (2010), Bull. Math. Biol. 72:1506-1533
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, W
## Flows (time unit: days):
##   S -> I          : lam * S
##   I -> R          : gamma * I
##   (outside) -> W  : xi * I / N
##   W -> (outside)  : xi * W

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  betaI = 0.1,    # direct transmission rate per day
  betaW = 0.4,    # water-borne transmission rate per day
  gamma = 1 / 5,  # recovery rate
  xi = 1 / 30     # shedding / decay rate of pathogen in water
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # Susceptible
  I = 10,    # Infectious
  R = 0,     # Recovered
  W = 0      # Water (pathogen)
)

# ---- Model equations -------------------------------------------------
cholera_siwr_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lam <- betaI * I / N + betaW * W  # force of infection

    # Flows recorded as outputs
    incidence <- lam * S

    # Rates of change
    dS <- -lam * S
    dI <- lam * S - gamma * I
    dR <- gamma * I
    dW <- xi * I / N - xi * W

    list(c(dS, dI, dR, dW), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = cholera_siwr_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", W = "#4FC3F7")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Cholera SIWR (water reservoir)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "Cholera SIWR (water reservoir): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Ross–Macdonald (malaria)

Transmission routes
ShIhSvIv

SIS humans and SI mosquitoes. Two separate populations, so totals are defined as variables.

Smith et al. (2012), PLoS Pathogens 8:e1002588; Anderson & May (1991), Infectious Diseases of Humans, ch. 14

Show R code
## Ross–Macdonald (malaria)
## SIS humans and SI mosquitoes. Two separate populations, so totals are defined as variables.
## Reference: Smith et al. (2012), PLoS Pathogens 8:e1002588; Anderson & May (1991), Infectious Diseases of Humans, ch. 14
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Ih, Sv, Iv
## Flows (time unit: days):
##   Sh -> Ih         : lam_h * Sh
##   Ih -> Sh         : r * Ih
##   (outside) -> Sv  : g * Nv
##   Sv -> Iv         : lam_v * Sv
##   Sv -> (outside)  : g * Sv
##   Iv -> (outside)  : g * Iv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 730, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a = 0.3,      # human bites per mosquito per day
  b = 0.5,      # probability an infectious bite infects a human
  cv = 0.5,     # probability a mosquito is infected biting an infected human
  r = 1 / 100,  # human recovery rate per day
  g = 1 / 10    # mosquito death rate per day
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 9990,   # Susceptible humans
  Ih = 10,     # Infected humans
  Sv = 50000,  # Susceptible mosquitoes
  Iv = 0       # Infectious mosquitoes
)

# ---- Model equations -------------------------------------------------
ross_macdonald_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Derived quantities
    Nh <- Sh + Ih              # human population
    Nv <- Sv + Iv              # mosquito population
    lam_h <- a * b * Iv / Nh   # force of infection on humans
    lam_v <- a * cv * Ih / Nh  # force of infection on mosquitoes

    # Flows recorded as outputs
    human_incidence <- lam_h * Sh

    # Rates of change
    dSh <- -lam_h * Sh + r * Ih
    dIh <- lam_h * Sh - r * Ih
    dSv <- g * Nv - lam_v * Sv - g * Sv
    dIv <- lam_v * Sv - g * Iv

    list(c(dSh, dIh, dSv, dIv), c(human_incidence = human_incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = ross_macdonald_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Ih = "#B71C1C", Sv = "#333333", Iv = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Ross–Macdonald (malaria)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("human_incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "Ross–Macdonald (malaria): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Dengue host SEIR – vector SEI

Transmission routes
ShEhIhRhSvEvIv

Human SEIR coupled to mosquito SEI with an extrinsic incubation period.

Manore et al. (2014), J. Theor. Biol. 356:174-191 (structure)

Show R code
## Dengue host SEIR – vector SEI
## Human SEIR coupled to mosquito SEI with an extrinsic incubation period.
## Reference: Manore et al. (2014), J. Theor. Biol. 356:174-191 (structure)
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Eh, Ih, Rh, Sv, Ev, Iv
## Flows (time unit: days):
##   Sh -> Eh         : lam_h * Sh
##   Eh -> Ih         : sigmaH * Eh
##   Ih -> Rh         : gammaH * Ih
##   (outside) -> Sv  : muV * Nv
##   Sv -> Ev         : lam_v * Sv
##   Ev -> Iv         : sigmaV * Ev
##   Sv -> (outside)  : muV * Sv
##   Ev -> (outside)  : muV * Ev
##   Iv -> (outside)  : muV * Iv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a = 0.5,           # bites per mosquito per day
  bh = 0.4,          # mosquito-to-human transmission probability
  bv = 0.4,          # human-to-mosquito transmission probability
  sigmaH = 1 / 5.5,  # 1/(intrinsic incubation period)
  gammaH = 1 / 5,    # human recovery rate
  sigmaV = 1 / 10,   # 1/(extrinsic incubation period)
  muV = 1 / 14       # mosquito death rate
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 99990,   # Susceptible humans
  Eh = 0,       # Exposed humans
  Ih = 10,      # Infectious humans
  Rh = 0,       # Recovered humans
  Sv = 200000,  # Susceptible mosquitoes
  Ev = 0,       # Exposed mosquitoes
  Iv = 0        # Infectious mosquitoes
)

# ---- Model equations -------------------------------------------------
dengue_vector_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Derived quantities
    Nh <- Sh + Eh + Ih + Rh    # human population
    Nv <- Sv + Ev + Iv         # mosquito population
    lam_h <- a * bh * Iv / Nh  # force of infection on humans
    lam_v <- a * bv * Ih / Nh  # force of infection on mosquitoes

    # Flows recorded as outputs
    human_cases <- sigmaH * Eh

    # Rates of change
    dSh <- -lam_h * Sh
    dEh <- lam_h * Sh - sigmaH * Eh
    dIh <- sigmaH * Eh - gammaH * Ih
    dRh <- gammaH * Ih
    dSv <- muV * Nv - lam_v * Sv - muV * Sv
    dEv <- lam_v * Sv - sigmaV * Ev - muV * Ev
    dIv <- sigmaV * Ev - muV * Iv

    list(c(dSh, dEh, dIh, dRh, dSv, dEv, dIv), c(human_cases = human_cases))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = dengue_vector_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Eh = "#E8C468", Ih = "#B71C1C", Rh = "#4E8A3E", Sv = "#333333", Ev = "#E8C468", Iv = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Dengue host SEIR – vector SEI")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("human_cases")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "Dengue host SEIR – vector SEI: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Rabies SEI (density-dependent)

Transmission routes
SEI

Wildlife rabies with density-dependent transmission (lam = beta * I, not divided by N) and logistic population regulation through density-dependent mortality.

Anderson et al. (1981), Nature 289:765-771

Show R code
## Rabies SEI (density-dependent)
## Wildlife rabies with density-dependent transmission (lam = beta * I, not divided by N)
## and logistic population regulation through density-dependent mortality.
## Reference: Anderson et al. (1981), Nature 289:765-771
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I
## Flows (time unit: years):
##   (outside) -> S  : b * N
##   S -> E          : lam * S
##   E -> I          : sigma * E
##   S -> (outside)  : d * S
##   E -> (outside)  : d * E
##   I -> (outside)  : d * I
##   I -> (outside)  : alpha * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.01)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  b = 1,        # per-capita birth rate per year
  mu = 0.5,     # baseline death rate per year
  K = 1000,     # carrying capacity
  beta = 0.08,  # density-dependent transmission coefficient per year
  sigma = 13,   # 1/(incubation period) per year (~4 weeks)
  alpha = 73    # rabies death rate per year (~5 days)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 900,  # Susceptible foxes
  E = 0,    # Incubating
  I = 5     # Rabid
)

# ---- Model equations -------------------------------------------------
rabies_density_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I

    # Derived quantities
    d <- mu + (b - mu) * N / K  # density-dependent death rate
    lam <- beta * I             # force of infection (density dependent)

    # Flows recorded as outputs
    cases <- sigma * E
    rabies_deaths <- alpha * I

    # Rates of change
    dS <- b * N - lam * S - d * S
    dE <- lam * S - sigma * E - d * E
    dI <- sigma * E - d * I - alpha * I

    list(c(dS, dE, dI), c(cases = cases, rabies_deaths = rabies_deaths))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = rabies_density_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Rabies SEI (density-dependent)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("cases", "rabies_deaths")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "Rabies SEI (density-dependent): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SIR with two age groups

Transmission routes
ScIcRcSaIaRa

Children and adults with a who-acquires-infection-from-whom (WAIFW) matrix and ageing.

Anderson & May (1991), Infectious Diseases of Humans, ch. 9; Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §3.1

Show R code
## SIR with two age groups
## Children and adults with a who-acquires-infection-from-whom (WAIFW) matrix and ageing.
## Reference: Anderson & May (1991), Infectious Diseases of Humans, ch. 9; Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §3.1
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sc, Ic, Rc, Sa, Ia, Ra
## Flows (time unit: years):
##   (outside) -> Sc  : mu * N
##   Sc -> Ic         : lam_c * Sc
##   Ic -> Rc         : gamma * Ic
##   Sa -> Ia         : lam_a * Sa
##   Ia -> Ra         : gamma * Ia
##   Sc -> Sa         : ageing * Sc
##   Ic -> Ia         : ageing * Ic
##   Rc -> Ra         : ageing * Rc
##   Sa -> (outside)  : mu_a * Sa
##   Ia -> (outside)  : mu_a * Ia
##   Ra -> (outside)  : mu_a * Ra

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.02)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  b_cc = 1000,      # transmission child -> child per year
  b_ca = 300,       # transmission adult -> child per year
  b_ac = 300,       # transmission child -> adult per year
  b_aa = 400,       # transmission adult -> adult per year
  gamma = 365 / 7,  # recovery rate per year
  ageing = 1 / 15,  # rate of moving from child to adult per year
  mu = 1 / 70       # birth rate per year (deaths in adults keep N constant)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sc = 2000,   # Susceptible children
  Ic = 10,     # Infectious children
  Rc = 17990,  # Recovered children
  Sa = 1000,   # Susceptible adults
  Ia = 0,      # Infectious adults
  Ra = 79000   # Recovered adults
)

# ---- Model equations -------------------------------------------------
sir_two_age_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sc + Ic + Rc + Sa + Ia + Ra

    # Derived quantities
    lam_c <- (b_cc * Ic + b_ca * Ia) / N  # force of infection on children
    lam_a <- (b_ac * Ic + b_aa * Ia) / N  # force of infection on adults
    mu_a <- mu * N / (Sa + Ia + Ra)       # adult death rate balancing births

    # Rates of change
    dSc <- mu * N - lam_c * Sc - ageing * Sc
    dIc <- lam_c * Sc - gamma * Ic - ageing * Ic
    dRc <- gamma * Ic - ageing * Rc
    dSa <- -lam_a * Sa + ageing * Sc - mu_a * Sa
    dIa <- lam_a * Sa - gamma * Ia + ageing * Ic - mu_a * Ia
    dRa <- gamma * Ia + ageing * Rc - mu_a * Ra

    list(c(dSc, dIc, dRc, dSa, dIa, dRa))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_two_age_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sc = "#333333", Ic = "#B71C1C", Rc = "#4E8A3E", Sa = "#333333", Ia = "#B71C1C", Ra = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SIR with two age groups")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SEIR with seasonal forcing

Transmission routes
SEIR

Sinusoidal seasonality in transmission produces multi-annual epidemic cycles (measles-like).

Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §5.2

Show R code
## SEIR with seasonal forcing
## Sinusoidal seasonality in transmission produces multi-annual epidemic cycles (measles-like).
## Reference: Keeling & Rohani (2008), Modeling Infectious Diseases in Humans and Animals, §5.2
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R
## Flows (time unit: years):
##   (outside) -> S  : mu * N
##   S -> E          : lam * S
##   E -> I          : sigma * E
##   I -> R          : gamma * I
##   S -> (outside)  : mu * S
##   E -> (outside)  : mu * E
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.01)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta0 = 1250,     # mean transmission rate per year
  beta1 = 0.25,     # amplitude of seasonality
  sigma = 365 / 8,  # 1/(latent period) per year
  gamma = 365 / 5,  # 1/(infectious period) per year
  mu = 1 / 50       # birth = death rate per year
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 6000,  # Susceptible
  E = 10,    # Exposed
  I = 10,    # Infectious
  R = 93980  # Recovered
)

# ---- Model equations -------------------------------------------------
seir_seasonal_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + R

    # Derived quantities
    beta <- beta0 * (1 + beta1 * cos(2 * pi * t))  # seasonal transmission rate
    lam <- beta * I / N                            # force of infection

    # Flows recorded as outputs
    incidence <- sigma * E

    # Rates of change
    dS <- mu * N - lam * S - mu * S
    dE <- lam * S - sigma * E - mu * E
    dI <- sigma * E - gamma * I - mu * I
    dR <- gamma * I - mu * R

    list(c(dS, dE, dI, dR), c(incidence = incidence))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seir_seasonal_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SEIR with seasonal forcing")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("incidence")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "SEIR with seasonal forcing: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

SEITR influenza-type model with antiviral treatment

Classic extensions
SEITrR

pandemic/seasonal influenza, antiviral treatment. Closed population, so N is constant. Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.

Brauer F. (2008) "Compartmental models in epidemiology", in Brauer, van den Driessche & Wu (eds), Mathematical Epidemiology, Lecture Notes in Mathematics 1945, Springer, pp. 19–79 ↗

Show R code
## SEITR influenza-type model with antiviral treatment
## pandemic/seasonal influenza, antiviral treatment.
## Closed population, so N is constant.
## Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.
## Reference: Brauer F. (2008) "Compartmental models in epidemiology", in Brauer, van den Driessche & Wu (eds), Mathematical Epidemiology, Lecture Notes in Mathematics 1945, Springer, pp. 19–79
## Source: https://doi.org/10.1007/978-3-540-78911-6_2
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, Tr, R
## Flows (time unit: days):
##   S -> E   : lambda * S
##   E -> I   : kappa * E
##   I -> Tr  : tau * I
##   I -> R   : alpha * I
##   Tr -> R  : eta * Tr

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.5,       # transmission rate (1/day) [illustrative]
  kappa = 1 / 1.5,  # progression from latent (1/day) [illustrative]
  tau = 0.3,        # treatment uptake rate (1/day) [illustrative]
  alpha = 1 / 4,    # untreated recovery rate (1/day) [illustrative]
  eta = 1 / 2.5,    # recovery rate under treatment (1/day) [illustrative]
  delta = 0.5       # relative infectiousness of treated people [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # susceptible
  E = 0,     # latent
  I = 10,    # infectious, untreated
  Tr = 0,    # infectious, under treatment
  R = 0      # recovered
)

# ---- Model equations -------------------------------------------------
sitr_treatment_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + Tr + R

    # Derived quantities
    lambda <- beta * (I + delta * Tr) / N                                   # force of infection
    R0 <- beta * (1 / (alpha + tau) + delta * tau / ((alpha + tau) * eta))  # basic reproduction number (about 1.26 with these values; set beta = 0.8 for about 2.0)

    # Rates of change
    dS <- -lambda * S
    dE <- lambda * S - kappa * E
    dI <- kappa * E - tau * I - alpha * I
    dTr <- tau * I - eta * Tr
    dR <- alpha * I + eta * Tr

    list(c(dS, dE, dI, dTr, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sitr_treatment_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", Tr = "#333333", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEITR influenza-type model with antiviral treatment")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIR with saturated (capacity-limited) treatment

Classic extensions
SIR

generic, limited treatment capacity. Mass-action (density-dependent) incidence as in the source papers. Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.

Wang W. & Ruan S. (2004) "Bifurcations in an epidemic model with constant removal rate of the infectives", J. Math. Anal. Appl. 291:775–793 ↗

Show R code
## SIR with saturated (capacity-limited) treatment
## generic, limited treatment capacity.
## Mass-action (density-dependent) incidence as in the source papers.
## Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.
## Reference: Wang W. & Ruan S. (2004) "Bifurcations in an epidemic model with constant removal rate of the infectives", J. Math. Anal. Appl. 291:775–793
## Source: https://doi.org/10.1016/j.jmaa.2003.11.043
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R
## Flows (time unit: days):
##   (outside) -> S  : Lambda
##   S -> I          : beta * I * S
##   I -> R          : gamma * I
##   I -> R          : treat_pc * I
##   S -> (outside)  : mu * S
##   I -> (outside)  : (mu + d_dis) * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3650, by = 5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  Lambda = 0.391,       # recruitment, equal to mu10000 (persons/day) [illustrative]
  mu = 1 / (70 * 365),  # natural death (1/day) [illustrative]
  beta = 3e-5,          # mass-action transmission coefficient [illustrative]
  gamma = 0.1,          # natural recovery (1/day) [illustrative]
  d_dis = 0.001,        # disease death (1/day) [illustrative]
  r_max = 0.1,          # maximal per-capita treatment rate at low prevalence (1/day) [illustrative]
  alpha_t = 0.01        # saturation constant (capacity effect) (1/person) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # susceptible
  I = 10,    # infectious
  R = 0      # removed
)

# ---- Model equations -------------------------------------------------
sir_saturated_treatment_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    treat_pc <- r_max / (1 + alpha_t * I)  # per-capita treatment rate, so the total treatment is r_maxI/(1+alpha_tI)

    # Rates of change
    dS <- Lambda - beta * I * S - mu * S
    dI <- beta * I * S - gamma * I - treat_pc * I - (mu + d_dis) * I
    dR <- gamma * I + treat_pc * I - mu * R

    list(c(dS, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_saturated_treatment_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIR with saturated (capacity-limited) treatment")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIR with saturated incidence (Capasso–Serio)

Classic extensions
SIR

cholera (Bari 1973 epidemic), generic. The incidence g(I)S with g bounded represents protective behaviour or saturation at high prevalence. Parameter values: 0 of 3 checked against the source; the rest are recalled or illustrative.

Capasso V. & Serio G. (1978) "A generalization of the Kermack–McKendrick deterministic epidemic model", Math. Biosci. 42:43–61 ↗

Show R code
## SIR with saturated incidence (Capasso–Serio)
## cholera (Bari 1973 epidemic), generic.
## The incidence g(I)S with g bounded represents protective behaviour or saturation at high prevalence.
## Parameter values: 0 of 3 checked against the source; the rest are recalled or illustrative.
## Reference: Capasso V. & Serio G. (1978) "A generalization of the Kermack–McKendrick deterministic epidemic model", Math. Biosci. 42:43–61
## Source: https://doi.org/10.1016/0025-5564(78
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R
## Flows (time unit: days):
##   S -> I  : lambda * S
##   I -> R  : gamma * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 5e-5,      # transmission coefficient [illustrative]
  alpha_s = 0.002,  # saturation ("psychological"/crowding) constant (1/person) [illustrative]
  gamma = 0.2       # recovery (1/day) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # susceptible
  I = 10,    # infectious
  R = 0      # recovered
)

# ---- Model equations -------------------------------------------------
sir_saturated_incidence_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lambda <- beta * I / (1 + alpha_s * I)  # saturating force of infection

    # Rates of change
    dS <- -lambda * S
    dI <- lambda * S - gamma * I
    dR <- gamma * I

    list(c(dS, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_saturated_incidence_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIR with saturated incidence (Capasso–Serio)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIR with aware (behaviour-changing) susceptibles

Classic extensions
SSaIR

generic emerging infection; awareness/behaviour. Awareness is triggered only by prevalence, so the disease-free state has Sa = 0 and the epidemic threshold R0 = beta/gamma is unchanged. Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.

The concept and qualitative result come from Funk S., Gilad E., Watkins C., Jansen V.A.A. (2009) "The spread of awareness and its impact on epidemic outbreaks", PNAS 106(16):6872–6877 ↗

Show R code
## SIR with aware (behaviour-changing) susceptibles
## generic emerging infection; awareness/behaviour.
## Awareness is triggered only by prevalence, so the disease-free state has Sa = 0 and the epidemic threshold R0 = beta/gamma is unchanged.
## Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.
## Reference: The concept and qualitative result come from Funk S., Gilad E., Watkins C., Jansen V.A.A. (2009) "The spread of awareness and its impact on epidemic outbreaks", PNAS 106(16):6872–6877
## Source: https://doi.org/10.1073/pnas.0810762106
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Sa, I, R
## Flows (time unit: days):
##   S -> Sa  : phi_a * I / N * S
##   Sa -> S  : omega_f * Sa
##   S -> I   : lambda * S
##   Sa -> I  : sigma_a * lambda * Sa
##   I -> R   : gamma * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.4,      # transmission rate (1/day) [illustrative]
  gamma = 0.2,     # recovery (1/day) [illustrative]
  phi_a = 2.0,     # awareness generation from contact with or news of cases (1/day) [illustrative]
  omega_f = 0.05,  # awareness fading (1/day) [illustrative]
  sigma_a = 0.3    # relative susceptibility of aware susceptibles [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # unaware susceptible
  Sa = 0,    # aware susceptible (reduced contacts)
  I = 10,    # infectious
  R = 0      # recovered
)

# ---- Model equations -------------------------------------------------
sir_awareness_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Sa + I + R

    # Derived quantities
    lambda <- beta * I / N  # force of infection

    # Rates of change
    dS <- -phi_a * I / N * S + omega_f * Sa - lambda * S
    dSa <- phi_a * I / N * S - omega_f * Sa - sigma_a * lambda * Sa
    dI <- lambda * S + sigma_a * lambda * Sa - gamma * I
    dR <- gamma * I

    list(c(dS, dSa, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_awareness_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Sa = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIR with aware (behaviour-changing) susceptibles")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SVIRS with imperfect (leaky) and waning vaccine and waning natural immunity

Classic extensions
SVIR

e.g. influenza, pertussis-like. N is constant. This differs from the existing "SVIR leaky vaccine" model in having vaccine waning, newborn vaccination and waning natural immunity. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

The structure follows Arino J., McCluskey C.C., van den Driessche P. (2003) "Global results for an epidemic model with vaccination that exhibits backward bifurcation", SIAM J. Appl. Math. 64:260–276 ↗

Show R code
## SVIRS with imperfect (leaky) and waning vaccine and waning natural immunity
## e.g. influenza, pertussis-like.
## N is constant. This differs from the existing "SVIR leaky vaccine" model in having vaccine waning, newborn vaccination and waning natural immunity.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: The structure follows Arino J., McCluskey C.C., van den Driessche P. (2003) "Global results for an epidemic model with vaccination that exhibits backward bifurcation", SIAM J. Appl. Math. 64:260–276
## Source: https://doi.org/10.1137/S0036139902413829
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, V, I, R
## Flows (time unit: years):
##   (outside) -> S  : (1 - p_nb) * mu * N
##   (outside) -> V  : p_nb * mu * N
##   S -> V          : nu_v * S
##   V -> S          : theta_v * V
##   S -> I          : lambda * S
##   V -> I          : sigma_v * lambda * V
##   I -> R          : gamma * I
##   R -> S          : omega_r * R
##   S -> (outside)  : mu * S
##   V -> (outside)  : mu * V
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu = 1 / 70,      # birth = death rate (1/yr) [illustrative]
  p_nb = 0.5,       # fraction of newborns vaccinated [illustrative]
  nu_v = 0.1,       # vaccination rate of susceptibles (1/yr) [illustrative]
  theta_v = 0.2,    # vaccine waning (mean 5 y) (1/yr) [illustrative]
  sigma_v = 0.2,    # relative susceptibility of vaccinees (vaccine efficacy 80%) [illustrative]
  beta = 100,       # transmission rate (1/yr) [illustrative]
  gamma = 365 / 7,  # recovery (7-day infectious period) (1/yr) [illustrative]
  omega_r = 0.1     # natural immunity waning (1/yr) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9000,  # susceptible
  V = 0,     # vaccinated (partially protected)
  I = 10,    # infectious
  R = 990    # recovered
)

# ---- Model equations -------------------------------------------------
svirs_waning_vaccine_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + V + I + R

    # Derived quantities
    lambda <- beta * I / N  # force of infection

    # Rates of change
    dS <- (1 - p_nb) * mu * N - nu_v * S + theta_v * V - lambda * S + omega_r * R - mu * S
    dV <- p_nb * mu * N + nu_v * S - theta_v * V - sigma_v * lambda * V - mu * V
    dI <- lambda * S + sigma_v * lambda * V - gamma * I - mu * I
    dR <- gamma * I - omega_r * R - mu * R

    list(c(dS, dV, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = svirs_waning_vaccine_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", V = "#2457C5", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SVIRS with imperfect (leaky) and waning vaccine and waning natural immunity")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIS with vaccination (Kribs-Zaleta & Velasco-Hernández)

Classic extensions
SIV

generic SIS infection with imperfect waning vaccine. This is the canonical small model where an endemic equilibrium coexists with a stable disease-free state when the vaccine-reduced R_phi < 1. Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.

Kribs-Zaleta C.M. & Velasco-Hernández J.X. (2000) "A simple vaccination model with multiple endemic states", Math. Biosci. 164:183–201 ↗

Show R code
## SIS with vaccination (Kribs-Zaleta & Velasco-Hernández)
## generic SIS infection with imperfect waning vaccine.
## This is the canonical small model where an endemic equilibrium coexists with a stable disease-free state when the vaccine-reduced R_phi < 1.
## Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.
## Reference: Kribs-Zaleta C.M. & Velasco-Hernández J.X. (2000) "A simple vaccination model with multiple endemic states", Math. Biosci. 164:183–201
## Source: https://doi.org/10.1016/S0025-5564(00
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, V
## Flows (time unit: years):
##   (outside) -> S  : mu * N
##   S -> I          : lambda * S
##   I -> S          : gamma * I
##   S -> V          : phi_v * S
##   V -> S          : theta_v * V
##   V -> I          : sigma_v * lambda * V
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   V -> (outside)  : mu * V

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu = 0.02,      # birth/death (turnover) (1/yr) [illustrative]
  beta = 20,      # transmission (1/yr) [illustrative]
  gamma = 10,     # recovery, returning to susceptible (1/yr) [illustrative]
  phi_v = 1.0,    # vaccination rate (1/yr) [illustrative]
  theta_v = 0.1,  # vaccine waning (1/yr) [illustrative]
  sigma_v = 0.3   # relative susceptibility of the vaccinated [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9900,  # susceptible
  I = 100,   # infectious
  V = 0      # vaccinated
)

# ---- Model equations -------------------------------------------------
siv_backward_bifurcation_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + V

    # Derived quantities
    lambda <- beta * I / N

    # Rates of change
    dS <- mu * N - lambda * S + gamma * I - phi_v * S + theta_v * V - mu * S
    dI <- lambda * S - gamma * I + sigma_v * lambda * V - mu * I
    dV <- phi_v * S - theta_v * V - sigma_v * lambda * V - mu * V

    list(c(dS, dI, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = siv_backward_bifurcation_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SIS with vaccination (Kribs-Zaleta & Velasco-Hernández)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

vaccination with waning and booster doses

Classic extensions
SV1WV2IR

pertussis, COVID-19, HPV-like. N is constant. Compare b_rate = 0 against b_rate > 0. Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

Illustrative structure. The rationale for waning and boosting follows Hethcote 2000 (SIAM Rev ↗

Show R code
## vaccination with waning and booster doses
## pertussis, COVID-19, HPV-like.
## N is constant. Compare b_rate = 0 against b_rate > 0.
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Illustrative structure. The rationale for waning and boosting follows Hethcote 2000 (SIAM Rev
## Source: https://doi.org/10.1137/S0036144500371907
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, V1, W, V2, I, R
## Flows (time unit: years):
##   (outside) -> S   : (1 - p_nb) * mu * N
##   (outside) -> V1  : p_nb * mu * N
##   V1 -> W          : omega1 * V1
##   V2 -> W          : omega2 * V2
##   R -> W           : omega_r * R
##   W -> V2          : b_rate * W
##   S -> I           : lambda * S
##   V1 -> I          : eps1 * lambda * V1
##   V2 -> I          : eps2 * lambda * V2
##   W -> I           : eps_w * lambda * W
##   I -> R           : gamma * I
##   S -> (outside)   : mu * S
##   V1 -> (outside)  : mu * V1
##   W -> (outside)   : mu * W
##   V2 -> (outside)  : mu * V2
##   I -> (outside)   : mu * I
##   R -> (outside)   : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu = 1 / 70,       # birth/death (1/yr) [illustrative]
  p_nb = 0.9,        # infant primary coverage [illustrative]
  omega1 = 1 / 4,    # waning of primary protection (1/yr) [illustrative]
  omega2 = 1 / 10,   # waning after booster (1/yr) [illustrative]
  omega_r = 1 / 20,  # waning of infection-derived immunity, flowing into W (1/yr) [illustrative]
  b_rate = 0.2,      # booster uptake among waned (1/yr) [illustrative]
  eps1 = 0.1,        # relative susceptibility in V1 [illustrative]
  eps2 = 0.1,        # relative susceptibility in V2 [illustrative]
  eps_w = 0.5,       # relative susceptibility of the waned [illustrative]
  beta = 300,        # transmission (1/yr) [illustrative]
  gamma = 365 / 21   # recovery (1/yr) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9000,  # never vaccinated, never infected
  V1 = 0,    # primary-vaccinated (high protection)
  W = 0,     # waned (partially susceptible)
  V2 = 0,    # boosted
  I = 10,    # infectious
  R = 990    # recovered
)

# ---- Model equations -------------------------------------------------
sirv_booster_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + V1 + W + V2 + I + R

    # Derived quantities
    lambda <- beta * I / N

    # Rates of change
    dS <- (1 - p_nb) * mu * N - lambda * S - mu * S
    dV1 <- p_nb * mu * N - omega1 * V1 - eps1 * lambda * V1 - mu * V1
    dW <- omega1 * V1 + omega2 * V2 + omega_r * R - b_rate * W - eps_w * lambda * W - mu * W
    dV2 <- -omega2 * V2 + b_rate * W - eps2 * lambda * V2 - mu * V2
    dI <- lambda * S + eps1 * lambda * V1 + eps2 * lambda * V2 + eps_w * lambda * W - gamma * I - mu * I
    dR <- -omega_r * R + gamma * I - mu * R

    list(c(dS, dV1, dW, dV2, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sirv_booster_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", V1 = "#2457C5", W = "#A5D6A7", V2 = "#2457C5", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "vaccination with waning and booster doses")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SEIQHR quarantine and isolation (Gumel et al. 2004, SARS)

Classic extensions
SEQIHRDd

SARS 2003 (Toronto, Hong Kong, Singapore, Beijing). The headline result is that optimal isolation of symptomatic cases is more effective than sub-optimal isolation combined with quarantine (search summary of the abstract). Parameter values: 0 of 14 checked against the source; the rest are recalled or illustrative.

Gumel A.B. et al. (2004) "Modelling strategies for controlling SARS outbreaks", Proc. R. Soc. B 271:2223–2232 ↗

Show R code
## SEIQHR quarantine and isolation (Gumel et al. 2004, SARS)
## SARS 2003 (Toronto, Hong Kong, Singapore, Beijing).
## The headline result is that optimal isolation of symptomatic cases is more effective than sub-optimal isolation combined with quarantine (search summary of the abstract).
## Parameter values: 0 of 14 checked against the source; the rest are recalled or illustrative.
## Reference: Gumel A.B. et al. (2004) "Modelling strategies for controlling SARS outbreaks", Proc. R. Soc. B 271:2223–2232
## Source: https://doi.org/10.1098/rspb.2004.2800
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, Q, I, H, R, Dd
## Flows (time unit: days):
##   (outside) -> S  : Pi_b
##   S -> E          : lambda * S
##   E -> Q          : gamma1 * E
##   E -> I          : kappa1 * E
##   Q -> H          : kappa2 * Q
##   I -> H          : gamma2 * I
##   I -> R          : sigma1 * I
##   H -> R          : sigma2 * H
##   I -> Dd         : d1 * I
##   H -> Dd         : d2 * H
##   S -> (outside)  : mu * S
##   E -> (outside)  : mu * E
##   Q -> (outside)  : mu * Q
##   I -> (outside)  : mu * I
##   H -> (outside)  : mu * H
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  Pi_b = 136,           # recruitment (recalled; or set to muN0) (persons/day)
  mu = 1 / (70 * 365),  # natural death (1/day) [illustrative]
  beta = 0.2,           # transmission rate (1/day) [recalled]
  eps_E = 0.3,          # relative infectiousness of E [recalled]
  eps_Q = 0,            # relative infectiousness of Q [recalled]
  eps_H = 0.36,         # relative infectiousness of the isolated (Toronto hospital transmission) [recalled]
  gamma1 = 0.1,         # quarantine rate of exposed (1/day) [recalled]
  gamma2 = 0.5,         # isolation rate of symptomatic (1/day) [recalled]
  kappa1 = 0.1,         # progression E→I (1/day) [recalled]
  kappa2 = 0.125,       # progression Q→H (1/day) [recalled]
  sigma1 = 0.0337,      # recovery of I (1/day) [recalled]
  sigma2 = 0.0386,      # recovery of H (1/day) [recalled]
  d1 = 0.0068,          # SARS death rate in I (1/day) [recalled]
  d2 = 0.0045           # SARS death rate in H (1/day) [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 4000000,  # susceptible
  E = 10,       # exposed/asymptomatic, mildly infectious
  Q = 0,        # quarantined exposed
  I = 1,        # symptomatic infectious
  H = 0,        # isolated/hospitalised (Gumel's J)
  R = 0,        # recovered
  Dd = 0        # cumulative SARS deaths
)

# ---- Model equations -------------------------------------------------
seiqhr_sars_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + Q + I + H + R

    # Derived quantities
    lambda <- beta * (I + eps_E * E + eps_Q * Q + eps_H * H) / N

    # Rates of change
    dS <- Pi_b - lambda * S - mu * S
    dE <- lambda * S - gamma1 * E - kappa1 * E - mu * E
    dQ <- gamma1 * E - kappa2 * Q - mu * Q
    dI <- kappa1 * E - gamma2 * I - sigma1 * I - d1 * I - mu * I
    dH <- kappa2 * Q + gamma2 * I - sigma2 * H - d2 * H - mu * H
    dR <- sigma1 * I + sigma2 * H - mu * R
    dDd <- d1 * I + d2 * H

    list(c(dS, dE, dQ, dI, dH, dR, dDd))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = seiqhr_sars_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", Q = "#00838F", I = "#B71C1C", H = "#6A2C9C", R = "#4E8A3E", Dd = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SEIQHR quarantine and isolation (Gumel et al. 2004, SARS)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

sensitive and resistant strains with treatment and de novo resistance (single drug)

Classic extensions
XYwYr

bacterial infection with antibiotic treatment. Resistance takes over when f·h > Δr, as the ρ equation shows. Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Bonhoeffer S., Lipsitch M., Levin B.R. (1997) "Evaluating treatment protocols to prevent antibiotic resistance", PNAS 94(22):12106–12111 ↗

Show R code
## sensitive and resistant strains with treatment and de novo resistance (single drug)
## bacterial infection with antibiotic treatment.
## Resistance takes over when f·h > Δr, as the ρ equation shows.
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
## Reference: Bonhoeffer S., Lipsitch M., Levin B.R. (1997) "Evaluating treatment protocols to prevent antibiotic resistance", PNAS 94(22):12106–12111
## Source: https://doi.org/10.1073/pnas.94.22.12106
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: X, Yw, Yr
## Flows (time unit: weeks):
##   (outside) -> X   : lambda_in
##   X -> (outside)   : d_x * X
##   X -> Yw          : b_tr * Yw * X
##   X -> Yr          : b_tr * Yr * X
##   Yw -> X          : (r_w + f_tr * h_max * (1 - s_res)) * Yw
##   Yw -> Yr         : f_tr * h_max * s_res * Yw
##   Yr -> X          : r_r * Yr
##   Yw -> (outside)  : c_inf * Yw
##   Yr -> (outside)  : c_inf * Yr

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # weeks

# ---- Parameters ------------------------------------------------------
parameters <- c(
  lambda_in = 100,  # entry of uninfected hosts (hosts/time)
  d_x = 1,          # death/removal of uninfected (1/time)
  c_inf = 1.5,      # death/removal of infected hosts (1/time)
  b_tr = 0.05,      # mass-action transmission [illustrative]
  h_max = 1,        # cure rate under treatment (1/time)
  f_tr = 0.5,       # fraction of infected treated [illustrative]
  s_res = 0.001,    # fraction of treated wild-type infections that acquire resistance
  r_w = 0,          # spontaneous recovery, wild-type (1/time)
  r_r = 0.1         # spontaneous recovery, resistant (cost of resistance Δr = r_r − r_w) (1/time)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  X = 99,  # uninfected hosts
  Yw = 1,  # infected with wild-type (sensitive)
  Yr = 0   # infected with resistant strain
)

# ---- Model equations -------------------------------------------------
amr_bonhoeffer_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- X + Yw + Yr

    # Derived quantities
    res_frac <- Yr / (Yw + Yr)  # proportion of infections that are resistant (ρ)

    # Rates of change
    dX <- lambda_in - d_x * X - b_tr * Yw * X - b_tr * Yr * X + (r_w + f_tr * h_max * (1 - s_res)) * Yw + r_r * Yr
    dYw <- b_tr * Yw * X - (r_w + f_tr * h_max * (1 - s_res)) * Yw - f_tr * h_max * s_res * Yw - c_inf * Yw
    dYr <- b_tr * Yr * X + f_tr * h_max * s_res * Yw - r_r * Yr - c_inf * Yr

    list(c(dX, dYw, dYr))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = amr_bonhoeffer_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(X = "#333333", Yw = "#333333", Yr = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (weeks)", ylab = "Number of individuals",
        main = "sensitive and resistant strains with treatment and de novo resistance (single drug)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

hospital colonisation with sensitive and resistant bacteria and patient turnover

Classic extensions
XScRc

nosocomial antibiotic-resistant bacteria (e.g. VRE, MRSA). Total admissions equal total discharges, so N is constant. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Lipsitch M., Bergstrom C.T., Levin B.R. (2000) "The epidemiology of antibiotic resistance in hospitals: paradoxes and prescriptions", PNAS 97(4):1938–1943 ↗

Show R code
## hospital colonisation with sensitive and resistant bacteria and patient turnover
## nosocomial antibiotic-resistant bacteria (e.g. VRE, MRSA).
## Total admissions equal total discharges, so N is constant.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Lipsitch M., Bergstrom C.T., Levin B.R. (2000) "The epidemiology of antibiotic resistance in hospitals: paradoxes and prescriptions", PNAS 97(4):1938–1943
## Source: https://doi.org/10.1073/pnas.97.4.1938
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: X, Sc, Rc
## Flows (time unit: days):
##   (outside) -> X   : (1 - m_s - m_r) * mu_d * N
##   (outside) -> Sc  : m_s * mu_d * N
##   (outside) -> Rc  : m_r * mu_d * N
##   X -> Sc          : beta_s * Sc / N * X
##   X -> Rc          : beta_r * Rc / N * X
##   Sc -> X          : (tau1 + tau2 + gamma) * Sc
##   Rc -> X          : (tau2 + gamma) * Rc
##   X -> (outside)   : mu_d * X
##   Sc -> (outside)  : mu_d * Sc
##   Rc -> (outside)  : mu_d * Rc

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu_d = 0.1,    # discharge rate = admission rate (10-day stay) (1/day) [illustrative]
  m_s = 0.10,    # fraction admitted colonised with sensitive [illustrative]
  m_r = 0.01,    # fraction admitted colonised with resistant [illustrative]
  beta_s = 1.0,  # transmission, sensitive (1/day) [illustrative]
  beta_r = 1.0,  # transmission, resistant (1/day) [illustrative]
  tau1 = 0.1,    # treatment rate with drug 1 (clears sensitive only) (1/day) [illustrative]
  tau2 = 0.05,   # treatment rate with drug 2 (clears both) (1/day) [illustrative]
  gamma = 0.03   # spontaneous clearance (1/day) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  X = 90,  # uncolonised patients
  Sc = 9,  # colonised with sensitive strain
  Rc = 1   # colonised with resistant strain
)

# ---- Model equations -------------------------------------------------
amr_hospital_lipsitch_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- X + Sc + Rc

    # Derived quantities
    prop_res <- Rc / (Sc + Rc)

    # Rates of change
    dX <- (1 - m_s - m_r) * mu_d * N - beta_s * Sc / N * X - beta_r * Rc / N * X + (tau1 + tau2 + gamma) * Sc + (tau2 + gamma) * Rc - mu_d * X
    dSc <- m_s * mu_d * N + beta_s * Sc / N * X - (tau1 + tau2 + gamma) * Sc - mu_d * Sc
    dRc <- m_r * mu_d * N + beta_r * Rc / N * X - (tau2 + gamma) * Rc - mu_d * Rc

    list(c(dX, dSc, dRc))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = amr_hospital_lipsitch_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(X = "#333333", Sc = "#333333", Rc = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "hospital colonisation with sensitive and resistant bacteria and patient turnover")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

two-strain SIR with partial cross-immunity (reduced susceptibility)

Classic extensions
SI1I2R1R2J1J2R12

influenza strains, dengue serotypes (without ADE), RSV. There are 8 compartments and no simultaneous coinfection. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Keeling M.J. & Rohani P. (2008), Chapter 4 ("Multi-pathogen / multi-host"), Section 4.1 and companion Program 4.1 (from memory; the companion site was unreachable). Original framework: Castillo-Chavez C., Hethcote H.W., Andreasen V., Levin S.A., Liu W.M.… ↗

Show R code
## two-strain SIR with partial cross-immunity (reduced susceptibility)
## influenza strains, dengue serotypes (without ADE), RSV.
## There are 8 compartments and no simultaneous coinfection.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Keeling M.J. & Rohani P. (2008), Chapter 4 ("Multi-pathogen / multi-host"), Section 4.1 and companion Program 4.1 (from memory; the companion site was unreachable). Original framework: Castillo-Chavez C., Hethcote H.W., Andreasen V., Levin S.A., Liu W.M.…
## Source: https://doi.org/10.1007/BF00275810
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I1, I2, R1, R2, J1, J2, R12
## Flows (time unit: days):
##   (outside) -> S    : mu * N
##   S -> I1           : lambda1 * S
##   S -> I2           : lambda2 * S
##   I1 -> R1          : gamma1 * I1
##   I2 -> R2          : gamma2 * I2
##   R1 -> J2          : sigma12 * lambda2 * R1
##   R2 -> J1          : sigma21 * lambda1 * R2
##   J1 -> R12         : gamma1 * J1
##   J2 -> R12         : gamma2 * J2
##   S -> (outside)    : mu * S
##   I1 -> (outside)   : mu * I1
##   I2 -> (outside)   : mu * I2
##   R1 -> (outside)   : mu * R1
##   R2 -> (outside)   : mu * R2
##   J1 -> (outside)   : mu * J1
##   J2 -> (outside)   : mu * J2
##   R12 -> (outside)  : mu * R12

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu = 1 / (50 * 365),  # birth/death (1/day) [illustrative]
  beta1 = 0.5,          # strain 1 transmission (1/day) [illustrative]
  beta2 = 0.45,         # strain 2 transmission (1/day) [illustrative]
  gamma1 = 1 / 7,       # recovery, strain 1 (1/day) [illustrative]
  gamma2 = 1 / 7,       # recovery, strain 2 (1/day) [illustrative]
  sigma12 = 0.5,        # susceptibility to strain 2 after strain 1 (1 = no cross-immunity, 0 = full) [illustrative]
  sigma21 = 0.5,        # susceptibility to strain 1 after strain 2 [illustrative]
  N0 = 100000           # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.9 * N0    # naive to both
  I1 <- 10         # first infection, strain 1
  I2 <- 10         # first infection, strain 2
  R1 <- 0.05 * N0  # recovered from 1, susceptible to 2
  R2 <- 0.05 * N0  # recovered from 2, susceptible to 1
  J1 <- 0          # infected with strain 1 after recovering from 2
  J2 <- 0          # infected with strain 2 after recovering from 1
  R12 <- 0         # immune to both
  c(S = S, I1 = I1, I2 = I2, R1 = R1, R2 = R2, J1 = J1, J2 = J2, R12 = R12)
})

# ---- Model equations -------------------------------------------------
two_strain_cross_immunity_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I1 + I2 + R1 + R2 + J1 + J2 + R12

    # Derived quantities
    lambda1 <- beta1 * (I1 + J1) / N
    lambda2 <- beta2 * (I2 + J2) / N

    # Rates of change
    dS <- mu * N - lambda1 * S - lambda2 * S - mu * S
    dI1 <- lambda1 * S - gamma1 * I1 - mu * I1
    dI2 <- lambda2 * S - gamma2 * I2 - mu * I2
    dR1 <- gamma1 * I1 - sigma12 * lambda2 * R1 - mu * R1
    dR2 <- gamma2 * I2 - sigma21 * lambda1 * R2 - mu * R2
    dJ1 <- sigma21 * lambda1 * R2 - gamma1 * J1 - mu * J1
    dJ2 <- sigma12 * lambda2 * R1 - gamma2 * J2 - mu * J2
    dR12 <- gamma1 * J1 + gamma2 * J2 - mu * R12

    list(c(dS, dI1, dI2, dR1, dR2, dJ1, dJ2, dR12))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = two_strain_cross_immunity_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I1 = "#B71C1C", I2 = "#B71C1C", R1 = "#4E8A3E", R2 = "#4E8A3E", J1 = "#333333", J2 = "#333333", R12 = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "two-strain SIR with partial cross-immunity (reduced susceptibility)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

two-strain superinfection (virulence evolution)

Classic extensions
XY1Y2

evolution of virulence; generic parasite. Without superinfection (s_sup = 0) the strain with the highest R0 = b·(Λ/μ)/(μ+v) excludes the other. Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.

Nowak M.A. & May R.M. (1994) "Superinfection and the evolution of parasite virulence", Proc. R. Soc. Lond. B 255:81–89 ↗

Show R code
## two-strain superinfection (virulence evolution)
## evolution of virulence; generic parasite.
## Without superinfection (s_sup = 0) the strain with the highest R0 = b·(Λ/μ)/(μ+v) excludes the other.
## Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.
## Reference: Nowak M.A. & May R.M. (1994) "Superinfection and the evolution of parasite virulence", Proc. R. Soc. Lond. B 255:81–89
## Source: https://doi.org/10.1098/rspb.1994.0012
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: X, Y1, Y2
## Flows (time unit: years):
##   (outside) -> X   : Lambda
##   X -> Y1          : b1 * Y1 * X
##   X -> Y2          : b2 * Y2 * X
##   Y2 -> Y1         : s_sup * b1 * Y1 * Y2
##   X -> (outside)   : mu * X
##   Y1 -> (outside)  : (mu + v1) * Y1
##   Y2 -> (outside)  : (mu + v2) * Y2

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  Lambda = 100,  # host recruitment (hosts/yr) [illustrative]
  mu = 0.1,      # natural host death (1/yr) [illustrative]
  b1 = 0.005,    # transmission, strain 1 [illustrative]
  b2 = 0.004,    # transmission, strain 2 [illustrative]
  v1 = 0.5,      # virulence (extra death), strain 1 (1/yr) [illustrative]
  v2 = 0.2,      # virulence, strain 2 (1/yr) [illustrative]
  s_sup = 1.0    # superinfection coefficient (strain 1 superinfecting Y2 hosts) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  X = 990,  # uninfected hosts
  Y1 = 5,   # infected with the more virulent strain 1
  Y2 = 5    # infected with the less virulent strain 2
)

# ---- Model equations -------------------------------------------------
superinfection_nowak_may_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- X + Y1 + Y2

    # Rates of change
    dX <- Lambda - b1 * Y1 * X - b2 * Y2 * X - mu * X
    dY1 <- b1 * Y1 * X + s_sup * b1 * Y1 * Y2 - (mu + v1) * Y1
    dY2 <- b2 * Y2 * X - s_sup * b1 * Y1 * Y2 - (mu + v2) * Y2

    list(c(dX, dY1, dY2))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = superinfection_nowak_may_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(X = "#333333", Y1 = "#333333", Y2 = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "two-strain superinfection (virulence evolution)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIR with reinfection of recovered (partial immunity; reinfection threshold)

Classic extensions
SIRV

TB, pertussis, RSV, malaria-like partial immunity. Gomes et al. report that above the reinfection threshold reinfection dominates, prevalence is high and vaccination fails to protect (abstract via repository). Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.

Gomes M.G.M., White L.J., Medley G.F. (2004) "Infection, reinfection, and vaccination under suboptimal immune protection: epidemiological perspectives", J. Theor. Biol. 228(4):539–549 ↗

Show R code
## SIR with reinfection of recovered (partial immunity; reinfection threshold)
## TB, pertussis, RSV, malaria-like partial immunity.
## Gomes et al. report that above the reinfection threshold reinfection dominates, prevalence is high and vaccination fails to protect (abstract via repository).
## Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.
## Reference: Gomes M.G.M., White L.J., Medley G.F. (2004) "Infection, reinfection, and vaccination under suboptimal immune protection: epidemiological perspectives", J. Theor. Biol. 228(4):539–549
## Source: https://doi.org/10.1016/j.jtbi.2004.02.015
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, V
## Flows (time unit: years):
##   (outside) -> S  : (1 - p_nb) * mu * N
##   (outside) -> V  : p_nb * mu * N
##   S -> I          : lambda * S
##   R -> I          : sigma_r * lambda * R
##   V -> I          : sigma_r * lambda * V
##   I -> R          : gamma * I
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R
##   V -> (outside)  : mu * V

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu = 1 / 70,     # birth/death (1/yr) [illustrative]
  gamma = 52,      # recovery (1 week) (1/yr) [illustrative]
  beta = 260,      # transmission (R0 ≈ 5) (1/yr) [illustrative]
  sigma_r = 0.25,  # relative susceptibility to reinfection (reinfection threshold at R0 = 1/sigma_r = 4) [illustrative]
  p_nb = 0         # newborn vaccine coverage (set to 0.9 to show vaccine failure above the reinfection threshold) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # susceptible
  I = 10,    # infectious
  R = 0,     # recovered, partially immune
  V = 0      # vaccinated at birth (same partial protection as R)
)

# ---- Model equations -------------------------------------------------
sir_reinfection_partial_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R + V

    # Derived quantities
    lambda <- beta * I / N

    # Rates of change
    dS <- (1 - p_nb) * mu * N - lambda * S - mu * S
    dI <- lambda * S + sigma_r * lambda * R + sigma_r * lambda * V - gamma * I - mu * I
    dR <- -sigma_r * lambda * R + gamma * I - mu * R
    dV <- p_nb * mu * N - sigma_r * lambda * V - mu * V

    list(c(dS, dI, dR, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_reinfection_partial_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SIR with reinfection of recovered (partial immunity; reinfection threshold)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIR with vertical (mother-to-child) transmission

Classic extensions
SIR

hepatitis B, rubella (congenital), HTLV, Chagas. Vertical transmission adds q_v·b_birth to the infected's "reproductive output", so R0 = (beta + q_vb_birth)/(gamma + mu). Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.

Busenberg S. & Cooke K. (1993) Vertically Transmitted Diseases: Models and Dynamics, Biomathematics 23, Springer ↗

Show R code
## SIR with vertical (mother-to-child) transmission
## hepatitis B, rubella (congenital), HTLV, Chagas.
## Vertical transmission adds q_v·b_birth to the infected's "reproductive output", so R0 = (beta + q_vb_birth)/(gamma + mu).
## Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.
## Reference: Busenberg S. & Cooke K. (1993) Vertically Transmitted Diseases: Models and Dynamics, Biomathematics 23, Springer
## Source: https://doi.org/10.1007/978-3-642-75301-5
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R
## Flows (time unit: years):
##   (outside) -> S  : b_birth * (S + R + (1 - q_v) * I)
##   (outside) -> I  : b_birth * q_v * I
##   S -> I          : lambda * S
##   I -> R          : gamma * I
##   S -> (outside)  : mu * S
##   I -> (outside)  : mu * I
##   R -> (outside)  : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  b_birth = 1 / 20,  # per-capita birth rate (1/yr) [illustrative, adjusted for numerical stability]
  mu = 1 / 20,       # natural death (1/yr) [illustrative, adjusted for numerical stability]
  q_v = 0.3,         # probability a child of an infected mother is born infected [illustrative]
  beta = 1.5,        # horizontal transmission (1/yr) [illustrative, adjusted for numerical stability]
  gamma = 0.5        # recovery (1/yr) [illustrative, adjusted for numerical stability]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9900,  # susceptible
  I = 100,   # infectious
  R = 0      # recovered/immune
)

# ---- Model equations -------------------------------------------------
sir_vertical_transmission_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lambda <- beta * I / N

    # Rates of change
    dS <- b_birth * (S + R + (1 - q_v) * I) - lambda * S - mu * S
    dI <- b_birth * q_v * I + lambda * S - gamma * I - mu * I
    dR <- gamma * I - mu * R

    list(c(dS, dI, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sir_vertical_transmission_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "SIR with vertical (mother-to-child) transmission")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

acute infection with diagnosis, isolation-at-home and treatment (no carriers)

Classic extensions
SIuIdR

generic acute bacterial or viral infection with case finding (e.g. diphtheria, scabies, early COVID testing). R0 is about 1.53 with these values, against 4 without diagnosis. Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.

Illustrative structure in the spirit of the treatment and control chapters of Vynnycky E. & White R.G. (2010) An Introduction to Infectious Disease Modelling, Oxford University Press, ISBN 978-0-19-856576-5, and Martcheva M. (2015) An Introduction to… ↗

Show R code
## acute infection with diagnosis, isolation-at-home and treatment (no carriers)
## generic acute bacterial or viral infection with case finding (e.g. diphtheria, scabies, early COVID testing).
## R0 is about 1.53 with these values, against 4 without diagnosis.
## Parameter values: 0 of 5 checked against the source; the rest are recalled or illustrative.
## Reference: Illustrative structure in the spirit of the treatment and control chapters of Vynnycky E. & White R.G. (2010) An Introduction to Infectious Disease Modelling, Oxford University Press, ISBN 978-0-19-856576-5, and Martcheva M. (2015) An Introduction to…
## Source: https://doi.org/10.1007/978-1-4899-7612-3
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Iu, Id, R
## Flows (time unit: days):
##   S -> Iu   : lambda * S
##   Iu -> Id  : delta_d * Iu
##   Iu -> R   : gamma_u * Iu
##   Id -> R   : gamma_d * Id

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.4,        # transmission (1/day) [illustrative]
  delta_d = 0.2,     # diagnosis rate (mean 5 days to diagnosis) (1/day) [illustrative]
  kappa_d = 0.2,     # relative infectiousness after diagnosis (isolation plus treatment) [illustrative]
  gamma_u = 1 / 10,  # recovery, undiagnosed (1/day) [illustrative]
  gamma_d = 1 / 3    # recovery, diagnosed and treated (1/day) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # susceptible
  Iu = 10,   # infectious, undiagnosed
  Id = 0,    # diagnosed and treated, reduced contacts
  R = 0      # recovered
)

# ---- Model equations -------------------------------------------------
sidr_diagnosis_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Iu + Id + R

    # Derived quantities
    lambda <- beta * (Iu + kappa_d * Id) / N
    R0 <- beta / (gamma_u + delta_d) + beta * delta_d * kappa_d / ((gamma_u + delta_d) * gamma_d)

    # Rates of change
    dS <- -lambda * S
    dIu <- lambda * S - delta_d * Iu - gamma_u * Iu
    dId <- delta_d * Iu - gamma_d * Id
    dR <- gamma_u * Iu + gamma_d * Id

    list(c(dS, dIu, dId, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sidr_diagnosis_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Iu = "#B71C1C", Id = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "acute infection with diagnosis, isolation-at-home and treatment (no carriers)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Codeço endemic/epidemic cholera with aquatic reservoir and dose–response

Water, enteric & NTDs
SIRB

Cholera (Vibrio cholerae). Parameter values: 6 of 6 checked against the source; the rest are recalled or illustrative.

Codeço CT. Endemic and epidemic dynamics of cholera: the role of the aquatic reservoir. BMC Infect Dis 2001;1:1 ↗

Show R code
## Codeço endemic/epidemic cholera with aquatic reservoir and dose–response
## Cholera (Vibrio cholerae).
## Parameter values: 6 of 6 checked against the source; the rest are recalled or illustrative.
## Reference: Codeço CT. Endemic and epidemic dynamics of cholera: the role of the aquatic reservoir. BMC Infect Dis 2001;1:1
## Source: https://doi.org/10.1186/1471-2334-1-1
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, B
## Flows (time unit: days):
##   (outside) -> S  : mu_h * N
##   S -> (outside)  : mu_h * S
##   S -> I          : a * B / (K + B) * S
##   I -> R          : r * I
##   I -> (outside)  : mu_h * I
##   R -> (outside)  : mu_h * R
##   (outside) -> B  : e * I
##   B -> (outside)  : (-nb_mb) * B

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 730, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu_h = 0.0001,  # human birth = death rate (the paper's n) (day^-1) [verified]
  a = 1,          # rate of exposure to contaminated water (day^-1) [verified]
  K = 1e6,        # concentration giving a 50% chance of infection (cells/ml) [verified]
  r = 0.2,        # recovery rate (5-day infection) (day^-1) [verified]
  nb_mb = -0.33,  # net growth rate of vibrios, nb − mb. It is negative, so the population cannot persist without human input (day^-1) [verified]
  e = 10          # contribution of each infected person to the aquatic population (cells ml^-1 day^-1 person^-1) [verified]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9999,  # susceptible
  I = 1,     # infected
  R = 0,     # recovered/removed. Added for bookkeeping: the original has only S and I with constant total H, and recovered people are
  B = 0      # toxigenic V. cholerae concentration in water (cells/ml)
)

# ---- Model equations -------------------------------------------------
cholera_codeco2001_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    lam_B <- B / (K + B)                  # probability of infection on exposure (saturating dose–response)
    S_crit <- K * r * (-nb_mb) / (e * a)  # critical susceptible number for invasion: 6600 for Community 2 and 13,200 for Community 1. These match the paper's Table 2 S_C values, which

    # Rates of change
    dS <- mu_h * N - mu_h * S - a * B / (K + B) * S
    dI <- a * B / (K + B) * S - r * I - mu_h * I
    dR <- r * I - mu_h * R
    dB <- e * I - (-nb_mb) * B

    list(c(dS, dI, dR, dB))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = cholera_codeco2001_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", B = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Codeço endemic/epidemic cholera with aquatic reservoir and dose–response")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Haiti cholera with water reservoir, vaccination and water-sanitation intervention

Water, enteric & NTDs
SIARVW

Cholera, Haiti 2010–11. Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.

Andrews JR, Basu S. Transmission dynamics and control of cholera in Haiti: an epidemic model. Lancet 2011;377(9773):1248–1255 ↗

Show R code
## Haiti cholera with water reservoir, vaccination and water-sanitation intervention
## Cholera, Haiti 2010–11.
## Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.
## Reference: Andrews JR, Basu S. Transmission dynamics and control of cholera in Haiti: an epidemic model. Lancet 2011;377(9773):1248–1255
## Source: https://doi.org/10.1016/S0140-6736(11
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, A, R, V, W
## Flows (time unit: days):
##   S -> I          : p_sym * lam * S
##   S -> A          : (1 - p_sym) * lam * S
##   I -> R          : gam_c * I
##   I -> (outside)  : cfr_rate * I
##   A -> R          : gam_c * A
##   R -> S          : omega_r * R
##   S -> V          : nu_v * eff_v * S
##   V -> S          : omega_v * V
##   (outside) -> W  : xi_w * (I + rel_a * A)
##   W -> (outside)  : del_w * W

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 300, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_w = 1.0,        # contaminated-water exposure rate (fitted in the paper) [I] (day^-1)
  beta_h = 0.1,        # direct (short-cycle) transmission [I] (day^-1)
  kap_w = 1e6,         # half-saturation dose, as in Codeço [M] (cells/ml)
  p_sym = 0.2,         # fraction of infections that are symptomatic [I]
  rel_a = 0.1,         # relative shedding/infectiousness of asymptomatic infections [I]
  gam_c = 0.2,         # recovery rate (5 days) [M] (day^-1)
  cfr_rate = 0.002,    # symptomatic death rate (fitted in the paper) [I] (day^-1)
  xi_w = 10,           # shedding [I] (cells ml^-1 day^-1 per symptomatic person)
  del_w = 0.033,       # vibrio decay rate (30-day survival) [M, as used in Hartley/Morris/Smith 2006-style models] (day^-1)
  omega_r = 1 / 1095,  # waning of natural immunity (about 3 years) [M] (day^-1)
  nu_v = 0,            # vaccination rate; try 0.001 [I] (day^-1)
  eff_v = 0.65,        # vaccine efficacy [I]
  omega_v = 1 / 1095,  # vaccine waning [I] (day^-1)
  red_w = 0.001436,    # equals −log(0.99)/7, a 1%/week reduction in contaminated-water consumption [V for the 1%/week scenario] (day^-1)
  t_int = 120          # start of the water intervention [I] (day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9,     # susceptible
  I = 1000,  # symptomatic infected
  A = 0,     # asymptomatic infected
  R = 0,     # recovered/immune
  V = 0,     # vaccinated (OCV)
  W = 1e3    # V. cholerae concentration in the water reservoir (cells/ml)
)

# ---- Model equations -------------------------------------------------
cholera_andrews_basu2011_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + A + R + V

    # Derived quantities
    water_mult <- exp(-red_w * max(t - t_int, 0))
    lam <- beta_w * water_mult * W / (kap_w + W) + beta_h * (I + rel_a * A) / N

    # Rates of change
    dS <- -p_sym * lam * S - (1 - p_sym) * lam * S + omega_r * R - nu_v * eff_v * S + omega_v * V
    dI <- p_sym * lam * S - gam_c * I - cfr_rate * I
    dA <- (1 - p_sym) * lam * S - gam_c * A
    dR <- gam_c * I + gam_c * A - omega_r * R
    dV <- nu_v * eff_v * S - omega_v * V
    dW <- xi_w * (I + rel_a * A) - del_w * W

    list(c(dS, dI, dA, dR, dV, dW))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = cholera_andrews_basu2011_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", A = "#E57373", R = "#4E8A3E", V = "#2457C5", W = "#A5D6A7")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Haiti cholera with water reservoir, vaccination and water-sanitation intervention")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Typhoid with chronic carriers, short- and long-cycle (water) transmission

Water, enteric & NTDs
S1I1CarRS2I2W

Typhoid fever (S. Typhi), South Asia (Kolkata, Delhi, Vellore). Parameter values: 3 of 11 checked against the source; the rest are recalled or illustrative.

Pitzer VE, Bowles CC, Baker S, Kang G, Balaji V, Farrar JJ, Grenfell BT. Predicting the impact of vaccination on the transmission dynamics of typhoid in South Asia: a mathematical modeling study. PLoS Negl Trop Dis 2014;8(1):e2642 ↗

Show R code
## Typhoid with chronic carriers, short- and long-cycle (water) transmission
## Typhoid fever (S. Typhi), South Asia (Kolkata, Delhi, Vellore).
## Parameter values: 3 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Pitzer VE, Bowles CC, Baker S, Kang G, Balaji V, Farrar JJ, Grenfell BT. Predicting the impact of vaccination on the transmission dynamics of typhoid in South Asia: a mathematical modeling study. PLoS Negl Trop Dis 2014;8(1):e2642
## Source: https://doi.org/10.1371/journal.pntd.0002642
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S1, I1, Car, R, S2, I2, W
## Flows (time unit: weeks):
##   (outside) -> S1   : mu_h * N
##   S1 -> I1          : lam * S1
##   I1 -> Car         : theta_c * delta_i * I1
##   I1 -> R           : (1 - theta_c) * delta_i * I1
##   R -> S2           : omega_r * R
##   S2 -> I2          : lam * S2
##   I2 -> R           : delta_i * I2
##   S1 -> (outside)   : mu_h * S1
##   I1 -> (outside)   : mu_h * I1
##   Car -> (outside)  : mu_h * Car
##   R -> (outside)    : mu_h * R
##   S2 -> (outside)   : mu_h * S2
##   I2 -> (outside)   : mu_h * I2
##   (outside) -> W    : xi_w * prev_w
##   W -> (outside)    : xi_w * W

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # weeks

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_ty = 2.8,       # basic reproduction number, Kolkata fit [verified]
  frac_short = 0.9,  # share of R0 from short-cycle transmission [verified]
  r_car = 0.01,      # relative infectiousness of carriers (best fit). The model also applies it to I2 [V for carriers; M/I for applying it to I2].
  theta_c = 0.03,    # probability that a primary infection becomes a lifelong carrier. The paper uses age-specific values of 0.3–10.1%, so 0.03 is an [I] average [V range].
  p_clin = 0.1,      # fraction of primary infections that are clinical typhoid. Used only for reporting [V].
  rep_f = 0.0052,    # reporting fraction [verified]
  delta_i = 0.25,    # recovery rate (4-week infectiousness) [M] (week^-1)
  omega_r = 0.0096,  # waning R → S2 (about 104 weeks) [M] (week^-1)
  xi_w = 0.333,      # decay of water contamination (about 3 weeks) [M] (week^-1)
  mu_h = 0.000385,   # birth = death rate (50-year life) [I]. The paper used observed demography. (week^-1)
  amp_w = 0.3        # seasonal amplitude of long-cycle transmission [I]. The paper fitted seasonality.
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S1 = 0.3,     # fully susceptible
  I1 = 100,     # primary (clinical-capable) infection
  Car = 0.005,  # chronic carrier. C is avoided because it is an R function
  R = 0.4,      # immune
  S2 = 0.3,     # partially susceptible after waning
  I2 = 0,       # subclinical reinfection
  W = 0         # water-supply contamination, scaled to prevalence units
)

# ---- Model equations -------------------------------------------------
typhoid_pitzer2014_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S1 + I1 + Car + R + S2 + I2

    # Derived quantities
    D_eff <- 1 / (delta_i + mu_h) + theta_c * delta_i / (delta_i + mu_h) * r_car / mu_h  # effective infectious duration per primary case (about 4.78 weeks)
    beta_s <- frac_short * R0_ty / D_eff                                                 # short-cycle rate (week^-1)
    beta_l <- (1 - frac_short) * R0_ty / D_eff                                           # long-cycle rate. W is scaled so that its equilibrium equals the weighted prevalence.
    prev_w <- (I1 + r_car * Car + r_car * I2) / N
    lam <- beta_s * prev_w + beta_l * (1 + amp_w * cos(6.2831853 * t / 52)) * W

    # Rates of change
    dS1 <- mu_h * N - lam * S1 - mu_h * S1
    dI1 <- lam * S1 - theta_c * delta_i * I1 - (1 - theta_c) * delta_i * I1 - mu_h * I1
    dCar <- theta_c * delta_i * I1 - mu_h * Car
    dR <- (1 - theta_c) * delta_i * I1 - omega_r * R + delta_i * I2 - mu_h * R
    dS2 <- omega_r * R - lam * S2 - mu_h * S2
    dI2 <- lam * S2 - delta_i * I2 - mu_h * I2
    dW <- xi_w * prev_w - xi_w * W

    list(c(dS1, dI1, dCar, dR, dS2, dI2, dW))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = typhoid_pitzer2014_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S1 = "#333333", I1 = "#B71C1C", Car = "#8E0E0E", R = "#4E8A3E", S2 = "#333333", I2 = "#B71C1C", W = "#A5D6A7")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (weeks)", ylab = "Number of individuals",
        main = "Typhoid with chronic carriers, short- and long-cycle (water) transmission")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Wild poliovirus with OPV vaccine virus and circulating vaccine-derived (cVDPV) lineage

Water, enteric & NTDs
SIWIVIVDR

Poliomyelitis (WPV, Sabin OPV, cVDPV). Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

Structure inspired by Fine PEM, Carneiro IAM. Transmissibility and persistence of oral polio vaccine viruses: implications for the global poliomyelitis eradication initiative. Am J Epidemiol 1999;150(10):1001–1021 ↗

Show R code
## Wild poliovirus with OPV vaccine virus and circulating vaccine-derived (cVDPV) lineage
## Poliomyelitis (WPV, Sabin OPV, cVDPV).
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Structure inspired by Fine PEM, Carneiro IAM. Transmissibility and persistence of oral polio vaccine viruses: implications for the global poliomyelitis eradication initiative. Am J Epidemiol 1999;150(10):1001–1021
## Source: https://doi.org/10.1093/oxfordjournals.aje.a009924
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, IW, IV, IVD, R
## Flows (time unit: days):
##   (outside) -> S   : mu_h * N * (1 - p_opv * take_v)
##   (outside) -> IV  : mu_h * N * p_opv * take_v
##   S -> IW          : bw * IW / N * S
##   S -> IV          : bv * IV / N * (1 - p_rev) * S
##   S -> IVD         : (bv * IV / N * p_rev + bvd * IVD / N) * S
##   IW -> R          : gam_p * IW
##   IV -> R          : gam_p * IV
##   IVD -> R         : gam_p * IVD
##   R -> S           : omega_p * R
##   S -> (outside)   : mu_h * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 20, by = 0.02)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu_h = 1 / (50 * 365),    # birth/death [I] (day^-1)
  gam_p = 1 / 28,           # recovery (about 4 weeks of excretion) [M] (day^-1)
  R0_w = 6,                 # WPV R0; reported range about 4–13 in high-transmission settings [M]
  rel_v = 0.4,              # Sabin virus transmissibility relative to WPV [I]
  rel_vd = 0.9,             # cVDPV transmissibility relative to WPV [I]
  p_rev = 0.01,             # fraction of Sabin onward transmissions producing revertant (cVDPV) virus [I]
  p_opv = 0.8,              # routine OPV coverage at birth (cohort) [I]
  take_v = 0.5,             # per-dose take, intestinal infection [I]
  omega_p = 1 / (5 * 365),  # waning of intestinal immunity [I] (day^-1)
  par_ratio = 0.005,        # paralysis-to-infection ratio, about 1/200 for WPV1 [M, widely cited]
  N0 = 100000               # total initial population [illustrative default]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.1 * N0  # susceptible to intestinal infection
  IW <- 10       # wild-type infection
  IV <- 0        # OPV (Sabin-like) infection
  IVD <- 0       # cVDPV (reverted) infection
  R <- 0.9 * N0  # intestinally immune
  c(S = S, IW = IW, IV = IV, IVD = IVD, R = R)
})

# ---- Model equations -------------------------------------------------
polio_wpv_opv_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + IW + IV + IVD + R

    # Derived quantities
    bw <- R0_w * gam_p
    bv <- rel_v * R0_w * gam_p
    bvd <- rel_vd * R0_w * gam_p
    inc_par <- par_ratio * (bw * IW + bvd * IVD) / N * S  # paralytic incidence

    # Rates of change
    dS <- mu_h * N * (1 - p_opv * take_v) - bw * IW / N * S - bv * IV / N * (1 - p_rev) * S - (bv * IV / N * p_rev + bvd * IVD / N) * S + omega_p * R - mu_h * S
    dIW <- bw * IW / N * S - gam_p * IW
    dIV <- mu_h * N * p_opv * take_v + bv * IV / N * (1 - p_rev) * S - gam_p * IV
    dIVD <- (bv * IV / N * p_rev + bvd * IVD / N) * S - gam_p * IVD
    dR <- gam_p * IW + gam_p * IV + gam_p * IVD - omega_p * R

    list(c(dS, dIW, dIV, dIVD, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = polio_wpv_opv_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", IW = "#B71C1C", IV = "#B71C1C", IVD = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Wild poliovirus with OPV vaccine virus and circulating vaccine-derived (cVDPV) lineage")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Rotavirus with maternal immunity and repeated infections of decreasing severity

Water, enteric & NTDs
MS0I1R1S1I2R2S2I3R3S3

Rotavirus gastroenteritis. Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.

Pitzer VE, Viboud C, Simonsen L, Steiner C, Panozzo CA, Alonso WJ, Miller MA, Glass RI, Glasser JW, Parashar UD, Grenfell BT. Demographic variability, vaccination, and the spatiotemporal dynamics of rotavirus epidemics. Science 2009;325(5938):290–294 ↗

Show R code
## Rotavirus with maternal immunity and repeated infections of decreasing severity
## Rotavirus gastroenteritis.
## Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.
## Reference: Pitzer VE, Viboud C, Simonsen L, Steiner C, Panozzo CA, Alonso WJ, Miller MA, Glass RI, Glasser JW, Parashar UD, Grenfell BT. Demographic variability, vaccination, and the spatiotemporal dynamics of rotavirus epidemics. Science 2009;325(5938):290–294
## Source: https://doi.org/10.1126/science.1172330
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: M, S0, I1, R1, S1, I2, R2, S2, I3, R3, S3
## Flows (time unit: weeks):
##   (outside) -> M  : mu_h * N
##   M -> S0         : omega_m * M
##   S0 -> I1        : lam * S0
##   I1 -> R1        : gam_1 * I1
##   R1 -> S1        : omega_r * R1
##   S1 -> I2        : sus_1 * lam * S1
##   I2 -> R2        : gam_2 * I2
##   R2 -> S2        : omega_r * R2
##   S2 -> I3        : sus_2 * lam * S2
##   I3 -> R3        : gam_2 * I3
##   R3 -> S3        : omega_r * R3
##   S3 -> I3        : sus_2 * lam * S3
##   M -> (outside)  : mu_h * M

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # weeks

# ---- Parameters ------------------------------------------------------
parameters <- c(
  mu_h = 0.0003,     # birth/death. The paper used state-specific US birth rates (about 14–15 per 1000 per year) [I] (week^-1)
  omega_m = 1 / 13,  # loss of maternal immunity (13 weeks) [V via PMC9746532; M that Pitzer 2009 used it] (week^-1)
  gam_1 = 1,         # recovery from the first infection (1 week) [M] (week^-1)
  gam_2 = 2,         # recovery from later infections (half as long) [M] (week^-1)
  rho_inf = 0.5,     # relative infectiousness of 2nd+ infections [M]
  sus_1 = 0.62,      # relative susceptibility after 1 infection [V, Velázquez via PMC9746532]
  sus_2 = 0.37,      # relative susceptibility after 2+ infections [V, Velázquez via PMC9746532]. Pitzer may have used 0.35 [M].
  omega_r = 1 / 52,  # waning of temporary immunity (1 year) [M] (week^-1)
  R0_rv = 15,        # R0; fitted values were roughly 10–25 [M]
  amp_s = 0.15,      # seasonal amplitude [M/I]
  phi_s = 0,         # seasonal phase [I] (week)
  ps1 = 0.47,        # proportion symptomatic for the 1st, 2nd and 3rd+ infections. Velázquez 1996-type values; the 4th infection is about 0.2 [M]. Used for outputs only.
  ps2 = 0.25,        # proportion symptomatic for the 1st, 2nd and 3rd+ infections. Velázquez 1996-type values; the 4th infection is about 0.2 [M]. Used for outputs only.
  ps3 = 0.32,        # proportion symptomatic for the 1st, 2nd and 3rd+ infections. Velázquez 1996-type values; the 4th infection is about 0.2 [M]. Used for outputs only.
  N0 = 100000        # total initial population [illustrative default]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  M <- 0.01 * N0   # maternal immunity
  S0 <- 0.05 * N0  # naive susceptible
  I1 <- 10         # first infection
  R1 <- 0          # temporary immunity after the 1st infection
  S1 <- 0.1 * N0   # susceptible after the 1st infection
  I2 <- 0          # second infection
  R2 <- 0          # temporary immunity after the 2nd infection
  S2 <- 0.2 * N0   # susceptible after the 2nd infection
  I3 <- 0          # third or later infection
  R3 <- 0          # temporary immunity after the 3rd or later infection
  S3 <- 0.64 * N0  # susceptible after 3+ infections
  c(M = M, S0 = S0, I1 = I1, R1 = R1, S1 = S1, I2 = I2, R2 = R2, S2 = S2, I3 = I3, R3 = R3, S3 = S3)
})

# ---- Model equations -------------------------------------------------
rotavirus_pitzer2009_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- M + S0 + I1 + R1 + S1 + I2 + R2 + S2 + I3 + R3 + S3

    # Derived quantities
    b0 <- R0_rv * (gam_1 + mu_h)
    bt <- b0 * (1 + amp_s * cos(6.2831853 * (t - phi_s) / 52))
    lam <- bt * (I1 + rho_inf * (I2 + I3)) / N
    inc_sev <- lam * (ps1 * S0 + ps2 * sus_1 * S1 + ps3 * sus_2 * S2)  # incidence of rotavirus gastroenteritis (RVGE)

    # Rates of change
    dM <- mu_h * N - omega_m * M - mu_h * M
    dS0 <- omega_m * M - lam * S0
    dI1 <- lam * S0 - gam_1 * I1
    dR1 <- gam_1 * I1 - omega_r * R1
    dS1 <- omega_r * R1 - sus_1 * lam * S1
    dI2 <- sus_1 * lam * S1 - gam_2 * I2
    dR2 <- gam_2 * I2 - omega_r * R2
    dS2 <- omega_r * R2 - sus_2 * lam * S2
    dI3 <- sus_2 * lam * S2 - gam_2 * I3 + sus_2 * lam * S3
    dR3 <- gam_2 * I3 - omega_r * R3
    dS3 <- omega_r * R3 - sus_2 * lam * S3

    list(c(dM, dS0, dI1, dR1, dS1, dI2, dR2, dS2, dI3, dR3, dS3))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = rotavirus_pitzer2009_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(M = "#F3C6D3", S0 = "#333333", I1 = "#B71C1C", R1 = "#4E8A3E", S1 = "#333333", I2 = "#B71C1C", R2 = "#4E8A3E", S2 = "#333333", I3 = "#B71C1C", R3 = "#4E8A3E", S3 = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (weeks)", ylab = "Number of individuals",
        main = "Rotavirus with maternal immunity and repeated infections of decreasing severity")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Norovirus SEIAR with asymptomatic reinfection and waning disease immunity

Water, enteric & NTDs
SEIAR

Norovirus gastroenteritis. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Simmons K, Gambhir M, Leon J, Lopman B. Duration of immunity to norovirus gastroenteritis. Emerg Infect Dis 2013;19(8):1260–1267 ↗

Show R code
## Norovirus SEIAR with asymptomatic reinfection and waning disease immunity
## Norovirus gastroenteritis.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Simmons K, Gambhir M, Leon J, Lopman B. Duration of immunity to norovirus gastroenteritis. Emerg Infect Dis 2013;19(8):1260–1267
## Source: https://doi.org/10.3201/eid1908.130472
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, A, R
## Flows (time unit: days):
##   (outside) -> S  : mu_h * N
##   S -> E          : lam * S
##   E -> I          : 1 / inc_d * E
##   I -> A          : 1 / sym_d * I
##   A -> R          : 1 / shed_d * A
##   R -> A          : lam * R
##   R -> S          : 1 / theta_d * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  inc_d = 1,              # incubation, E → I [V, "about 1 day"] (day)
  sym_d = 2,              # symptomatic duration, I → A [V, "1 day, half the symptomatic phase"] (day)
  shed_d = 10,            # asymptomatic shedding, A → R [V, Model B] (day)
  eps_ea = 0.05,          # relative infectiousness of E and A: 0.05 in Model B, 0.25 in Model C, 0 in Model A [V]
  theta_d = 5.1 * 365,    # duration of disease immunity, R → S. Fitted values: 5.1 years (Model A/B), 8.7 (C), 4.1 (D, 20% genetically resistant) [V] (day)
  R0_nv = 1.7,            # R0. Reported values: 1.64–1.88 for most models, 7.16 for C [V]
  mu_h = 1 / (70 * 365),  # birth = death [I] (day^-1)
  N0 = 100000             # total initial population [illustrative default]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.3 * N0  # susceptible to infection and disease
  E <- 0         # exposed (presymptomatic)
  I <- 100       # symptomatic
  A <- 0         # asymptomatic infected (post-symptomatic shedding, or reinfection of immune people)
  R <- 0.7 * N0  # immune to disease but not to infection
  c(S = S, E = E, I = I, A = A, R = R)
})

# ---- Model equations -------------------------------------------------
norovirus_simmons2013_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + I + A + R

    # Derived quantities
    Dinf <- eps_ea * inc_d + sym_d + eps_ea * shed_d  # effective infectious duration (2.55 days)
    b_nv <- R0_nv / Dinf                              # about 0.667/day
    lam <- b_nv * (I + eps_ea * (E + A)) / N

    # Rates of change
    dS <- mu_h * N - lam * S + 1 / theta_d * R
    dE <- lam * S - 1 / inc_d * E
    dI <- 1 / inc_d * E - 1 / sym_d * I
    dA <- 1 / sym_d * I - 1 / shed_d * A + lam * R
    dR <- 1 / shed_d * A - lam * R - 1 / theta_d * R

    list(c(dS, dE, dI, dA, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = norovirus_simmons2013_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", A = "#E57373", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Norovirus SEIAR with asymptomatic reinfection and waning disease immunity")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Schistosomiasis: human mean worm burden with SEI snails (Macdonald / Anderson–May hybrid)

Water, enteric & NTDs
WmSnEnIn

Schistosomiasis (S. haematobium / S. mansoni). Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.

Structure follows the Macdonald (1965) / Anderson & May (1985, 1991) mean-worm-burden + snail-infection framework, as implemented in, for example, Sokolow SH et al. PNAS 2015;112(31):9650–9655 ↗

Show R code
## Schistosomiasis: human mean worm burden with SEI snails (Macdonald / Anderson–May hybrid)
## Schistosomiasis (S. haematobium / S. mansoni).
## Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.
## Reference: Structure follows the Macdonald (1965) / Anderson & May (1985, 1991) mean-worm-burden + snail-infection framework, as implemented in, for example, Sokolow SH et al. PNAS 2015;112(31):9650–9655
## Source: https://doi.org/10.1073/pnas.1502651112
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Wm, Sn, En, In
## Flows (time unit: days):
##   (outside) -> Sn  : r_s * (1 - Nsn / K_s) * (Sn + En)
##   Sn -> En         : beta_sn * miracidia * Sn
##   En -> In         : sig_n * En
##   Sn -> (outside)  : mu_n * Sn
##   En -> (outside)  : mu_n * En
##   In -> (outside)  : mu_i * In
##   (outside) -> Wm  : lam_h * In
##   Wm -> (outside)  : (mu_wm + mu_h + tau_t) * Wm

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  H = 1000,                 # human population, held constant [I] (persons)
  r_s = 0.1,                # snail intrinsic birth rate [M/I] (day^-1)
  K_s = 10000,              # snail carrying capacity [I] (snails)
  mu_n = 1 / 60,            # snail death rate [M] (day^-1)
  sig_n = 1 / 40,           # snail prepatent period (about 4–6 weeks) [M] (day^-1)
  mu_i = 1 / 10,            # infected snail death rate [M] (day^-1)
  beta_sn = 7.5e-7,         # miracidial infection of snails. [I] chosen to give about 5% snail prevalence at Wm = 50. (day^-1 per mated pair)
  lam_h = 1e-4,             # cercarial establishment [I] (worms day^-1 per infected snail per person)
  mu_wm = 1 / (3.3 * 365),  # adult worm death (about 3–4 year life span) [M] (day^-1)
  mu_h = 1 / (60 * 365),    # human death rate [I] (day^-1)
  k_agg = 0.2,              # negative binomial aggregation [M/I]
  tau_t = 0                 # continuous praziquantel treatment (MDA) rate. Pulsed MDA alternative: ifelse((t - 365floor(t/365)) < 7, -log(1 - 0.750.86)/7, 0) [I; 86% worm kill is an M value
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Wm = 50,    # mean adult worm burden per human (worms/person)
  Sn = 7000,  # susceptible snails
  En = 300,   # exposed (prepatent) snails
  In = 400    # infected (cercariae-shedding) snails
)

# ---- Model equations -------------------------------------------------
schisto_snail_mwb_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Derived quantities
    phi_m <- Wm / (Wm + 2 * k_agg + 1)  # crude mating-probability approximation. The exact Macdonald mating function is an integral; this is [I].
    Nsn <- Sn + En + In
    miracidia <- 0.5 * Wm * H * phi_m   # mated pairs, a proxy for egg output

    # Rates of change
    dWm <- lam_h * In - (mu_wm + mu_h + tau_t) * Wm
    dSn <- r_s * (1 - Nsn / K_s) * (Sn + En) - beta_sn * miracidia * Sn - mu_n * Sn
    dEn <- beta_sn * miracidia * Sn - sig_n * En - mu_n * En
    dIn <- sig_n * En - mu_i * In

    list(c(dWm, dSn, dEn, dIn))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = schisto_snail_mwb_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Wm = "#A5D6A7", Sn = "#333333", En = "#E8C468", In = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Schistosomiasis: human mean worm burden with SEI snails (Macdonald / Anderson–May hybrid)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Soil-transmitted helminths: mean worm burden with environmental reservoir of infective stages (Anderson & May)

Water, enteric & NTDs
MwLr

Ascaris / Trichuris / hookworm. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Anderson RM, May RM. Infectious Diseases of Humans: Dynamics and Control. Oxford University Press, 1991, chapters 16–17 (mean-worm-burden ODE with free-living reservoir, density-dependent fecundity and negative-binomial aggregation) [M]. Implemented for… ↗

Show R code
## Soil-transmitted helminths: mean worm burden with environmental reservoir of infective stages (Anderson & May)
## Ascaris / Trichuris / hookworm.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Anderson RM, May RM. Infectious Diseases of Humans: Dynamics and Control. Oxford University Press, 1991, chapters 16–17 (mean-worm-burden ODE with free-living reservoir, density-dependent fecundity and negative-binomial aggregation) [M]. Implemented for…
## Source: https://doi.org/10.1186/1756-3305-7-266
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Mw, Lr
## Flows (time unit: years):
##   (outside) -> Mw  : beta_l * Lr
##   Mw -> (outside)  : (mu_w + mu_h + tau_m) * Mw
##   (outside) -> Lr  : lam_e * Mw * fdd
##   Lr -> (outside)  : mu_l * Lr

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_sth = 3,   # basic reproduction number (Ascaris about 1–5) [M]
  mu_w = 1,     # adult worm death (Ascaris about 1 year) [M] (yr^-1)
  mu_h = 0.02,  # host death [I] (yr^-1)
  mu_l = 6,     # infective-stage loss (about 2 months) [I] (yr^-1)
  k_agg = 0.5,  # aggregation [M]
  z_dd = 0.93,  # per-worm density-dependent fecundity factor, exp(−γ) [M/I]
  lam_e = 1,    # egg output [I; normalisation] (stages per worm per yr, normalised)
  tau_m = 0     # continuous MDA kill rate. Pulsed alternative: ifelse((t - floor(t)) < 0.02, -log(1 - 0.750.95)/0.02, 0) (75% coverage, 95% efficacy of albendazole against Ascar
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Mw = 10,  # mean worm burden per host
  Lr = 5    # infective stages in the environment, normalised per host
)

# ---- Model equations -------------------------------------------------
sth_mean_worm_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Derived quantities
    beta_l <- R0_sth * (mu_w + mu_h) * mu_l / lam_e      # uptake rate of infective stages (18.36/yr)
    fdd <- (1 + Mw * (1 - z_dd) / k_agg)^(-(k_agg + 1))  # density-dependent fecundity under negative-binomial aggregation
    prev_sth <- 1 - (1 + Mw / k_agg)^(-k_agg)            # prevalence of infection

    # Rates of change
    dMw <- beta_l * Lr - (mu_w + mu_h + tau_m) * Mw
    dLr <- lam_e * Mw * fdd - mu_l * Lr

    list(c(dMw, dLr))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sth_mean_worm_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Mw = "#F3C6D3", Lr = "#E8C468")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Soil-transmitted helminths: mean worm burden with environmental reservoir of infective stages (Anderson & May)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Trachoma SIS with periodic mass azithromycin (Lietman 1999)

Water, enteric & NTDs
SI

Ocular Chlamydia trachomatis. Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.

Lietman T, Porco T, Dawson C, Blower S. Global elimination of trachoma: how frequently should we administer mass chemotherapy? Nat Med 1999;5(5):572–576 ↗

Show R code
## Trachoma SIS with periodic mass azithromycin (Lietman 1999)
## Ocular Chlamydia trachomatis.
## Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.
## Reference: Lietman T, Porco T, Dawson C, Blower S. Global elimination of trachoma: how frequently should we administer mass chemotherapy? Nat Med 1999;5(5):572–576
## Source: https://doi.org/10.1038/8451
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I
## Flows (time unit: weeks):
##   S -> I  : b_tr * I / N * S
##   I -> S  : (1 / dur_tr + mda_rate) * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 10, by = 0.01)  # weeks

# ---- Parameters ------------------------------------------------------
parameters <- c(
  dur_tr = 26,    # mean duration of infection in children [I] (week)
  R0_tr = 2,      # gives about 50% endemic prevalence (1 − 1/R0) [I]
  cover_m = 0.8,  # MDA coverage [I]
  eff_m = 0.95,   # azithromycin efficacy [M]
  int_m = 52,     # treatment interval: 52 annual, 26 biannual [V that these intervals were compared] (week)
  t_start = 52,   # first MDA round [I] (week)
  win_m = 1       # length of the treatment pulse [I] (week)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 500,  # uninfected
  I = 500   # infected with ocular chlamydia
)

# ---- Model equations -------------------------------------------------
trachoma_sis_mda_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I

    # Derived quantities
    b_tr <- R0_tr / dur_tr
    mda_on <- ifelse(t >= t_start & (t - t_start - int_m * floor((t - t_start) / int_m)) < win_m, 1, 0)
    mda_rate <- mda_on * (-log(1 - cover_m * eff_m) / win_m)  # a pulse giving the cover×eff kill in each round

    # Rates of change
    dS <- -b_tr * I / N * S + (1 / dur_tr + mda_rate) * I
    dI <- b_tr * I / N * S - (1 / dur_tr + mda_rate) * I

    list(c(dS, dI))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = trachoma_sis_mda_model, parms = parameters, hmax = 0.01)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (weeks)", ylab = "Number of individuals",
        main = "Trachoma SIS with periodic mass azithromycin (Lietman 1999)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Cryptosporidiosis with environmental oocysts and exponential dose–response

Water, enteric & NTDs
SIROc

Cryptosporidium (also adaptable to Giardia cysts). Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Author-constructed SIRS + environment structure [I]. The dose–response is Haas CN et al., exponential model with r ≈ 0.004 per oocyst (IOWA strain): J Am Water Works Assoc 1996;88(9):131–136 [M]. Giardia exponential r ≈ 0.0199 (Rose, Haas & Regli 1991, Am…

Show R code
## Cryptosporidiosis with environmental oocysts and exponential dose–response
## Cryptosporidium (also adaptable to Giardia cysts).
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
## Reference: Author-constructed SIRS + environment structure [I]. The dose–response is Haas CN et al., exponential model with r ≈ 0.004 per oocyst (IOWA strain): J Am Water Works Assoc 1996;88(9):131–136 [M]. Giardia exponential r ≈ 0.0199 (Rose, Haas & Regli 1991, Am…
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, R, Oc
## Flows (time unit: days):
##   (outside) -> S   : mu_h * N
##   S -> I           : lam * S
##   I -> R           : gam_c * I
##   R -> S           : omega_c * R
##   (outside) -> Oc  : sh_vol * I
##   Oc -> (outside)  : mu_o * Oc

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3, by = 0.005)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  exp_c = 1,              # water-consumption events per day [I] (day^-1)
  vol_d = 1,              # volume ingested per event [I] (L)
  r_dose = 0.004,         # exponential dose–response parameter [M] (per oocyst)
  gam_c = 1 / 14,         # recovery and end of shedding [M/I] (day^-1)
  sh_vol = 2e-4,          # shedding divided by water volume. [I] chosen to give R0 ≈ 2. (oocysts L^-1 day^-1 per infected)
  mu_o = 0.05,            # oocyst inactivation and flushing [I] (day^-1)
  omega_c = 1 / 365,      # waning immunity [I] (day^-1)
  mu_h = 1 / (70 * 365),  # birth/death [I] (day^-1)
  rem_t = 0               # fractional log-removal by treatment, for example 0.99 [I]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 9990,  # susceptible
  I = 10,    # infected/shedding
  R = 0,     # immune (partial, waning)
  Oc = 0     # oocysts per litre in the community water source
)

# ---- Model equations -------------------------------------------------
crypto_oocyst_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + R

    # Derived quantities
    dose <- vol_d * Oc * (1 - rem_t)
    lam <- exp_c * (1 - exp(-r_dose * dose))                                # daily infection probability as a hazard
    R0_cr <- exp_c * r_dose * vol_d * sh_vol * N / (mu_o * (gam_c + mu_h))  # linearised R0 (about 2)

    # Rates of change
    dS <- mu_h * N - lam * S + omega_c * R
    dI <- lam * S - gam_c * I
    dR <- gam_c * I - omega_c * R
    dOc <- sh_vol * I - mu_o * Oc

    list(c(dS, dI, dR, dOc))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = crypto_oocyst_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", R = "#4E8A3E", Oc = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Cryptosporidiosis with environmental oocysts and exponential dose–response")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Leptospirosis: rodent reservoir, environmental leptospires, humans as dead-end hosts

Water, enteric & NTDs
ShIhRhSrIrBl

Leptospirosis. Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.

Structure after Holt J, Davis S, Leirs H. A model of leptospirosis infection in an African rodent to determine risk to humans: seasonal fluctuations and the impact of rodent control. Acta Trop 2006;99(2–3):218–225 ↗

Show R code
## Leptospirosis: rodent reservoir, environmental leptospires, humans as dead-end hosts
## Leptospirosis.
## Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.
## Reference: Structure after Holt J, Davis S, Leirs H. A model of leptospirosis infection in an African rodent to determine risk to humans: seasonal fluctuations and the impact of rodent control. Acta Trop 2006;99(2–3):218–225
## Source: https://doi.org/10.1016/j.actatropica.2006.08.003
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Ih, Rh, Sr, Ir, Bl
## Flows (time unit: days):
##   (outside) -> Sr  : b_r * Nr * (1 - Nr / K_r)
##   Sr -> Ir         : (bd_r * Ir / Nr + be_r * envB) * Sr
##   Sr -> (outside)  : mu_r * Sr
##   Ir -> (outside)  : mu_r * Ir
##   (outside) -> Bl  : sh_r * Ir
##   Bl -> (outside)  : del_b * Bl
##   (outside) -> Sh  : mu_h * N
##   Sh -> Ih         : be_h * envB * Sh
##   Ih -> Rh         : gam_h * Ih
##   Ih -> (outside)  : cfr_h * Ih
##   Rh -> Sh         : omega_h * Rh

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 5, by = 0.005)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  b_r = 1 / 180,            # rodent birth rate per rodent [I] (day^-1)
  K_r = 1000,               # rodent carrying capacity [I] (rodents)
  mu_r = 1 / 365,           # rodent death [I] (day^-1)
  bd_r = 0.002,             # direct rodent transmission, scaled by Ir/Nr [I] (day^-1 per infected rodent)
  be_r = 0.05,              # rodent infection from the environment [I] (day^-1)
  sh_r = 1,                 # shedding [I] (units day^-1 per infected rodent)
  del_b = 0.1,              # leptospire decay [I] (day^-1)
  K_b = 1000,               # half-saturation [I] (units)
  be_h = 1e-4,              # human exposure rate. It scales with flooding: multiply by (1 + amp_fcos(6.2831853t/365)) [I] (day^-1)
  gam_h = 1 / 14,           # human recovery [M/I] (day^-1)
  cfr_h = 0.005,            # disease death hazard (CFR about 5–10% in severe cases) [I] (day^-1)
  omega_h = 1 / (5 * 365),  # waning serovar immunity [I] (day^-1)
  mu_h = 1 / (70 * 365)     # human birth/death [I] (day^-1)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 9990,  # susceptible humans
  Ih = 0,     # infected (clinical) humans
  Rh = 0,     # recovered humans
  Sr = 900,   # susceptible rodents
  Ir = 100,   # infected (chronically shedding) rodents
  Bl = 0      # leptospires in the environment (arbitrary units)
)

# ---- Model equations -------------------------------------------------
lepto_rodent_env_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Ih + Rh

    # Derived quantities
    Nr <- Sr + Ir
    envB <- Bl / (K_b + Bl)

    # Rates of change
    dSh <- mu_h * N - be_h * envB * Sh + omega_h * Rh
    dIh <- be_h * envB * Sh - gam_h * Ih - cfr_h * Ih
    dRh <- gam_h * Ih - omega_h * Rh
    dSr <- b_r * Nr * (1 - Nr / K_r) - (bd_r * Ir / Nr + be_r * envB) * Sr - mu_r * Sr
    dIr <- (bd_r * Ir / Nr + be_r * envB) * Sr - mu_r * Ir
    dBl <- sh_r * Ir - del_b * Bl

    list(c(dSh, dIh, dRh, dSr, dIr, dBl))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = lepto_rodent_env_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Ih = "#B71C1C", Rh = "#4E8A3E", Sr = "#333333", Ir = "#B71C1C", Bl = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Leptospirosis: rodent reservoir, environmental leptospires, humans as dead-end hosts")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Scabies SIS with egg-stage reinfection and two-dose ivermectin MDA

Water, enteric & NTDs
SIIe

Scabies (Sarcoptes scabiei). Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Lydeamore MJ, Campbell PT, Regan DG, Tong SYC, Andrews RM, Steer AC, Romani L, Kaldor JM, McVernon J, McCaw JM. A biological model of scabies infection dynamics and treatment informs mass drug administration strategies to increase the likelihood of… ↗

Show R code
## Scabies SIS with egg-stage reinfection and two-dose ivermectin MDA
## Scabies (Sarcoptes scabiei).
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
## Reference: Lydeamore MJ, Campbell PT, Regan DG, Tong SYC, Andrews RM, Steer AC, Romani L, Kaldor JM, McVernon J, McCaw JM. A biological model of scabies infection dynamics and treatment informs mass drug administration strategies to increase the likelihood of…
## Source: https://doi.org/10.1016/j.mbs.2018.08.007
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I, Ie
## Flows (time unit: days):
##   S -> I   : b_sc * I / N * S
##   I -> S   : 1 / dur_sc * I
##   I -> Ie  : mda_k * I
##   Ie -> I  : hatch * Ie
##   Ie -> S  : mda_k * Ie

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3, by = 0.005)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_sc = 1.5,     # R0 [I]
  dur_sc = 365,    # untreated infestation duration, chronic [I] (day)
  hatch = 1 / 7,   # egg hatch/maturation back to active infestation (ivermectin is not ovicidal) [M] (day^-1)
  cover_s = 0.85,  # MDA coverage [I]
  eff_s = 0.95,    # ivermectin kill of active mites [I]
  t1 = 100,        # first MDA dose [I] (day)
  t2 = 110,        # second dose, 7–14 days later [M on interval] (day)
  win_s = 1,       # pulse width [I] (day)
  N0 = 100000      # total initial population [illustrative default]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.7 * N0  # uninfested
  I <- 0.3 * N0  # infested (active mites)
  Ie <- 0        # post-treatment carriage of surviving eggs, which re-hatch unless a second dose is given
  c(S = S, I = I, Ie = Ie)
})

# ---- Model equations -------------------------------------------------
scabies_mda_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I + Ie

    # Derived quantities
    b_sc <- R0_sc / dur_sc
    dose_on <- ifelse((t >= t1 & t < t1 + win_s) | (t >= t2 & t < t2 + win_s), 1, 0)
    mda_k <- dose_on * (-log(1 - cover_s * eff_s) / win_s)

    # Rates of change
    dS <- -b_sc * I / N * S + 1 / dur_sc * I + mda_k * Ie
    dI <- b_sc * I / N * S - 1 / dur_sc * I - mda_k * I + hatch * Ie
    dIe <- mda_k * I - hatch * Ie - mda_k * Ie

    list(c(dS, dI, dIe))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = scabies_mda_model, parms = parameters, hmax = 0.005)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I = "#B71C1C", Ie = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Scabies SIS with egg-stage reinfection and two-dose ivermectin MDA")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Pandemic influenza SLIAR model with antiviral treatment and pre-epidemic vaccination/prophylaxis (Arino et al. 2008)

Respiratory
SSTLLTIpIpTAATRDead

Pandemic influenza. Parameter values: 0 of 25 checked against the source; the rest are recalled or illustrative.

Arino J, Brauer F, van den Driessche P, Watmough J, Wu J. "A model for influenza with vaccination and antiviral treatment." J Theor Biol 2008;253:118–130. https://doi.org/10.1016/j.jtbi.2008.02.026 (PDF:… ↗

Show R code
## Pandemic influenza SLIAR model with antiviral treatment and pre-epidemic vaccination/prophylaxis (Arino et al. 2008)
## Pandemic influenza.
## Parameter values: 0 of 25 checked against the source; the rest are recalled or illustrative.
## Reference: Arino J, Brauer F, van den Driessche P, Watmough J, Wu J. "A model for influenza with vaccination and antiviral treatment." J Theor Biol 2008;253:118–130. https://doi.org/10.1016/j.jtbi.2008.02.026 (PDF:…
## Source: https://doi.org/10.1016/j.jtbi.2008.02.026
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, ST, L, LT, Ip, IpT, A, AT, R, Dead
## Flows (time unit: days):
##   S -> L       : foi * S
##   ST -> LT     : sig_S * foi * ST
##   L -> Ip      : p_sym * k_lat * L
##   L -> A       : (1 - p_sym) * k_lat * L
##   L -> LT      : phi_L * L
##   LT -> L      : theta_L * LT
##   LT -> IpT    : p_sym * tau_av * k_latT * LT
##   LT -> AT     : (1 - p_sym * tau_av) * k_latT * LT
##   Ip -> R      : f_rec * alpha_r * Ip
##   Ip -> Dead   : (1 - f_rec) * alpha_r * Ip
##   Ip -> IpT    : phi_I * Ip
##   IpT -> Ip    : theta_I * IpT
##   IpT -> R     : fT_rec * alpha_T * IpT
##   IpT -> Dead  : (1 - fT_rec) * alpha_T * IpT
##   A -> R       : eta_r * A
##   A -> AT      : phi_A * A
##   AT -> A      : theta_A * AT
##   AT -> R      : eta_T * AT

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  k_lat = 0.526,     # exit rate from L, i.e. 1/1.9 d (1/day)
  k_latT = 0.526,    # exit rate from LT (1/day)
  alpha_r = 0.244,   # exit rate from Ip, i.e. 1/4.1 d (1/day)
  eta_r = 0.244,     # exit rate from A (1/day)
  alpha_T = 0.323,   # exit rate from IpT (1/day)
  eta_T = 0.323,     # exit rate from AT (1/day)
  p_sym = 0.667,     # fraction of latents who become symptomatic
  tau_av = 0.4,      # factor by which antivirals reduce the fraction who develop symptoms
  f_rec = 0.98,      # fraction of Ip who recover (1 - CFR)
  fT_rec = 1.0,      # fraction of IpT who recover
  eps_L = 0,         # relative infectivity of latents; Longini assumes 0
  delta_a = 0.5,     # relative infectivity of asymptomatics
  phi_L = 1.61,      # treatment rate, = -ln(1-0.8), i.e. 80% treated within 1 day (1/day)
  phi_I = 1.61,      # treatment rate, = -ln(1-0.8), i.e. 80% treated within 1 day (1/day)
  phi_A = 1.61,      # treatment rate, = -ln(1-0.8), i.e. 80% treated within 1 day (1/day)
  theta_L = 0.0179,  # relapse rate from treated back to untreated (1/day)
  theta_I = 0.0179,  # relapse rate from treated back to untreated (1/day)
  theta_A = 0.0179,  # relapse rate from treated back to untreated (1/day)
  sig_S = 0.7,       # relative susceptibility of ST
  sig_L = 0.2,       # relative infectivity of treated classes
  sig_I = 0.2,       # relative infectivity of treated classes
  sig_A = 0.2,       # relative infectivity of treated classes
  q_w = 0.4,         # fraction of symptomatics who withdraw from mixing
  b0 = 0.581,        # transmission coefficient under standard incidence with q_w = 0.4 (1/day)
  g_vac = 0          # fraction of S0 treated or vaccinated before the epidemic; the paper explores 0–0.7
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 1,      # untreated susceptible
  ST = 1988,  # treated (prophylaxed or vaccinated) susceptible
  L = 0,      # latent
  LT = 0,     # treated latent
  Ip = 12,    # symptomatic infective
  IpT = 0,    # treated symptomatic infective
  A = 0,      # asymptomatic infective
  AT = 0,     # treated asymptomatic infective
  R = 0,      # recovered
  Dead = 0    # influenza deaths (cumulative)
)

# ---- Model equations -------------------------------------------------
flu_arino2008_antiviral_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + ST + L + LT + Ip + IpT + A + AT + R

    # Derived quantities
    N_M <- N - q_w * Ip - q_w * IpT                                                                                        # mixing population
    Lam <- eps_L * L + eps_L * sig_L * LT + (1 - q_w) * Ip + (1 - q_w) * sig_I * IpT + delta_a * A + delta_a * sig_A * AT  # total infectivity
    foi <- b0 * Lam / N_M                                                                                                  # force of infection on untreated susceptibles

    # Rates of change
    dS <- -foi * S
    dST <- -sig_S * foi * ST
    dL <- foi * S - p_sym * k_lat * L - (1 - p_sym) * k_lat * L - phi_L * L + theta_L * LT
    dLT <- sig_S * foi * ST + phi_L * L - theta_L * LT - p_sym * tau_av * k_latT * LT - (1 - p_sym * tau_av) * k_latT * LT
    dIp <- p_sym * k_lat * L - f_rec * alpha_r * Ip - (1 - f_rec) * alpha_r * Ip - phi_I * Ip + theta_I * IpT
    dIpT <- p_sym * tau_av * k_latT * LT + phi_I * Ip - theta_I * IpT - fT_rec * alpha_T * IpT - (1 - fT_rec) * alpha_T * IpT
    dA <- (1 - p_sym) * k_lat * L - eta_r * A - phi_A * A + theta_A * AT
    dAT <- (1 - p_sym * tau_av) * k_latT * LT + phi_A * A - theta_A * AT - eta_T * AT
    dR <- f_rec * alpha_r * Ip + fT_rec * alpha_T * IpT + eta_r * A + eta_T * AT
    dDead <- (1 - f_rec) * alpha_r * Ip + (1 - fT_rec) * alpha_T * IpT

    list(c(dS, dST, dL, dLT, dIp, dIpT, dA, dAT, dR, dDead))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = flu_arino2008_antiviral_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", ST = "#333333", L = "#E8C468", LT = "#E8C468", Ip = "#B71C1C", IpT = "#B71C1C", A = "#E57373", AT = "#E57373", R = "#4E8A3E", Dead = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Pandemic influenza SLIAR model with antiviral treatment and pre-epidemic vaccination/prophylaxis (Arino et al. 2008)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Seasonal influenza SIRS with sinusoidal forcing and an annual vaccination campaign

Respiratory
SIpRV

Seasonal influenza. vaccinating only S assumes vaccine is not wasted on R. Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.

[ILLUSTRATIVE] textbook SIRS-V structure (Keeling & Rohani 2008, Modeling Infectious Diseases in Humans and Animals, Princeton UP, ch. 5). The reproduction-number anchor is Biggerstaff M, Cauchemez S, Reed C, Gambhir M, Finelli L. "Estimates of the… ↗

Show R code
## Seasonal influenza SIRS with sinusoidal forcing and an annual vaccination campaign
## Seasonal influenza.
## vaccinating only S assumes vaccine is not wasted on R.
## Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.
## Reference: [ILLUSTRATIVE] textbook SIRS-V structure (Keeling & Rohani 2008, Modeling Infectious Diseases in Humans and Animals, Princeton UP, ch. 5). The reproduction-number anchor is Biggerstaff M, Cauchemez S, Reed C, Gambhir M, Finelli L. "Estimates of the…
## Source: https://doi.org/10.1186/1471-2334-14-480
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ip, R, V
## Flows (time unit: days):
##   (outside) -> S  : mu * N
##   S -> Ip         : foi * S
##   Ip -> R         : g_rec * Ip
##   R -> S          : omega_R * R
##   S -> V          : ve * vrate * S
##   V -> S          : omega_V * V
##   S -> (outside)  : mu * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3650, by = 5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_base = 1.3,      # mean reproduction number
  g_rec = 0.25,       # recovery rate (4 days) (1/day) [illustrative]
  amp = 0.2,          # seasonal amplitude [illustrative]
  t_peak = 15,        # day of peak transmission (day of year) [illustrative]
  two_pi = 6.283185,  # constant
  omega_R = 1 / 730,  # waning of infection immunity, including drift (1/day) [illustrative]
  omega_V = 1 / 240,  # waning of vaccine protection (1/day) [illustrative]
  cov = 0.45,         # target coverage per campaign [illustrative]
  ve = 0.5,           # vaccine efficacy (all-or-nothing) [illustrative]
  camp_start = 274,   # campaign from Oct 1 for 60 days [illustrative]
  camp_len = 60,      # campaign from Oct 1 for 60 days (day of year, days) [illustrative]
  mu = 3.9e-5         # birth = death rate (1/day) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 0.6 * 1e6,  # susceptible
  Ip = 100,       # infectious
  R = 0.4 * 1e6,  # infection-derived immunity
  V = 0           # vaccine-protected
)

# ---- Model equations -------------------------------------------------
flu_seasonal_vax_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Ip + R + V

    # Derived quantities
    beta_t <- R0_base * g_rec * (1 + amp * cos(two_pi * (t - t_peak) / 365))
    tyear <- t - 365 * floor(t / 365)
    in_camp <- (tyear >= camp_start) * (tyear < camp_start + camp_len)
    vrate <- in_camp * (-log(1 - cov) / camp_len)
    foi <- beta_t * Ip / N

    # Rates of change
    dS <- mu * N - foi * S + omega_R * R - ve * vrate * S + omega_V * V - mu * S
    dIp <- foi * S - g_rec * Ip
    dR <- g_rec * Ip - omega_R * R
    dV <- ve * vrate * S - omega_V * V

    list(c(dS, dIp, dR, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = flu_seasonal_vax_model, parms = parameters, hmax = 5)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ip = "#B71C1C", R = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Seasonal influenza SIRS with sinusoidal forcing and an annual vaccination campaign")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SIDARTHE model of COVID-19 in Italy (Giordano et al. 2020)

Respiratory
SIuDgARdThHEx

COVID-19. Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.

Giordano G, Blanchini F, Bruno R, Colaneri P, Di Filippo A, Di Matteo A, Colaneri M. "Modelling the COVID-19 epidemic and implementation of population-wide interventions in Italy." Nat Med 2020;26:855–860. https://doi.org/10.1038/s41591-020-0883-7 ↗

Show R code
## SIDARTHE model of COVID-19 in Italy (Giordano et al. 2020)
## COVID-19.
## Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.
## Reference: Giordano G, Blanchini F, Bruno R, Colaneri P, Di Filippo A, Di Matteo A, Colaneri M. "Modelling the COVID-19 epidemic and implementation of population-wide interventions in Italy." Nat Med 2020;26:855–860. https://doi.org/10.1038/s41591-020-0883-7
## Source: https://doi.org/10.1038/s41591-020-0883-7
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Iu, Dg, A, Rd, Th, H, Ex
## Flows (time unit: days):
##   S -> Iu   : foi * S
##   Iu -> Dg  : eps_v * Iu
##   Iu -> A   : zeta * Iu
##   Iu -> H   : lambda_h * Iu
##   Dg -> Rd  : eta * Dg
##   Dg -> H   : rho_h * Dg
##   A -> Rd   : theta * A
##   A -> Th   : mu_t * A
##   A -> H    : kappa_h * A
##   Rd -> Th  : nu_t * Rd
##   Rd -> H   : xi_h * Rd
##   Th -> Ex  : tau_t * Th
##   Th -> H   : sigma_h * Th

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 300, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  Npop = 60e6,       # value taken from the notes text [recalled]
  zeta = 0.125,      # value taken from the notes text [recalled]
  lambda_h = 0.034,  # value taken from the notes text [recalled]
  eta = 0.125,       # value taken from the notes text [recalled]
  rho_h = 0.034,     # value taken from the notes text [recalled]
  theta = 0.371,     # value taken from the notes text [recalled]
  mu_t = 0.012,      # value taken from the notes text [recalled]
  kappa_h = 0.017,   # value taken from the notes text [recalled]
  nu_t = 0.027,      # value taken from the notes text [recalled]
  xi_h = 0.017,      # value taken from the notes text [recalled]
  tau_t = 0.003,     # value taken from the notes text [recalled]
  sigma_h = 0.017    # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 60e6 - 223,  # susceptible
  Iu = 200,        # infected, asymptomatic or mild, undetected (I)
  Dg = 20,         # diagnosed, asymptomatic or mild (D)
  A = 1,           # ailing: symptomatic, undetected (A)
  Rd = 2,          # recognised: symptomatic, detected (R)
  Th = 0,          # threatened: life-threatening, detected, in ICU (T)
  H = 0,           # healed
  Ex = 0           # extinct (dead)
)

# ---- Model equations -------------------------------------------------
covid_sidarthe_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Iu + Dg + A + Rd + Th + H

    # Derived quantities
    alpha_v <- ifelse(t >= 3, 0.422, 0.570)
    gamma_v <- ifelse(t >= 3, 0.285, 0.456)
    bd_v <- ifelse(t >= 3, 0.0057, 0.011)
    eps_v <- ifelse(t >= 11, 0.143, 0.171)
    foi <- (alpha_v * Iu + bd_v * Dg + gamma_v * A + bd_v * Rd) / Npop

    # Rates of change
    dS <- -foi * S
    dIu <- foi * S - eps_v * Iu - zeta * Iu - lambda_h * Iu
    dDg <- eps_v * Iu - eta * Dg - rho_h * Dg
    dA <- zeta * Iu - theta * A - mu_t * A - kappa_h * A
    dRd <- eta * Dg + theta * A - nu_t * Rd - xi_h * Rd
    dTh <- mu_t * A + nu_t * Rd - tau_t * Th - sigma_h * Th
    dH <- lambda_h * Iu + rho_h * Dg + kappa_h * A + xi_h * Rd + sigma_h * Th
    dEx <- tau_t * Th

    list(c(dS, dIu, dDg, dA, dRd, dTh, dH, dEx))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = covid_sidarthe_model, parms = parameters, hmax = 0.5)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Iu = "#B71C1C", Dg = "#111111", A = "#E57373", Rd = "#4E8A3E", Th = "#333333", H = "#6A2C9C", Ex = "#E8C468")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SIDARTHE model of COVID-19 in Italy (Giordano et al. 2020)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Early Wuhan SARS-CoV-2 SEIR with Erlang stages and reporting delay (deterministic version of Kucharski et al. 2020)

Respiratory
SE1E2I1I2RmQrCr

COVID-19. Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.

Kucharski AJ, Russell TW, Diamond C, Liu Y, Edmunds J, Funk S, Eggo RM; CMMID COVID-19 working group. "Early dynamics of transmission and control of COVID-19: a mathematical modelling study." Lancet Infect Dis 2020;20:553–558.… ↗

Show R code
## Early Wuhan SARS-CoV-2 SEIR with Erlang stages and reporting delay (deterministic version of Kucharski et al. 2020)
## COVID-19.
## Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.
## Reference: Kucharski AJ, Russell TW, Diamond C, Liu Y, Edmunds J, Funk S, Eggo RM; CMMID COVID-19 working group. "Early dynamics of transmission and control of COVID-19: a mathematical modelling study." Lancet Infect Dis 2020;20:553–558.…
## Source: https://doi.org/10.1016/S1473-3099(20
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E1, E2, I1, I2, Rm, Qr, Cr
## Flows (time unit: days):
##   S -> E1          : foi * S
##   E1 -> E2         : 2 / inc_per * E1
##   E2 -> I1         : 2 / inc_per * E2
##   I1 -> I2         : 2 / inf_per * I1
##   I2 -> Rm         : 2 / inf_per * I2
##   (outside) -> Qr  : p_rep * (2 / inc_per) * E2
##   Qr -> Cr         : 1 / rep_delay * Qr

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 120, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  inc_per = 5.2,    # mean incubation (days)
  inf_per = 2.9,    # mean infectious period (days)
  rep_delay = 6.1,  # mean onset-to-confirmation delay (days)
  R_pre = 2.35,     # R(t) one week before and one week after the 23 Jan 2020 travel restrictions
  R_post = 1.05,    # R(t) one week before and one week after the 23 Jan 2020 travel restrictions
  t_switch = 23,    # date of the change (day) [illustrative]
  p_rep = 1         # fraction of cases eventually confirmed [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 11e6,  # susceptible
  E1 = 20,   # incubating, Erlang-2
  E2 = 0,    # incubating, Erlang-2
  I1 = 10,   # infectious, Erlang-2
  I2 = 0,    # infectious, Erlang-2
  Rm = 0,    # removed
  Qr = 0,    # symptomatic cases awaiting confirmation
  Cr = 0     # cumulative confirmed cases
)

# ---- Model equations -------------------------------------------------
covid_kucharski2020_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E1 + E2 + I1 + I2 + Rm

    # Derived quantities
    Rt <- ifelse(t >= t_switch, R_post, R_pre)
    foi <- Rt / inf_per * (I1 + I2) / N

    # Rates of change
    dS <- -foi * S
    dE1 <- foi * S - 2 / inc_per * E1
    dE2 <- 2 / inc_per * E1 - 2 / inc_per * E2
    dI1 <- 2 / inc_per * E2 - 2 / inf_per * I1
    dI2 <- 2 / inf_per * I1 - 2 / inf_per * I2
    dRm <- 2 / inf_per * I2
    dQr <- p_rep * (2 / inc_per) * E2 - 1 / rep_delay * Qr
    dCr <- 1 / rep_delay * Qr

    list(c(dS, dE1, dE2, dI1, dI2, dRm, dQr, dCr))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = covid_kucharski2020_model, parms = parameters, hmax = 0.2)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E1 = "#E8C468", E2 = "#E8C468", I1 = "#B71C1C", I2 = "#B71C1C", Rm = "#4E8A3E", Qr = "#00838F", Cr = "#8E0E0E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Early Wuhan SARS-CoV-2 SEIR with Erlang stages and reporting delay (deterministic version of Kucharski et al. 2020)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

COVID-19 SEIR-H with two-level vaccine protection, waning and reinfection

Respiratory
SVWvEIpHcRDead

COVID-19. vh_red would need a separate exposed class for vaccinated people (E_v) to act properly. Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.

[ILLUSTRATIVE] generic structure of the kind used in many 2021 vaccination models. Values are chosen to be plausible, not taken from a single paper. Waning anchor: Andrews N et al. "Covid-19 vaccine effectiveness against the Omicron (B.1.1.529) variant." N… ↗

Show R code
## COVID-19 SEIR-H with two-level vaccine protection, waning and reinfection
## COVID-19.
## vh_red would need a separate exposed class for vaccinated people (E_v) to act properly.
## Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.
## Reference: [ILLUSTRATIVE] generic structure of the kind used in many 2021 vaccination models. Values are chosen to be plausible, not taken from a single paper. Waning anchor: Andrews N et al. "Covid-19 vaccine effectiveness against the Omicron (B.1.1.529) variant." N…
## Source: https://doi.org/10.1056/NEJMoa2119451
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, V, Wv, E, Ip, Hc, R, Dead
## Flows (time unit: days):
##   S -> E      : foi * S
##   V -> E      : (1 - ve_V) * foi * V
##   Wv -> E     : (1 - ve_W) * foi * Wv
##   S -> V      : vac_rate * S
##   V -> Wv     : wane_V * V
##   Wv -> V     : boost_rate * Wv
##   R -> Wv     : wane_R * R
##   E -> Ip     : sig_lat * E
##   Ip -> Hc    : p_hosp * g_rec * Ip
##   Ip -> R     : (1 - p_hosp) * g_rec * Ip
##   Hc -> R     : (1 - p_die) * h_rec * Hc
##   Hc -> Dead  : p_die * h_rec * Hc

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 730, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_eff = 3,         # illustrative
  sig_lat = 1 / 3,    # latent period of 3 days (1/day) [illustrative]
  g_rec = 1 / 5,      # infectious period of 5 days (1/day) [illustrative]
  p_hosp = 0.02,      # hospitalised fraction, unvaccinated [illustrative]
  h_rec = 1 / 8,      # hospital stay (1/day) [illustrative]
  p_die = 0.15,       # in-hospital fatality [illustrative]
  vac_rate = 0.005,   # per-capita vaccination rate of S (1/day) [illustrative]
  ve_V = 0.8,         # leaky protection against infection [illustrative]
  ve_W = 0.3,         # leaky protection against infection [illustrative]
  vh_red = 0.8,       # extra reduction in hospitalisation if infected after vaccination [illustrative]
  wane_V = 1 / 120,   # waning from V to Wv (1/day) [illustrative]
  wane_R = 1 / 180,   # waning of infection immunity, R to Wv (1/day) [illustrative]
  boost_rate = 0.002  # booster from Wv back to V (1/day) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 0.7 * 1e6,  # susceptible, unvaccinated
  V = 0,          # recently vaccinated (high protection)
  Wv = 0,         # vaccinated, waned protection
  E = 100,        # exposed
  Ip = 100,       # infectious
  Hc = 0,         # hospitalised
  R = 0.3 * 1e6,  # recovered (hybrid or natural immunity)
  Dead = 0        # COVID deaths
)

# ---- Model equations -------------------------------------------------
covid_vax_waning_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + V + Wv + E + Ip + Hc + R

    # Derived quantities
    foi <- R0_eff * g_rec * Ip / N

    # Rates of change
    dS <- -foi * S - vac_rate * S
    dV <- -(1 - ve_V) * foi * V + vac_rate * S - wane_V * V + boost_rate * Wv
    dWv <- -(1 - ve_W) * foi * Wv + wane_V * V - boost_rate * Wv + wane_R * R
    dE <- foi * S + (1 - ve_V) * foi * V + (1 - ve_W) * foi * Wv - sig_lat * E
    dIp <- sig_lat * E - p_hosp * g_rec * Ip - (1 - p_hosp) * g_rec * Ip
    dHc <- p_hosp * g_rec * Ip - (1 - p_die) * h_rec * Hc - p_die * h_rec * Hc
    dR <- -wane_R * R + (1 - p_hosp) * g_rec * Ip + (1 - p_die) * h_rec * Hc
    dDead <- p_die * h_rec * Hc

    list(c(dS, dV, dWv, dE, dIp, dHc, dR, dDead))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = covid_vax_waning_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", V = "#2457C5", Wv = "#A5D6A7", E = "#E8C468", Ip = "#B71C1C", Hc = "#6A2C9C", R = "#4E8A3E", Dead = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "COVID-19 SEIR-H with two-level vaccine protection, waning and reinfection")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

RSV with seasonal forcing and repeated infections with graded immunity (Weber, Weber & Milligan 2001)

Respiratory
MS1E1I1R1S2E2I2R2

RSV. Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

Weber A, Weber M, Milligan P. "Modeling epidemics caused by respiratory syncytial virus (RSV)." Math Biosci 2001;172(2):95–113. https://doi.org/10.1016/S0025-5564(01)00066-9 ↗

Show R code
## RSV with seasonal forcing and repeated infections with graded immunity (Weber, Weber & Milligan 2001)
## RSV.
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Weber A, Weber M, Milligan P. "Modeling epidemics caused by respiratory syncytial virus (RSV)." Math Biosci 2001;172(2):95–113. https://doi.org/10.1016/S0025-5564(01)00066-9
## Source: https://doi.org/10.1016/S0025-5564(01
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: M, S1, E1, I1, R1, S2, E2, I2, R2
## Flows (time unit: years):
##   (outside) -> M  : mu * N
##   M -> S1         : xi_m * M
##   S1 -> E1        : foi1 * S1
##   E1 -> I1        : sig_lat * E1
##   I1 -> R1        : nu * I1
##   R1 -> S2        : gam_w * R1
##   S2 -> E2        : f2 * foi1 * S2
##   E2 -> I2        : sig_lat * E2
##   I2 -> R2        : nu * I2
##   R2 -> S2        : gam_w * R2
##   M -> (outside)  : mu * M

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  b0 = 60,            # mean transmission rate (1/year) [illustrative]
  b1 = 0.3,           # seasonal amplitude [illustrative]
  phase = 0,          # seasonal phase (rad) [illustrative]
  two_pi = 6.283185,  # constant
  sig_lat = 91.25,    # 4-day latency (1/year)
  nu = 36.5,          # 10-day infectious period (1/year)
  gam_w = 1.825,      # loss of immunity after about 200 days (1/year)
  xi_m = 2,           # loss of maternal protection (about 6 months) (1/year) [illustrative]
  f2 = 0.5,           # relative susceptibility of S2 [illustrative]
  mu = 0.0133,        # birth = death rate, i.e. 1/75 y (1/year) [illustrative]
  N0 = 1e6            # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  M <- 0.01 * N0   # maternally protected
  S1 <- 0.05 * N0  # susceptible, never infected
  E1 <- 0          # exposed, first infection
  I1 <- 100        # infectious, first infection
  R1 <- 0.04 * N0  # temporarily immune after the first infection
  S2 <- 0.85 * N0  # susceptible, previously infected (reduced susceptibility)
  E2 <- 0          # exposed, reinfection
  I2 <- 100        # infectious, reinfection
  R2 <- 0.05 * N0  # temporarily immune after reinfection
  c(M = M, S1 = S1, E1 = E1, I1 = I1, R1 = R1, S2 = S2, E2 = E2, I2 = I2, R2 = R2)
})

# ---- Model equations -------------------------------------------------
rsv_weber2001_graded_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- M + S1 + E1 + I1 + R1 + S2 + E2 + I2 + R2

    # Derived quantities
    beta1 <- b0 * (1 + b1 * cos(two_pi * t + phase))
    foi1 <- beta1 * (I1 + I2) / N

    # Rates of change
    dM <- mu * N - xi_m * M - mu * M
    dS1 <- xi_m * M - foi1 * S1
    dE1 <- foi1 * S1 - sig_lat * E1
    dI1 <- sig_lat * E1 - nu * I1
    dR1 <- nu * I1 - gam_w * R1
    dS2 <- gam_w * R1 - f2 * foi1 * S2 + gam_w * R2
    dE2 <- f2 * foi1 * S2 - sig_lat * E2
    dI2 <- sig_lat * E2 - nu * I2
    dR2 <- nu * I2 - gam_w * R2

    list(c(dM, dS1, dE1, dI1, dR1, dS2, dE2, dI2, dR2))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = rsv_weber2001_graded_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(M = "#F3C6D3", S1 = "#333333", E1 = "#E8C468", I1 = "#B71C1C", R1 = "#4E8A3E", S2 = "#333333", E2 = "#E8C468", I2 = "#B71C1C", R2 = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "RSV with seasonal forcing and repeated infections with graded immunity (Weber, Weber & Milligan 2001)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Pertussis with vaccination, waning, and mild reinfections in partially immune people (Hethcote-style)

Respiratory
SIpRVWIm

Pertussis. Hethcote's 1997 paper is age-structured with many age groups. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Hethcote HW. "An age-structured model for pertussis transmission." Math Biosci 1997;145:89–136. https://doi.org/10.1016/S0025-5564(97)00014-X ↗

Show R code
## Pertussis with vaccination, waning, and mild reinfections in partially immune people (Hethcote-style)
## Pertussis.
## Hethcote's 1997 paper is age-structured with many age groups.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Hethcote HW. "An age-structured model for pertussis transmission." Math Biosci 1997;145:89–136. https://doi.org/10.1016/S0025-5564(97)00014-X
## Source: https://doi.org/10.1016/S0025-5564(97
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ip, R, V, W, Im
## Flows (time unit: years):
##   (outside) -> S  : (1 - p_vac) * mu * N
##   (outside) -> V  : p_vac * mu * N
##   S -> Ip         : foi * S
##   Ip -> R         : g_rec * Ip
##   R -> W          : w_nat * R
##   V -> W          : w_vac * V
##   W -> Im         : foi * W
##   Im -> R         : g_rec * Im
##   S -> (outside)  : mu * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_p = 15,       # illustrative; the literature for pertussis spans roughly 5–17
  g_rec = 17.4,    # 21-day infectious period (1/year) [illustrative]
  rel_m = 0.5,     # relative infectiousness of mild cases [illustrative]
  w_nat = 1 / 30,  # waning of natural immunity (1/year) [illustrative]
  w_vac = 1 / 10,  # waning of vaccine immunity (1/year) [illustrative]
  p_vac = 0.9,     # fraction of births effectively vaccinated [illustrative]
  mu = 0.0133,     # birth = death rate (1/year) [illustrative]
  N0 = 1e6         # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.05 * N0  # fully susceptible (never infected or vaccinated)
  Ip <- 50        # typical (severe) primary infection
  R <- 0.5 * N0   # fully immune after infection
  V <- 0.3 * N0   # vaccine-immune
  W <- 0.15 * N0  # waned, partially immune
  Im <- 50        # mild infection in a waned person
  c(S = S, Ip = Ip, R = R, V = V, W = W, Im = Im)
})

# ---- Model equations -------------------------------------------------
pertussis_hethcote_mild_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Ip + R + V + W + Im

    # Derived quantities
    foi <- R0_p * (g_rec + mu) * (Ip + rel_m * Im) / N

    # Rates of change
    dS <- (1 - p_vac) * mu * N - foi * S - mu * S
    dIp <- foi * S - g_rec * Ip
    dR <- g_rec * Ip - w_nat * R + g_rec * Im
    dV <- p_vac * mu * N - w_vac * V
    dW <- w_nat * R + w_vac * V - foi * W
    dIm <- foi * W - g_rec * Im

    list(c(dS, dIp, dR, dV, dW, dIm))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = pertussis_hethcote_mild_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ip = "#B71C1C", R = "#4E8A3E", V = "#2457C5", W = "#A5D6A7", Im = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Pertussis with vaccination, waning, and mild reinfections in partially immune people (Hethcote-style)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Measles MSEIR with two-dose vaccination (dose-1 failures eligible for dose 2)

Respiratory
MS0SfVEIpR

Measles. Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.

[ILLUSTRATIVE] MSEIR structure from Hethcote HW, "The mathematics of infectious diseases," SIAM Review 2000;42:599–653, https://doi.org/10.1137/S0036144500371907. The values below are from my recall and were not fetched this session: ↗

Show R code
## Measles MSEIR with two-dose vaccination (dose-1 failures eligible for dose 2)
## Measles.
## Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.
## Reference: [ILLUSTRATIVE] MSEIR structure from Hethcote HW, "The mathematics of infectious diseases," SIAM Review 2000;42:599–653, https://doi.org/10.1137/S0036144500371907. The values below are from my recall and were not fetched this session:
## Source: https://doi.org/10.1137/S0036144500371907
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: M, S0, Sf, V, E, Ip, R
## Flows (time unit: years):
##   (outside) -> M  : mu * N
##   M -> S0         : xi_m * M
##   S0 -> V         : e1 * nu1 * S0
##   S0 -> Sf        : (1 - e1) * nu1 * S0
##   Sf -> V         : e2 * nu2 * Sf
##   S0 -> E         : foi * S0
##   Sf -> E         : foi * Sf
##   E -> Ip         : sig_lat * E
##   Ip -> R         : g_rec * Ip
##   M -> (outside)  : mu * M

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_m = 15,          # Guerra 2017 range (recall)
  sig_lat = 365 / 8,  # 8-day latent period (1/year) [illustrative]
  g_rec = 365 / 5,    # 5-day infectious period (1/year) [illustrative]
  xi_m = 2,           # loss of maternal antibody at about 6 months (1/year) [illustrative]
  nu1 = 1.5,          # per-capita dose-1 rate in S0 (1/year) [illustrative]
  nu2 = 0.3,          # dose-2 rate in Sf (1/year) [illustrative]
  e1 = 0.93,          # efficacy of dose 1, and of dose 2 given to a dose-1 failure
  e2 = 0.97,          # efficacy of dose 1, and of dose 2 given to a dose-1 failure
  mu = 0.0133,        # birth = death rate (1/year) [illustrative]
  N0 = 1e6            # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  M <- 0.01 * N0   # maternally protected
  S0 <- 0.05 * N0  # susceptible, unvaccinated
  Sf <- 0.01 * N0  # susceptible after failed or missed dose 1, eligible for dose 2
  V <- 0.6 * N0    # vaccine-immune
  E <- 10          # exposed
  Ip <- 10         # infectious
  R <- 0.33 * N0   # recovered
  c(M = M, S0 = S0, Sf = Sf, V = V, E = E, Ip = Ip, R = R)
})

# ---- Model equations -------------------------------------------------
measles_mseir_2dose_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- M + S0 + Sf + V + E + Ip + R

    # Derived quantities
    foi <- R0_m * (g_rec + mu) * Ip / N

    # Rates of change
    dM <- mu * N - xi_m * M - mu * M
    dS0 <- xi_m * M - e1 * nu1 * S0 - (1 - e1) * nu1 * S0 - foi * S0
    dSf <- (1 - e1) * nu1 * S0 - e2 * nu2 * Sf - foi * Sf
    dV <- e1 * nu1 * S0 + e2 * nu2 * Sf
    dE <- foi * S0 + foi * Sf - sig_lat * E
    dIp <- sig_lat * E - g_rec * Ip
    dR <- g_rec * Ip

    list(c(dM, dS0, dSf, dV, dE, dIp, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = measles_mseir_2dose_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(M = "#F3C6D3", S0 = "#333333", Sf = "#333333", V = "#2457C5", E = "#E8C468", Ip = "#B71C1C", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Measles MSEIR with two-dose vaccination (dose-1 failures eligible for dose 2)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

TB with treatment, acquired and transmitted MDR, and a fitness cost (after Dye & Williams 2000 and Cohen & Murray 2004)

Respiratory
SLsLrIsIrRt

Tuberculosis. Parameter values: 0 of 14 checked against the source; the rest are recalled or illustrative.

Show R code
## TB with treatment, acquired and transmitted MDR, and a fitness cost (after Dye & Williams 2000 and Cohen & Murray 2004)
## Tuberculosis.
## Parameter values: 0 of 14 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ls, Lr, Is, Ir, Rt
## Flows (time unit: years):
##   (outside) -> S   : (mu * N + mu_tb * (Is + Ir))
##   S -> Is          : p_fast * foi_s * S
##   S -> Ls          : (1 - p_fast) * foi_s * S
##   S -> Ir          : p_fast * foi_r * S
##   S -> Lr          : (1 - p_fast) * foi_r * S
##   Ls -> Is         : (v_react + p_fast * (1 - x_prot) * foi_s) * Ls
##   Ls -> Ir         : p_fast * (1 - x_prot) * foi_r * Ls
##   Lr -> Ir         : (v_react + p_fast * (1 - x_prot) * foi_r) * Lr
##   Lr -> Is         : p_fast * (1 - x_prot) * foi_s * Lr
##   Rt -> Is         : (relapse + p_fast * (1 - x_prot) * foi_s) * Rt
##   Rt -> Ir         : p_fast * (1 - x_prot) * foi_r * Rt
##   Is -> Rt         : (cure_nat + det * succ_s) * Is
##   Is -> Ir         : det * a_res * Is
##   Ir -> Rt         : (cure_nat + det * succ_r) * Ir
##   Is -> (outside)  : (mu + mu_tb) * Is

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_s = 10,       # effective contacts per infectious case per year (1/year) [illustrative]
  fit_r = 0.8,       # relative transmissibility of MDR
  p_fast = 0.14,     # fraction who progress quickly after infection [illustrative]
  v_react = 0.0005,  # reactivation from latency (1/year) [illustrative]
  x_prot = 0.65,     # protection of latent and recovered people against reinfection-driven fast progression [illustrative]
  cure_nat = 0.2,    # self-cure (1/year) [illustrative]
  mu_tb = 0.2,       # TB death, untreated (1/year) [illustrative]
  det = 0.7,         # detection and treatment rate (1/year) [illustrative]
  succ_s = 0.85,     # treatment success for DS-TB [illustrative]
  a_res = 0.03,      # fraction of DS treatments that produce MDR [illustrative]
  succ_r = 0.5,      # first-line treatment success for MDR [illustrative]
  relapse = 0.002,   # relapse from Rt to DS active disease (1/year) [illustrative]
  mu = 0.02,         # birth = non-TB death rate (1/year)
  N0 = 1e6           # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.7 * N0     # uninfected
  Ls <- 0.29 * N0   # latent, drug-sensitive
  Lr <- 0           # latent, MDR
  Is <- 0.002 * N0  # active TB, drug-sensitive
  Ir <- 10          # active TB, MDR
  Rt <- 0           # recovered/treated, latent infection cleared or controlled
  c(S = S, Ls = Ls, Lr = Lr, Is = Is, Ir = Ir, Rt = Rt)
})

# ---- Model equations -------------------------------------------------
tb_mdr_treatment_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Ls + Lr + Is + Ir + Rt

    # Derived quantities
    foi_s <- beta_s * Is / N
    foi_r <- beta_s * fit_r * Ir / N

    # Rates of change
    dS <- (mu * N + mu_tb * (Is + Ir)) - p_fast * foi_s * S - (1 - p_fast) * foi_s * S - p_fast * foi_r * S - (1 - p_fast) * foi_r * S
    dLs <- (1 - p_fast) * foi_s * S - (v_react + p_fast * (1 - x_prot) * foi_s) * Ls - p_fast * (1 - x_prot) * foi_r * Ls
    dLr <- (1 - p_fast) * foi_r * S - (v_react + p_fast * (1 - x_prot) * foi_r) * Lr - p_fast * (1 - x_prot) * foi_s * Lr
    dIs <- p_fast * foi_s * S + (v_react + p_fast * (1 - x_prot) * foi_s) * Ls + p_fast * (1 - x_prot) * foi_s * Lr + (relapse + p_fast * (1 - x_prot) * foi_s) * Rt - (cure_nat + det * succ_s) * Is - det * a_res * Is - (mu + mu_tb) * Is
    dIr <- p_fast * foi_r * S + p_fast * (1 - x_prot) * foi_r * Ls + (v_react + p_fast * (1 - x_prot) * foi_r) * Lr + p_fast * (1 - x_prot) * foi_r * Rt + det * a_res * Is - (cure_nat + det * succ_r) * Ir
    dRt <- -(relapse + p_fast * (1 - x_prot) * foi_s) * Rt - p_fast * (1 - x_prot) * foi_r * Rt + (cure_nat + det * succ_s) * Is + (cure_nat + det * succ_r) * Ir

    list(c(dS, dLs, dLr, dIs, dIr, dRt))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = tb_mdr_treatment_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ls = "#E8C468", Lr = "#E8C468", Is = "#B71C1C", Ir = "#B71C1C", Rt = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "TB with treatment, acquired and transmitted MDR, and a fitness cost (after Dye & Williams 2000 and Cohen & Murray 2004)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Pneumococcal carriage: vaccine-type (VT) and non-vaccine-type (NVT) serotypes with competition (Lipsitch 1997)

Respiratory
XY1Y2Y12XvY1vY2vY12v

Streptococcus pneumoniae carriage. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Lipsitch M. "Vaccination against colonizing bacteria with multiple serotypes." PNAS 1997;94:6571–6576. https://doi.org/10.1073/pnas.94.12.6571 (PMC: https://pmc.ncbi.nlm.nih.gov/articles/PMC21091) ↗

Show R code
## Pneumococcal carriage: vaccine-type (VT) and non-vaccine-type (NVT) serotypes with competition (Lipsitch 1997)
## Streptococcus pneumoniae carriage.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Lipsitch M. "Vaccination against colonizing bacteria with multiple serotypes." PNAS 1997;94:6571–6576. https://doi.org/10.1073/pnas.94.12.6571 (PMC: https://pmc.ncbi.nlm.nih.gov/articles/PMC21091)
## Source: https://doi.org/10.1073/pnas.94.12.6571
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: X, Y1, Y2, Y12, Xv, Y1v, Y2v, Y12v
## Flows (time unit: days):
##   (outside) -> X   : (1 - p_vac) * mu * N
##   (outside) -> Xv  : p_vac * mu * N
##   X -> Y1          : foi1 * X
##   X -> Y2          : foi2 * X
##   Y1 -> Y12        : comp * foi2 * Y1
##   Y2 -> Y12        : comp * foi1 * Y2
##   Y1 -> X          : clr * Y1
##   Y2 -> X          : clr * Y2
##   Y12 -> Y2        : clr * Y12
##   Y12 -> Y1        : clr * Y12
##   Xv -> Y1v        : (1 - ve) * foi1 * Xv
##   Xv -> Y2v        : foi2 * Xv
##   Y1v -> Y12v      : comp * foi2 * Y1v
##   Y2v -> Y12v      : comp * (1 - ve) * foi1 * Y2v
##   Y1v -> Xv        : clr * Y1v
##   Y2v -> Xv        : clr * Y2v
##   Y12v -> Y2v      : clr * Y12v
##   Y12v -> Y1v      : clr * Y12v
##   X -> (outside)   : mu * X

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3650, by = 5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  b_vt = 0.06,         # acquisition rate per carrier contact [illustrative]
  b_nvt = 0.055,       # acquisition rate per carrier contact (1/day) [illustrative]
  clr = 1 / 42,        # clearance of each serotype (carriage of about 6 weeks) (1/day) [illustrative]
  comp = 0.3,          # relative rate at which a carrier acquires the second serotype (1 = no competition) [illustrative]
  ve = 0.9,            # vaccine reduction in VT acquisition [illustrative]
  p_vac = 0.9,         # fraction of births vaccinated [illustrative]
  mu = 1 / (5 * 365),  # turnover of the at-risk population (children under 5) (1/day) [illustrative]
  N0 = 1e5             # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  X <- 0.6 * N0    # unvaccinated, uncolonised
  Y1 <- 0.15 * N0  # unvaccinated, carrying VT
  Y2 <- 0.15 * N0  # unvaccinated, carrying NVT
  Y12 <- 0.1 * N0  # unvaccinated, carrying both
  Xv <- 0          # the same four states in vaccinated people
  Y1v <- 0         # the same four states in vaccinated people
  Y2v <- 0         # the same four states in vaccinated people
  Y12v <- 0        # the same four states in vaccinated people
  c(X = X, Y1 = Y1, Y2 = Y2, Y12 = Y12, Xv = Xv, Y1v = Y1v, Y2v = Y2v, Y12v = Y12v)
})

# ---- Model equations -------------------------------------------------
pneumo_vt_nvt_lipsitch_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- X + Y1 + Y2 + Y12 + Xv + Y1v + Y2v + Y12v

    # Derived quantities
    foi1 <- b_vt * (Y1 + Y12 + Y1v + Y12v) / N
    foi2 <- b_nvt * (Y2 + Y12 + Y2v + Y12v) / N

    # Rates of change
    dX <- (1 - p_vac) * mu * N - foi1 * X - foi2 * X + clr * Y1 + clr * Y2 - mu * X
    dY1 <- foi1 * X - comp * foi2 * Y1 - clr * Y1 + clr * Y12
    dY2 <- foi2 * X - comp * foi1 * Y2 - clr * Y2 + clr * Y12
    dY12 <- comp * foi2 * Y1 + comp * foi1 * Y2 - clr * Y12 - clr * Y12
    dXv <- p_vac * mu * N - (1 - ve) * foi1 * Xv - foi2 * Xv + clr * Y1v + clr * Y2v
    dY1v <- (1 - ve) * foi1 * Xv - comp * foi2 * Y1v - clr * Y1v + clr * Y12v
    dY2v <- foi2 * Xv - comp * (1 - ve) * foi1 * Y2v - clr * Y2v + clr * Y12v
    dY12v <- comp * foi2 * Y1v + comp * (1 - ve) * foi1 * Y2v - clr * Y12v - clr * Y12v

    list(c(dX, dY1, dY2, dY12, dXv, dY1v, dY2v, dY12v))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = pneumo_vt_nvt_lipsitch_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(X = "#333333", Y1 = "#333333", Y2 = "#333333", Y12 = "#333333", Xv = "#333333", Y1v = "#333333", Y2v = "#333333", Y12v = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Pneumococcal carriage: vaccine-type (VT) and non-vaccine-type (NVT) serotypes with competition (Lipsitch 1997)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Varicella with latent VZV, exogenous boosting and zoster reactivation (after Brisson et al. 2000)

Respiratory
MSEIpLbZsZoRzV

Varicella / herpes zoster. Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.

Show R code
## Varicella with latent VZV, exogenous boosting and zoster reactivation (after Brisson et al. 2000)
## Varicella / herpes zoster.
## Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: M, S, E, Ip, Lb, Zs, Zo, Rz, V
## Flows (time unit: years):
##   (outside) -> M  : (1 - p_vac) * mu * N
##   (outside) -> V  : p_vac * mu * N
##   M -> S          : xi_m * M
##   S -> E          : foi * S
##   E -> Ip         : sig_lat * E
##   Ip -> Lb        : g_rec * Ip
##   Lb -> Zs        : w_boost * Lb
##   Zs -> Lb        : boost_f * foi * Zs
##   Zs -> Zo        : react * Zs
##   Zo -> Rz        : z_rec * Zo
##   M -> (outside)  : mu * M

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_v = 8,            # illustrative (varicella R0 is often given as 7–12)
  sig_lat = 365 / 14,  # 14-day latent period (1/year) [illustrative]
  g_rec = 365 / 7,     # 7-day infectious period (1/year) [illustrative]
  xi_m = 2,            # loss of maternal protection (1/year) [illustrative]
  w_boost = 1 / 20,    # loss of boosted protection (1/year)
  boost_f = 1,         # fraction of varicella exposures that boost latent people [illustrative]
  react = 0.004,       # zoster reactivation rate in Zs (1/year) [illustrative]
  z_rec = 365 / 21,    # zoster duration (1/year) [illustrative]
  rel_z = 0.2,         # infectiousness of zoster relative to varicella [illustrative]
  p_vac = 0,           # fraction of births vaccinated; vary between 0 and 0.9 [illustrative]
  mu = 0.0133,         # birth = death rate (1/year) [illustrative]
  N0 = 1e6             # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  M <- 0.005 * N0  # maternally protected
  S <- 0.08 * N0   # susceptible to varicella
  E <- 0           # latent (pre-infectious) varicella
  Ip <- 100        # varicella, infectious
  Lb <- 0.5 * N0   # VZV-latent, boosted (protected from zoster)
  Zs <- 0.41 * N0  # VZV-latent, susceptible to zoster
  Zo <- 0          # active zoster
  Rz <- 0          # post-zoster, immune
  V <- 0           # varicella-vaccinated, immune; vaccine-strain latency ignored
  c(M = M, S = S, E = E, Ip = Ip, Lb = Lb, Zs = Zs, Zo = Zo, Rz = Rz, V = V)
})

# ---- Model equations -------------------------------------------------
varicella_zoster_brisson_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- M + S + E + Ip + Lb + Zs + Zo + Rz + V

    # Derived quantities
    foi <- R0_v * (g_rec + mu) * (Ip + rel_z * Zo) / N

    # Rates of change
    dM <- (1 - p_vac) * mu * N - xi_m * M - mu * M
    dS <- xi_m * M - foi * S
    dE <- foi * S - sig_lat * E
    dIp <- sig_lat * E - g_rec * Ip
    dLb <- g_rec * Ip - w_boost * Lb + boost_f * foi * Zs
    dZs <- w_boost * Lb - boost_f * foi * Zs - react * Zs
    dZo <- react * Zs - z_rec * Zo
    dRz <- z_rec * Zo
    dV <- p_vac * mu * N

    list(c(dM, dS, dE, dIp, dLb, dZs, dZo, dRz, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = varicella_zoster_brisson_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(M = "#F3C6D3", S = "#333333", E = "#E8C468", Ip = "#B71C1C", Lb = "#E8C468", Zs = "#333333", Zo = "#333333", Rz = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Varicella with latent VZV, exogenous boosting and zoster reactivation (after Brisson et al. 2000)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Rubella with two age groups, routine infant vaccination, and congenital rubella syndrome (CRS) as an output

Respiratory
SchIchRchSadIadRadVCRS

Rubella / CRS. Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Show R code
## Rubella with two age groups, routine infant vaccination, and congenital rubella syndrome (CRS) as an output
## Rubella / CRS.
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sch, Ich, Rch, Sad, Iad, Rad, V, CRS
## Flows (time unit: years):
##   (outside) -> Sch  : (1 - p_vac) * births
##   (outside) -> V    : p_vac * births
##   Sch -> Ich        : foi * Sch
##   Ich -> Rch        : g_rec * Ich
##   Sad -> Iad        : foi * Sad
##   Iad -> Rad        : g_rec * Iad
##   Sch -> Sad        : age_out * Sch
##   Ich -> Iad        : age_out * Ich
##   Rch -> Rad        : age_out * Rch
##   Sad -> (outside)  : mu_ad * Sad
##   V -> (outside)    : 1 / 70 * V
##   (outside) -> CRS  : crs_rate

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 80, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_r = 7,           # illustrative; commonly cited range about 6–7
  g_rec = 365 / 11,   # 11-day infectious period (1/year) [illustrative]
  age_out = 1 / 15,   # ageing from child to adult (1/year)
  mu_ad = 1 / 55,     # adult death rate, so life expectancy is 70 (1/year) [illustrative]
  p_vac = 0.8,        # fraction of births vaccinated [illustrative]
  preg_frac = 0.015,  # fraction of adults who are women in the first 16 weeks of pregnancy at any time [illustrative]
  q_crs = 0.6,        # probability of CRS given infection in that window, averaged over weeks 0–16 [illustrative]
  births = 1e6 / 70,  # constant births, roughly balancing deaths (persons/year) [illustrative]
  N0 = 1e6            # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  Sch <- 0.1 * N0   # susceptible child, 0–14 y
  Ich <- 50         # infectious child
  Rch <- 0.1 * N0   # immune child
  Sad <- 0.05 * N0  # susceptible adult, 15+ y
  Iad <- 50         # infectious adult
  Rad <- 0.6 * N0   # immune adult
  V <- 0.15 * N0    # vaccinated, all ages
  CRS <- 0          # cumulative CRS births
  c(Sch = Sch, Ich = Ich, Rch = Rch, Sad = Sad, Iad = Iad, Rad = Rad, V = V, CRS = CRS)
})

# ---- Model equations -------------------------------------------------
rubella_crs_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sch + Ich + Rch + Sad + Iad + Rad + V

    # Derived quantities
    foi <- R0_r * g_rec * (Ich + Iad) / N
    crs_rate <- q_crs * preg_frac * foi * Sad

    # Rates of change
    dSch <- (1 - p_vac) * births - foi * Sch - age_out * Sch
    dIch <- foi * Sch - g_rec * Ich - age_out * Ich
    dRch <- g_rec * Ich - age_out * Rch
    dSad <- -foi * Sad + age_out * Sch - mu_ad * Sad
    dIad <- foi * Sad - g_rec * Iad + age_out * Ich
    dRad <- g_rec * Iad + age_out * Rch
    dV <- p_vac * births - 1 / 70 * V
    dCRS <- crs_rate

    list(c(dSch, dIch, dRch, dSad, dIad, dRad, dV, dCRS))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = rubella_crs_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sch = "#333333", Ich = "#B71C1C", Rch = "#4E8A3E", Sad = "#333333", Iad = "#B71C1C", Rad = "#4E8A3E", V = "#2457C5", CRS = "#8E0E0E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Rubella with two age groups, routine infant vaccination, and congenital rubella syndrome (CRS) as an output")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Mumps SEIR with vaccine-derived immunity that wanes (Lewnard & Grad 2018)

Respiratory
SEIpRV

Mumps. the booster flow acts on all of S, so it also reaches never-vaccinated people. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Lewnard JA, Grad YH. "Vaccine waning and mumps re-emergence in the United States." Sci Transl Med 2018;10(433):eaao5945. https://doi.org/10.1126/scitranslmed.aao5945 ↗

Show R code
## Mumps SEIR with vaccine-derived immunity that wanes (Lewnard & Grad 2018)
## Mumps.
## the booster flow acts on all of S, so it also reaches never-vaccinated people.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Lewnard JA, Grad YH. "Vaccine waning and mumps re-emergence in the United States." Sci Transl Med 2018;10(433):eaao5945. https://doi.org/10.1126/scitranslmed.aao5945
## Source: https://doi.org/10.1126/scitranslmed.aao5945
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, Ip, R, V
## Flows (time unit: years):
##   (outside) -> S  : (1 - p_vac) * mu * N
##   (outside) -> V  : p_vac * mu * N
##   S -> E          : foi * S
##   E -> Ip         : sig_lat * E
##   Ip -> R         : g_rec * Ip
##   V -> S          : w_vac * V
##   S -> V          : boost_v * S
##   S -> (outside)  : mu * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_mu = 7,           # illustrative
  sig_lat = 365 / 17,  # 17-day latent period (1/year) [illustrative]
  g_rec = 365 / 8,     # 8-day infectious period (1/year) [illustrative]
  p_vac = 0.92,        # fraction of births effectively vaccinated (coverage × efficacy) [illustrative]
  w_vac = 1 / 27,      # waning of vaccine immunity (1/year)
  boost_v = 0,         # rate of a booster (third) dose that returns S to V; set > 0 for scenarios (1/year) [illustrative]
  mu = 0.0133,         # birth = death rate (1/year) [illustrative]
  N0 = 1e6             # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.05 * N0  # susceptible
  E <- 10         # exposed
  Ip <- 10        # infectious
  R <- 0.3 * N0   # infection-derived immunity, lifelong
  V <- 0.65 * N0  # vaccine-derived immunity
  c(S = S, E = E, Ip = Ip, R = R, V = V)
})

# ---- Model equations -------------------------------------------------
mumps_waning_lewnard_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + Ip + R + V

    # Derived quantities
    foi <- R0_mu * (g_rec + mu) * Ip / N

    # Rates of change
    dS <- (1 - p_vac) * mu * N - foi * S + w_vac * V - boost_v * S - mu * S
    dE <- foi * S - sig_lat * E
    dIp <- sig_lat * E - g_rec * Ip
    dR <- g_rec * Ip
    dV <- p_vac * mu * N - w_vac * V + boost_v * S

    list(c(dS, dE, dIp, dR, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = mumps_waning_lewnard_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", Ip = "#B71C1C", R = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Mumps SEIR with vaccine-derived immunity that wanes (Lewnard & Grad 2018)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Meningococcal (serogroup C) carriage SCIS model with conjugate vaccine that protects against carriage (Trotter et al. 2005)

Respiratory
SCaRimVCases

Neisseria meningitidis C (applies to Hib with relabelling). Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.

Trotter CL, Gay NJ, Edmunds WJ. "Dynamic models of meningococcal carriage, disease, and the impact of serogroup C conjugate vaccination." Am J Epidemiol 2005;162:89–100. https://doi.org/10.1093/aje/kwi160 (record: https://researchonline.lshtm.ac.uk/6806/) ↗

Show R code
## Meningococcal (serogroup C) carriage SCIS model with conjugate vaccine that protects against carriage (Trotter et al. 2005)
## Neisseria meningitidis C (applies to Hib with relabelling).
## Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.
## Reference: Trotter CL, Gay NJ, Edmunds WJ. "Dynamic models of meningococcal carriage, disease, and the impact of serogroup C conjugate vaccination." Am J Epidemiol 2005;162:89–100. https://doi.org/10.1093/aje/kwi160 (record: https://researchonline.lshtm.ac.uk/6806/)
## Source: https://doi.org/10.1093/aje/kwi160
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ca, Rim, V, Cases
## Flows (time unit: years):
##   (outside) -> S      : (1 - p_vac) * mu * N
##   (outside) -> V      : p_vac * mu * N
##   S -> Ca             : foi * S
##   V -> Ca             : (1 - ve_c) * foi * V
##   Ca -> Rim           : clr * Ca
##   Rim -> S            : w_nat * Rim
##   V -> S              : w_vac * V
##   S -> V              : vac_rate * S
##   Rim -> V            : vac_rate * Rim
##   (outside) -> Cases  : ccr * acq
##   S -> (outside)      : mu * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  R0_c = 1.36,   # Trotter 2005 abstract
  clr = 2,       # carriage clearance (about 6 months) (1/year) [illustrative]
  w_nat = 0.5,   # waning of immunity after carriage (1/year) [illustrative]
  ve_c = 0.75,   # vaccine efficacy against acquiring carriage [illustrative]
  w_vac = 0.1,   # waning of vaccine protection (1/year) [illustrative]
  vac_rate = 0,  # catch-up vaccination of S and Rim, e.g. 2 during a campaign (1/year) [illustrative]
  p_vac = 0.9,   # fraction of births vaccinated [illustrative]
  ccr = 1e-4,    # invasive cases per carriage acquisition (case:carrier ratio) [illustrative]
  mu = 0.0133,   # birth = death rate (1/year) [illustrative]
  N0 = 1e6       # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S <- 0.6 * N0     # susceptible to carriage
  Ca <- 0.01 * N0   # carrier
  Rim <- 0.39 * N0  # immune after carriage
  V <- 0            # vaccinated, protected
  Cases <- 0        # cumulative invasive disease cases
  c(S = S, Ca = Ca, Rim = Rim, V = V, Cases = Cases)
})

# ---- Model equations -------------------------------------------------
meningo_carriage_trotter_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Ca + Rim + V

    # Derived quantities
    foi <- R0_c * (clr + mu) * Ca / N
    acq <- foi * S + (1 - ve_c) * foi * V  # total acquisitions

    # Rates of change
    dS <- (1 - p_vac) * mu * N - foi * S + w_nat * Rim + w_vac * V - vac_rate * S - mu * S
    dCa <- foi * S + (1 - ve_c) * foi * V - clr * Ca
    dRim <- clr * Ca - w_nat * Rim - vac_rate * Rim
    dV <- p_vac * mu * N - (1 - ve_c) * foi * V - w_vac * V + vac_rate * S + vac_rate * Rim
    dCases <- ccr * acq

    list(c(dS, dCa, dRim, dV, dCases))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = meningo_carriage_trotter_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ca = "#8E0E0E", Rim = "#4E8A3E", V = "#2457C5", Cases = "#8E0E0E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Meningococcal (serogroup C) carriage SCIS model with conjugate vaccine that protects against carriage (Trotter et al. 2005)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

SARS SEIJR model with diagnosis/isolation and two susceptibility classes (Chowell et al. 2003/2004)

Respiratory
S1S2EIpJRDead

SARS (the template also suits MERS). Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

Show R code
## SARS SEIJR model with diagnosis/isolation and two susceptibility classes (Chowell et al. 2003/2004)
## SARS (the template also suits MERS).
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S1, S2, E, Ip, J, R, Dead
## Flows (time unit: days):
##   S1 -> E     : foi * S1
##   S2 -> E     : p_low * foi * S2
##   E -> Ip     : k_inc * E
##   Ip -> J     : alpha_d * Ip
##   Ip -> R     : g1 * Ip
##   Ip -> Dead  : delta_d * Ip
##   J -> R      : g2 * J
##   J -> Dead   : delta_d * J

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 150, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_s = 0.25,     # transmission rate (1/day)
  q_e = 0.1,         # relative infectiousness of E
  l_iso = 0.43,      # relative infectiousness of J
  p_low = 1 / 3,     # relative susceptibility of S2
  rho_hr = 0.77,     # initial fraction in S1
  k_inc = 0.15707,   # E to Ip (6.4-day incubation) (1/day)
  alpha_d = 0.2061,  # diagnosis and isolation (4.85 days); Toronto used 1/3 after controls (1/day)
  g1 = 0.035285,     # recovery from Ip (1/day)
  g2 = 0.0426,       # recovery from J (1/day)
  delta_d = 0.0279,  # disease death rate (1/day)
  N0 = 6.7e6         # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- with(as.list(parameters), {
  S1 <- rho_hr * N0        # high-risk susceptible
  S2 <- (1 - rho_hr) * N0  # lower-risk susceptible (relative susceptibility p_low)
  E <- 0                   # exposed, low infectivity
  Ip <- 1                  # symptomatic infectious, undiagnosed
  J <- 0                   # diagnosed and isolated
  R <- 0                   # recovered
  Dead <- 0                # SARS deaths
  c(S1 = S1, S2 = S2, E = E, Ip = Ip, J = J, R = R, Dead = Dead)
})

# ---- Model equations -------------------------------------------------
sars_chowell_seijr_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S1 + S2 + E + Ip + J + R

    # Derived quantities
    foi <- beta_s * (Ip + q_e * E + l_iso * J) / N

    # Rates of change
    dS1 <- -foi * S1
    dS2 <- -p_low * foi * S2
    dE <- foi * S1 + p_low * foi * S2 - k_inc * E
    dIp <- k_inc * E - alpha_d * Ip - g1 * Ip - delta_d * Ip
    dJ <- alpha_d * Ip - g2 * J - delta_d * J
    dR <- g1 * Ip + g2 * J
    dDead <- delta_d * Ip + delta_d * J

    list(c(dS1, dS2, dE, dIp, dJ, dR, dDead))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = sars_chowell_seijr_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S1 = "#333333", S2 = "#333333", E = "#E8C468", Ip = "#B71C1C", J = "#333333", R = "#4E8A3E", Dead = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "SARS SEIJR model with diagnosis/isolation and two susceptibility classes (Chowell et al. 2003/2004)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

HIV universal test-and-treat (4 untreated + 4 ART stages)

Sexually transmitted & blood-borne
SI1I2I3I4A1A2A3A4

HIV, generalised heterosexual epidemic (South Africa). Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.

Granich RM, Gilks CF, Dye C, De Cock KM, Williams BG. Universal voluntary HIV testing with immediate antiretroviral therapy as a strategy for elimination of HIV transmission: a mathematical model. Lancet 2009;373(9657):48–57 ↗

Show R code
## HIV universal test-and-treat (4 untreated + 4 ART stages)
## HIV, generalised heterosexual epidemic (South Africa).
## Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.
## Reference: Granich RM, Gilks CF, Dye C, De Cock KM, Williams BG. Universal voluntary HIV testing with immediate antiretroviral therapy as a strategy for elimination of HIV transmission: a mathematical model. Lancet 2009;373(9657):48–57
## Source: https://doi.org/10.1016/S0140-6736(08
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, I1, I2, I3, I4, A1, A2, A3, A4
## Flows (time unit: years):
##   (outside) -> S   : (mu * N + rho_p * I4 + rho_a * A4)
##   S -> I1          : foi * S
##   I1 -> I2         : rho_p * I1
##   I2 -> I3         : rho_p * I2
##   I3 -> I4         : rho_p * I3
##   I4 -> (outside)  : rho_p * I4
##   I1 -> A1         : tau_t * I1
##   I2 -> A2         : tau_t * I2
##   I3 -> A3         : tau_t * I3
##   I4 -> A4         : tau_t * I4
##   A1 -> A2         : rho_a * A1
##   A2 -> A3         : rho_a * A2
##   A3 -> A4         : rho_a * A3
##   A4 -> (outside)  : rho_a * A4
##   A1 -> I1         : dropout * A1
##   A2 -> I2         : dropout * A2
##   A3 -> I3         : dropout * A3
##   A4 -> I4         : dropout * A4
##   S -> (outside)   : mu * S
##   I1 -> (outside)  : mu * I1
##   I2 -> (outside)  : mu * I2
##   I3 -> (outside)  : mu * I3
##   I4 -> (outside)  : mu * I4
##   A1 -> (outside)  : mu * A1
##   A2 -> (outside)  : mu * A2
##   A3 -> (outside)  : mu * A3
##   A4 -> (outside)  : mu * A4

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  lambda0 = 0.6,    # transmission coefficient at low prevalence (/yr) [illustrative]
  alpha_h = 4,      # strength of the prevalence-dependent decline in transmission [illustrative]
  n_h = 1,          # shape exponent of that decline [illustrative]
  rho_p = 0.4,      # progression rate between untreated stages (4 stages of 2.5 yr give about 10 yr mean survival) (/yr) [illustrative]
  rho_a = 0.1,      # progression between stages while on ART (slower) (/yr) [illustrative]
  eps_art = 0.01,   # relative infectiousness on ART (99% reduction) [recalled]
  tau = 1,          # testing plus ART initiation rate; 1/yr = annual universal test-and-treat (/yr) [illustrative]
  dropout = 0.015,  # ART discontinuation (/yr) [illustrative]
  mu = 0.02,        # background adult mortality/exit (/yr) [illustrative]
  t_start = 10      # year the strategy starts (yr) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 0.85,   # susceptible adults aged 15+
  I1 = 0.04,  # untreated HIV, stage 1
  I2 = 0.04,  # untreated HIV, stage 2
  I3 = 0.04,  # untreated HIV, stage 3
  I4 = 0.03,  # untreated HIV, stage 4 (AIDS)
  A1 = 0,     # on ART, entered from stage 1
  A2 = 0,     # on ART, entered from stage 2
  A3 = 0,     # on ART, entered from stage 3
  A4 = 0      # on ART, entered from stage 4
)

# ---- Model equations -------------------------------------------------
hiv_art_tasp_granich_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + I1 + I2 + I3 + I4 + A1 + A2 + A3 + A4

    # Derived quantities
    P <- (I1 + I2 + I3 + I4 + A1 + A2 + A3 + A4) / N                          # HIV prevalence
    lam_eff <- lambda0 * exp(-alpha_h * P^n_h)                                # saturating transmission rate (heterogeneity proxy)
    foi <- lam_eff * (I1 + I2 + I3 + I4 + eps_art * (A1 + A2 + A3 + A4)) / N  # force of infection
    tau_t <- ifelse(t >= t_start, tau, 0)                                     # time-switched testing/ART rate

    # Rates of change
    dS <- (mu * N + rho_p * I4 + rho_a * A4) - foi * S - mu * S
    dI1 <- foi * S - rho_p * I1 - tau_t * I1 + dropout * A1 - mu * I1
    dI2 <- rho_p * I1 - rho_p * I2 - tau_t * I2 + dropout * A2 - mu * I2
    dI3 <- rho_p * I2 - rho_p * I3 - tau_t * I3 + dropout * A3 - mu * I3
    dI4 <- rho_p * I3 - rho_p * I4 - tau_t * I4 + dropout * A4 - mu * I4
    dA1 <- tau_t * I1 - rho_a * A1 - dropout * A1 - mu * A1
    dA2 <- tau_t * I2 + rho_a * A1 - rho_a * A2 - dropout * A2 - mu * A2
    dA3 <- tau_t * I3 + rho_a * A2 - rho_a * A3 - dropout * A3 - mu * A3
    dA4 <- tau_t * I4 + rho_a * A3 - rho_a * A4 - dropout * A4 - mu * A4

    list(c(dS, dI1, dI2, dI3, dI4, dA1, dA2, dA3, dA4))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hiv_art_tasp_granich_model, parms = parameters, hmax = 0.05)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", I1 = "#B71C1C", I2 = "#B71C1C", I3 = "#B71C1C", I4 = "#B71C1C", A1 = "#E57373", A2 = "#E57373", A3 = "#E57373", A4 = "#E57373")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "HIV universal test-and-treat (4 untreated + 4 ART stages)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

HIV in core and non-core risk groups with preferential mixing and ART

Sexually transmitted & blood-borne
S_hI_hY_hA_hS_lI_lY_lA_l

HIV, heterogeneous sexual activity. Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.

The core-group concept and the mixing structure come from Hethcote HW, Yorke JA. Gonorrhea Transmission Dynamics and Control. Lecture Notes in Biomathematics 56, Springer, 1984 ↗

Show R code
## HIV in core and non-core risk groups with preferential mixing and ART
## HIV, heterogeneous sexual activity.
## Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.
## Reference: The core-group concept and the mixing structure come from Hethcote HW, Yorke JA. Gonorrhea Transmission Dynamics and Control. Lecture Notes in Biomathematics 56, Springer, 1984
## Source: https://doi.org/10.1007/978-3-662-07544-9
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S_h, I_h, Y_h, A_h, S_l, I_l, Y_l, A_l
## Flows (time unit: years):
##   (outside) -> S_h  : f_h * (mu * N + delta_y * (Y_h + Y_l))
##   (outside) -> S_l  : (1 - f_h) * (mu * N + delta_y * (Y_h + Y_l))
##   S_h -> I_h        : foi_h * S_h
##   S_l -> I_l        : foi_l * S_l
##   I_h -> Y_h        : nu_i * I_h
##   I_l -> Y_l        : nu_i * I_l
##   I_h -> A_h        : tau * I_h
##   I_l -> A_l        : tau * I_l
##   Y_h -> (outside)  : delta_y * Y_h
##   Y_l -> (outside)  : delta_y * Y_l
##   S_h -> (outside)  : mu * S_h
##   I_h -> (outside)  : mu * I_h
##   Y_h -> (outside)  : mu * Y_h
##   A_h -> (outside)  : mu * A_h
##   S_l -> (outside)  : mu * S_l
##   I_l -> (outside)  : mu * I_l
##   Y_l -> (outside)  : mu * Y_l
##   A_l -> (outside)  : mu * A_l

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 60, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp_h = 10,       # partner change rate, core (partners/yr) [illustrative]
  cp_l = 1,        # partner change rate, non-core (partners/yr) [illustrative]
  beta_p = 0.05,   # transmission probability per partnership (per partnership) [illustrative]
  eps_mix = 0.5,   # assortativity: 0 = proportionate mixing, 1 = fully assortative [illustrative]
  nu_i = 0.1,      # progression from chronic infection to AIDS (/yr) [illustrative]
  delta_y = 1,     # AIDS mortality (/yr) [illustrative]
  tau = 0.2,       # ART initiation (/yr) [illustrative]
  eps_art = 0.04,  # relative infectiousness on ART (96% reduction; HPTN 052, Cohen et al. NEJM 2011, DOI 10.1056/NEJMoa1105243) [recalled]
  mu = 0.03,       # exit from the sexually active population (/yr) [illustrative]
  f_h = 0.05       # fraction of recruits entering the core [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S_h = 0.049,  # susceptible, high activity (core)
  I_h = 0.001,  # infected untreated, high activity
  Y_h = 0,      # AIDS, high activity
  A_h = 0,      # on ART, high activity
  S_l = 0.95,   # susceptible, low activity
  I_l = 0,      # infected untreated, low activity
  Y_l = 0,      # AIDS, low activity
  A_l = 0       # on ART, low activity
)

# ---- Model equations -------------------------------------------------
hiv_core_noncore_mixing_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S_h + I_h + Y_h + A_h + S_l + I_l + Y_l + A_l

    # Derived quantities
    N_h <- S_h + I_h + Y_h + A_h                                                          # core group total
    N_l <- S_l + I_l + Y_l + A_l                                                          # non-core group total
    Ninf_h <- I_h + eps_art * A_h                                                         # effective infectious count, core
    Ninf_l <- I_l + eps_art * A_l                                                         # effective infectious count, non-core
    act_h <- cp_h * (N_h - Y_h)                                                           # partnerships offered by the core (AIDS cases assumed inactive)
    act_l <- cp_l * (N_l - Y_l)                                                           # partnerships offered by the non-core
    prop_inf <- (cp_h * Ninf_h + cp_l * Ninf_l) / (act_h + act_l)                         # infectious fraction among partnerships under proportionate mixing
    foi_h <- cp_h * beta_p * (eps_mix * Ninf_h / (N_h - Y_h) + (1 - eps_mix) * prop_inf)  # force of infection, core
    foi_l <- cp_l * beta_p * (eps_mix * Ninf_l / (N_l - Y_l) + (1 - eps_mix) * prop_inf)  # force of infection, non-core

    # Rates of change
    dS_h <- f_h * (mu * N + delta_y * (Y_h + Y_l)) - foi_h * S_h - mu * S_h
    dI_h <- foi_h * S_h - nu_i * I_h - tau * I_h - mu * I_h
    dY_h <- nu_i * I_h - delta_y * Y_h - mu * Y_h
    dA_h <- tau * I_h - mu * A_h
    dS_l <- (1 - f_h) * (mu * N + delta_y * (Y_h + Y_l)) - foi_l * S_l - mu * S_l
    dI_l <- foi_l * S_l - nu_i * I_l - tau * I_l - mu * I_l
    dY_l <- nu_i * I_l - delta_y * Y_l - mu * Y_l
    dA_l <- tau * I_l - mu * A_l

    list(c(dS_h, dI_h, dY_h, dA_h, dS_l, dI_l, dY_l, dA_l))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hiv_core_noncore_mixing_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S_h = "#333333", I_h = "#B71C1C", Y_h = "#333333", A_h = "#E57373", S_l = "#333333", I_l = "#B71C1C", Y_l = "#333333", A_l = "#E57373")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "HIV in core and non-core risk groups with preferential mixing and ART")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

HIV among MSM with PrEP and treatment as prevention

Sexually transmitted & blood-borne
SSpIaIcDxAv

HIV in MSM; PrEP. Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.

Structure is a generic composite in the style of many PrEP models. The efficacy value comes from McCormack S, Dunn DT, et al. Pre-exposure prophylaxis to prevent the acquisition of HIV-1 infection (PROUD). Lancet 2016;387:53–60 ↗

Show R code
## HIV among MSM with PrEP and treatment as prevention
## HIV in MSM; PrEP.
## Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.
## Reference: Structure is a generic composite in the style of many PrEP models. The efficacy value comes from McCormack S, Dunn DT, et al. Pre-exposure prophylaxis to prevent the acquisition of HIV-1 infection (PROUD). Lancet 2016;387:53–60
## Source: https://doi.org/10.1016/S0140-6736(15
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Sp, Ia, Ic, Dx, Av
## Flows (time unit: years):
##   (outside) -> S   : (mu * N + nu_d * (Ic + Dx))
##   S -> Sp          : kappa_p * S
##   Sp -> S          : omega_p * Sp
##   S -> Ia          : foi * S
##   Sp -> Ia         : (1 - eff_prep) * foi * Sp
##   Ia -> Ic         : sig_a * Ia
##   Ic -> Dx         : tau_d * Ic
##   Dx -> Av         : tau_a * Dx
##   Ic -> (outside)  : nu_d * Ic
##   Dx -> (outside)  : nu_d * Dx
##   S -> (outside)   : mu * S
##   Sp -> (outside)  : mu * Sp
##   Ia -> (outside)  : mu * Ia
##   Ic -> (outside)  : mu * Ic
##   Dx -> (outside)  : mu * Dx
##   Av -> (outside)  : mu * Av

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp = 5,           # partner change rate (partners/yr) [illustrative]
  beta_c = 0.1,     # transmission probability per partnership from chronic infection (per partnership) [illustrative]
  ra = 10,          # relative infectiousness of acute infection [illustrative]
  sig_a = 4,        # exit from acute stage (about 3 months) (/yr) [illustrative]
  eff_prep = 0.86,  # PrEP efficacy [recalled]
  eps_art = 0,      # infectiousness when virally suppressed (U=U) [illustrative]
  kappa_p = 0.2,    # PrEP uptake (/yr) [illustrative]
  omega_p = 0.5,    # PrEP discontinuation (/yr) [illustrative]
  tau_d = 0.5,      # diagnosis rate (/yr) [illustrative]
  tau_a = 2,        # ART initiation after diagnosis (/yr) [illustrative]
  nu_d = 0.1,       # HIV death if untreated (/yr) [illustrative]
  mu = 0.03,        # exit (/yr) [illustrative]
  tau_p = 4         # HIV testing rate while on PrEP (3-monthly); only relevant if a PrEP-specific infection compartment is added (/yr) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 0.88,    # susceptible not on PrEP
  Sp = 0,      # susceptible on PrEP
  Ia = 0.005,  # acute (primary) infection
  Ic = 0.06,   # chronic undiagnosed
  Dx = 0.02,   # diagnosed, not on ART
  Av = 0.035   # on ART, virally suppressed
)

# ---- Model equations -------------------------------------------------
hiv_prep_msm_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Sp + Ia + Ic + Dx + Av

    # Derived quantities
    foi <- cp * beta_c * (ra * Ia + Ic + Dx + eps_art * Av) / N  # force of infection

    # Rates of change
    dS <- (mu * N + nu_d * (Ic + Dx)) - kappa_p * S + omega_p * Sp - foi * S - mu * S
    dSp <- kappa_p * S - omega_p * Sp - (1 - eff_prep) * foi * Sp - mu * Sp
    dIa <- foi * S + (1 - eff_prep) * foi * Sp - sig_a * Ia - mu * Ia
    dIc <- sig_a * Ia - tau_d * Ic - nu_d * Ic - mu * Ic
    dDx <- tau_d * Ic - tau_a * Dx - nu_d * Dx - mu * Dx
    dAv <- tau_a * Dx - mu * Av

    list(c(dS, dSp, dIa, dIc, dDx, dAv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hiv_prep_msm_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Sp = "#333333", Ia = "#B71C1C", Ic = "#B71C1C", Dx = "#111111", Av = "#E57373")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "HIV among MSM with PrEP and treatment as prevention")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Gonorrhoea SIS with core/non-core groups, two sexes

Sexually transmitted & blood-borne
Sm_cIm_cSm_nIm_nSf_cIf_cSf_nIf_n

Gonorrhoea, heterosexual. Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.

Hethcote HW, Yorke JA. Gonorrhea Transmission Dynamics and Control. Lecture Notes in Biomathematics vol. 56. Springer-Verlag, 1984 ↗

Show R code
## Gonorrhoea SIS with core/non-core groups, two sexes
## Gonorrhoea, heterosexual.
## Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.
## Reference: Hethcote HW, Yorke JA. Gonorrhea Transmission Dynamics and Control. Lecture Notes in Biomathematics vol. 56. Springer-Verlag, 1984
## Source: https://doi.org/10.1007/978-3-662-07544-9
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sm_c, Im_c, Sm_n, Im_n, Sf_c, If_c, Sf_n, If_n
## Flows (time unit: years):
##   Sm_c -> Im_c  : foi_m_c * Sm_c
##   Im_c -> Sm_c  : 1 / dur_m * Im_c
##   Sm_n -> Im_n  : foi_m_n * Sm_n
##   Im_n -> Sm_n  : 1 / dur_m * Im_n
##   Sf_c -> If_c  : foi_f_c * Sf_c
##   If_c -> Sf_c  : 1 / dur_f * If_c
##   Sf_n -> If_n  : foi_f_n * Sf_n
##   If_n -> Sf_n  : 1 / dur_f * If_n

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 20, by = 0.02)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp_c = 20,     # core partner rate (partners/yr) [illustrative]
  cp_n = 1,      # non-core partner rate (partners/yr) [illustrative]
  b_mf = 0.5,    # male-to-female transmission probability (per partnership) [illustrative]
  b_fm = 0.25,   # female-to-male transmission probability (per partnership) [illustrative]
  dur_m = 0.05,  # mean infectious duration in men (most symptomatic, treated in about 2–3 weeks) (yr) [illustrative]
  dur_f = 0.25,  # mean infectious duration in women (many asymptomatic) (yr) [illustrative]
  eps_mix = 0.6  # assortativity [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sm_c = 0.019,  # susceptible core men
  Im_c = 0.001,  # infected core men
  Sm_n = 0.98,   # susceptible non-core men
  Im_n = 0,      # infected non-core men
  Sf_c = 0.019,  # susceptible core women
  If_c = 0.001,  # infected core women
  Sf_n = 0.98,   # susceptible non-core women
  If_n = 0       # infected non-core women
)

# ---- Model equations -------------------------------------------------
gonorrhoea_core_group_hethcote_yorke_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sm_c + Im_c + Sm_n + Im_n + Sf_c + If_c + Sf_n + If_n

    # Derived quantities
    Nm_c <- Sm_c + Im_c                                                        # core men total
    Nm_n <- Sm_n + Im_n                                                        # non-core men total
    Nf_c <- Sf_c + If_c                                                        # core women total
    Nf_n <- Sf_n + If_n                                                        # non-core women total
    pf_mix <- (cp_c * If_c + cp_n * If_n) / (cp_c * Nf_c + cp_n * Nf_n)        # prevalence among female partnerships (proportionate mixing)
    pm_mix <- (cp_c * Im_c + cp_n * Im_n) / (cp_c * Nm_c + cp_n * Nm_n)        # prevalence among male partnerships (proportionate mixing)
    foi_m_c <- cp_c * b_fm * (eps_mix * If_c / Nf_c + (1 - eps_mix) * pf_mix)  # force of infection, core men
    foi_m_n <- cp_n * b_fm * (eps_mix * If_n / Nf_n + (1 - eps_mix) * pf_mix)  # force of infection, non-core men
    foi_f_c <- cp_c * b_mf * (eps_mix * Im_c / Nm_c + (1 - eps_mix) * pm_mix)  # force of infection, core women
    foi_f_n <- cp_n * b_mf * (eps_mix * Im_n / Nm_n + (1 - eps_mix) * pm_mix)  # force of infection, non-core women

    # Rates of change
    dSm_c <- -foi_m_c * Sm_c + 1 / dur_m * Im_c
    dIm_c <- foi_m_c * Sm_c - 1 / dur_m * Im_c
    dSm_n <- -foi_m_n * Sm_n + 1 / dur_m * Im_n
    dIm_n <- foi_m_n * Sm_n - 1 / dur_m * Im_n
    dSf_c <- -foi_f_c * Sf_c + 1 / dur_f * If_c
    dIf_c <- foi_f_c * Sf_c - 1 / dur_f * If_c
    dSf_n <- -foi_f_n * Sf_n + 1 / dur_f * If_n
    dIf_n <- foi_f_n * Sf_n - 1 / dur_f * If_n

    list(c(dSm_c, dIm_c, dSm_n, dIm_n, dSf_c, dIf_c, dSf_n, dIf_n))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = gonorrhoea_core_group_hethcote_yorke_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sm_c = "#333333", Im_c = "#B71C1C", Sm_n = "#333333", Im_n = "#B71C1C", Sf_c = "#333333", If_c = "#B71C1C", Sf_n = "#333333", If_n = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Gonorrhoea SIS with core/non-core groups, two sexes")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Gonorrhoea in MSM with cefixime-sensitive and cefixime-resistant strains

Sexually transmitted & blood-borne
SUsEsAsTscTsoUrErArTrcTro

Gonorrhoea, antimicrobial resistance. Parameter values: 8 of 9 checked against the source; the rest are recalled or illustrative.

Whittles LK, White PJ, Didelot X. Estimating the fitness cost and benefit of cefixime resistance in Neisseria gonorrhoeae to inform prescription policy: a modelling study. PLoS Med 2017;14(10):e1002416 ↗

Show R code
## Gonorrhoea in MSM with cefixime-sensitive and cefixime-resistant strains
## Gonorrhoea, antimicrobial resistance.
## Parameter values: 8 of 9 checked against the source; the rest are recalled or illustrative.
## Reference: Whittles LK, White PJ, Didelot X. Estimating the fitness cost and benefit of cefixime resistance in Neisseria gonorrhoeae to inform prescription policy: a modelling study. PLoS Med 2017;14(10):e1002416
## Source: https://doi.org/10.1371/journal.pmed.1002416
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Us, Es, As, Tsc, Tso, Ur, Er, Ar, Trc, Tro
## Flows (time unit: years):
##   S -> Us    : foi_s * S
##   S -> Ur    : foi_r * S
##   Us -> Es   : psi * sig_inc * Us
##   Us -> As   : (1 - psi) * sig_inc * Us
##   Ur -> Er   : psi * sig_inc * Ur
##   Ur -> Ar   : (1 - psi) * sig_inc * Ur
##   As -> S    : nu * As
##   Ar -> S    : alpha_r * nu * Ar
##   Es -> Tsc  : pi_cef * mu_t * Es
##   Es -> Tso  : (1 - pi_cef) * mu_t * Es
##   Er -> Trc  : pi_cef * mu_t * Er
##   Er -> Tro  : (1 - pi_cef) * mu_t * Er
##   Tsc -> S   : rho * Tsc
##   Tso -> S   : rho * Tso
##   Tro -> S   : rho * Tro
##   Trc -> S   : (1 - phi_f) * rho * Trc
##   Trc -> Ar  : phi_f * rho * Trc

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 10, by = 0.01)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  theta = 5.2,    # transmission rate (/yr) [verified]
  psi = 0.63,     # proportion of infections becoming symptomatic [verified]
  sig_inc = 77,   # rate of leaving incubation (about 4.7 days) (/yr) [verified]
  nu = 1.8,       # natural recovery from asymptomatic infection, sensitive strain (/yr) [verified]
  alpha_r = 1.8,  # relative natural recovery rate of the resistant strain (fitness cost) [verified]
  mu_t = 136,     # treatment-seeking rate when symptomatic (/yr) [verified]
  rho = 53,       # recovery rate after treatment (/yr) [verified]
  phi_f = 0.83,   # cefixime treatment failure for resistant infection [verified]
  pi_cef = 0.3    # proportion treated with cefixime [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 600000,  # susceptible MSM
  Us = 0,      # incubating, sensitive strain
  Es = 0,      # symptomatic, sensitive
  As = 618,    # asymptomatic, sensitive
  Tsc = 0,     # treated with cefixime, sensitive
  Tso = 0,     # treated with another antibiotic, sensitive
  Ur = 0,      # incubating, resistant strain
  Er = 0,      # symptomatic, resistant
  Ar = 49,     # asymptomatic, resistant
  Trc = 0,     # treated with cefixime, resistant
  Tro = 0      # treated with another antibiotic, resistant
)

# ---- Model equations -------------------------------------------------
gonorrhoea_amr_whittles_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Us + Es + As + Tsc + Tso + Ur + Er + Ar + Trc + Tro

    # Derived quantities
    Inf_s <- Us + Es + As + Tsc + Tso  # infected with sensitive strain
    Inf_r <- Ur + Er + Ar + Trc + Tro  # infected with resistant strain
    foi_s <- theta * Inf_s / N         # force of infection, sensitive
    foi_r <- theta * Inf_r / N         # force of infection, resistant

    # Rates of change
    dS <- -foi_s * S - foi_r * S + nu * As + alpha_r * nu * Ar + rho * Tsc + rho * Tso + rho * Tro + (1 - phi_f) * rho * Trc
    dUs <- foi_s * S - psi * sig_inc * Us - (1 - psi) * sig_inc * Us
    dEs <- psi * sig_inc * Us - pi_cef * mu_t * Es - (1 - pi_cef) * mu_t * Es
    dAs <- (1 - psi) * sig_inc * Us - nu * As
    dTsc <- pi_cef * mu_t * Es - rho * Tsc
    dTso <- (1 - pi_cef) * mu_t * Es - rho * Tso
    dUr <- foi_r * S - psi * sig_inc * Ur - (1 - psi) * sig_inc * Ur
    dEr <- psi * sig_inc * Ur - pi_cef * mu_t * Er - (1 - pi_cef) * mu_t * Er
    dAr <- (1 - psi) * sig_inc * Ur - alpha_r * nu * Ar + phi_f * rho * Trc
    dTrc <- pi_cef * mu_t * Er - (1 - phi_f) * rho * Trc - phi_f * rho * Trc
    dTro <- (1 - pi_cef) * mu_t * Er - rho * Tro

    list(c(dS, dUs, dEs, dAs, dTsc, dTso, dUr, dEr, dAr, dTrc, dTro))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = gonorrhoea_amr_whittles_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Us = "#333333", Es = "#E8C468", As = "#E57373", Tsc = "#333333", Tso = "#333333", Ur = "#333333", Er = "#E8C468", Ar = "#E57373", Trc = "#333333", Tro = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Gonorrhoea in MSM with cefixime-sensitive and cefixime-resistant strains")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Syphilis with incubating, primary, secondary, early/late latent stages and waning immunity

Sexually transmitted & blood-borne
SEP1P2LeLlR

Syphilis. Parameter values: 6 of 11 checked against the source; the rest are recalled or illustrative.

Garnett GP, Aral SO, Hoyle DV, Cates W Jr, Anderson RM. The natural history of syphilis: implications for the transmission dynamics and control of infection. Sex Transm Dis 1997;24(4):185–200 ↗

Show R code
## Syphilis with incubating, primary, secondary, early/late latent stages and waning immunity
## Syphilis.
## Parameter values: 6 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Garnett GP, Aral SO, Hoyle DV, Cates W Jr, Anderson RM. The natural history of syphilis: implications for the transmission dynamics and control of infection. Sex Transm Dis 1997;24(4):185–200
## Source: https://doi.org/10.1097/00007435-199704000-00002
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, P1, P2, Le, Ll, R
## Flows (time unit: years):
##   (outside) -> S   : mu * N
##   S -> E           : foi * S
##   E -> P1          : sig_inc * E
##   P1 -> P2         : 1 / (f_prim * d_inf) * P1
##   P2 -> Le         : 1 / ((1 - f_prim) * d_inf) * P2
##   Le -> P2         : rel * Le
##   Le -> Ll         : el * Le
##   P1 -> R          : tau * P1
##   P2 -> R          : tau * P2
##   Le -> R          : tau * Le
##   Ll -> R          : tau_l * Ll
##   R -> S           : w_imm * R
##   S -> (outside)   : mu * S
##   E -> (outside)   : mu * E
##   P1 -> (outside)  : mu * P1
##   P2 -> (outside)  : mu * P2
##   Le -> (outside)  : mu * Le
##   Ll -> (outside)  : mu * Ll
##   R -> (outside)   : mu * R

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp = 15,             # mean partner change rate (MSM, Ontario) (partners/yr) [verified]
  beta_s = 0.3,        # transmission probability (per partnership) [illustrative]
  sig_inc = 365 / 25,  # 1/incubation period (25 days) (/yr) [verified]
  d_inf = 154 / 365,   # total primary + secondary infectious period (yr) [verified]
  f_prim = 0.3,        # fraction of d_inf spent in primary stage [illustrative]
  tau = 0.4,           # treatment rate of infectious and early-latent cases (/yr) [verified]
  tau_l = 0.1,         # treatment rate in late latent (/yr) [illustrative]
  rel = 0.25,          # relapse rate early latent to secondary (/yr) [illustrative]
  el = 1,              # progression early to late latent (early latent about 1 yr) (/yr) [illustrative]
  w_imm = 1 / 5,       # waning of post-treatment immunity (5 yr) (/yr) [verified]
  mu = 5831 / 366279   # entry/exit rate (/yr) [verified]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 0.99,    # susceptible
  E = 0,       # incubating
  P1 = 0.005,  # primary (infectious)
  P2 = 0.005,  # secondary (infectious)
  Le = 0,      # early latent (non-infectious; relapse possible)
  Ll = 0,      # late latent
  R = 0        # treated, temporarily immune
)

# ---- Model equations -------------------------------------------------
syphilis_staged_garnett_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + E + P1 + P2 + Le + Ll + R

    # Derived quantities
    foi <- cp * beta_s * (P1 + P2) / N  # force of infection

    # Rates of change
    dS <- mu * N - foi * S + w_imm * R - mu * S
    dE <- foi * S - sig_inc * E - mu * E
    dP1 <- sig_inc * E - 1 / (f_prim * d_inf) * P1 - tau * P1 - mu * P1
    dP2 <- 1 / (f_prim * d_inf) * P1 - 1 / ((1 - f_prim) * d_inf) * P2 + rel * Le - tau * P2 - mu * P2
    dLe <- 1 / ((1 - f_prim) * d_inf) * P2 - rel * Le - el * Le - tau * Le - mu * Le
    dLl <- el * Le - tau_l * Ll - mu * Ll
    dR <- tau * P1 + tau * P2 + tau * Le + tau_l * Ll - w_imm * R - mu * R

    list(c(dS, dE, dP1, dP2, dLe, dLl, dR))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = syphilis_staged_garnett_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", P1 = "#F0A35E", P2 = "#F0A35E", Le = "#E8C468", Ll = "#E8C468", R = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Syphilis with incubating, primary, secondary, early/late latent stages and waning immunity")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Chlamydia SEIRS, two sexes, symptomatic/asymptomatic, with screening

Sexually transmitted & blood-borne
SfEfIafIsfRfSmEmIamIsmRm

Chlamydia trachomatis. Parameter values: 1 of 12 checked against the source; the rest are recalled or illustrative.

Althaus CL, Heijne JCM, Roellin A, Low N. Transmission dynamics of Chlamydia trachomatis affect the impact of screening programmes. Epidemics 2010;2(3):123–131 ↗

Show R code
## Chlamydia SEIRS, two sexes, symptomatic/asymptomatic, with screening
## Chlamydia trachomatis.
## Parameter values: 1 of 12 checked against the source; the rest are recalled or illustrative.
## Reference: Althaus CL, Heijne JCM, Roellin A, Low N. Transmission dynamics of Chlamydia trachomatis affect the impact of screening programmes. Epidemics 2010;2(3):123–131
## Source: https://doi.org/10.1016/j.epidem.2010.04.002
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sf, Ef, Iaf, Isf, Rf, Sm, Em, Iam, Ism, Rm
## Flows (time unit: days):
##   Sf -> Ef   : foi_f * Sf
##   Ef -> Isf  : p_sym_f * sig_inc * Ef
##   Ef -> Iaf  : (1 - p_sym_f) * sig_inc * Ef
##   Iaf -> Rf  : 1 / d_af * Iaf
##   Iaf -> Sf  : scr_f * Iaf
##   Isf -> Sf  : 1 / d_s * Isf
##   Rf -> Sf   : w_imm * Rf
##   Sm -> Em   : foi_m * Sm
##   Em -> Ism  : p_sym_m * sig_inc * Em
##   Em -> Iam  : (1 - p_sym_m) * sig_inc * Em
##   Iam -> Rm  : 1 / d_am * Iam
##   Iam -> Sm  : scr_m * Iam
##   Ism -> Sm  : 1 / d_s * Ism
##   Rm -> Sm   : w_imm * Rm

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3650, by = 5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp = 1 / 365,       # partner change rate (1 per year, equal by sex to keep partnerships balanced) (partners/day) [illustrative]
  beta_mf = 0.3,      # male-to-female transmission (per partnership) [illustrative]
  beta_fm = 0.3,      # female-to-male transmission (per partnership) [illustrative]
  sig_inc = 1 / 14,   # exit from latent period (/day) [illustrative]
  p_sym_f = 0.1,      # symptomatic fraction, women [illustrative]
  p_sym_m = 0.2,      # symptomatic fraction, men [illustrative]
  d_af = 433,         # asymptomatic duration, women (days) [verified]
  d_am = 433,         # asymptomatic duration, men (days) [illustrative]
  d_s = 30,           # time to treatment when symptomatic (days) [illustrative]
  w_imm = 1 / 365,    # waning of immunity (R back to S) (/day) [illustrative]
  scr_f = 0.2 / 365,  # screening plus treatment rate, women (/day) [illustrative]
  scr_m = 0.05 / 365  # screening plus treatment rate, men (/day) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sf = 0.95,   # susceptible women
  Ef = 0,      # exposed (latent) women
  Iaf = 0.05,  # asymptomatic infected women
  Isf = 0,     # symptomatic infected women
  Rf = 0,      # immune women
  Sm = 0.95,   # susceptible men
  Em = 0,      # exposed men
  Iam = 0.05,  # asymptomatic infected men
  Ism = 0,     # symptomatic infected men
  Rm = 0       # immune men
)

# ---- Model equations -------------------------------------------------
chlamydia_seirs_screening_althaus_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sf + Ef + Iaf + Isf + Rf + Sm + Em + Iam + Ism + Rm

    # Derived quantities
    Nf <- Sf + Ef + Iaf + Isf + Rf            # women total
    Nm <- Sm + Em + Iam + Ism + Rm            # men total
    foi_f <- cp * beta_mf * (Iam + Ism) / Nm  # force of infection on women
    foi_m <- cp * beta_fm * (Iaf + Isf) / Nf  # force of infection on men

    # Rates of change
    dSf <- -foi_f * Sf + scr_f * Iaf + 1 / d_s * Isf + w_imm * Rf
    dEf <- foi_f * Sf - p_sym_f * sig_inc * Ef - (1 - p_sym_f) * sig_inc * Ef
    dIaf <- (1 - p_sym_f) * sig_inc * Ef - 1 / d_af * Iaf - scr_f * Iaf
    dIsf <- p_sym_f * sig_inc * Ef - 1 / d_s * Isf
    dRf <- 1 / d_af * Iaf - w_imm * Rf
    dSm <- -foi_m * Sm + scr_m * Iam + 1 / d_s * Ism + w_imm * Rm
    dEm <- foi_m * Sm - p_sym_m * sig_inc * Em - (1 - p_sym_m) * sig_inc * Em
    dIam <- (1 - p_sym_m) * sig_inc * Em - 1 / d_am * Iam - scr_m * Iam
    dIsm <- p_sym_m * sig_inc * Em - 1 / d_s * Ism
    dRm <- 1 / d_am * Iam - w_imm * Rm

    list(c(dSf, dEf, dIaf, dIsf, dRf, dSm, dEm, dIam, dIsm, dRm))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = chlamydia_seirs_screening_althaus_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sf = "#333333", Ef = "#E8C468", Iaf = "#B71C1C", Isf = "#B71C1C", Rf = "#4E8A3E", Sm = "#333333", Em = "#E8C468", Iam = "#B71C1C", Ism = "#B71C1C", Rm = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Chlamydia SEIRS, two sexes, symptomatic/asymptomatic, with screening")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

HPV (single high-risk type) SIRS-type model, two sexes, with vaccination

Sexually transmitted & blood-borne
SfIfRfVfSmImRmVm

HPV 16/18, vaccination. Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.

Simplified from Elbasha EH, Dasbach EJ, Insinga RP. Model for assessing human papillomavirus vaccination strategies. Emerg Infect Dis 2007;13(1):28–41 ↗

Show R code
## HPV (single high-risk type) SIRS-type model, two sexes, with vaccination
## HPV 16/18, vaccination.
## Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.
## Reference: Simplified from Elbasha EH, Dasbach EJ, Insinga RP. Model for assessing human papillomavirus vaccination strategies. Emerg Infect Dis 2007;13(1):28–41
## Source: https://doi.org/10.3201/eid1301.060438
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sf, If, Rf, Vf, Sm, Im, Rm, Vm
## Flows (time unit: years):
##   (outside) -> Sf  : mu * Nf * (1 - vc_f)
##   (outside) -> Vf  : mu * Nf * vc_f
##   (outside) -> Sm  : mu * Nm * (1 - vc_m)
##   (outside) -> Vm  : mu * Nm * vc_m
##   Sf -> If         : foi_f * Sf
##   Vf -> If         : (1 - ve) * foi_f * Vf
##   If -> Rf         : p_imm_f * clr_f * If
##   If -> Sf         : (1 - p_imm_f) * clr_f * If
##   Rf -> Sf         : w_nat * Rf
##   Vf -> Sf         : w_vac * Vf
##   Sm -> Im         : foi_m * Sm
##   Vm -> Im         : (1 - ve) * foi_m * Vm
##   Im -> Rm         : p_imm_m * clr_m * Im
##   Im -> Sm         : (1 - p_imm_m) * clr_m * Im
##   Rm -> Sm         : w_nat * Rm
##   Vm -> Sm         : w_vac * Vm
##   Sf -> (outside)  : mu * Sf
##   If -> (outside)  : mu * If
##   Rf -> (outside)  : mu * Rf
##   Vf -> (outside)  : mu * Vf
##   Sm -> (outside)  : mu * Sm
##   Im -> (outside)  : mu * Im
##   Rm -> (outside)  : mu * Rm
##   Vm -> (outside)  : mu * Vm

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp = 2,         # partner change rate (partners/yr) [illustrative]
  beta_mf = 0.8,  # male-to-female transmission (Elbasha used high values) (per partnership) [illustrative]
  beta_fm = 0.7,  # female-to-male transmission (per partnership) [illustrative]
  clr_f = 1,      # clearance in women (about 1 yr) (/yr) [illustrative]
  clr_m = 1.5,    # clearance in men (/yr) [illustrative]
  p_imm_f = 0.5,  # fraction of women gaining natural immunity on clearance [illustrative]
  p_imm_m = 0.2,  # fraction of men gaining natural immunity on clearance [illustrative]
  w_nat = 0.1,    # waning of natural immunity (/yr) [illustrative]
  w_vac = 0,      # waning of vaccine protection (assumed lifelong) (/yr) [illustrative]
  vc_f = 0.7,     # vaccine coverage of girls at entry [illustrative]
  vc_m = 0,       # vaccine coverage of boys at entry [illustrative]
  ve = 0.9,       # vaccine efficacy (degree protection) [illustrative]
  mu = 1 / 30     # exit from the sexually active population (/yr) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sf = 0.9,   # susceptible women
  If = 0.05,  # infected women
  Rf = 0.05,  # naturally immune women
  Vf = 0,     # vaccinated women
  Sm = 0.9,   # susceptible men
  Im = 0.05,  # infected men
  Rm = 0.05,  # immune men
  Vm = 0      # vaccinated men
)

# ---- Model equations -------------------------------------------------
hpv_vaccination_simplified_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sf + If + Rf + Vf + Sm + Im + Rm + Vm

    # Derived quantities
    Nf <- Sf + If + Rf + Vf          # women total
    Nm <- Sm + Im + Rm + Vm          # men total
    foi_f <- cp * beta_mf * Im / Nm  # force of infection on women
    foi_m <- cp * beta_fm * If / Nf  # force of infection on men

    # Rates of change
    dSf <- mu * Nf * (1 - vc_f) - foi_f * Sf + (1 - p_imm_f) * clr_f * If + w_nat * Rf + w_vac * Vf - mu * Sf
    dIf <- foi_f * Sf + (1 - ve) * foi_f * Vf - p_imm_f * clr_f * If - (1 - p_imm_f) * clr_f * If - mu * If
    dRf <- p_imm_f * clr_f * If - w_nat * Rf - mu * Rf
    dVf <- mu * Nf * vc_f - (1 - ve) * foi_f * Vf - w_vac * Vf - mu * Vf
    dSm <- mu * Nm * (1 - vc_m) - foi_m * Sm + (1 - p_imm_m) * clr_m * Im + w_nat * Rm + w_vac * Vm - mu * Sm
    dIm <- foi_m * Sm + (1 - ve) * foi_m * Vm - p_imm_m * clr_m * Im - (1 - p_imm_m) * clr_m * Im - mu * Im
    dRm <- p_imm_m * clr_m * Im - w_nat * Rm - mu * Rm
    dVm <- mu * Nm * vc_m - (1 - ve) * foi_m * Vm - w_vac * Vm - mu * Vm

    list(c(dSf, dIf, dRf, dVf, dSm, dIm, dRm, dVm))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hpv_vaccination_simplified_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sf = "#333333", If = "#B71C1C", Rf = "#4E8A3E", Vf = "#2457C5", Sm = "#333333", Im = "#B71C1C", Rm = "#4E8A3E", Vm = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "HPV (single high-risk type) SIRS-type model, two sexes, with vaccination")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Genital herpes (HSV-2) with primary episode, latency and recurrent reactivation

Sexually transmitted & blood-borne
SIpLtRc

HSV-2. Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Structure follows Blower SM, Porco TC, Darby G. Predicting and preventing the emergence of antiviral drug resistance in HSV-2. Nat Med 1998;4(6):673–678 ↗

Show R code
## Genital herpes (HSV-2) with primary episode, latency and recurrent reactivation
## HSV-2.
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
## Reference: Structure follows Blower SM, Porco TC, Darby G. Predicting and preventing the emergence of antiviral drug resistance in HSV-2. Nat Med 1998;4(6):673–678
## Source: https://doi.org/10.1038/nm0698-673
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ip, Lt, Rc
## Flows (time unit: years):
##   (outside) -> S   : mu * N
##   S -> Ip          : foi * S
##   Ip -> Lt         : 1 / d_p * Ip
##   Lt -> Rc         : react * (1 - tx) * Lt
##   Rc -> Lt         : 1 / d_r * Rc
##   S -> (outside)   : mu * S
##   Ip -> (outside)  : mu * Ip
##   Lt -> (outside)  : mu * Lt
##   Rc -> (outside)  : mu * Rc

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp = 2,         # partner change rate (partners/yr) [illustrative]
  beta_p = 0.5,   # transmission from primary episode (per partnership) [illustrative]
  beta_r = 0.3,   # transmission from recurrence (per partnership) [illustrative]
  beta_l = 0.05,  # transmission from latency (asymptomatic shedding) (per partnership) [illustrative]
  d_p = 3 / 52,   # duration of primary episode (about 3 weeks) (yr) [illustrative]
  react = 4,      # reactivation rate (about 4 recurrences/yr in early years) (/yr) [illustrative]
  d_r = 5 / 365,  # duration of a recurrence (yr) [illustrative]
  mu = 1 / 30,    # exit from the sexually active population (/yr) [illustrative]
  tx = 0          # fraction of recurrences shortened by suppressive antivirals [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 0.8,    # susceptible
  Ip = 0.01,  # primary infection episode (high shedding)
  Lt = 0.17,  # latent (low-level asymptomatic shedding)
  Rc = 0.02   # recurrent episode (reactivated shedding)
)

# ---- Model equations -------------------------------------------------
hsv2_latent_reactivation_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Ip + Lt + Rc

    # Derived quantities
    foi <- cp * (beta_p * Ip + beta_r * Rc + beta_l * Lt) / N  # force of infection

    # Rates of change
    dS <- mu * N - foi * S - mu * S
    dIp <- foi * S - 1 / d_p * Ip - mu * Ip
    dLt <- 1 / d_p * Ip - react * (1 - tx) * Lt + 1 / d_r * Rc - mu * Lt
    dRc <- react * (1 - tx) * Lt - 1 / d_r * Rc - mu * Rc

    list(c(dS, dIp, dLt, dRc))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hsv2_latent_reactivation_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ip = "#B71C1C", Lt = "#E8C468", Rc = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Genital herpes (HSV-2) with primary episode, latency and recurrent reactivation")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Hepatitis B with acute infection, chronic carriers, vertical transmission and vaccination

Sexually transmitted & blood-borne
SAcCrRV

Hepatitis B. Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

The conceptual basis is Medley GF, Lindop NA, Edmunds WJ, Nokes DJ. Hepatitis-B virus endemicity: heterogeneity, catastrophic dynamics and control. Nat Med 2001;7(5):619–624 ↗

Show R code
## Hepatitis B with acute infection, chronic carriers, vertical transmission and vaccination
## Hepatitis B.
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: The conceptual basis is Medley GF, Lindop NA, Edmunds WJ, Nokes DJ. Hepatitis-B virus endemicity: heterogeneity, catastrophic dynamics and control. Nat Med 2001;7(5):619–624
## Source: https://doi.org/10.1038/87953
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ac, Cr, R, V
## Flows (time unit: years):
##   (outside) -> S   : (births_ok * (1 - vax) + mu_c * Cr)
##   (outside) -> V   : births_ok * vax
##   (outside) -> Cr  : mu * pv * Cr * pc0
##   (outside) -> Ac  : mu * pv * Cr * (1 - pc0)
##   S -> Ac          : foi * S
##   Ac -> Cr         : q_c * gam_a * Ac
##   Ac -> R          : (1 - q_c) * gam_a * Ac
##   Cr -> R          : gam_c * Cr
##   V -> S           : w_v * V
##   S -> (outside)   : mu * S
##   Ac -> (outside)  : mu * Ac
##   Cr -> (outside)  : (mu + mu_c) * Cr
##   R -> (outside)   : mu * R
##   V -> (outside)   : mu * V

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_h = 0.95,  # horizontal transmission coefficient (/yr) [recalled]
  eps_c = 0.16,   # relative infectiousness of carriers vs acute [recalled]
  gam_a = 4,      # exit from acute stage (about 3 months) (/yr) [recalled]
  q_c = 0.0886,   # probability that horizontal adult infection leads to carriage [recalled]
  gam_c = 0.025,  # carrier clearance (HBsAg loss) (/yr) [recalled]
  pv = 0.11,      # fraction of births to carrier mothers infected perinatally [recalled]
  pc0 = 0.885,    # probability that perinatal infection leads to carriage [recalled]
  mu = 0.00693,   # birth = natural death rate (/yr) [recalled]
  mu_c = 0.002,   # HBV-related excess carrier mortality (/yr) [illustrative]
  vax = 0.0,      # infant vaccination coverage times efficacy (scenario: 0.9) [illustrative]
  w_v = 0.1       # waning of vaccine immunity (/yr) [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 0.6,    # susceptible
  Ac = 0.01,  # acute infection
  Cr = 0.09,  # chronic carrier
  R = 0.3,    # recovered/immune
  V = 0       # vaccinated
)

# ---- Model equations -------------------------------------------------
hbv_carrier_vaccination_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- S + Ac + Cr + R + V

    # Derived quantities
    foi <- beta_h * (Ac + eps_c * Cr) / N  # force of infection
    births_ok <- mu * N - mu * pv * Cr     # births not perinatally infected

    # Rates of change
    dS <- (births_ok * (1 - vax) + mu_c * Cr) - foi * S + w_v * V - mu * S
    dAc <- mu * pv * Cr * (1 - pc0) + foi * S - q_c * gam_a * Ac - (1 - q_c) * gam_a * Ac - mu * Ac
    dCr <- mu * pv * Cr * pc0 + q_c * gam_a * Ac - gam_c * Cr - (mu + mu_c) * Cr
    dR <- (1 - q_c) * gam_a * Ac + gam_c * Cr - mu * R
    dV <- births_ok * vax - w_v * V - mu * V

    list(c(dS, dAc, dCr, dR, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hbv_carrier_vaccination_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ac = "#E57373", Cr = "#8E0E0E", R = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Hepatitis B with acute infection, chronic carriers, vertical transmission and vaccination")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Hepatitis C among PWID with risk groups and DAA treatment as prevention

Sexually transmitted & blood-borne
X0A0Ch0Tx0Fail0X1A1Ch1Tx1Fail1

HCV in people who inject drugs. Parameter values: 9 of 11 checked against the source; the rest are recalled or illustrative.

Martin NK, Vickerman P, Grebely J, Hellard M, Hutchinson SJ, Lima VD, Foster GR, Dillon JF, Goldberg DJ, Dore GJ, Hickman M. Hepatitis C virus treatment for prevention among people who inject drugs: modeling treatment scale-up in the age of direct-acting… ↗

Show R code
## Hepatitis C among PWID with risk groups and DAA treatment as prevention
## HCV in people who inject drugs.
## Parameter values: 9 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Martin NK, Vickerman P, Grebely J, Hellard M, Hutchinson SJ, Lima VD, Foster GR, Dillon JF, Goldberg DJ, Dore GJ, Hickman M. Hepatitis C virus treatment for prevention among people who inject drugs: modeling treatment scale-up in the age of direct-acting…
## Source: https://doi.org/10.1002/hep.26431
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: X0, A0, Ch0, Tx0, Fail0, X1, A1, Ch1, Tx1, Fail1
## Flows (time unit: years):
##   (outside) -> X0     : (1 - f_high) * exit_r * Ntot
##   (outside) -> X1     : f_high * exit_r * Ntot
##   X0 -> A0            : foi0 * X0
##   X1 -> A1            : foi1 * X1
##   A0 -> X0            : delta_c * psi_a * A0
##   A0 -> Ch0           : (1 - delta_c) * psi_a * A0
##   A1 -> X1            : delta_c * psi_a * A1
##   A1 -> Ch1           : (1 - delta_c) * psi_a * A1
##   Ch0 -> Tx0          : tx_pc * Ch0
##   Ch1 -> Tx1          : tx_pc * Ch1
##   Tx0 -> X0           : alpha_svr * omega * Tx0
##   Tx0 -> Fail0        : (1 - alpha_svr) * omega * Tx0
##   Tx1 -> X1           : alpha_svr * omega * Tx1
##   Tx1 -> Fail1        : (1 - alpha_svr) * omega * Tx1
##   X1 -> X0            : kappa * X1
##   A1 -> A0            : kappa * A1
##   Ch1 -> Ch0          : kappa * Ch1
##   Tx1 -> Tx0          : kappa * Tx1
##   Fail1 -> Fail0      : kappa * Fail1
##   X0 -> (outside)     : exit_r * X0
##   A0 -> (outside)     : exit_r * A0
##   Ch0 -> (outside)    : exit_r * Ch0
##   Tx0 -> (outside)    : exit_r * Tx0
##   Fail0 -> (outside)  : exit_r * Fail0
##   X1 -> (outside)     : exit_r * X1
##   A1 -> (outside)     : exit_r * A1
##   Ch1 -> (outside)    : exit_r * Ch1
##   Tx1 -> (outside)    : exit_r * Tx1
##   Fail1 -> (outside)  : exit_r * Fail1

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  pi_inf = 0.2,     # infection rate coefficient (/yr) [illustrative]
  Pi_h = 3.6,       # relative risk for high-risk PWID [verified]
  kappa = 12 / 14,  # high to low risk (duration 14 months) (/yr) [verified]
  f_high = 0.33,    # fraction of new injectors entering high risk [illustrative]
  delta_c = 0.26,   # proportion of acute infections clearing spontaneously [verified]
  psi_a = 2,        # exit from acute (6 months) (/yr) [verified]
  mu1 = 1 / 11,     # permanent cessation of injecting (11 yr career) (/yr) [verified]
  mu2 = 0.01,       # death rate (/yr) [verified]
  phi_tx = 8,       # treatments started per year (per 1000 PWID per yr) [verified]
  omega = 52 / 12,  # 1/treatment duration (12 weeks for IFN-free DAA) (/yr) [verified]
  alpha_svr = 0.9   # SVR with IFN-free DAAs [verified]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  X0 = 503,    # uninfected, low risk
  A0 = 5,      # acute HCV, low risk
  Ch0 = 150,   # chronic HCV, low risk
  Tx0 = 0,     # on treatment, low risk
  Fail0 = 12,  # failed treatment, low risk
  X1 = 230,    # uninfected, high risk
  A1 = 5,      # acute, high risk
  Ch1 = 90,    # chronic, high risk
  Tx1 = 0,     # on treatment, high risk
  Fail1 = 5    # failed treatment, high risk
)

# ---- Model equations -------------------------------------------------
hcv_pwid_treatment_martin_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- X0 + A0 + Ch0 + Tx0 + Fail0 + X1 + A1 + Ch1 + Tx1 + Fail1

    # Derived quantities
    Ntot <- X0 + A0 + Ch0 + Tx0 + Fail0 + X1 + A1 + Ch1 + Tx1 + Fail1                  # total PWID
    Chtot <- Ch0 + Ch1                                                                 # total chronic infections
    Infectious <- A0 + A1 + Ch0 + Ch1 + Fail0 + Fail1 + (1 - alpha_svr) * (Tx0 + Tx1)  # those on treatment who will achieve SVR are non-infectious [verified assumption]
    foi0 <- pi_inf * Infectious / Ntot                                                 # force of infection, low risk
    foi1 <- Pi_h * pi_inf * Infectious / Ntot                                          # force of infection, high risk
    tx_pc <- pmin(phi_tx / 1000 * Ntot / max(Chtot, 1e-6), 52)                         # per-capita treatment rate implementing a fixed number treated per year; capped
    exit_r <- mu1 + mu2                                                                # total exit rate (cessation plus death)

    # Rates of change
    dX0 <- (1 - f_high) * exit_r * Ntot - foi0 * X0 + delta_c * psi_a * A0 + alpha_svr * omega * Tx0 + kappa * X1 - exit_r * X0
    dA0 <- foi0 * X0 - delta_c * psi_a * A0 - (1 - delta_c) * psi_a * A0 + kappa * A1 - exit_r * A0
    dCh0 <- (1 - delta_c) * psi_a * A0 - tx_pc * Ch0 + kappa * Ch1 - exit_r * Ch0
    dTx0 <- tx_pc * Ch0 - alpha_svr * omega * Tx0 - (1 - alpha_svr) * omega * Tx0 + kappa * Tx1 - exit_r * Tx0
    dFail0 <- (1 - alpha_svr) * omega * Tx0 + kappa * Fail1 - exit_r * Fail0
    dX1 <- f_high * exit_r * Ntot - foi1 * X1 + delta_c * psi_a * A1 + alpha_svr * omega * Tx1 - kappa * X1 - exit_r * X1
    dA1 <- foi1 * X1 - delta_c * psi_a * A1 - (1 - delta_c) * psi_a * A1 - kappa * A1 - exit_r * A1
    dCh1 <- (1 - delta_c) * psi_a * A1 - tx_pc * Ch1 - kappa * Ch1 - exit_r * Ch1
    dTx1 <- tx_pc * Ch1 - alpha_svr * omega * Tx1 - (1 - alpha_svr) * omega * Tx1 - kappa * Tx1 - exit_r * Tx1
    dFail1 <- (1 - alpha_svr) * omega * Tx1 - kappa * Fail1 - exit_r * Fail1

    list(c(dX0, dA0, dCh0, dTx0, dFail0, dX1, dA1, dCh1, dTx1, dFail1))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hcv_pwid_treatment_martin_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(X0 = "#333333", A0 = "#E57373", Ch0 = "#8E0E0E", Tx0 = "#333333", Fail0 = "#5D4037", X1 = "#333333", A1 = "#E57373", Ch1 = "#8E0E0E", Tx1 = "#333333", Fail1 = "#5D4037")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Hepatitis C among PWID with risk groups and DAA treatment as prevention")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

HTLV-1 lifelong infection with sexual and mother-to-child transmission and disease (ATL/HAM)

Sexually transmitted & blood-borne
SfHfSmHmDz

HTLV-1. Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.

I found no canonical population-level deterministic HTLV-1 transmission ODE paper in this session; published HTLV ODE models are mostly within-host. This is a composite model with illustrative values. The epidemiological facts behind it (about 2–5%… ↗

Show R code
## HTLV-1 lifelong infection with sexual and mother-to-child transmission and disease (ATL/HAM)
## HTLV-1.
## Parameter values: 0 of 7 checked against the source; the rest are recalled or illustrative.
## Reference: I found no canonical population-level deterministic HTLV-1 transmission ODE paper in this session; published HTLV ODE models are mostly within-host. This is a composite model with illustrative values. The epidemiological facts behind it (about 2–5%…
## Source: https://doi.org/10.3389/fmicb.2012.00388
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sf, Hf, Sm, Hm, Dz
## Flows (time unit: years):
##   (outside) -> Sf  : 0.5 * births * (1 - pinf_birth)
##   (outside) -> Hf  : 0.5 * births * pinf_birth
##   (outside) -> Sm  : 0.5 * births * (1 - pinf_birth)
##   (outside) -> Hm  : 0.5 * births * pinf_birth
##   Sf -> Hf         : foi_f * Sf
##   Sm -> Hm         : foi_m * Sm
##   Hf -> Dz         : dis * Hf
##   Hm -> Dz         : dis * Hm
##   Dz -> (outside)  : (mu_d + mu) * Dz
##   Sf -> (outside)  : mu * Sf
##   Hf -> (outside)  : mu * Hf
##   Sm -> (outside)  : mu * Sm
##   Hm -> (outside)  : mu * Hm

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  cp = 1,        # partner change rate (partners/yr) [illustrative]
  b_mf = 0.06,   # male-to-female transmission (higher than reverse, per the literature) (per partnership/yr) [illustrative]
  b_fm = 0.01,   # female-to-male transmission (per partnership/yr) [illustrative]
  p_mtct = 0.2,  # infant infection probability from a carrier mother (prolonged breastfeeding) [illustrative]
  dis = 0.001,   # progression carrier to disease (about 4% lifetime over a 40-yr exposure) (/yr) [illustrative]
  mu = 1 / 50,   # natural death = birth (/yr) [illustrative]
  mu_d = 0.5     # disease-related death (ATL is rapidly fatal; HAM is not) (/yr) [illustrative]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sf = 0.49,   # susceptible women
  Hf = 0.01,   # HTLV-1 carrier women
  Sm = 0.495,  # susceptible men
  Hm = 0.005,  # carrier men
  Dz = 0       # ATL/HAM disease (both sexes)
)

# ---- Model equations -------------------------------------------------
htlv1_sexual_vertical_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sf + Hf + Sm + Hm + Dz

    # Derived quantities
    Nf <- Sf + Hf + Dz / 2          # women total (assumes disease split evenly by sex)
    Nm <- Sm + Hm + Dz / 2          # men total
    foi_f <- cp * b_mf * Hm / Nm    # force of infection on women
    foi_m <- cp * b_fm * Hf / Nf    # force of infection on men
    births <- mu * N + mu_d * Dz    # births keeping N constant
    pinf_birth <- p_mtct * Hf / Nf  # fraction of births infected vertically

    # Rates of change
    dSf <- 0.5 * births * (1 - pinf_birth) - foi_f * Sf - mu * Sf
    dHf <- 0.5 * births * pinf_birth + foi_f * Sf - dis * Hf - mu * Hf
    dSm <- 0.5 * births * (1 - pinf_birth) - foi_m * Sm - mu * Sm
    dHm <- 0.5 * births * pinf_birth + foi_m * Sm - dis * Hm - mu * Hm
    dDz <- dis * Hf + dis * Hm - (mu_d + mu) * Dz

    list(c(dSf, dHf, dSm, dHm, dDz))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = htlv1_sexual_vertical_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sf = "#333333", Hf = "#6A2C9C", Sm = "#333333", Hm = "#6A2C9C", Dz = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "HTLV-1 lifelong infection with sexual and mother-to-child transmission and disease (ATL/HAM)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Malaria SEIRS humans / SEI mosquitoes with saturating biting (Chitnis, Hyman & Cushing 2008) [VERIFIED-FULL]

Vector-borne
ShEhIhRhSvEvIv

Plasmodium falciparum malaria, generic high- or low-transmission settings in sub-Saharan Africa. Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.

Chitnis N, Hyman JM, Cushing JM (2008). Determining important parameters in the spread of malaria through the sensitivity analysis of a mathematical model. Bull Math Biol 70(5):1272–1296. https://doi.org/10.1007/s11538-008-9299-0 — the equations (1a–g) and… ↗

Show R code
## Malaria SEIRS humans / SEI mosquitoes with saturating biting (Chitnis, Hyman & Cushing 2008) [VERIFIED-FULL]
## Plasmodium falciparum malaria, generic high- or low-transmission settings in sub-Saharan Africa.
## Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.
## Reference: Chitnis N, Hyman JM, Cushing JM (2008). Determining important parameters in the spread of malaria through the sensitivity analysis of a mathematical model. Bull Math Biol 70(5):1272–1296. https://doi.org/10.1007/s11538-008-9299-0 — the equations (1a–g) and…
## Source: https://doi.org/10.1007/s11538-008-9299-0
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Eh, Ih, Rh, Sv, Ev, Iv
## Flows (time unit: days):
##   (outside) -> Sh  : Lambda_h
##   (outside) -> Sh  : psi_h * Nh
##   Sh -> Eh         : lambda_h * Sh
##   Eh -> Ih         : nu_h * Eh
##   Ih -> Rh         : gam_h * Ih
##   Rh -> Sh         : rho_h * Rh
##   Ih -> (outside)  : delta_h * Ih
##   Sh -> (outside)  : fh * Sh
##   Eh -> (outside)  : fh * Eh
##   Ih -> (outside)  : fh * Ih
##   Rh -> (outside)  : fh * Rh
##   (outside) -> Sv  : psi_v * Nv
##   Sv -> Ev         : lambda_v * Sv
##   Ev -> Iv         : nu_v * Ev
##   Sv -> (outside)  : fv * Sv
##   Ev -> (outside)  : fv * Ev
##   Iv -> (outside)  : fv * Iv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 7300, by = 10)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  Lambda_h = 0.033,  # immigration of humans (humans/day)
  psi_h = 1.1e-4,    # per-capita human birth rate (1/day)
  psi_v = 0.13,      # per-capita mosquito birth rate (1/day)
  sig_v = 0.50,      # bites one mosquito would want per day (1/day)
  sig_h = 19,        # maximum bites one human can receive per day (1/day)
  bet_hv = 0.022,    # P(infection) per bite, infectious mosquito → susceptible human
  bet_vh = 0.48,     # P(infection) per bite, infectious human → mosquito
  bet_vh_r = 0.048,  # P(infection) per bite, recovered human → mosquito
  nu_h = 0.10,       # human latent progression rate (1/day)
  nu_v = 0.091,      # mosquito latent progression rate (EIP ≈ 11–12 days) (1/day)
  gam_h = 0.0035,    # human recovery rate (1/day)
  delta_h = 9.0e-5,  # disease-induced death rate (1/day)
  rho_h = 5.5e-4,    # loss of immunity (1/day)
  mu1h = 1.6e-5,     # density-independent human death and emigration (1/day)
  mu2h = 3.0e-7,     # density-dependent human death
  mu1v = 0.033,      # density-independent mosquito death (1/day)
  mu2v = 2.0e-5      # density-dependent mosquito death
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 500,   # susceptible humans
  Eh = 10,    # exposed (latent) humans
  Ih = 10,    # infectious humans
  Rh = 4,     # recovered: immune, asymptomatic, slightly infectious
  Sv = 4800,  # susceptible mosquitoes
  Ev = 30,    # exposed mosquitoes
  Iv = 20     # infectious mosquitoes
)

# ---- Model equations -------------------------------------------------
malaria_chitnis2008_seirs_sei_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Eh + Ih + Rh

    # Derived quantities
    Nh <- Sh + Eh + Ih + Rh
    Nv <- Sv + Ev + Iv
    fh <- mu1h + mu2h * Nh                                              # human per-capita death rate
    fv <- mu1v + mu2v * Nv                                              # mosquito per-capita death rate
    bites <- sig_v * Nv * sig_h * Nh / (sig_v * Nv + sig_h * Nh)        # total human–mosquito contacts per day
    lambda_h <- (bites / Nh) * bet_hv * Iv / Nv                         # force of infection on humans
    lambda_v <- (bites / Nv) * (bet_vh * Ih / Nh + bet_vh_r * Rh / Nh)  # force of infection on mosquitoes

    # Rates of change
    dSh <- Lambda_h + psi_h * Nh - lambda_h * Sh + rho_h * Rh - fh * Sh
    dEh <- lambda_h * Sh - nu_h * Eh - fh * Eh
    dIh <- nu_h * Eh - gam_h * Ih - delta_h * Ih - fh * Ih
    dRh <- gam_h * Ih - rho_h * Rh - fh * Rh
    dSv <- psi_v * Nv - lambda_v * Sv - fv * Sv
    dEv <- lambda_v * Sv - nu_v * Ev - fv * Ev
    dIv <- nu_v * Ev - fv * Iv

    list(c(dSh, dEh, dIh, dRh, dSv, dEv, dIv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = malaria_chitnis2008_seirs_sei_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Eh = "#E8C468", Ih = "#B71C1C", Rh = "#4E8A3E", Sv = "#333333", Ev = "#E8C468", Iv = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Malaria SEIRS humans / SEI mosquitoes with saturating biting (Chitnis, Hyman & Cushing 2008) [VERIFIED-FULL]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Ross–Macdonald with superinfection clearance and boosted acquired immunity (Macdonald/Dietz superinfection + Aron 1988 boosting) [ILLUSTRATIVE-COMPOSITE]

Vector-borne
ShIhRhSvEvIv

P. falciparum malaria, stable endemic areas. Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Show R code
## Ross–Macdonald with superinfection clearance and boosted acquired immunity (Macdonald/Dietz superinfection + Aron 1988 boosting) [ILLUSTRATIVE-COMPOSITE]
## P. falciparum malaria, stable endemic areas.
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Ih, Rh, Sv, Ev, Iv
## Flows (time unit: days):
##   (outside) -> Sh  : mu_h * Nh
##   Sh -> Ih         : h_foi * Sh
##   Ih -> Rh         : r_eff * Ih
##   Rh -> Sh         : w_loss * Rh
##   Sh -> (outside)  : mu_h * Sh
##   Ih -> (outside)  : mu_h * Ih
##   Rh -> (outside)  : mu_h * Rh
##   (outside) -> Sv  : mu_v * Nv
##   Sv -> Ev         : lambda_v * Sv
##   Ev -> Iv         : nu_v * Ev
##   Sv -> (outside)  : mu_v * Sv
##   Ev -> (outside)  : mu_v * Ev
##   Iv -> (outside)  : mu_v * Iv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 7300, by = 10)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_bite = 0.3,     # human-biting rate per mosquito (1/day)
  p_mh = 0.1,       # mosquito → human transmission probability per bite
  p_hm = 0.5,       # human → mosquito transmission probability per bite
  r_clear = 0.005,  # clearance rate of a single infection (about 200 days) (1/day)
  tau_imm = 1825,   # immune memory without boosting (illustrative) (days)
  mu_h = 5.5e-5,    # human birth = death rate (50-year life) (1/day)
  mu_v = 0.1,       # mosquito death rate (1/day)
  nu_v = 0.1,       # 1/EIP (1/day)
  m_ratio = 5       # sets the initial Nv; mosquito births balance deaths (mosquitoes/human)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 990,   # susceptible humans
  Ih = 10,    # infected/infectious humans (possibly carrying several broods)
  Rh = 0,     # immune humans: not infectious, immunity kept up by re-exposure
  Sv = 4950,  # susceptible mosquitoes
  Ev = 0,     # mosquitoes in extrinsic incubation
  Iv = 50     # infectious mosquitoes
)

# ---- Model equations -------------------------------------------------
malaria_superinfection_boosting_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Ih + Rh

    # Derived quantities
    Nh <- Sh + Ih + Rh
    Nv <- Sv + Ev + Iv
    h_foi <- pmax(a_bite * p_mh * Iv / Nh, 1e-10)  # human force of infection (EIR × b); the floor avoids 0/0
    r_eff <- h_foi / (exp(h_foi / r_clear) - 1)    # superinfection-adjusted clearance rate (tends to r_clear as h_foi → 0)
    w_loss <- h_foi / (exp(h_foi * tau_imm) - 1)   # immunity loss rate with boosting (tends to 1/tau_imm as h_foi → 0)
    lambda_v <- a_bite * p_hm * Ih / Nh            # mosquito force of infection

    # Rates of change
    dSh <- mu_h * Nh - h_foi * Sh + w_loss * Rh - mu_h * Sh
    dIh <- h_foi * Sh - r_eff * Ih - mu_h * Ih
    dRh <- r_eff * Ih - w_loss * Rh - mu_h * Rh
    dSv <- mu_v * Nv - lambda_v * Sv - mu_v * Sv
    dEv <- lambda_v * Sv - nu_v * Ev - mu_v * Ev
    dIv <- nu_v * Ev - mu_v * Iv

    list(c(dSh, dIh, dRh, dSv, dEv, dIv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = malaria_superinfection_boosting_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Ih = "#B71C1C", Rh = "#4E8A3E", Sv = "#333333", Ev = "#E8C468", Iv = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Ross–Macdonald with superinfection clearance and boosted acquired immunity (Macdonald/Dietz superinfection + Aron 1988 boosting) [ILLUSTRATIVE-COMPOSITE]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Two-serotype dengue with secondary infection (after Esteva & Vargas 2003) [ILLUSTRATIVE-COMPOSITE]

Vector-borne
ShI1I2R1R2I12I21RhSvIv1Iv2

Dengue (DENV, two co-circulating serotypes), tropical urban settings. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Show R code
## Two-serotype dengue with secondary infection (after Esteva & Vargas 2003) [ILLUSTRATIVE-COMPOSITE]
## Dengue (DENV, two co-circulating serotypes), tropical urban settings.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, I1, I2, R1, R2, I12, I21, Rh, Sv, Iv1, Iv2
## Flows (time unit: days):
##   (outside) -> Sh   : mu_h * Nh
##   Sh -> I1          : lam1 * Sh
##   Sh -> I2          : lam2 * Sh
##   I1 -> R1          : gam_h * I1
##   I2 -> R2          : gam_h * I2
##   R1 -> I12         : x_sec * lam2 * R1
##   R2 -> I21         : x_sec * lam1 * R2
##   I12 -> Rh         : gam_h * I12
##   I21 -> Rh         : gam_h * I21
##   Sh -> (outside)   : mu_h * Sh
##   I1 -> (outside)   : mu_h * I1
##   I2 -> (outside)   : mu_h * I2
##   R1 -> (outside)   : mu_h * R1
##   R2 -> (outside)   : mu_h * R2
##   I12 -> (outside)  : mu_h * I12
##   I21 -> (outside)  : mu_h * I21
##   Rh -> (outside)   : mu_h * Rh
##   (outside) -> Sv   : A_v
##   Sv -> Iv1         : lamv1 * Sv
##   Sv -> Iv2         : lamv2 * Sv
##   Sv -> (outside)   : mu_v * Sv
##   Iv1 -> (outside)  : mu_v * Iv1
##   Iv2 -> (outside)  : mu_v * Iv2

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3650, by = 5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_bite = 0.5,    # biting rate (1/day)
  p_vh = 0.5,      # mosquito → human per-bite transmission probability
  p_hv = 0.5,      # human → mosquito per-bite transmission probability
  gam_h = 0.1429,  # recovery rate (7-day viraemia) (1/day)
  mu_h = 4.2e-5,   # human birth/death rate (65-year life) (1/day)
  mu_v = 0.0714,   # mosquito death rate (14-day life) (1/day)
  A_v = 1428,      # mosquito recruitment, = mu_v × 20000 (mosquitoes/day)
  x_sec = 1.0      # susceptibility of R1/R2 to the heterologous serotype relative to Sh. Esteva & Vargas's enhancement factor; values >1 model ADE.
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 9989,   # susceptible to both serotypes
  I1 = 10,     # primary infection with serotype 1
  I2 = 1,      # primary infection with serotype 2
  R1 = 0,      # recovered from serotype 1, susceptible to serotype 2
  R2 = 0,      # recovered from serotype 2, susceptible to serotype 1
  I12 = 0,     # secondary infection with serotype 2 after serotype 1
  I21 = 0,     # secondary infection with serotype 1 after serotype 2
  Rh = 0,      # immune to both
  Sv = 20000,  # susceptible mosquitoes
  Iv1 = 0,     # mosquitoes infectious with serotype 1
  Iv2 = 0      # mosquitoes infectious with serotype 2
)

# ---- Model equations -------------------------------------------------
dengue_two_serotype_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + I1 + I2 + R1 + R2 + I12 + I21 + Rh

    # Derived quantities
    Nh <- Sh + I1 + I2 + R1 + R2 + I12 + I21 + Rh
    Nv <- Sv + Iv1 + Iv2
    lam1 <- a_bite * p_vh * Iv1 / Nh          # human force of infection, serotype 1
    lam2 <- a_bite * p_vh * Iv2 / Nh          # human force of infection, serotype 2
    lamv1 <- a_bite * p_hv * (I1 + I21) / Nh  # mosquito force of infection, serotype 1
    lamv2 <- a_bite * p_hv * (I2 + I12) / Nh  # mosquito force of infection, serotype 2

    # Rates of change
    dSh <- mu_h * Nh - lam1 * Sh - lam2 * Sh - mu_h * Sh
    dI1 <- lam1 * Sh - gam_h * I1 - mu_h * I1
    dI2 <- lam2 * Sh - gam_h * I2 - mu_h * I2
    dR1 <- gam_h * I1 - x_sec * lam2 * R1 - mu_h * R1
    dR2 <- gam_h * I2 - x_sec * lam1 * R2 - mu_h * R2
    dI12 <- x_sec * lam2 * R1 - gam_h * I12 - mu_h * I12
    dI21 <- x_sec * lam1 * R2 - gam_h * I21 - mu_h * I21
    dRh <- gam_h * I12 + gam_h * I21 - mu_h * Rh
    dSv <- A_v - lamv1 * Sv - lamv2 * Sv - mu_v * Sv
    dIv1 <- lamv1 * Sv - mu_v * Iv1
    dIv2 <- lamv2 * Sv - mu_v * Iv2

    list(c(dSh, dI1, dI2, dR1, dR2, dI12, dI21, dRh, dSv, dIv1, dIv2))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = dengue_two_serotype_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", I1 = "#B71C1C", I2 = "#B71C1C", R1 = "#4E8A3E", R2 = "#4E8A3E", I12 = "#B71C1C", I21 = "#B71C1C", Rh = "#4E8A3E", Sv = "#333333", Iv1 = "#B71C1C", Iv2 = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Two-serotype dengue with secondary infection (after Esteva & Vargas 2003) [ILLUSTRATIVE-COMPOSITE]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Zika with mosquito-borne and sexual transmission (Gao et al. 2016) [VERIFIED-PARAMS]

Vector-borne
ShEhI1I2AhRhSvEvIv

Zika virus, Latin America 2015–16 (fitted to Brazil, Colombia, El Salvador). Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.

Gao D, Lou Y, He D, Porco TC, Kuang Y, Chowell G, Ruan S (2016). Prevention and control of Zika as a mosquito-borne and sexually transmitted disease: a mathematical modeling analysis. Sci Rep 6:28070. https://doi.org/10.1038/srep28070 ; PMC:… ↗

Show R code
## Zika with mosquito-borne and sexual transmission (Gao et al. 2016) [VERIFIED-PARAMS]
## Zika virus, Latin America 2015–16 (fitted to Brazil, Colombia, El Salvador).
## Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.
## Reference: Gao D, Lou Y, He D, Porco TC, Kuang Y, Chowell G, Ruan S (2016). Prevention and control of Zika as a mosquito-borne and sexually transmitted disease: a mathematical modeling analysis. Sci Rep 6:28070. https://doi.org/10.1038/srep28070 ; PMC:…
## Source: https://doi.org/10.1038/srep28070
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Eh, I1, I2, Ah, Rh, Sv, Ev, Iv
## Flows (time unit: days):
##   Sh -> Eh         : lambda_h * Sh
##   Eh -> I1         : theta * nu_h * Eh
##   Eh -> Ah         : (1 - theta) * nu_h * Eh
##   I1 -> I2         : gam_h1 * I1
##   I2 -> Rh         : gam_h2 * I2
##   Ah -> Rh         : gam_ha * Ah
##   (outside) -> Sv  : mu_v * Nv
##   Sv -> Ev         : lambda_v * Sv
##   Ev -> Iv         : nu_v * Ev
##   Sv -> (outside)  : mu_v * Sv
##   Ev -> (outside)  : mu_v * Ev
##   Iv -> (outside)  : mu_v * Iv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_bite = 0.5,
  b_vh = 0.4,       # P(mosquito → human) per bite
  p_hv = 0.5,       # P(symptomatic human → mosquito) per bite
  eta = 0.1,        # relative human → mosquito infectivity of Eh
  bet_sex = 0.05,   # sexual transmission rate from symptomatic humans
  kappa = 0.6,      # relative sexual infectivity of Eh
  tau_c = 0.3,      # relative sexual infectivity of convalescent I2
  theta = 0.18,     # proportion symptomatic
  m_ratio = 5,
  nu_h = 0.2,       # 1/(5 d) intrinsic incubation [2–7 d] (1/day)
  nu_v = 0.1,       # 1/(10 d) extrinsic incubation [8–12 d] (1/day)
  gam_h1 = 0.2,     # 1/(5 d) acute phase [3–7 d] (1/day)
  gam_h2 = 0.05,    # 1/(20 d) convalescent phase [14–30 d] (1/day)
  gam_ha = 0.1429,  # 1/(7 d) asymptomatic infection [5–10 d] (1/day)
  mu_v = 0.0714     # 1/(14 d) mosquito life [4–35 d] (1/day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 99990,   # susceptible humans
  Eh = 0,       # exposed humans (mildly infectious to mosquitoes and sexually)
  I1 = 10,      # symptomatic acute
  I2 = 0,       # convalescent (sexually infectious only)
  Ah = 0,       # asymptomatic
  Rh = 0,       # recovered
  Sv = 500000,  # susceptible mosquitoes
  Ev = 0,       # exposed mosquitoes
  Iv = 0        # infectious mosquitoes
)

# ---- Model equations -------------------------------------------------
zika_gao2016_sexual_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Eh + I1 + I2 + Ah + Rh

    # Derived quantities
    Nh <- Sh + Eh + I1 + I2 + Ah + Rh
    Nv <- Sv + Ev + Iv
    lambda_h <- a_bite * b_vh * Iv / Nh + bet_sex * (kappa * Eh + I1 + tau_c * I2) / Nh  # vector-borne plus sexual force of infection
    lambda_v <- a_bite * p_hv * (eta * Eh + I1) / Nh                                     # mosquito force of infection

    # Rates of change
    dSh <- -lambda_h * Sh
    dEh <- lambda_h * Sh - theta * nu_h * Eh - (1 - theta) * nu_h * Eh
    dI1 <- theta * nu_h * Eh - gam_h1 * I1
    dI2 <- gam_h1 * I1 - gam_h2 * I2
    dAh <- (1 - theta) * nu_h * Eh - gam_ha * Ah
    dRh <- gam_h2 * I2 + gam_ha * Ah
    dSv <- mu_v * Nv - lambda_v * Sv - mu_v * Sv
    dEv <- lambda_v * Sv - nu_v * Ev - mu_v * Ev
    dIv <- nu_v * Ev - mu_v * Iv

    list(c(dSh, dEh, dI1, dI2, dAh, dRh, dSv, dEv, dIv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = zika_gao2016_sexual_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Eh = "#E8C468", I1 = "#B71C1C", I2 = "#B71C1C", Ah = "#E57373", Rh = "#4E8A3E", Sv = "#333333", Ev = "#E8C468", Iv = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Zika with mosquito-borne and sexual transmission (Gao et al. 2016) [VERIFIED-PARAMS]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Chikungunya SEIIR humans / SEI Aedes albopictus, Réunion 2005–06 (Yakob & Clements 2013) [VERIFIED-PARAMS]

Vector-borne
ShEhIshIahRhXvYvZv

Chikungunya, Réunion Island epidemic 2005–2006. Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.

Yakob L, Clements ACA (2013). A mathematical model of chikungunya dynamics and control: the major epidemic on Réunion Island. PLoS ONE 8(3):e57448. https://doi.org/10.1371/journal.pone.0057448 . Parameter values were read from the paper this session; β1… ↗

Show R code
## Chikungunya SEIIR humans / SEI Aedes albopictus, Réunion 2005–06 (Yakob & Clements 2013) [VERIFIED-PARAMS]
## Chikungunya, Réunion Island epidemic 2005–2006.
## Parameter values: 0 of 8 checked against the source; the rest are recalled or illustrative.
## Reference: Yakob L, Clements ACA (2013). A mathematical model of chikungunya dynamics and control: the major epidemic on Réunion Island. PLoS ONE 8(3):e57448. https://doi.org/10.1371/journal.pone.0057448 . Parameter values were read from the paper this session; β1…
## Source: https://doi.org/10.1371/journal.pone.0057448
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Eh, Ish, Iah, Rh, Xv, Yv, Zv
## Flows (time unit: days):
##   Sh -> Eh         : lambda_h * Sh
##   Eh -> Ish        : phi_s * lat_h * Eh
##   Eh -> Iah        : (1 - phi_s) * lat_h * Eh
##   Ish -> Rh        : gam_h * Ish
##   Iah -> Rh        : gam_h * Iah
##   (outside) -> Xv  : mu_v * Nv
##   Xv -> Yv         : lambda_v * Xv
##   Yv -> Zv         : lat_v * Yv
##   Xv -> (outside)  : mu_v * Xv
##   Yv -> (outside)  : mu_v * Yv
##   Zv -> (outside)  : mu_v * Zv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta1 = 0.14,  # mosquito → human transmission rate (fitted; Macdonald "abm"-type) (1/day)
  beta2 = 0.40,  # human → mosquito transmission rate (fitted) (1/day)
  lat_h = 0.5,   # human latent rate (2–6 d; Thiboutot et al. 2010) (1/day)
  lat_v = 0.5,   # mosquito latent rate (2–6 d for Ae. albopictus; Dubrulle et al. 2009) (1/day)
  omega = 0.25,  # symptom-onset rate (pre-patent 4–7 d; Ziegler et al. 2008). Used only for the reporting lag. (1/day)
  phi_s = 0.97,  # proportion symptomatic (0.83–0.97)
  gam_h = 0.25,  # recovery rate (1–7 d; Chhabra et al. 2008) (1/day)
  mu_v = 0.05    # mosquito mortality (20–30 d life; Delatte et al. 2009). Mosquito birth = death. (1/day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 785000,  # susceptible humans
  Eh = 0,       # exposed
  Ish = 10,     # symptomatic infectious
  Iah = 0,      # asymptomatic infectious
  Rh = 0,       # recovered
  Xv = 1.0e6,   # susceptible mosquitoes
  Yv = 0,       # exposed mosquitoes
  Zv = 0        # infectious mosquitoes
)

# ---- Model equations -------------------------------------------------
chikungunya_yakob2013_reunion_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Eh + Ish + Iah + Rh

    # Derived quantities
    Nh <- Sh + Eh + Ish + Iah + Rh
    Nv <- Xv + Yv + Zv
    lambda_h <- beta1 * Zv / Nv           # human force of infection; beta1 already contains a·b·m
    lambda_v <- beta2 * (Ish + Iah) / Nh  # mosquito force of infection; independent of m
    inc_sympt <- phi_s * lat_h * Eh       # symptomatic incidence, for comparison with case data; the paper adds an omega lag

    # Rates of change
    dSh <- -lambda_h * Sh
    dEh <- lambda_h * Sh - phi_s * lat_h * Eh - (1 - phi_s) * lat_h * Eh
    dIsh <- phi_s * lat_h * Eh - gam_h * Ish
    dIah <- (1 - phi_s) * lat_h * Eh - gam_h * Iah
    dRh <- gam_h * Ish + gam_h * Iah
    dXv <- mu_v * Nv - lambda_v * Xv - mu_v * Xv
    dYv <- lambda_v * Xv - lat_v * Yv - mu_v * Yv
    dZv <- lat_v * Yv - mu_v * Zv

    list(c(dSh, dEh, dIsh, dIah, dRh, dXv, dYv, dZv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = chikungunya_yakob2013_reunion_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Eh = "#E8C468", Ish = "#B71C1C", Iah = "#B71C1C", Rh = "#4E8A3E", Xv = "#333333", Yv = "#333333", Zv = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Chikungunya SEIIR humans / SEI Aedes albopictus, Réunion 2005–06 (Yakob & Clements 2013) [VERIFIED-PARAMS]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

West Nile virus: mosquitoes–birds–humans (Bowman et al. 2005) [VERIFIED-FULL]

Vector-borne
MuMiBuBiShEhIhHhRh

West Nile virus, North America (single season). Parameter values: 0 of 18 checked against the source; the rest are recalled or illustrative.

Bowman C, Gumel AB, van den Driessche P, Wu J, Zhu H (2005). A mathematical model for assessing control strategies against West Nile virus. Bull Math Biol 67:1107–1133. https://doi.org/10.1016/j.bulm.2005.01.002 ; full text in the YorkSpace repository. The… ↗

Show R code
## West Nile virus: mosquitoes–birds–humans (Bowman et al. 2005) [VERIFIED-FULL]
## West Nile virus, North America (single season).
## Parameter values: 0 of 18 checked against the source; the rest are recalled or illustrative.
## Reference: Bowman C, Gumel AB, van den Driessche P, Wu J, Zhu H (2005). A mathematical model for assessing control strategies against West Nile virus. Bull Math Biol 67:1107–1133. https://doi.org/10.1016/j.bulm.2005.01.002 ; full text in the YorkSpace repository. The…
## Source: https://doi.org/10.1016/j.bulm.2005.01.002
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Mu, Mi, Bu, Bi, Sh, Eh, Ih, Hh, Rh
## Flows (time unit: days):
##   (outside) -> Mu  : Pi_M
##   Mu -> Mi         : lam_M * Mu
##   Mu -> (outside)  : mu_M * Mu
##   Mi -> (outside)  : mu_M * Mi
##   (outside) -> Bu  : Pi_B
##   Bu -> Bi         : lam_B * Bu
##   Bu -> (outside)  : mu_B * Bu
##   Bi -> (outside)  : mu_B * Bi
##   Bi -> (outside)  : d_B * Bi
##   (outside) -> Sh  : Pi_H
##   Sh -> Eh         : lam_H * Sh
##   Eh -> Ih         : alpha_h * Eh
##   Ih -> Hh         : delta_h * Ih
##   Hh -> Rh         : tau_h * Hh
##   Hh -> (outside)  : d_H * Hh
##   Sh -> (outside)  : mu_H * Sh
##   Eh -> (outside)  : mu_H * Eh
##   Ih -> (outside)  : mu_H * Ih
##   Hh -> (outside)  : mu_H * Hh
##   Rh -> (outside)  : mu_H * Rh

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  Pi_M = 20000,      # mosquito recruitment. "Variable" in the paper; this value is illustrative. (mosquitoes/day)
  mu_M = 0.05,       # mosquito death. "Variable" in the paper; illustrative 20-day life. (1/day)
  Pi_B = 1000,       # bird recruitment (birds/day)
  mu_B = 0.001,      # 1/(1000 d) bird life (1/day)
  Pi_H = 30,         # human recruitment (humans/day)
  mu_H = 3.914e-5,   # 1/(70×365 d) (1/day)
  b1 = 0.09,         # biting rate on birds (1/day)
  b2_base = 0.09,    # biting rate on humans before protection (1/day)
  comp_c = 0,        # fraction complying with personal protection (0–1)
  q_eff = 0,         # efficacy of personal protection (0–1)
  bet1 = 0.16,       # bird → mosquito transmission probability
  bet2 = 0.88,       # mosquito → bird transmission probability
  bet3 = 0.88,       # mosquito → human transmission probability
  d_B = 5e-5,        # WNV-induced bird death (1/day)
  d_H = 5e-7,        # WNV-induced human death (1/day)
  alpha_h = 0.0714,  # 1/(14 d) incubation in humans (1/day)
  delta_h = 1,       # hospitalisation rate (1/day)
  tau_h = 0.0714     # 1/(14 d) recovery (1/day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Mu = 400000,  # uninfected female mosquitoes
  Mi = 0,       # infected (infectious) mosquitoes
  Bu = 999990,  # uninfected birds
  Bi = 10,      # infected birds
  Sh = 766500,  # susceptible humans
  Eh = 0,       # infected, asymptomatic (incubating)
  Ih = 0,       # symptomatic
  Hh = 0,       # hospitalised
  Rh = 0        # recovered (lifelong immunity)
)

# ---- Model equations -------------------------------------------------
wnv_bowman2005_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Eh + Ih + Hh + Rh

    # Derived quantities
    NM <- Mu + Mi
    NB <- Bu + Bi
    NH <- Sh + Eh + Ih + Hh + Rh
    b2 <- b2_base * (1 - comp_c * q_eff)  # biting rate on humans after protection
    lam_M <- b1 * bet1 * Bi / NB          # mosquito force of infection
    lam_B <- b1 * bet2 * Mi / NB          # bird force of infection
    lam_H <- b2 * bet3 * Mi / NH          # human force of infection

    # Rates of change
    dMu <- Pi_M - lam_M * Mu - mu_M * Mu
    dMi <- lam_M * Mu - mu_M * Mi
    dBu <- Pi_B - lam_B * Bu - mu_B * Bu
    dBi <- lam_B * Bu - mu_B * Bi - d_B * Bi
    dSh <- Pi_H - lam_H * Sh - mu_H * Sh
    dEh <- lam_H * Sh - alpha_h * Eh - mu_H * Eh
    dIh <- alpha_h * Eh - delta_h * Ih - mu_H * Ih
    dHh <- delta_h * Ih - tau_h * Hh - d_H * Hh - mu_H * Hh
    dRh <- tau_h * Hh - mu_H * Rh

    list(c(dMu, dMi, dBu, dBi, dSh, dEh, dIh, dHh, dRh))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = wnv_bowman2005_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Mu = "#F3C6D3", Mi = "#F3C6D3", Bu = "#333333", Bi = "#333333", Sh = "#333333", Eh = "#E8C468", Ih = "#B71C1C", Hh = "#6A2C9C", Rh = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "West Nile virus: mosquitoes–birds–humans (Bowman et al. 2005) [VERIFIED-FULL]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Lyme borreliosis: stage-structured Ixodes ticks with a mouse reservoir and deer as tick-only hosts [ILLUSTRATIVE-COMPOSITE]

Vector-borne
LNsNiAsAiMsMi

Lyme disease (Borrelia burgdorferi s.l.), temperate woodland in NE USA or Europe. Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.

Show R code
## Lyme borreliosis: stage-structured Ixodes ticks with a mouse reservoir and deer as tick-only hosts [ILLUSTRATIVE-COMPOSITE]
## Lyme disease (Borrelia burgdorferi s.l.), temperate woodland in NE USA or Europe.
## Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: L, Ns, Ni, As, Ai, Ms, Mi
## Flows (time unit: days):
##   (outside) -> L   : eps_egg * a_A * Deer * (As + Ai)
##   L -> Ns          : a_L * s_L * (Hm - p_ML * Mi) * L
##   L -> Ni          : a_L * s_L * p_ML * Mi * L
##   L -> (outside)   : (a_L * Hm * (1 - s_L) + mu_L + mu_L2 * L) * L
##   Ns -> As         : a_N * s_N * (Hm - p_ML * Mi) * Ns
##   Ns -> Ai         : a_N * s_N * p_ML * Mi * Ns
##   Ns -> (outside)  : (a_N * Hm * (1 - s_N) + mu_N) * Ns
##   Ni -> Ai         : a_N * s_N * Hm * Ni
##   Ni -> (outside)  : (a_N * Hm * (1 - s_N) + mu_N) * Ni
##   As -> (outside)  : (a_A * Deer + mu_A) * As
##   Ai -> (outside)  : (a_A * Deer + mu_A) * Ai
##   (outside) -> Ms  : mu_m * K_m
##   Ms -> Mi         : a_N * p_NM * Ni * Ms
##   Ms -> (outside)  : mu_m * Ms
##   Mi -> (outside)  : mu_m * Mi

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 7300, by = 10)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_L = 1e-4,      # larval host-finding rate per mouse
  a_N = 1e-4,      # nymphal host-finding rate per mouse
  a_A = 0.005,     # adult host-finding rate per deer
  Deer = 5,        # deer density, constant; not competent for Borrelia (deer)
  s_L = 0.3,       # survival from fed larva to nymph
  s_N = 0.3,       # survival from fed nymph to adult
  eps_egg = 1000,  # effective larvae per fed adult (larvae/fed adult)
  mu_L = 0.01,     # larval death (1/day)
  mu_L2 = 4.8e-5,  # density-dependent larval death
  mu_N = 0.005,    # nymphal death (1/day)
  mu_A = 0.005,    # adult death (1/day)
  p_NM = 0.8,      # infected nymph → mouse transmission probability
  p_ML = 0.5,      # infected mouse → feeding larva transmission probability
  mu_m = 0.005,    # mouse death (1/day)
  K_m = 100        # mouse population; recruitment = mu_m·K_m (mice)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  L = 10000,  # questing larvae (born uninfected; no transovarial transmission)
  Ns = 1000,  # uninfected questing nymphs
  Ni = 0,     # infected questing nymphs
  As = 100,   # uninfected questing adults
  Ai = 0,     # infected adults
  Ms = 95,    # susceptible mice (reservoir)
  Mi = 5      # infected mice (lifelong)
)

# ---- Model equations -------------------------------------------------
lyme_tick_mouse_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Ms + Mi

    # Derived quantities
    Hm <- Ms + Mi          # mice
    Nticks <- L + Ns + Ni + As + Ai
    prev_mouse <- Mi / Hm  # mouse infection prevalence
    nip <- Ni / (Ns + Ni)  # nymphal infection prevalence (the human-risk index)

    # Rates of change
    dL <- eps_egg * a_A * Deer * (As + Ai) - a_L * s_L * (Hm - p_ML * Mi) * L - a_L * s_L * p_ML * Mi * L - (a_L * Hm * (1 - s_L) + mu_L + mu_L2 * L) * L
    dNs <- a_L * s_L * (Hm - p_ML * Mi) * L - a_N * s_N * (Hm - p_ML * Mi) * Ns - a_N * s_N * p_ML * Mi * Ns - (a_N * Hm * (1 - s_N) + mu_N) * Ns
    dNi <- a_L * s_L * p_ML * Mi * L - a_N * s_N * Hm * Ni - (a_N * Hm * (1 - s_N) + mu_N) * Ni
    dAs <- a_N * s_N * (Hm - p_ML * Mi) * Ns - (a_A * Deer + mu_A) * As
    dAi <- a_N * s_N * p_ML * Mi * Ns + a_N * s_N * Hm * Ni - (a_A * Deer + mu_A) * Ai
    dMs <- mu_m * K_m - a_N * p_NM * Ni * Ms - mu_m * Ms
    dMi <- a_N * p_NM * Ni * Ms - mu_m * Mi

    list(c(dL, dNs, dNi, dAs, dAi, dMs, dMi))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = lyme_tick_mouse_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(L = "#E8C468", Ns = "#333333", Ni = "#333333", As = "#E57373", Ai = "#E57373", Ms = "#F3C6D3", Mi = "#F3C6D3")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Lyme borreliosis: stage-structured Ixodes ticks with a mouse reservoir and deer as tick-only hosts [ILLUSTRATIVE-COMPOSITE]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Visceral leishmaniasis (anthroponotic, Indian subcontinent) with asymptomatic, VL, treatment and PKDL; sandfly SEI (simplified after Stauch et al. 2011) [ILLUSTRATIVE-COMPOSITE]

Vector-borne
ShAhKhTxPhRhSfEfIf

Visceral leishmaniasis (L. donovani, Phlebotomus argentipes), Bihar, India / Nepal. Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.

Show R code
## Visceral leishmaniasis (anthroponotic, Indian subcontinent) with asymptomatic, VL, treatment and PKDL; sandfly SEI (simplified after Stauch et al. 2011) [ILLUSTRATIVE-COMPOSITE]
## Visceral leishmaniasis (L. donovani, Phlebotomus argentipes), Bihar, India / Nepal.
## Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Ah, Kh, Tx, Ph, Rh, Sf, Ef, If
## Flows (time unit: days):
##   (outside) -> Sh  : mu_h * Nh
##   Sh -> Ah         : lambda_h * Sh
##   Ah -> Kh         : p_K * w_A * Ah
##   Ah -> Rh         : (1 - p_K) * w_A * Ah
##   Kh -> Tx         : r_tx * Kh
##   Kh -> (outside)  : d_K * Kh
##   Tx -> Ph         : f_P * w_Tx * Tx
##   Tx -> Rh         : (1 - f_P) * w_Tx * Tx
##   Ph -> Rh         : w_P * Ph
##   Rh -> Sh         : rho_h * Rh
##   Sh -> (outside)  : mu_h * Sh
##   Ah -> (outside)  : mu_h * Ah
##   Kh -> (outside)  : mu_h * Kh
##   Tx -> (outside)  : mu_h * Tx
##   Ph -> (outside)  : mu_h * Ph
##   Rh -> (outside)  : mu_h * Rh
##   (outside) -> Sf  : mu_f * Nf
##   Sf -> Ef         : lambda_f * Sf
##   Ef -> If         : nu_f * Ef
##   Sf -> (outside)  : mu_f * Sf
##   Ef -> (outside)  : mu_f * Ef
##   If -> (outside)  : mu_f * If

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 7300, by = 10)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_sf = 0.25,       # sandfly biting rate (bite every 4 days) (1/day)
  p_fh = 0.1,        # sandfly → human per-bite infection probability
  k_A = 0.025,       # per-bite infectivity of asymptomatic humans to sandflies
  k_K = 0.5,         # per-bite infectivity of VL patients
  k_Tx = 0.25,       # per-bite infectivity of treated patients
  k_P = 0.3,         # per-bite infectivity of PKDL patients
  w_A = 0.00667,     # exit from asymptomatic (150 d) (1/day)
  p_K = 0.1,         # fraction of infections progressing to VL
  r_tx = 0.0167,     # VL onset → treatment (60 d) (1/day)
  d_K = 0.00167,     # VL death before treatment (1/day)
  w_Tx = 0.0357,     # treatment duration (28 d) (1/day)
  f_P = 0.025,       # fraction developing PKDL after treatment
  w_P = 0.00091,     # PKDL duration (3 yr) (1/day)
  rho_h = 0.000548,  # waning immunity (5 yr) (1/day)
  mu_h = 4.57e-5,    # human birth/death (60-yr life) (1/day)
  mu_f = 0.0714,     # sandfly death (14-d life) (1/day)
  nu_f = 0.2         # 1/EIP (5 d) (1/day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 9800,   # susceptible humans
  Ah = 150,    # asymptomatic infected (weakly infectious)
  Kh = 10,     # clinical VL (kala-azar, infectious)
  Tx = 0,      # under treatment
  Ph = 0,      # post-kala-azar dermal leishmaniasis (infectious)
  Rh = 40,     # recovered/immune
  Sf = 49000,  # susceptible sandflies
  Ef = 500,    # sandflies in extrinsic incubation
  If = 500     # infectious sandflies
)

# ---- Model equations -------------------------------------------------
vl_leishmaniasis_isc_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Ah + Kh + Tx + Ph + Rh

    # Derived quantities
    Nh <- Sh + Ah + Kh + Tx + Ph + Rh
    Nf <- Sf + Ef + If
    lambda_h <- a_sf * p_fh * If / Nh                                     # human force of infection
    lambda_f <- a_sf * (k_A * Ah + k_K * Kh + k_Tx * Tx + k_P * Ph) / Nh  # sandfly force of infection

    # Rates of change
    dSh <- mu_h * Nh - lambda_h * Sh + rho_h * Rh - mu_h * Sh
    dAh <- lambda_h * Sh - p_K * w_A * Ah - (1 - p_K) * w_A * Ah - mu_h * Ah
    dKh <- p_K * w_A * Ah - r_tx * Kh - d_K * Kh - mu_h * Kh
    dTx <- r_tx * Kh - f_P * w_Tx * Tx - (1 - f_P) * w_Tx * Tx - mu_h * Tx
    dPh <- f_P * w_Tx * Tx - w_P * Ph - mu_h * Ph
    dRh <- (1 - p_K) * w_A * Ah + (1 - f_P) * w_Tx * Tx + w_P * Ph - rho_h * Rh - mu_h * Rh
    dSf <- mu_f * Nf - lambda_f * Sf - mu_f * Sf
    dEf <- lambda_f * Sf - nu_f * Ef - mu_f * Ef
    dIf <- nu_f * Ef - mu_f * If

    list(c(dSh, dAh, dKh, dTx, dPh, dRh, dSf, dEf, dIf))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = vl_leishmaniasis_isc_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Ah = "#E57373", Kh = "#333333", Tx = "#333333", Ph = "#F0A35E", Rh = "#4E8A3E", Sf = "#333333", Ef = "#E8C468", If = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Visceral leishmaniasis (anthroponotic, Indian subcontinent) with asymptomatic, VL, treatment and PKDL; sandfly SEI (simplified after Stauch et al. 2011) [ILLUSTRATIVE-COMPOSITE]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Domestic Chagas transmission: Triatoma infestans with humans, dogs and non-competent chickens (after Cohen & Gürtler 2001) [ILLUSTRATIVE-COMPOSITE]

Vector-borne
HsHiGsGiVsVi

Chagas disease (Trypanosoma cruzi), rural households, Santiago del Estero, Argentina. Parameter values: 0 of 14 checked against the source; the rest are recalled or illustrative.

Show R code
## Domestic Chagas transmission: Triatoma infestans with humans, dogs and non-competent chickens (after Cohen & Gürtler 2001) [ILLUSTRATIVE-COMPOSITE]
## Chagas disease (Trypanosoma cruzi), rural households, Santiago del Estero, Argentina.
## Parameter values: 0 of 14 checked against the source; the rest are recalled or illustrative.
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Hs, Hi, Gs, Gi, Vs, Vi
## Flows (time unit: days):
##   (outside) -> Hs  : mu_hum * Nh
##   Hs -> Hi         : lam_hum * Hs
##   Hs -> (outside)  : mu_hum * Hs
##   Hi -> (outside)  : mu_hum * Hi
##   (outside) -> Gs  : mu_dog * Ng
##   Gs -> Gi         : lam_dog * Gs
##   Gs -> (outside)  : mu_dog * Gs
##   Gi -> (outside)  : mu_dog * Gi
##   (outside) -> Vs  : r_bug * Nv
##   Vs -> Vi         : lam_bug * Vs
##   Vs -> (outside)  : (mu_bug + (r_bug - mu_bug) * Nv / K_bug) * Vs
##   Vi -> (outside)  : (mu_bug + (r_bug - mu_bug) * Nv / K_bug) * Vi

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 7300, by = 10)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_b = 0.1429,      # blood meals per bug per day (weekly) (1/day)
  w_hum = 1,         # feeding weight on humans
  w_dog = 2.5,       # feeding weight on dogs (dogs preferred)
  w_chk = 1,         # feeding weight on chickens
  N_chk = 5,         # constant (chickens)
  p_vh = 5.8e-4,     # bug → human per-contact probability (Nouvellet 2013)
  p_vg = 0.001,      # bug → dog per-contact probability
  p_hv = 0.05,       # infected human → bug per-meal probability
  p_gv = 0.49,       # infected dog → bug per-meal probability
  mu_hum = 4.57e-5,  # human birth/death (1/day)
  mu_dog = 9.13e-4,  # dog birth/death (3-yr life) (1/day)
  r_bug = 0.02,      # bug per-capita recruitment (1/day)
  mu_bug = 0.005,    # bug death (1/day)
  K_bug = 1000       # (bugs/house)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Hs = 5,    # uninfected humans
  Hi = 0,    # infected humans (chronic, lifelong)
  Gs = 1.9,  # uninfected dogs
  Gi = 0.1,  # infected dogs
  Vs = 990,  # uninfected triatomine bugs
  Vi = 10    # infected bugs (lifelong)
)

# ---- Model equations -------------------------------------------------
chagas_household_cohen2001_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Hs + Hi + Gs + Gi

    # Derived quantities
    Nh <- Hs + Hi
    Ng <- Gs + Gi
    Nv <- Vs + Vi
    wsum <- w_hum * Nh + w_dog * Ng + w_chk * N_chk
    f_hum <- w_hum * Nh / wsum                                          # share of bites on humans
    f_dog <- w_dog * Ng / wsum                                          # share of bites on dogs
    lam_hum <- a_b * f_hum * p_vh * Vi / Nh                             # human force of infection
    lam_dog <- a_b * f_dog * p_vg * Vi / Ng                             # dog force of infection
    lam_bug <- a_b * (f_hum * p_hv * Hi / Nh + f_dog * p_gv * Gi / Ng)  # bug force of infection

    # Rates of change
    dHs <- mu_hum * Nh - lam_hum * Hs - mu_hum * Hs
    dHi <- lam_hum * Hs - mu_hum * Hi
    dGs <- mu_dog * Ng - lam_dog * Gs - mu_dog * Gs
    dGi <- lam_dog * Gs - mu_dog * Gi
    dVs <- r_bug * Nv - lam_bug * Vs - (mu_bug + (r_bug - mu_bug) * Nv / K_bug) * Vs
    dVi <- lam_bug * Vs - (mu_bug + (r_bug - mu_bug) * Nv / K_bug) * Vi

    list(c(dHs, dHi, dGs, dGi, dVs, dVi))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = chagas_household_cohen2001_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Hs = "#6A2C9C", Hi = "#6A2C9C", Gs = "#333333", Gi = "#333333", Vs = "#2457C5", Vi = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Domestic Chagas transmission: Triatoma infestans with humans, dogs and non-competent chickens (after Cohen & Gürtler 2001) [ILLUSTRATIVE-COMPOSITE]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Urban yellow fever: SEIR humans with severe (toxic) phase and reactive vaccination; Aedes aegypti SEI [ILLUSTRATIVE-COMPOSITE]

Vector-borne
ShEhIhSevRhVhSvEvIv

Yellow fever (urban cycle), e.g. Angola/DRC 2016-type outbreak. Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.

a standard structure. Natural-history values are recalled, not re-checked this session:

Show R code
## Urban yellow fever: SEIR humans with severe (toxic) phase and reactive vaccination; Aedes aegypti SEI [ILLUSTRATIVE-COMPOSITE]
## Yellow fever (urban cycle), e.g. Angola/DRC 2016-type outbreak.
## Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.
## Reference: a standard structure. Natural-history values are recalled, not re-checked this session:
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Eh, Ih, Sev, Rh, Vh, Sv, Ev, Iv
## Flows (time unit: days):
##   Sh -> Eh          : lambda_h * Sh
##   Sh -> Vh          : vacc * Sh
##   Eh -> Ih          : nu_h * Eh
##   Ih -> Sev         : p_sev * gam_h * Ih
##   Ih -> Rh          : (1 - p_sev) * gam_h * Ih
##   Sev -> (outside)  : cfr_sev * w_sev * Sev
##   Sev -> Rh         : (1 - cfr_sev) * w_sev * Sev
##   (outside) -> Sv   : mu_v * Nv
##   Sv -> Ev          : lambda_v * Sv
##   Ev -> Iv          : nu_v * Ev
##   Sv -> (outside)   : mu_v * Sv
##   Ev -> (outside)   : mu_v * Ev
##   Iv -> (outside)   : mu_v * Iv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_bite = 0.5,    # (1/day)
  p_vh = 0.5,      # mosquito → human
  p_hv = 0.5,      # human → mosquito
  nu_h = 0.2326,   # 1/4.3 d intrinsic incubation (Johansson 2010) (1/day)
  gam_h = 0.2222,  # 1/4.5 d viraemia (1/day)
  p_sev = 0.12,    # fraction entering the severe phase (Johansson 2014)
  cfr_sev = 0.47,  # case fatality among severe (Johansson 2014)
  w_sev = 0.1429,  # 1/7 d duration of the severe phase (1/day)
  nu_v = 0.1,      # 1/10 d EIP (Johansson 2010) (1/day)
  mu_v = 0.0714,   # (1/day)
  v_rate = 0.01,   # campaign vaccination rate (1/day)
  v_eff = 0.99,    # vaccine efficacy (WHO)
  t_vac = 60       # campaign start (day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 79990,   # susceptible
  Eh = 0,       # incubating
  Ih = 10,      # viraemic (infectious)
  Sev = 0,      # severe/toxic phase (assumed non-infectious)
  Rh = 0,       # recovered
  Vh = 20000,   # vaccinated/immune
  Sv = 200000,  # susceptible mosquitoes
  Ev = 0,       # exposed mosquitoes
  Iv = 0        # infectious mosquitoes
)

# ---- Model equations -------------------------------------------------
yellow_fever_urban_vacc_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Eh + Ih + Sev + Rh + Vh

    # Derived quantities
    Nh <- Sh + Eh + Ih + Sev + Rh + Vh
    Nv <- Sv + Ev + Iv
    lambda_h <- a_bite * p_vh * Iv / Nh            # human force of infection
    lambda_v <- a_bite * p_hv * Ih / Nh            # mosquito force of infection
    vacc <- ifelse(t >= t_vac, v_rate * v_eff, 0)  # effective vaccination rate

    # Rates of change
    dSh <- -lambda_h * Sh - vacc * Sh
    dEh <- lambda_h * Sh - nu_h * Eh
    dIh <- nu_h * Eh - p_sev * gam_h * Ih - (1 - p_sev) * gam_h * Ih
    dSev <- p_sev * gam_h * Ih - cfr_sev * w_sev * Sev - (1 - cfr_sev) * w_sev * Sev
    dRh <- (1 - p_sev) * gam_h * Ih + (1 - cfr_sev) * w_sev * Sev
    dVh <- vacc * Sh
    dSv <- mu_v * Nv - lambda_v * Sv - mu_v * Sv
    dEv <- lambda_v * Sv - nu_v * Ev - mu_v * Ev
    dIv <- nu_v * Ev - mu_v * Iv

    list(c(dSh, dEh, dIh, dSev, dRh, dVh, dSv, dEv, dIv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = yellow_fever_urban_vacc_model, parms = parameters, hmax = 0.5)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Eh = "#E8C468", Ih = "#B71C1C", Sev = "#333333", Rh = "#4E8A3E", Vh = "#2457C5", Sv = "#333333", Ev = "#E8C468", Iv = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Urban yellow fever: SEIR humans with severe (toxic) phase and reactive vaccination; Aedes aegypti SEI [ILLUSTRATIVE-COMPOSITE]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Rift Valley fever: Aedes (with transovarial transmission and eggs), Culex and livestock (Gaff, Hartley & Leahy 2007) [STRUCTURE-FROM-RECALL]

Vector-borne
PaQaSaEaIaPcScEcIcSlElIlRl

Rift Valley fever, East Africa (Kenya-type floodplain). Parameter values: 0 of 24 checked against the source; the rest are recalled or illustrative.

Gaff HD, Hartley DM, Leahy NP (2007). An epidemiological model of Rift Valley fever. Electron J Differ Equ 2007(115):1–12. Open access: https://ejde.math.txstate.edu/Volumes/2007/115/abstr.html . The abstract confirms two mosquito populations (Aedes, which… ↗

Show R code
## Rift Valley fever: Aedes (with transovarial transmission and eggs), Culex and livestock (Gaff, Hartley & Leahy 2007) [STRUCTURE-FROM-RECALL]
## Rift Valley fever, East Africa (Kenya-type floodplain).
## Parameter values: 0 of 24 checked against the source; the rest are recalled or illustrative.
## Reference: Gaff HD, Hartley DM, Leahy NP (2007). An epidemiological model of Rift Valley fever. Electron J Differ Equ 2007(115):1–12. Open access: https://ejde.math.txstate.edu/Volumes/2007/115/abstr.html . The abstract confirms two mosquito populations (Aedes, which…
## Source: https://ejde.math.txstate.edu/Volumes/2007/115/abstr.html
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Pa, Qa, Sa, Ea, Ia, Pc, Sc, Ec, Ic, Sl, El, Il, Rl
## Flows (time unit: days):
##   (outside) -> Pa  : (b_a * (Sa + Ea) + b_a * (1 - q_tov) * Ia)
##   (outside) -> Qa  : b_a * q_tov * Ia
##   Pa -> Sa         : th_a * flood * pmax(0, 1 - Nas / K_a) * Pa
##   Qa -> Ia         : th_a * flood * pmax(0, 1 - Nas / K_a) * Qa
##   Pa -> (outside)  : mu_ea * Pa
##   Qa -> (outside)  : mu_ea * Qa
##   Sa -> Ea         : lam_a * Sa
##   Ea -> Ia         : eps_a * Ea
##   Sa -> (outside)  : mu_a * Sa
##   Ea -> (outside)  : mu_a * Ea
##   Ia -> (outside)  : mu_a * Ia
##   (outside) -> Pc  : b_c * Ncs
##   Pc -> Sc         : th_c * flood * pmax(0, 1 - Ncs / K_c) * Pc
##   Pc -> (outside)  : mu_ec * Pc
##   Sc -> Ec         : lam_c * Sc
##   Ec -> Ic         : eps_c * Ec
##   Sc -> (outside)  : mu_c * Sc
##   Ec -> (outside)  : mu_c * Ec
##   Ic -> (outside)  : mu_c * Ic
##   (outside) -> Sl  : mu_l * Nl
##   Sl -> El         : lam_l * Sl
##   El -> Il         : eps_l * El
##   Il -> Rl         : gam_l * Il
##   Il -> (outside)  : d_l * Il
##   Sl -> (outside)  : mu_l * Sl
##   El -> (outside)  : mu_l * El
##   Il -> (outside)  : mu_l * Il
##   Rl -> (outside)  : mu_l * Rl

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 730, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  b_a = 0.2,      # (eggs/female/day)
  q_tov = 0.05,   # transovarial fraction of eggs from infectious Aedes
  th_a = 0.05,    # egg hatching rate (multiplied by the flood indicator) (1/day)
  mu_ea = 0.001,  # Aedes egg mortality; desiccation-resistant eggs (1/day)
  K_a = 2e5,      # Aedes adult capacity (mosquitoes)
  mu_a = 0.05,    # Aedes adult mortality (1/day)
  eps_a = 0.143,  # 1/7 d EIP in Aedes (1/day)
  a_a = 0.3,      # Aedes biting rate (1/day)
  b_c = 0.2,      # (eggs/female/day)
  th_c = 0.1,     # Culex hatching rate (1/day)
  mu_ec = 0.1,    # Culex egg mortality (1/day)
  K_c = 2e5,      # Culex adult capacity (mosquitoes)
  mu_c = 0.05,    # Culex adult mortality (1/day)
  eps_c = 0.143,  # 1/7 d EIP in Culex (1/day)
  a_c = 0.3,      # Culex biting rate (1/day)
  p_al = 0.3,     # Aedes → livestock per-bite probability
  p_cl = 0.3,     # Culex → livestock per-bite probability
  p_la = 0.3,     # livestock → Aedes per-bite probability
  p_lc = 0.3,     # livestock → Culex per-bite probability
  eps_l = 0.25,   # 1/4 d incubation in livestock (1/day)
  gam_l = 0.25,   # 1/4 d infectious period (1/day)
  d_l = 0.025,    # RVF-induced livestock death (1/day)
  mu_l = 5.5e-4,  # livestock death (5-yr life); births = mu_l·Nl (1/day)
  flood_len = 60  # wet-season window each year (days)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Pa = 1e6,   # uninfected Aedes eggs
  Qa = 0,     # infected Aedes eggs (transovarial)
  Sa = 1e5,   # susceptible Aedes
  Ea = 0,     # exposed Aedes
  Ia = 10,    # infectious Aedes
  Pc = 1e5,   # Culex eggs
  Sc = 1e5,   # susceptible Culex
  Ec = 0,     # exposed Culex
  Ic = 0,     # infectious Culex
  Sl = 1000,  # susceptible livestock
  El = 0,     # exposed livestock
  Il = 0,     # infectious livestock
  Rl = 0      # recovered livestock
)

# ---- Model equations -------------------------------------------------
rvf_gaff2007_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sl + El + Il + Rl

    # Derived quantities
    Nas <- Sa + Ea + Ia                                          # adult Aedes
    Ncs <- Sc + Ec + Ic                                          # adult Culex
    Nl <- Sl + El + Il + Rl                                      # livestock
    flood <- ifelse(t - 365 * floor(t / 365) < flood_len, 1, 0)  # annual flooding window
    lam_l <- (a_a * p_al * Ia + a_c * p_cl * Ic) / Nl            # livestock force of infection
    lam_a <- a_a * p_la * Il / Nl                                # Aedes force of infection
    lam_c <- a_c * p_lc * Il / Nl                                # Culex force of infection

    # Rates of change
    dPa <- (b_a * (Sa + Ea) + b_a * (1 - q_tov) * Ia) - th_a * flood * pmax(0, 1 - Nas / K_a) * Pa - mu_ea * Pa
    dQa <- b_a * q_tov * Ia - th_a * flood * pmax(0, 1 - Nas / K_a) * Qa - mu_ea * Qa
    dSa <- th_a * flood * pmax(0, 1 - Nas / K_a) * Pa - lam_a * Sa - mu_a * Sa
    dEa <- lam_a * Sa - eps_a * Ea - mu_a * Ea
    dIa <- th_a * flood * pmax(0, 1 - Nas / K_a) * Qa + eps_a * Ea - mu_a * Ia
    dPc <- b_c * Ncs - th_c * flood * pmax(0, 1 - Ncs / K_c) * Pc - mu_ec * Pc
    dSc <- th_c * flood * pmax(0, 1 - Ncs / K_c) * Pc - lam_c * Sc - mu_c * Sc
    dEc <- lam_c * Sc - eps_c * Ec - mu_c * Ec
    dIc <- eps_c * Ec - mu_c * Ic
    dSl <- mu_l * Nl - lam_l * Sl - mu_l * Sl
    dEl <- lam_l * Sl - eps_l * El - mu_l * El
    dIl <- eps_l * El - gam_l * Il - d_l * Il - mu_l * Il
    dRl <- gam_l * Il - mu_l * Rl

    list(c(dPa, dQa, dSa, dEa, dIa, dPc, dSc, dEc, dIc, dSl, dEl, dIl, dRl))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = rvf_gaff2007_model, parms = parameters, hmax = 1)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Pa = "#F0A35E", Qa = "#00838F", Sa = "#333333", Ea = "#E8C468", Ia = "#B71C1C", Pc = "#F0A35E", Sc = "#333333", Ec = "#E8C468", Ic = "#B71C1C", Sl = "#333333", El = "#E8C468", Il = "#B71C1C", Rl = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Rift Valley fever: Aedes (with transovarial transmission and eggs), Culex and livestock (Gaff, Hartley & Leahy 2007) [STRUCTURE-FROM-RECALL]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Bubonic plague: rats (SIR with inherited resistance), flea index, free infectious fleas, human SIR (Keeling & Gilligan 2000) [STRUCTURE-FROM-RECALL]

Vector-borne
SratIratRratNfFfShumIhumRhum

Bubonic plague (Yersinia pestis), Rattus rattus – Xenopsylla cheopis – humans; historical Europe. Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.

Keeling MJ, Gilligan CA (2000). Metapopulation dynamics of bubonic plague. Nature 407:903–906. https://doi.org/10.1038/35038073 ; and Keeling MJ, Gilligan CA (2000) Bubonic plague: a metapopulation model of a zoonosis. Proc R Soc B 267:2219–2230,… ↗

Show R code
## Bubonic plague: rats (SIR with inherited resistance), flea index, free infectious fleas, human SIR (Keeling & Gilligan 2000) [STRUCTURE-FROM-RECALL]
## Bubonic plague (Yersinia pestis), Rattus rattus – Xenopsylla cheopis – humans; historical Europe.
## Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.
## Reference: Keeling MJ, Gilligan CA (2000). Metapopulation dynamics of bubonic plague. Nature 407:903–906. https://doi.org/10.1038/35038073 ; and Keeling MJ, Gilligan CA (2000) Bubonic plague: a metapopulation model of a zoonosis. Proc R Soc B 267:2219–2230,…
## Source: https://doi.org/10.1038/35038073
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Srat, Irat, Rrat, Nf, Ff, Shum, Ihum, Rhum
## Flows (time unit: years):
##   (outside) -> Srat  : r_R * Srat
##   (outside) -> Srat  : r_R * (1 - p_inh) * Rrat
##   (outside) -> Rrat  : r_R * p_inh * Rrat
##   Srat -> (outside)  : (d_R + r_R * Nrat / K_R) * Srat
##   Rrat -> (outside)  : (d_R + r_R * Nrat / K_R) * Rrat
##   Srat -> Irat       : lam_rat * Srat
##   Irat -> Rrat       : m_R * g_R * Irat
##   Irat -> (outside)  : m_R * (1 - g_R) * Irat
##   Irat -> (outside)  : d_R * Irat
##   (outside) -> Nf    : r_F * Nf
##   Nf -> (outside)    : r_F * Nf / K_F * Nf
##   (outside) -> Ff    : (d_R + m_R * (1 - g_R)) * Irat * Nf
##   Ff -> (outside)    : d_F * Ff
##   (outside) -> Shum  : mu_hum * Nhum
##   Shum -> Ihum       : lam_hum * Shum
##   Ihum -> Rhum       : m_H * g_H * Ihum
##   Ihum -> (outside)  : m_H * (1 - g_H) * Ihum
##   Shum -> (outside)  : mu_hum * Shum
##   Ihum -> (outside)  : mu_hum * Ihum
##   Rhum -> (outside)  : mu_hum * Rhum

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 50, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  r_R = 5,        # rat birth rate (1/yr)
  p_inh = 0.975,  # probability offspring of resistant rats inherit resistance
  K_R = 2500,     # rat carrying capacity (rats)
  d_R = 0.2,      # rat natural death (1/yr)
  bet_R = 4.7,    # rat infection rate per free flea (1/yr)
  a_s = 0.004,    # flea searching efficiency (1/rat)
  m_R = 20,       # rat infectious-period rate (≈18 d) (1/yr)
  g_R = 0.02,     # probability an infected rat survives and becomes resistant
  r_F = 20,       # flea growth rate on rats (1/yr)
  K_F = 6.57,     # flea carrying capacity per rat (fleas/rat)
  d_F = 10,       # death rate of free fleas (1/yr)
  bet_H = 0.01,   # value taken from the notes text [recalled]
  mu_hum = 0.02,  # value taken from the notes text [recalled]
  m_H = 26,       # value taken from the notes text [recalled]
  g_H = 0.4       # value taken from the notes text [recalled]
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Srat = 2499,   # susceptible rats
  Irat = 1,      # infected rats
  Rrat = 0,      # resistant rats
  Nf = 6.57,     # mean fleas per rat (flea index; not a head count)
  Ff = 0,        # free infectious fleas (searching)
  Shum = 10000,  # susceptible humans
  Ihum = 0,      # infected humans
  Rhum = 0       # recovered humans
)

# ---- Model equations -------------------------------------------------
plague_keeling_gilligan2000_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Shum + Ihum + Rhum

    # Derived quantities
    Nrat <- Srat + Irat + Rrat
    Nhum <- Shum + Ihum + Rhum
    lam_rat <- bet_R * Ff * (1 - exp(-a_s * Nrat)) / Nrat  # rat force of infection: fleas that find rats infect them
    fleas_to_humans <- Ff * exp(-a_s * Nrat)               # free fleas that fail to find a rat and land on humans (K&G's human force of infection)
    lam_hum <- bet_H * fleas_to_humans / Nhum              # human force of infection

    # Rates of change
    dSrat <- r_R * Srat + r_R * (1 - p_inh) * Rrat - (d_R + r_R * Nrat / K_R) * Srat - lam_rat * Srat
    dIrat <- lam_rat * Srat - m_R * g_R * Irat - m_R * (1 - g_R) * Irat - d_R * Irat
    dRrat <- r_R * p_inh * Rrat - (d_R + r_R * Nrat / K_R) * Rrat + m_R * g_R * Irat
    dNf <- r_F * Nf - r_F * Nf / K_F * Nf
    dFf <- (d_R + m_R * (1 - g_R)) * Irat * Nf - d_F * Ff
    dShum <- mu_hum * Nhum - lam_hum * Shum - mu_hum * Shum
    dIhum <- lam_hum * Shum - m_H * g_H * Ihum - m_H * (1 - g_H) * Ihum - mu_hum * Ihum
    dRhum <- m_H * g_H * Ihum - mu_hum * Rhum

    list(c(dSrat, dIrat, dRrat, dNf, dFf, dShum, dIhum, dRhum))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = plague_keeling_gilligan2000_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Srat = "#333333", Irat = "#B71C1C", Rrat = "#4E8A3E", Nf = "#333333", Ff = "#5D4037", Shum = "#333333", Ihum = "#B71C1C", Rhum = "#4E8A3E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Bubonic plague: rats (SIR with inherited resistance), flea index, free infectious fleas, human SIR (Keeling & Gilligan 2000) [STRUCTURE-FROM-RECALL]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

African trypanosomiasis: humans + cattle reservoir + tsetse with two-stage incubation (after Rogers 1988) [ILLUSTRATIVE-COMPOSITE]

Vector-borne
ShEhIhRhScEcIcRcSfEf1Ef2If

Rhodesian sleeping sickness / animal trypanosomiasis (T. brucei rhodesiense, Glossina spp.), East Africa. Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.

Rogers DJ (1988). A general model for the African trypanosomiases. Parasitology 97(1):193–212. https://doi.org/10.1017/S0031182000066853 . It is a multi-host, single-vector Ross–Macdonald generalisation with host-specific feeding fractions and… ↗

Show R code
## African trypanosomiasis: humans + cattle reservoir + tsetse with two-stage incubation (after Rogers 1988) [ILLUSTRATIVE-COMPOSITE]
## Rhodesian sleeping sickness / animal trypanosomiasis (T. brucei rhodesiense, Glossina spp.), East Africa.
## Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.
## Reference: Rogers DJ (1988). A general model for the African trypanosomiases. Parasitology 97(1):193–212. https://doi.org/10.1017/S0031182000066853 . It is a multi-host, single-vector Ross–Macdonald generalisation with host-specific feeding fractions and…
## Source: https://doi.org/10.1017/S0031182000066853
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sh, Eh, Ih, Rh, Sc, Ec, Ic, Rc, Sf, Ef1, Ef2, If
## Flows (time unit: days):
##   (outside) -> Sh   : mu_h * Nh
##   Sh -> Eh          : lam_h * Sh
##   Eh -> Ih          : nu_h * Eh
##   Ih -> Rh          : gam_h * Ih
##   Ih -> (outside)   : d_h * Ih
##   Rh -> Sh          : rho_h * Rh
##   Sh -> (outside)   : mu_h * Sh
##   Eh -> (outside)   : mu_h * Eh
##   Ih -> (outside)   : mu_h * Ih
##   Rh -> (outside)   : mu_h * Rh
##   (outside) -> Sc   : mu_c * Nc
##   Sc -> Ec          : lam_c * Sc
##   Ec -> Ic          : nu_c * Ec
##   Ic -> Rc          : gam_c * Ic
##   Rc -> Sc          : rho_c * Rc
##   Sc -> (outside)   : mu_c * Sc
##   Ec -> (outside)   : mu_c * Ec
##   Ic -> (outside)   : mu_c * Ic
##   Rc -> (outside)   : mu_c * Rc
##   (outside) -> Sf   : mu_f * Nf
##   Sf -> Ef1         : lam_f * Sf
##   Ef1 -> Ef2        : 2 / eip * Ef1
##   Ef2 -> If         : 2 / eip * Ef2
##   Sf -> (outside)   : mu_f * Sf
##   Ef1 -> (outside)  : mu_f * Ef1
##   Ef2 -> (outside)  : mu_f * Ef2
##   If -> (outside)   : mu_f * If

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 7300, by = 10)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  a_f = 0.25,      # feeding rate (every 4 d) (1/day)
  f_hum = 0.1,     # fraction of meals on humans; the rest on cattle
  b_fh = 0.6,      # fly → human per-bite probability
  b_fc = 0.6,      # fly → cattle per-bite probability
  p_hf = 0.05,     # human → fly per-meal probability (low because mainly teneral flies are susceptible)
  p_cf = 0.05,     # cattle → fly per-meal probability
  mu_f = 0.03,     # tsetse mortality (1/day)
  eip = 25,        # EIP of T. brucei (two Erlang stages at rate 2/eip each) (days)
  nu_h = 0.0714,   # 1/14 d human incubation (1/day)
  gam_h = 0.0083,  # 1/120 d to treatment/recovery (1/day)
  d_h = 0.0017,    # disease death (1/day)
  rho_h = 0.0167,  # loss of protection after treatment (1/day)
  mu_h = 4.57e-5,  # human birth/death (1/day)
  nu_c = 0.1,      # cattle incubation (1/day)
  gam_c = 0.01,    # cattle recovery (100 d infectious) (1/day)
  rho_c = 0.02,    # cattle waning (1/day)
  mu_c = 3.65e-4   # cattle birth/death (7.5 yr) (1/day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sh = 995,   # susceptible humans
  Eh = 0,     # incubating humans
  Ih = 5,     # infectious humans
  Rh = 0,     # treated/recovered (temporary)
  Sc = 490,   # susceptible cattle
  Ec = 0,     # incubating cattle
  Ic = 10,    # infectious cattle
  Rc = 0,     # recovered cattle
  Sf = 5000,  # susceptible tsetse
  Ef1 = 0,    # tsetse in EIP stage 1
  Ef2 = 0,    # tsetse in EIP stage 2
  If = 0      # infectious tsetse
)

# ---- Model equations -------------------------------------------------
trypanosomiasis_rogers1988_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Sh + Eh + Ih + Rh

    # Derived quantities
    Nh <- Sh + Eh + Ih + Rh
    Nc <- Sc + Ec + Ic + Rc
    Nf <- Sf + Ef1 + Ef2 + If
    lam_h <- a_f * f_hum * b_fh * If / Nh                                   # human force of infection
    lam_c <- a_f * (1 - f_hum) * b_fc * If / Nc                             # cattle force of infection
    lam_f <- a_f * (f_hum * p_hf * Ih / Nh + (1 - f_hum) * p_cf * Ic / Nc)  # tsetse force of infection

    # Rates of change
    dSh <- mu_h * Nh - lam_h * Sh + rho_h * Rh - mu_h * Sh
    dEh <- lam_h * Sh - nu_h * Eh - mu_h * Eh
    dIh <- nu_h * Eh - gam_h * Ih - d_h * Ih - mu_h * Ih
    dRh <- gam_h * Ih - rho_h * Rh - mu_h * Rh
    dSc <- mu_c * Nc - lam_c * Sc + rho_c * Rc - mu_c * Sc
    dEc <- lam_c * Sc - nu_c * Ec - mu_c * Ec
    dIc <- nu_c * Ec - gam_c * Ic - mu_c * Ic
    dRc <- gam_c * Ic - rho_c * Rc - mu_c * Rc
    dSf <- mu_f * Nf - lam_f * Sf - mu_f * Sf
    dEf1 <- lam_f * Sf - 2 / eip * Ef1 - mu_f * Ef1
    dEf2 <- 2 / eip * Ef1 - 2 / eip * Ef2 - mu_f * Ef2
    dIf <- 2 / eip * Ef2 - mu_f * If

    list(c(dSh, dEh, dIh, dRh, dSc, dEc, dIc, dRc, dSf, dEf1, dEf2, dIf))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = trypanosomiasis_rogers1988_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sh = "#333333", Eh = "#E8C468", Ih = "#B71C1C", Rh = "#4E8A3E", Sc = "#333333", Ec = "#E8C468", Ic = "#B71C1C", Rc = "#4E8A3E", Sf = "#333333", Ef1 = "#E8C468", Ef2 = "#E8C468", If = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "African trypanosomiasis: humans + cattle reservoir + tsetse with two-stage incubation (after Rogers 1988) [ILLUSTRATIVE-COMPOSITE]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Bluetongue: cattle + sheep + Culicoides with temperature-dependent biting, mortality and EIP (after Gubbins et al. 2008) [STRUCTURE-FROM-RECALL]

Vector-borne
ScowIcowRcowSshIshRshSvEv1Ev2Ev3Iv

Bluetongue virus (BTV-8), northern Europe / UK. Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.

Gubbins S, Carpenter S, Baylis M, Wood JLN, Mellor PS (2008). Assessing the risk of bluetongue to UK livestock: uncertainty and sensitivity analyses of a temperature-dependent model for the basic reproduction number. J R Soc Interface 5(20):363–371.… ↗

Show R code
## Bluetongue: cattle + sheep + Culicoides with temperature-dependent biting, mortality and EIP (after Gubbins et al. 2008) [STRUCTURE-FROM-RECALL]
## Bluetongue virus (BTV-8), northern Europe / UK.
## Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.
## Reference: Gubbins S, Carpenter S, Baylis M, Wood JLN, Mellor PS (2008). Assessing the risk of bluetongue to UK livestock: uncertainty and sensitivity analyses of a temperature-dependent model for the basic reproduction number. J R Soc Interface 5(20):363–371.…
## Source: https://doi.org/10.1098/rsif.2007.1110
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Scow, Icow, Rcow, Ssh, Ish, Rsh, Sv, Ev1, Ev2, Ev3, Iv
## Flows (time unit: days):
##   Scow -> Icow      : lam_cow * Scow
##   Icow -> Rcow      : r_cow * Icow
##   Ssh -> Ish        : lam_sh * Ssh
##   Ish -> Rsh        : r_sh * Ish
##   Ish -> (outside)  : d_sh * Ish
##   (outside) -> Sv   : mu_v * Nv
##   Sv -> Ev1         : lam_v * Sv
##   Ev1 -> Ev2        : 3 * nu_eip * Ev1
##   Ev2 -> Ev3        : 3 * nu_eip * Ev2
##   Ev3 -> Iv         : 3 * nu_eip * Ev3
##   Sv -> (outside)   : mu_v * Sv
##   Ev1 -> (outside)  : mu_v * Ev1
##   Ev2 -> (outside)  : mu_v * Ev2
##   Ev3 -> (outside)  : mu_v * Ev3
##   Iv -> (outside)   : mu_v * Iv

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  T_mean = 15,     # illustrative (°C)
  T_amp = 7,       # illustrative (°C)
  t_shift = 110,   # sets the temperature peak in mid-July (day)
  b_vh = 0.9,      # vector → host per-bite probability
  p_hv = 0.05,     # host → vector per-bite probability (recalled range 0.01–0.15)
  phi_c = 0.75,    # biting preference weight for cattle (illustrative)
  r_cow = 0.0485,  # 1/20.6 d cattle viraemia (recalled) (1/day)
  r_sh = 0.0625,   # 1/16 d sheep viraemia (recalled) (1/day)
  d_sh = 0.006,    # BTV-induced sheep death (illustrative) (1/day)
  k_eip = 3        # EIP stages (fixed in the flows)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Scow = 199,   # susceptible cattle
  Icow = 1,     # infected cattle
  Rcow = 0,     # recovered cattle
  Ssh = 800,    # susceptible sheep
  Ish = 0,      # infected sheep
  Rsh = 0,      # recovered sheep
  Sv = 100000,  # susceptible Culicoides
  Ev1 = 0,      # EIP stage 1
  Ev2 = 0,      # EIP stage 2
  Ev3 = 0,      # EIP stage 3
  Iv = 0        # infectious midges
)

# ---- Model equations -------------------------------------------------
bluetongue_gubbins2008_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Scow + Icow + Rcow + Ssh + Ish + Rsh

    # Derived quantities
    Temp <- T_mean + T_amp * sin(2 * 3.14159265 * (t - t_shift) / 365)                # daily temperature (°C)
    a_bite <- pmax(0.0002 * Temp * (Temp - 3.7) * pmax(41.9 - Temp, 0)^(1 / 2.7), 0)  # biting rate (1/day)
    mu_v <- 0.009 * exp(0.16 * Temp)                                                  # midge mortality (1/day)
    nu_eip <- pmax(0.018 * (Temp - 13.4), 0)                                          # EIP completion rate (1/day)
    Ncow <- Scow + Icow + Rcow
    Nsh <- Ssh + Ish + Rsh
    Nv <- Sv + Ev1 + Ev2 + Ev3 + Iv
    Hw <- phi_c * Ncow + (1 - phi_c) * Nsh                                            # preference-weighted host total
    lam_cow <- a_bite * b_vh * phi_c * Iv / Hw                                        # cattle force of infection
    lam_sh <- a_bite * b_vh * (1 - phi_c) * Iv / Hw                                   # sheep force of infection
    lam_v <- a_bite * p_hv * (phi_c * Icow + (1 - phi_c) * Ish) / Hw                  # midge force of infection

    # Rates of change
    dScow <- -lam_cow * Scow
    dIcow <- lam_cow * Scow - r_cow * Icow
    dRcow <- r_cow * Icow
    dSsh <- -lam_sh * Ssh
    dIsh <- lam_sh * Ssh - r_sh * Ish - d_sh * Ish
    dRsh <- r_sh * Ish
    dSv <- mu_v * Nv - lam_v * Sv - mu_v * Sv
    dEv1 <- lam_v * Sv - 3 * nu_eip * Ev1 - mu_v * Ev1
    dEv2 <- 3 * nu_eip * Ev1 - 3 * nu_eip * Ev2 - mu_v * Ev2
    dEv3 <- 3 * nu_eip * Ev2 - 3 * nu_eip * Ev3 - mu_v * Ev3
    dIv <- 3 * nu_eip * Ev3 - mu_v * Iv

    list(c(dScow, dIcow, dRcow, dSsh, dIsh, dRsh, dSv, dEv1, dEv2, dEv3, dIv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = bluetongue_gubbins2008_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Scow = "#333333", Icow = "#B71C1C", Rcow = "#4E8A3E", Ssh = "#333333", Ish = "#B71C1C", Rsh = "#4E8A3E", Sv = "#333333", Ev1 = "#E8C468", Ev2 = "#E8C468", Ev3 = "#E8C468", Iv = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Bluetongue: cattle + sheep + Culicoides with temperature-dependent biting, mortality and EIP (after Gubbins et al. 2008) [STRUCTURE-FROM-RECALL]")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Ebola SEIR with intervention-time switch (Congo 1995 / Uganda 2000)

Zoonoses & emerging
SEIRD

Ebola virus disease, human. The single structural feature that distinguishes this model is the step-then-exponential-decay transmission rate, implemented with ifelse on t. Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.

Chowell G, Hengartner NW, Castillo-Chavez C, Fenimore PW, Hyman JM (2004). The basic reproductive number of Ebola and the effects of public health measures: the cases of Congo and Uganda. J Theor Biol 229(1):119–126 ↗

Show R code
## Ebola SEIR with intervention-time switch (Congo 1995 / Uganda 2000)
## Ebola virus disease, human.
## The single structural feature that distinguishes this model is the step-then-exponential-decay transmission rate, implemented with ifelse on t.
## Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.
## Reference: Chowell G, Hengartner NW, Castillo-Chavez C, Fenimore PW, Hyman JM (2004). The basic reproductive number of Ebola and the effects of public health measures: the cases of Congo and Uganda. J Theor Biol 229(1):119–126
## Source: https://doi.org/10.1016/j.jtbi.2004.03.006
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, R, D
## Flows (time unit: days):
##   S -> E  : lambda * S
##   E -> I  : k * E
##   I -> D  : gamma * cfr * I
##   I -> R  : gamma * (1 - cfr) * I

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 250, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta0 = 0.33,    # transmission rate before interventions (Congo 1995; 0.30 commonly quoted for Uganda 2000) [unverified] (per day)
  q = 0.98,        # exponential decay rate of transmission after interventions begin [unverified] (per day)
  tau = 130,       # day interventions begin (relative to epidemic start used in the fit) [unverified] (days)
  k = 0.1887,      # rate of leaving latency (mean incubation 5.3 d) [unverified] (per day)
  gamma = 0.1783,  # rate of leaving infectiousness (mean infectious period 5.61 d) [unverified] (per day)
  cfr = 0.67       # proportion of infectious cases that die (Congo 1995; ≈0.53 Uganda 2000) [unverified] (dimensionless)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 5364500,  # susceptible
  E = 0,        # infected, not yet infectious (latent)
  I = 1,        # infectious (symptomatic, in community)
  R = 0,        # recovered, immune
  D = 0         # died of disease (absorbing; cumulative deaths)
)

# ---- Model equations -------------------------------------------------
ebola_chowell_2004_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + E + I + R

    # Derived quantities
    N <- S + E + I + R                                             # living population
    beta_t <- ifelse(t < tau, beta0, beta0 * exp(-q * (t - tau)))  # time-varying transmission rate
    lambda <- beta_t * I / N                                       # force of infection
    R0 <- beta0 / gamma                                            # basic reproduction number (authors report ≈1.83 Congo 1995, ≈1.34 Uganda 2000) [unverified]
    CC_rate <- (k) * E                                             # rate of new cumulative cases (bookkeeping for fitting to cumulative case curves) (integrate with cumsum for the cumulative count)

    # Rates of change
    dS <- -lambda * S
    dE <- lambda * S - k * E
    dI <- k * E - gamma * cfr * I - gamma * (1 - cfr) * I
    dR <- gamma * (1 - cfr) * I
    dD <- gamma * cfr * I

    list(c(dS, dE, dI, dR, dD), c(CC_rate = CC_rate))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = ebola_chowell_2004_model, parms = parameters, hmax = 0.5)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", R = "#4E8A3E", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Ebola SEIR with intervention-time switch (Congo 1995 / Uganda 2000)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("CC_rate")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "Ebola SEIR with intervention-time switch (Congo 1995 / Uganda 2000): outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Ebola West Africa 2014 with case isolation and vaccination (Legrand extension)

Zoonoses & emerging
SEIHFnRV

Ebola virus disease, human, Liberia/Sierra Leone 2014. Overlaps substantially with the already-implemented Legrand 2007 model — the compartment skeleton (S,E,I,H,F,R) is the same; the additions are the V compartment with a vaccination lever and the 2014 West-African parameterisation. Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.

Rivers CM, Lofgren ET, Marathe M, Eubank S, Lewis BL (2014). Modeling the impact of interventions on an epidemic of Ebola in Sierra Leone and Liberia. PLOS Currents Outbreaks, Oct 16 ed ↗

Show R code
## Ebola West Africa 2014 with case isolation and vaccination (Legrand extension)
## Ebola virus disease, human, Liberia/Sierra Leone 2014.
## Overlaps substantially with the already-implemented Legrand 2007 model — the compartment skeleton (S,E,I,H,F,R) is the same; the additions are the V compartment with a vaccination lever and the 2014 West-African parameterisation.
## Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.
## Reference: Rivers CM, Lofgren ET, Marathe M, Eubank S, Lewis BL (2014). Modeling the impact of interventions on an epidemic of Ebola in Sierra Leone and Liberia. PLOS Currents Outbreaks, Oct 16 ed
## Source: https://doi.org/10.1371/currents.outbreaks.fd38dd85078565450b0be3fcd78f5ccf
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, I, H, Fn, R, V
## Flows (time unit: days):
##   S -> E   : lambda * S
##   S -> V   : nu * eff * S
##   E -> I   : alpha * E
##   I -> H   : gamma_h * theta * I
##   I -> Fn  : gamma_d * cfr * (1 - theta) * I
##   I -> R   : gamma_i * (1 - cfr) * (1 - theta) * I
##   H -> Fn  : gamma_dh * cfr * H
##   H -> R   : gamma_dh * (1 - cfr) * H
##   Fn -> R  : gamma_f * Fn

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_i = 0.16,       # community transmission rate [unverified] (per day)
  beta_h = 0.062,      # transmission rate from hospitalised cases (reflects imperfect infection control) [unverified] (per day)
  beta_f = 0.489,      # transmission rate at funerals/from unburied bodies [unverified] (per day)
  alpha = 1 / 11,      # rate of leaving latency (mean incubation ≈11 d) [unverified] (per day)
  theta = 0.248,       # proportion of cases hospitalised/isolated [unverified] (dimensionless)
  gamma_h = 1 / 4.12,  # rate community case → hospital [unverified] (per day)
  gamma_i = 1 / 10,    # rate of leaving community infectiousness [unverified] (per day)
  gamma_d = 1 / 8,     # rate infectious → death (community) [unverified] (per day)
  gamma_dh = 1 / 6,    # rate hospitalised → death [unverified] (per day)
  gamma_f = 1 / 2.01,  # rate of burial (removal of Fn) [unverified] (per day)
  cfr = 0.7,           # case fatality proportion [unverified] (dimensionless)
  nu = 0,              # per-capita vaccination rate (intervention lever; set >0 to simulate a campaign) [illustrative] (per day)
  eff = 0.9            # vaccine efficacy (take) [illustrative] (dimensionless)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 1e6,  # susceptible
  E = 0,    # latent (infected, not infectious)
  I = 10,   # infectious in the community
  H = 0,    # hospitalised / isolated infectious
  Fn = 0,   # deceased, not yet buried (funeral-transmission compartment)
  R = 0,    # removed (recovered or safely buried)
  V = 0     # vaccinated, immune
)

# ---- Model equations -------------------------------------------------
ebola_rivers_2014_intervention_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + E + I + H + Fn + R + V

    # Derived quantities
    N <- S + E + I + H + R + V                             # living population
    lambda <- (beta_i * I + beta_h * H + beta_f * Fn) / N  # total force of infection

    # Rates of change
    dS <- -lambda * S - nu * eff * S
    dE <- lambda * S - alpha * E
    dI <- alpha * E - gamma_h * theta * I - gamma_d * cfr * (1 - theta) * I - gamma_i * (1 - cfr) * (1 - theta) * I
    dH <- gamma_h * theta * I - gamma_dh * cfr * H - gamma_dh * (1 - cfr) * H
    dFn <- gamma_d * cfr * (1 - theta) * I + gamma_dh * cfr * H - gamma_f * Fn
    dR <- gamma_i * (1 - cfr) * (1 - theta) * I + gamma_dh * (1 - cfr) * H + gamma_f * Fn
    dV <- nu * eff * S

    list(c(dS, dE, dI, dH, dFn, dR, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = ebola_rivers_2014_intervention_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", I = "#B71C1C", H = "#6A2C9C", Fn = "#5D4037", R = "#4E8A3E", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Ebola West Africa 2014 with case isolation and vaccination (Legrand extension)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Nipah virus bat → pig → human spillover with nosocomial transmission

Zoonoses & emerging
SbIbRbSpEpIpRpShEhIhHhRhDh

Nipah virus, Pteropus bats / pigs / humans. 13 compartments — at the structural limit, so the pig Rp and bat Rb classes can be dropped for a leaner variant. Parameter values: 0 of 16 checked against the source; the rest are recalled or illustrative.

Structure after Pulliam JRC, Epstein JH, Dushoff J, Rahman SA, Bunning M, Jamaluddin AA, Hyatt AD, Field HE, Dobson AP, Daszak P, Henipavirus Ecology Research Group (2012). Agricultural intensification, priming for persistence and the emergence of Nipah… ↗

Show R code
## Nipah virus bat → pig → human spillover with nosocomial transmission
## Nipah virus, Pteropus bats / pigs / humans.
## 13 compartments — at the structural limit, so the pig Rp and bat Rb classes can be dropped for a leaner variant.
## Parameter values: 0 of 16 checked against the source; the rest are recalled or illustrative.
## Reference: Structure after Pulliam JRC, Epstein JH, Dushoff J, Rahman SA, Bunning M, Jamaluddin AA, Hyatt AD, Field HE, Dobson AP, Daszak P, Henipavirus Ecology Research Group (2012). Agricultural intensification, priming for persistence and the emergence of Nipah…
## Source: https://doi.org/10.1098/rsif.2011.0223
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sb, Ib, Rb, Sp, Ep, Ip, Rp, Sh, Eh, Ih, Hh, Rh, Dh
## Flows (time unit: days):
##   (outside) -> Sb  : mu_b * Nb
##   Sb -> Ib         : lambda_b * Sb
##   Ib -> Rb         : gamma_b * Ib
##   Rb -> Sb         : wane_b * Rb
##   Sb -> (outside)  : mu_b * Sb
##   (outside) -> Sp  : mu_p * Np
##   Sp -> Ep         : lambda_p * Sp
##   Ep -> Ip         : sigma_p * Ep
##   Ip -> Rp         : gamma_p * Ip
##   Sp -> (outside)  : mu_p * Sp
##   Sh -> Eh         : lambda_h * Sh
##   Eh -> Ih         : sigma_h * Eh
##   Ih -> Hh         : eta_h * Ih
##   Ih -> Rh         : gamma_h * Ih
##   Hh -> Dh         : gamma_h * cfr_h * Hh
##   Hh -> Rh         : gamma_h * (1 - cfr_h) * Hh

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 730, by = 1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_bb = 0.02,    # bat-to-bat transmission rate [illustrative] (per day)
  gamma_b = 1 / 14,  # bat recovery rate [illustrative] (per day)
  mu_b = 1 / 2920,   # bat birth = death rate (8-year lifespan) [illustrative] (per day)
  wane_b = 1 / 365,  # loss of bat immunity (drives periodic reintroduction) [illustrative] (per day)
  beta_bp = 5e-5,    # bat-to-pig spillover coefficient (contaminated fruit/urine route) [illustrative] (per bat per day)
  beta_pp = 1.2,     # pig-to-pig transmission rate (highly transmissible in intensive piggeries) [illustrative] (per day)
  sigma_p = 1 / 7,   # pig latency rate [illustrative] (per day)
  gamma_p = 1 / 10,  # pig recovery rate [illustrative] (per day)
  mu_p = 1 / 180,    # pig turnover (birth = offtake), drives susceptible replenishment [illustrative] (per day)
  beta_ph = 1e-6,    # pig-to-human transmission coefficient (occupational contact) [illustrative] (per pig per day)
  beta_hh = 0.02,    # community human-to-human transmission rate [illustrative] (per day)
  beta_hosp = 0.15,  # nosocomial/caregiver transmission rate from Hh [illustrative] (per day)
  sigma_h = 1 / 9,   # human latency rate (incubation ≈9 d) [illustrative] (per day)
  eta_h = 1 / 4,     # rate community case → hospital [illustrative] (per day)
  gamma_h = 1 / 14,  # human removal rate [illustrative] (per day)
  cfr_h = 0.7        # human case fatality (Bangladesh outbreaks ≈70%) [unverified] (dimensionless)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sb = 9000,   # susceptible bats
  Ib = 100,    # infectious bats
  Rb = 900,    # immune bats
  Sp = 10000,  # susceptible pigs
  Ep = 0,      # latent pigs
  Ip = 0,      # infectious pigs
  Rp = 0,      # recovered/immune pigs
  Sh = 50000,  # susceptible humans
  Eh = 0,      # latent humans
  Ih = 0,      # infectious humans in the community
  Hh = 0,      # hospitalised humans (source of nosocomial transmission)
  Rh = 0,      # recovered humans
  Dh = 0       # human deaths (cumulative)
)

# ---- Model equations -------------------------------------------------
nipah_bat_pig_human_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- Sh + Eh + Ih + Hh + Rh

    # Derived quantities
    Nb <- Sb + Ib + Rb                                                # bat population
    Np <- Sp + Ep + Ip + Rp                                           # pig population
    N <- Sh + Eh + Ih + Hh + Rh                                       # living human population
    lambda_b <- beta_bb * Ib / Nb                                     # bat force of infection
    lambda_p <- beta_pp * Ip / Np + beta_bp * Ib                      # pig force of infection (within-herd frequency-dependent plus spillover)
    lambda_h <- beta_ph * Ip + beta_hh * Ih / N + beta_hosp * Hh / N  # human force of infection

    # Rates of change
    dSb <- mu_b * Nb - lambda_b * Sb + wane_b * Rb - mu_b * Sb
    dIb <- lambda_b * Sb - gamma_b * Ib
    dRb <- gamma_b * Ib - wane_b * Rb
    dSp <- mu_p * Np - lambda_p * Sp - mu_p * Sp
    dEp <- lambda_p * Sp - sigma_p * Ep
    dIp <- sigma_p * Ep - gamma_p * Ip
    dRp <- gamma_p * Ip
    dSh <- -lambda_h * Sh
    dEh <- lambda_h * Sh - sigma_h * Eh
    dIh <- sigma_h * Eh - eta_h * Ih - gamma_h * Ih
    dHh <- eta_h * Ih - gamma_h * cfr_h * Hh - gamma_h * (1 - cfr_h) * Hh
    dRh <- gamma_h * Ih + gamma_h * (1 - cfr_h) * Hh
    dDh <- gamma_h * cfr_h * Hh

    list(c(dSb, dIb, dRb, dSp, dEp, dIp, dRp, dSh, dEh, dIh, dHh, dRh, dDh))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = nipah_bat_pig_human_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sb = "#333333", Ib = "#B71C1C", Rb = "#4E8A3E", Sp = "#333333", Ep = "#E8C468", Ip = "#B71C1C", Rp = "#4E8A3E", Sh = "#333333", Eh = "#E8C468", Ih = "#B71C1C", Hh = "#6A2C9C", Rh = "#4E8A3E", Dh = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Nipah virus bat → pig → human spillover with nosocomial transmission")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

MERS-CoV with camel spillover and hospital amplification

Zoonoses & emerging
ScIcRcSEIiHhRD

Middle East respiratory syndrome coronavirus, dromedary camels → humans. The paper's central structural claim — repeated zoonotic index cases plus sub-critical human-to-human spread with hospital-based amplification — is captured by the two separate forces of infection and the two case accumulators, which let th Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.

Chowell G, Blumberg S, Simonsen L, Miller MA, Viboud C (2014). Synthesizing data and models for the spread of MERS-CoV, 2013: key role of index cases and hospital transmission. Epidemics 9:40–51 ↗

Show R code
## MERS-CoV with camel spillover and hospital amplification
## Middle East respiratory syndrome coronavirus, dromedary camels → humans.
## The paper's central structural claim — repeated zoonotic index cases plus sub-critical human-to-human spread with hospital-based amplification — is captured by the two separate forces of infection and the two case accumulators, which let th
## Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.
## Reference: Chowell G, Blumberg S, Simonsen L, Miller MA, Viboud C (2014). Synthesizing data and models for the spread of MERS-CoV, 2013: key role of index cases and hospital transmission. Epidemics 9:40–51
## Source: https://doi.org/10.1016/j.epidem.2014.09.011
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sc, Ic, Rc, S, E, Ii, Hh, R, D
## Flows (time unit: days):
##   (outside) -> Sc  : mu_c * Nc
##   Sc -> Ic         : lambda_c * Sc
##   Sc -> (outside)  : mu_c * Sc
##   S -> E           : (lambda_z + lambda_s) * S
##   E -> Ii          : sigma * E
##   Ii -> Hh         : eta * Ii
##   Ii -> R          : gamma_i * Ii
##   Hh -> D          : gamma_h * cfr * Hh
##   Hh -> R          : gamma_h * (1 - cfr) * Hh

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 400, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_cc = 0.25,    # camel-to-camel transmission rate [illustrative] (per day)
  gamma_c = 1 / 14,  # camel recovery rate [illustrative] (per day)
  mu_c = 1 / 1825,   # camel birth = death rate, replenishes susceptible calves [illustrative] (per day)
  wane_c = 1 / 730,  # loss of camel immunity [illustrative] (per day)
  beta_ch = 2e-9,    # camel-to-human spillover coefficient [illustrative] (per camel per day)
  beta_com = 0.04,   # human-to-human transmission rate in the community [unverified] (per day)
  beta_hosp = 0.55,  # human-to-human transmission rate in hospital [unverified] (per day)
  sigma = 1 / 5.2,   # latency rate (mean incubation ≈5.2 d) [unverified] (per day)
  eta = 1 / 5,       # rate community case → hospitalisation (mean ≈5 d onset-to-admission) [unverified] (per day)
  gamma_i = 1 / 10,  # community removal rate [unverified] (per day)
  gamma_h = 1 / 13,  # hospital removal rate [unverified] (per day)
  cfr = 0.4          # case fatality among hospitalised cases [unverified] (dimensionless)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sc = 9000,  # susceptible camels
  Ic = 1000,  # infectious camels (actively shedding, mostly juveniles)
  Rc = 0,     # immune camels
  S = 1e6,    # susceptible humans
  E = 0,      # latent humans
  Ii = 0,     # infectious humans in the community
  Hh = 0,     # hospitalised humans (amplification setting)
  R = 0,      # recovered humans
  D = 0       # human deaths (cumulative)
)

# ---- Model equations -------------------------------------------------
mers_camel_human_hospital_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + E + Ii + Hh + R

    # Derived quantities
    Nc <- Sc + Ic + Rc                                                                    # camel population
    N <- S + E + Ii + Hh + R                                                              # living human population
    lambda_c <- beta_cc * Ic / Nc                                                         # camel force of infection
    lambda_z <- beta_ch * Ic                                                              # zoonotic hazard to a susceptible human
    lambda_s <- beta_com * Ii / N + beta_hosp * Hh / N                                    # human-to-human force of infection
    Rhuman <- beta_com / (eta + gamma_i) + (eta / (eta + gamma_i)) * beta_hosp / gamma_h  # human-to-human reproduction number (authors' headline result: overall R below or near 1, ≈0.4–1.5 across published estimates, with hospital
    CZ_rate <- (lambda_z) * S                                                             # rate of new cumulative zoonotic (index) cases (integrate with cumsum for the cumulative count)
    CS_rate <- (lambda_s) * S                                                             # rate of new cumulative secondary (human-acquired) cases (integrate with cumsum for the cumulative count)

    # Rates of change
    dSc <- mu_c * Nc - lambda_c * Sc - mu_c * Sc
    dIc <- lambda_c * Sc
    dRc <- 0
    dS <- -(lambda_z + lambda_s) * S
    dE <- (lambda_z + lambda_s) * S - sigma * E
    dIi <- sigma * E - eta * Ii - gamma_i * Ii
    dHh <- eta * Ii - gamma_h * cfr * Hh - gamma_h * (1 - cfr) * Hh
    dR <- gamma_i * Ii + gamma_h * (1 - cfr) * Hh
    dD <- gamma_h * cfr * Hh

    list(c(dSc, dIc, dRc, dS, dE, dIi, dHh, dR, dD), c(CZ_rate = CZ_rate, CS_rate = CS_rate))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = mers_camel_human_hospital_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sc = "#333333", Ic = "#B71C1C", Rc = "#4E8A3E", S = "#333333", E = "#E8C468", Ii = "#B71C1C", Hh = "#6A2C9C", R = "#4E8A3E", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "MERS-CoV with camel spillover and hospital amplification")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("CZ_rate", "CS_rate")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (days)", ylab = "Value",
        main = "MERS-CoV with camel spillover and hospital amplification: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Avian influenza in poultry with human spillover and a mutant human-transmissible strain

Zoonoses & emerging
SaIaSIiImRD

Highly pathogenic avian influenza H5N1, poultry → humans. The paper's point is that Ii is a dead-end (no human-to-human spread) while Im is not, so two outbreak regimes are possible depending on whether R0m exceeds 1. Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.

Iwami S, Takeuchi Y, Liu X (2007). Avian–human influenza epidemic model. Mathematical Biosciences 207(1):1–25 ↗

Show R code
## Avian influenza in poultry with human spillover and a mutant human-transmissible strain
## Highly pathogenic avian influenza H5N1, poultry → humans.
## The paper's point is that Ii is a dead-end (no human-to-human spread) while Im is not, so two outbreak regimes are possible depending on whether R0m exceeds 1.
## Parameter values: 0 of 13 checked against the source; the rest are recalled or illustrative.
## Reference: Iwami S, Takeuchi Y, Liu X (2007). Avian–human influenza epidemic model. Mathematical Biosciences 207(1):1–25
## Source: https://doi.org/10.1016/j.mbs.2006.08.001
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sa, Ia, S, Ii, Im, R, D
## Flows (time unit: days):
##   (outside) -> Sa  : ca
##   Sa -> Ia         : lambda_a * Sa
##   Sa -> (outside)  : (mu_a + cull) * Sa
##   Ia -> (outside)  : (mu_a + delta_a + cull) * Ia
##   (outside) -> S   : ch
##   S -> Ii          : lambda_ah * S
##   S -> Im          : lambda_m * S
##   Ii -> Im         : nu * Ii
##   Ii -> (outside)  : (mu_h + delta_i) * Ii
##   Im -> R          : gamma_m * Im
##   Im -> D          : delta_m * Im
##   Im -> (outside)  : mu_h * Im
##   S -> (outside)   : mu_h * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 365, by = 0.5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  ca = 1000,         # recruitment of susceptible birds (poultry restocking); named ca because c is reserved [illustrative] (birds per day)
  mu_a = 1 / 500,    # natural bird death rate [illustrative] (per day)
  beta_a = 2e-5,     # bird-to-bird transmission coefficient (mass action) [illustrative] (per bird per day)
  delta_a = 0.4,     # disease-induced bird death rate (HPAI kills within days) [illustrative] (per day)
  cull = 0,          # additional culling rate applied to birds (policy lever from the 2008 companion paper) [illustrative] (per day)
  ch = 100,          # human recruitment [illustrative] (humans per day)
  mu_h = 1 / 25550,  # natural human death rate (70-year life expectancy) [illustrative] (per day)
  beta_ah = 1e-8,    # bird-to-human transmission coefficient [illustrative] (per bird per day)
  beta_m = 2e-7,     # mutant human-to-human transmission coefficient [illustrative] (per human per day)
  nu = 1e-4,         # rate at which an avian-strain human case yields mutant-strain infection (mutation) [illustrative] (per day)
  delta_i = 0.5,     # disease death rate, avian-strain human cases (H5N1 CFR ≈60%) [illustrative] (per day)
  delta_m = 0.1,     # disease death rate, mutant-strain cases [illustrative] (per day)
  gamma_m = 1 / 7    # recovery rate, mutant-strain cases [illustrative] (per day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sa = 1e5,  # susceptible birds
  Ia = 10,   # infected birds
  S = 1e6,   # susceptible humans
  Ii = 0,    # humans infected with the avian (non-transmissible) strain
  Im = 0,    # humans infected with the mutant human-transmissible strain
  R = 0,     # recovered humans
  D = 0      # cumulative human deaths
)

# ---- Model equations -------------------------------------------------
h5n1_poultry_human_iwami_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + Ii + Im + R

    # Derived quantities
    Na <- Sa + Ia                                             # bird population
    N <- S + Ii + Im + R                                      # living human population
    lambda_a <- beta_a * Ia                                   # bird force of infection (density-dependent)
    lambda_ah <- beta_ah * Ia                                 # spillover hazard to humans
    lambda_m <- beta_m * Im                                   # mutant-strain force of infection
    R0a <- beta_a * ca / ((mu_a + delta_a + cull) * mu_a)     # bird-strain reproduction number
    R0m <- beta_m * ch / ((mu_h + delta_m + gamma_m) * mu_h)  # mutant-strain reproduction number

    # Rates of change
    dSa <- ca - lambda_a * Sa - (mu_a + cull) * Sa
    dIa <- lambda_a * Sa - (mu_a + delta_a + cull) * Ia
    dS <- ch - lambda_ah * S - lambda_m * S - mu_h * S
    dIi <- lambda_ah * S - nu * Ii - (mu_h + delta_i) * Ii
    dIm <- lambda_m * S + nu * Ii - gamma_m * Im - delta_m * Im - mu_h * Im
    dR <- gamma_m * Im
    dD <- delta_m * Im

    list(c(dSa, dIa, dS, dIi, dIm, dR, dD))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = h5n1_poultry_human_iwami_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sa = "#333333", Ia = "#B71C1C", S = "#333333", Ii = "#B71C1C", Im = "#B71C1C", R = "#4E8A3E", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Avian influenza in poultry with human spillover and a mutant human-transmissible strain")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Mpox with a rodent reservoir and residual smallpox vaccination immunity

Zoonoses & emerging
SrErIrSVEIiRD

Mpox (monkeypox), rodent reservoir → humans. Two usable regimes from one structure: (a) the classical African setting — spillover-driven, R0h < 1, outbreaks that fade without the reservoir term; (b) a 2022-style outbreak — set beta_rh = 0, raise beta_hh above threshold, and read S as Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.

Bhunu CP, Mushayabasa S (2011). Modelling the transmission dynamics of pox-like infections. IAENG International Journal of Applied Mathematics 41(2):141–149. URL https://www.iaeng.org/IJAM/issues_v41/issue_2/IJAM_41_2_09.pdf . Historical human-to-human… ↗

Show R code
## Mpox with a rodent reservoir and residual smallpox vaccination immunity
## Mpox (monkeypox), rodent reservoir → humans.
## Two usable regimes from one structure: (a) the classical African setting — spillover-driven, R0h < 1, outbreaks that fade without the reservoir term; (b) a 2022-style outbreak — set beta_rh = 0, raise beta_hh above threshold, and read S as
## Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.
## Reference: Bhunu CP, Mushayabasa S (2011). Modelling the transmission dynamics of pox-like infections. IAENG International Journal of Applied Mathematics 41(2):141–149. URL https://www.iaeng.org/IJAM/issues_v41/issue_2/IJAM_41_2_09.pdf . Historical human-to-human…
## Source: https://doi.org/10.1093/oxfordjournals.aje.a114747
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sr, Er, Ir, S, V, E, Ii, R, D
## Flows (time unit: days):
##   (outside) -> Sr  : br * Nr
##   Sr -> Er         : lambda_r * Sr
##   Sr -> (outside)  : mu_r * Sr
##   (outside) -> S   : bh
##   S -> E           : lambda_h * S
##   S -> V           : nu * S
##   V -> E           : lambda_h * (1 - eps_v) * V
##   V -> S           : wane_v * V
##   E -> Ii          : sigma_h * E
##   Ii -> R          : gamma_h * (1 - cfr) * Ii
##   Ii -> D          : gamma_h * cfr * Ii
##   S -> (outside)   : mu_h * S

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 1825, by = 2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  br = 0.004,         # rodent birth rate [illustrative] (per day)
  mu_r = 0.004,       # rodent natural death rate [illustrative] (per day)
  beta_rr = 0.15,     # rodent-to-rodent transmission rate [illustrative] (per day)
  sigma_r = 1 / 7,    # rodent latency rate [illustrative] (per day)
  gamma_r = 1 / 21,   # rodent recovery/removal rate [illustrative] (per day)
  beta_rh = 2e-6,     # rodent-to-human spillover coefficient (hunting/bushmeat) [illustrative] (per rodent per day)
  beta_hh = 0.035,    # human-to-human transmission rate [illustrative] (per day)
  sigma_h = 1 / 8.5,  # human latency rate (incubation ≈8.5 d) [illustrative] (per day)
  gamma_h = 1 / 21,   # human removal rate (rash duration 2–4 weeks) [illustrative] (per day)
  cfr = 0.035,        # human case fatality (clade I historical ≈1–10%) [illustrative] (dimensionless)
  nu = 0,             # vaccination rate of susceptibles (ring/targeted campaign lever) [illustrative] (per day)
  eps_v = 0.85,       # vaccine protection against infection (smallpox vaccine cross-protection ≈85%) [unverified] (dimensionless)
  wane_v = 1 / 7300,  # waning of vaccine protection (20-year mean) [illustrative] (per day)
  mu_h = 1 / 21900,   # human natural death rate [illustrative] (per day)
  bh = 5              # human recruitment [illustrative] (humans per day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sr = 10000,  # susceptible rodents
  Er = 0,      # latent rodents
  Ir = 100,    # infectious rodents
  S = 1e5,     # susceptible humans
  V = 2e4,     # humans with vaccine-derived (smallpox) protection
  E = 0,       # latent humans
  Ii = 0,      # infectious humans
  R = 0,       # recovered humans
  D = 0        # cumulative human mpox deaths
)

# ---- Model equations -------------------------------------------------
mpox_human_animal_reservoir_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + V + E + Ii + R

    # Derived quantities
    Nr <- Sr + Er + Ir                           # rodent population
    N <- S + V + E + Ii + R                      # living human population
    lambda_r <- beta_rr * Ir / Nr                # rodent force of infection
    lambda_h <- beta_rh * Ir + beta_hh * Ii / N  # human force of infection
    R0h <- beta_hh / (gamma_h + mu_h)            # human-to-human reproduction number (historically ≈0.3; a 2022-type MSM-outbreak scenario is produced by raising beta_hh so that R0h > 1)

    # Rates of change
    dSr <- br * Nr - lambda_r * Sr - mu_r * Sr
    dEr <- lambda_r * Sr
    dIr <- 0
    dS <- bh - lambda_h * S - nu * S + wane_v * V - mu_h * S
    dV <- nu * S - lambda_h * (1 - eps_v) * V - wane_v * V
    dE <- lambda_h * S + lambda_h * (1 - eps_v) * V - sigma_h * E
    dIi <- sigma_h * E - gamma_h * (1 - cfr) * Ii - gamma_h * cfr * Ii
    dR <- gamma_h * (1 - cfr) * Ii
    dD <- gamma_h * cfr * Ii

    list(c(dSr, dEr, dIr, dS, dV, dE, dIi, dR, dD))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = mpox_human_animal_reservoir_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sr = "#333333", Er = "#E8C468", Ir = "#B71C1C", S = "#333333", V = "#2457C5", E = "#E8C468", Ii = "#B71C1C", R = "#4E8A3E", D = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Mpox with a rodent reservoir and residual smallpox vaccination immunity")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Smallpox outbreak response: traced (ring) versus mass vaccination

Zoonoses & emerging
SEIiQVRDCV

Smallpox, human; emergency public-health vaccination response. This is a public-health response-logistics model: the policy question it answers is whether contact-traced vaccination or population-wide vaccination averts more deaths given a finite daily vaccination capacity. Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

Kaplan EH, Craft DL, Wein LM (2002). Emergency response to a smallpox attack: the case for mass vaccination. PNAS 99(16):10935–10940 ↗

Show R code
## Smallpox outbreak response: traced (ring) versus mass vaccination
## Smallpox, human; emergency public-health vaccination response.
## This is a public-health response-logistics model: the policy question it answers is whether contact-traced vaccination or population-wide vaccination averts more deaths given a finite daily vaccination capacity.
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Kaplan EH, Craft DL, Wein LM (2002). Emergency response to a smallpox attack: the case for mass vaccination. PNAS 99(16):10935–10940
## Source: https://doi.org/10.1073/pnas.162282799
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, Ii, Q, V, R, D, CV
## Flows (time unit: days):
##   S -> E           : lambda * S
##   S -> V           : rate_v * eps_pre * S
##   E -> V           : rate_v * eps_post * E
##   E -> Ii          : sigma * E
##   Ii -> Q          : theta * Ii
##   Q -> D           : gamma * cfr * Q
##   Q -> R           : gamma * (1 - cfr) * Q
##   (outside) -> CV  : pmin(want, cap)

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 180, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 0.105,    # transmission rate (gives roughly 3 secondary cases per infectious person over the symptomatic period) [illustrative] (per day)
  sigma = 1 / 14,  # rate of leaving latency (mean latent period ≈14 d) [unverified] (per day)
  theta = 1 / 3,   # rate at which symptomatic cases are detected and isolated (≈3 d from rash to isolation) [illustrative] (per day)
  gamma = 1 / 14,  # rate of resolution (recovery or death) in isolation [illustrative] (per day)
  cfr = 0.3,       # case fatality of ordinary variola major [unverified] (dimensionless)
  eps_pre = 0.95,  # efficacy of vaccination of a susceptible [illustrative] (dimensionless)
  eps_post = 0.5,  # efficacy of post-exposure vaccination of a latent individual (protection declines sharply after the first few days) [illustrative] (dimensionless)
  tau_v = 7,       # delay before the vaccination campaign starts [illustrative] (days)
  nu_ring = 4,     # traced-vaccination intensity [illustrative] (contacts traced and vaccinated per isolated case per day)
  nu_mass = 0,     # per-capita mass-vaccination rate once the campaign is running [illustrative] (per day)
  cap = 2e5        # maximum daily vaccination throughput [illustrative] (doses per day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 1e7,  # susceptible, not yet vaccinated
  E = 0,    # infected, latent (not infectious, not yet symptomatic)
  Ii = 10,  # infectious, in the community
  Q = 0,    # isolated/removed from circulation (hospitalised or quarantined)
  V = 0,    # vaccinated and protected
  R = 0,    # recovered, immune
  D = 0,    # cumulative deaths
  CV = 0    # cumulative vaccine doses delivered
)

# ---- Model equations -------------------------------------------------
smallpox_emergency_vaccination_response_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + E + Ii + Q + V + R

    # Derived quantities
    N <- S + E + Ii + Q + V + R                                  # living population
    active <- ifelse(t < tau_v, 0, 1)                            # campaign switch
    want <- active * (nu_mass * (S + E) + nu_ring * theta * Ii)  # doses demanded per day
    rate_v <- pmin(want, cap) / pmax(S + E, 1)                   # per-capita vaccination rate, capped by logistic throughput
    lambda <- beta * Ii / N                                      # force of infection

    # Rates of change
    dS <- -lambda * S - rate_v * eps_pre * S
    dE <- lambda * S - rate_v * eps_post * E - sigma * E
    dIi <- sigma * E - theta * Ii
    dQ <- theta * Ii - gamma * cfr * Q - gamma * (1 - cfr) * Q
    dV <- rate_v * eps_pre * S + rate_v * eps_post * E
    dR <- gamma * (1 - cfr) * Q
    dD <- gamma * cfr * Q
    dCV <- pmin(want, cap)

    list(c(dS, dE, dIi, dQ, dV, dR, dD, dCV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = smallpox_emergency_vaccination_response_model, parms = parameters, hmax = 0.2)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", Ii = "#B71C1C", Q = "#00838F", V = "#2457C5", R = "#4E8A3E", D = "#111111", CV = "#8E0E0E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Smallpox outbreak response: traced (ring) versus mass vaccination")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Brucellosis in small ruminants with an environmental reservoir and human cases

Zoonoses & emerging
SsEsIsVsWShEhIahIch

Brucellosis, sheep/goats → humans, with Brucella in the environment. The structural signature of this family is the additive direct + indirect force of infection with an explicit environmental bacterial compartment W, and the result that vaccination and disinfection of adults alone cannot eradicate because y Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.

Hou Q, Sun X, Zhang J, Liu Y, Wang Y, Jin Z (2013). Modeling the transmission dynamics of sheep brucellosis in Inner Mongolia Autonomous Region, China. Mathematical Biosciences 242(1):51–58 ↗

Show R code
## Brucellosis in small ruminants with an environmental reservoir and human cases
## Brucellosis, sheep/goats → humans, with Brucella in the environment.
## The structural signature of this family is the additive direct + indirect force of infection with an explicit environmental bacterial compartment W, and the result that vaccination and disinfection of adults alone cannot eradicate because y
## Parameter values: 0 of 17 checked against the source; the rest are recalled or illustrative.
## Reference: Hou Q, Sun X, Zhang J, Liu Y, Wang Y, Jin Z (2013). Modeling the transmission dynamics of sheep brucellosis in Inner Mongolia Autonomous Region, China. Mathematical Biosciences 242(1):51–58
## Source: https://doi.org/10.1016/j.mbs.2012.11.012
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Ss, Es, Is, Vs, W, Sh, Eh, Iah, Ich
## Flows (time unit: years):
##   (outside) -> Ss  : As
##   Ss -> Es         : lambda_s * Ss
##   Ss -> Vs         : nu_s * Ss
##   Vs -> Ss         : wane_s * Vs
##   Es -> Is         : sigma_s * Es
##   Ss -> (outside)  : mu_s * Ss
##   (outside) -> W   : shed * Is
##   W -> (outside)   : (decay + disinf) * W
##   Sh -> Eh         : lambda_h * Sh
##   Eh -> Iah        : sigma_h * Eh
##   Iah -> Ich       : chron * Iah
##   Iah -> Sh        : gamma_a * Iah
##   Ich -> Sh        : gamma_c * Ich
##   Sh -> (outside)  : mu_h * Sh

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 30, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  As = 2.5e5,      # sheep recruitment (lambs + purchases) [illustrative] (sheep per year)
  mu_s = 0.4,      # sheep removal rate (slaughter + natural death) [illustrative] (per year)
  beta_ss = 2e-7,  # direct sheep-to-sheep transmission coefficient [illustrative] (per sheep per year)
  beta_sw = 1e-6,  # indirect (environment-to-sheep) transmission coefficient [illustrative] (per unit load per year)
  sigma_s = 2.0,   # rate exposed sheep become infectious (≈6-month latency) [illustrative] (per year)
  nu_s = 0,        # sheep vaccination rate (control lever; S19/Rev-1) [illustrative] (per year)
  wane_s = 0.5,    # waning of sheep vaccine protection [illustrative] (per year)
  shed = 15,       # environmental shedding rate [illustrative] (load units per infectious sheep per year)
  decay = 3.0,     # natural decay of Brucella in the environment [illustrative] (per year)
  disinf = 0,      # additional removal of environmental load by disinfection (control lever) [illustrative] (per year)
  beta_hs = 1e-8,  # sheep-to-human transmission coefficient (occupational contact) [illustrative] (per sheep per year)
  beta_hw = 1e-8,  # environment-to-human transmission coefficient [illustrative] (per unit load per year)
  sigma_h = 4.0,   # rate exposed → acute human case (≈0.25 year onset) [verified as a published value for this quantity in the related literature] (per year)
  chron = 1.0,     # rate acute → chronic human infection [illustrative] (per year)
  gamma_a = 2.0,   # recovery rate from acute infection [illustrative] (per year)
  gamma_c = 0.4,   # recovery rate from chronic infection [illustrative] (per year)
  mu_h = 0.006     # human natural death rate [verified as a published value in the related literature] (per year)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Ss = 1e6,  # susceptible sheep
  Es = 1e4,  # exposed/latently infected sheep
  Is = 1e3,  # infectious sheep (shedding; abortion/parturition products)
  Vs = 0,    # vaccinated sheep
  W = 1e3,   # Brucella in the environment (bacterial load, arbitrary units)
  Sh = 2e6,  # susceptible humans
  Eh = 0,    # exposed humans
  Iah = 0,   # acutely infected humans
  Ich = 0    # chronically infected humans
)

# ---- Model equations -------------------------------------------------
brucellosis_sheep_human_environment_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- Sh + Eh + Iah + Ich

    # Derived quantities
    Ns <- Ss + Es + Is + Vs                                                                                                             # sheep population
    N <- Sh + Eh + Iah + Ich                                                                                                            # human population
    lambda_s <- beta_ss * Is + beta_sw * W                                                                                              # sheep force of infection
    lambda_h <- beta_hs * Is + beta_hw * W                                                                                              # human force of infection
    R0s <- (beta_ss + beta_sw * shed / (decay + disinf)) * As * sigma_s / (mu_s * (mu_s + sigma_s + nu_s) * (mu_s + shed * 0 + 1e-12))  # sheep reproduction number (write out explicitly in the generator; the point is that R0s has a direct and an indirect additive component, whi
    CHh_rate <- (sigma_h) * Eh                                                                                                          # rate of new cumulative reported human cases (integrate with cumsum for the cumulative count)

    # Rates of change
    dSs <- As - lambda_s * Ss - nu_s * Ss + wane_s * Vs - mu_s * Ss
    dEs <- lambda_s * Ss - sigma_s * Es
    dIs <- sigma_s * Es
    dVs <- nu_s * Ss - wane_s * Vs
    dW <- shed * Is - (decay + disinf) * W
    dSh <- -lambda_h * Sh + gamma_a * Iah + gamma_c * Ich - mu_h * Sh
    dEh <- lambda_h * Sh - sigma_h * Eh
    dIah <- sigma_h * Eh - chron * Iah - gamma_a * Iah
    dIch <- chron * Iah - gamma_c * Ich

    list(c(dSs, dEs, dIs, dVs, dW, dSh, dEh, dIah, dIch), c(CHh_rate = CHh_rate))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = brucellosis_sheep_human_environment_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Ss = "#333333", Es = "#E8C468", Is = "#B71C1C", Vs = "#2457C5", W = "#A5D6A7", Sh = "#333333", Eh = "#E8C468", Iah = "#B71C1C", Ich = "#B71C1C")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Brucellosis in small ruminants with an environmental reservoir and human cases")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

output_names <- c("CHh_rate")
matplot(out$time, out[, output_names, drop = FALSE], type = "l", lty = 1, lwd = 2,
        col = seq_along(output_names), xlab = "Time (years)", ylab = "Value",
        main = "Brucellosis in small ruminants with an environmental reservoir and human cases: outputs")
legend("topright", legend = output_names, col = seq_along(output_names),
       lty = 1, lwd = 2, bty = "n")

Dog rabies with mass vaccination and dog-to-human transmission

Zoonoses & emerging
SdEdIdVdShEhIhDhDd

Rabies, domestic dogs → humans, urban African setting (N'Djaména, Chad). Transmission is density-dependent with an explicit area term — beta_d has units km²·dog⁻¹·week⁻¹, so the per-capita hazard is beta_d Id / area. Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.

Zinsstag J, Dürr S, Penny MA, Mindekem R, Roth F, Menendez Gonzalez S, Naissengar S, Hattendorf J (2009). Transmission dynamics and economics of rabies control in dogs and humans in an African city. PNAS 106(35):14996–15001 ↗

Show R code
## Dog rabies with mass vaccination and dog-to-human transmission
## Rabies, domestic dogs → humans, urban African setting (N'Djaména, Chad).
## Transmission is density-dependent with an explicit area term — beta_d has units km²·dog⁻¹·week⁻¹, so the per-capita hazard is beta_d Id / area.
## Parameter values: 0 of 15 checked against the source; the rest are recalled or illustrative.
## Reference: Zinsstag J, Dürr S, Penny MA, Mindekem R, Roth F, Menendez Gonzalez S, Naissengar S, Hattendorf J (2009). Transmission dynamics and economics of rabies control in dogs and humans in an African city. PNAS 106(35):14996–15001
## Source: https://doi.org/10.1073/pnas.0904740106
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sd, Ed, Id, Vd, Sh, Eh, Ih, Dh, Dd
## Flows (time unit: weeks):
##   (outside) -> Sd  : bd * Nd
##   Sd -> Ed         : lambda_d * Sd
##   Sd -> Vd         : nu_d * Sd
##   Vd -> Sd         : wane_d * Vd
##   Ed -> Id         : sigma_d * rd * Ed
##   Ed -> Sd         : sigma_d * (1 - rd) * Ed
##   Id -> Dd         : mu_d * Id
##   Sd -> (outside)  : (md + cull_d) * Sd
##   Sh -> Eh         : lambda_h * Sh
##   Eh -> Ih         : sigma_h * rh * (1 - pep) * Eh
##   Eh -> Sh         : sigma_h * (1 - rh * (1 - pep)) * Eh
##   Ih -> Dh         : mu_h * Ih

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 520, by = 1)  # weeks

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_d = 0.0807,    # dog-to-dog transmission rate [verified] (estimated by the authors) (km² per dog per week)
  beta_dh = 0.0002,   # dog-to-human transmission rate [verified] (km² per dog per week)
  area = 100,         # area of the study city, converting counts to densities [illustrative] (km²)
  sigma_d = 1 / 3.1,  # rate of leaving dog incubation (≈22 d mean incubation) [illustrative] (per week)
  mu_d = 1 / 0.6,     # rabies-induced death rate of rabid dogs (≈4 d survival); the sensitivity analysis identifies this as one of the three most influential parameters [verified as i
  rd = 0.49,          # probability that an exposed dog develops clinical rabies; also identified as highly influential [verified as influential; value illustrative] (dimensionless)
  bd = 0.0052,        # dog birth rate [illustrative] (per week)
  md = 0.0048,        # dog non-rabies death rate [illustrative] (per week)
  nu_d = 0,           # dog vaccination rate (campaign lever) [illustrative] (per week)
  wane_d = 1 / 52,    # waning of dog vaccine immunity + turnover of vaccinated dogs [illustrative] (per week)
  cull_d = 0,         # dog culling rate (alternative control the authors simulate) [illustrative] (per week)
  rh = 0.19,          # probability a bitten human develops rabies absent prophylaxis [illustrative] (dimensionless)
  pep = 0,            # proportion of exposed humans receiving effective post-exposure prophylaxis [illustrative] (dimensionless)
  sigma_h = 1 / 6,    # rate of leaving human incubation (≈6 weeks) [illustrative] (per week)
  mu_h = 1            # death rate of clinical human rabies (uniformly fatal within days) [illustrative] (per week)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sd = 23000,  # susceptible dogs
  Ed = 10,     # incubating (exposed) dogs
  Id = 2,      # rabid, infectious dogs
  Vd = 0,      # vaccinated, immune dogs
  Sh = 6e5,    # susceptible humans
  Eh = 0,      # exposed humans (bitten, incubating)
  Ih = 0,      # humans with clinical rabies
  Dh = 0,      # cumulative human rabies deaths
  Dd = 0       # cumulative rabid-dog deaths (for fitting to weekly rabid-dog reports)
)

# ---- Model equations -------------------------------------------------
rabies_dog_human_vaccination_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- Sh + Eh + Ih

    # Derived quantities
    Nd <- Sd + Ed + Id + Vd            # dog population
    N <- Sh + Eh + Ih                  # human population
    lambda_d <- beta_d * Id / area     # dog force of infection (density-dependent: the transmission rate is per unit area)
    lambda_h <- beta_dh * Id / area    # human exposure hazard
    Re <- beta_d * Sd / (area * mu_d)  # effective reproduction ratio; the authors estimate Re ≈ 1.01 at the onset of observation, consistent with stable endemic dynamics [verified]
    cover <- Vd / Nd                   # vaccination coverage; a single campaign reaching ≥70% coverage was reported sufficient to interrupt transmission to humans for at least 6 ye

    # Rates of change
    dSd <- bd * Nd - lambda_d * Sd - nu_d * Sd + wane_d * Vd + sigma_d * (1 - rd) * Ed - (md + cull_d) * Sd
    dEd <- lambda_d * Sd - sigma_d * rd * Ed - sigma_d * (1 - rd) * Ed
    dId <- sigma_d * rd * Ed - mu_d * Id
    dVd <- nu_d * Sd - wane_d * Vd
    dSh <- -lambda_h * Sh + sigma_h * (1 - rh * (1 - pep)) * Eh
    dEh <- lambda_h * Sh - sigma_h * rh * (1 - pep) * Eh - sigma_h * (1 - rh * (1 - pep)) * Eh
    dIh <- sigma_h * rh * (1 - pep) * Eh - mu_h * Ih
    dDh <- mu_h * Ih
    dDd <- mu_d * Id

    list(c(dSd, dEd, dId, dVd, dSh, dEh, dIh, dDh, dDd))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = rabies_dog_human_vaccination_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sd = "#333333", Ed = "#E8C468", Id = "#B71C1C", Vd = "#2457C5", Sh = "#333333", Eh = "#E8C468", Ih = "#B71C1C", Dh = "#111111", Dd = "#111111")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (weeks)", ylab = "Number of individuals",
        main = "Dog rabies with mass vaccination and dog-to-human transmission")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Bovine tuberculosis in cattle and badgers with test-and-slaughter

Zoonoses & emerging
ScEcIcSbEbIbCRc

Mycobacterium bovis, cattle ↔ European badger (Meles meles). The two-host coupling is the whole point: the paper examines how much of cattle infection is attributable to badgers versus within-herd spread, and how badger culling changes that. Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.

Cox DR, Donnelly CA, Bourne FJ, Gettinby G, McInerney JP, Morrison WI, Woodroffe R (2005). Simple model for tuberculosis in cattle and badgers. PNAS 102(49):17588–17593 ↗

Show R code
## Bovine tuberculosis in cattle and badgers with test-and-slaughter
## Mycobacterium bovis, cattle ↔ European badger (Meles meles).
## The two-host coupling is the whole point: the paper examines how much of cattle infection is attributable to badgers versus within-herd spread, and how badger culling changes that.
## Parameter values: 0 of 12 checked against the source; the rest are recalled or illustrative.
## Reference: Cox DR, Donnelly CA, Bourne FJ, Gettinby G, McInerney JP, Morrison WI, Woodroffe R (2005). Simple model for tuberculosis in cattle and badgers. PNAS 102(49):17588–17593
## Source: https://doi.org/10.1073/pnas.0509003102
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Sc, Ec, Ic, Sb, Eb, Ib, CRc
## Flows (time unit: years):
##   (outside) -> Sc  : (mu_c * N + test * Ic)
##   Sc -> Ec         : lambda_c * Sc
##   Ec -> Ic         : sigma_c * Ec
##   Ic -> CRc        : test * Ic
##   Sc -> (outside)  : mu_c * Sc
##   (outside) -> Sb  : (mu_b * Nb + alpha_b * Ib + cull_b * Nb)
##   Sb -> Eb         : lambda_b * Sb
##   Eb -> Ib         : sigma_b * Eb
##   Sb -> (outside)  : (mu_b + cull_b) * Sb

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 40, by = 0.05)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_cc = 0.4,    # cattle-to-cattle transmission rate [illustrative] (per year)
  beta_bc = 0.02,   # badger-to-cattle transmission coefficient [illustrative] (per badger per year)
  beta_bb = 0.6,    # badger-to-badger transmission rate [illustrative] (per year)
  beta_cb = 0.002,  # cattle-to-badger transmission coefficient [illustrative] (per cattle per year)
  sigma_c = 2.0,    # rate cattle progress from test-negative to test-positive [illustrative] (per year)
  test = 1.0,       # herd-testing frequency × test sensitivity, i.e. rate at which Ic animals are detected and slaughtered [illustrative] (per year)
  mu_c = 0.25,      # cattle turnover (culling/sale, 4-year productive life) [illustrative] (per year)
  sigma_b = 1.5,    # badger progression rate to excreting state [illustrative] (per year)
  mu_b = 0.3,       # badger natural death rate [illustrative] (per year)
  alpha_b = 0.2,    # additional disease-induced badger death rate [illustrative] (per year)
  cull_b = 0,       # badger culling rate (the intervention the trial was designed to evaluate) [illustrative] (per year)
  perturb = 0       # proportional increase in beta_bc caused by culling-induced badger ranging ("perturbation effect"); e.g. 0.3 [illustrative] (dimensionless)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Sc = 10000,  # susceptible cattle
  Ec = 0,      # cattle infected but test-negative (occult/early infection)
  Ic = 10,     # cattle infected and test-positive / infectious
  Sb = 1000,   # susceptible badgers
  Eb = 0,      # badgers infected, not yet excreting
  Ib = 20,     # badgers excreting M. bovis
  CRc = 0      # cumulative cattle reactors removed (herd-breakdown counter)
)

# ---- Model equations -------------------------------------------------
btb_cattle_badger_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- Sc + Ec + Ic

    # Derived quantities
    N <- Sc + Ec + Ic                                                           # cattle herd size
    Nb <- Sb + Eb + Ib                                                          # badger population
    lambda_c <- beta_cc * Ic / N + beta_bc * (1 + perturb * (cull_b > 0)) * Ib  # cattle force of infection
    lambda_b <- beta_bb * Ib / Nb + beta_cb * Ic                                # badger force of infection

    # Rates of change
    dSc <- (mu_c * N + test * Ic) - lambda_c * Sc - mu_c * Sc
    dEc <- lambda_c * Sc - sigma_c * Ec
    dIc <- sigma_c * Ec - test * Ic
    dSb <- (mu_b * Nb + alpha_b * Ib + cull_b * Nb) - lambda_b * Sb - (mu_b + cull_b) * Sb
    dEb <- lambda_b * Sb - sigma_b * Eb
    dIb <- sigma_b * Eb
    dCRc <- test * Ic

    list(c(dSc, dEc, dIc, dSb, dEb, dIb, dCRc))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = btb_cattle_badger_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Sc = "#333333", Ec = "#E8C468", Ic = "#B71C1C", Sb = "#333333", Eb = "#E8C468", Ib = "#B71C1C", CRc = "#8E0E0E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Bovine tuberculosis in cattle and badgers with test-and-slaughter")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

African swine fever in wild boar with carcass-mediated environmental transmission

Animal & wildlife
SEIiRCr

African swine fever virus, Eurasian wild boar (Sus scrofa). The key structural feature is that dead infectious hosts move into a long-lived environmental compartment rather than out of the system: Ii > Cr is the flow that lets ASF persist at very low host density, because Cr decays on a timescale of Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

O'Neill X, White A, Ruiz-Fons F, Gortázar C (2020). Modelling the transmission and persistence of African swine fever in wild boar in contrasting European scenarios. Scientific Reports 10:5895 ↗

Show R code
## African swine fever in wild boar with carcass-mediated environmental transmission
## African swine fever virus, Eurasian wild boar (Sus scrofa).
## The key structural feature is that dead infectious hosts move into a long-lived environmental compartment rather than out of the system: Ii > Cr is the flow that lets ASF persist at very low host density, because Cr decays on a timescale of
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: O'Neill X, White A, Ruiz-Fons F, Gortázar C (2020). Modelling the transmission and persistence of African swine fever in wild boar in contrasting European scenarios. Scientific Reports 10:5895
## Source: https://doi.org/10.1038/s41598-020-62736-y
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, E, Ii, R, Cr
## Flows (time unit: days):
##   (outside) -> S   : pmax(dens, 0) * N
##   S -> E           : lambda * S
##   E -> Ii          : sigma * E
##   Ii -> Cr         : gamma * cfr * Ii
##   Ii -> R          : gamma * (1 - cfr) * Ii
##   S -> (outside)   : (mub + hunt) * S
##   Ii -> (outside)  : (mub + hunt) * Ii
##   Cr -> (outside)  : (decay_c + remove_c) * Cr

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 1095, by = 2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_dir = 0.3,    # direct boar-to-boar transmission rate [illustrative] (per day)
  beta_car = 0.05,   # carcass-to-boar transmission coefficient (rooting/sniffing contact) [illustrative] (per carcass per day)
  sigma = 1 / 4,     # rate of leaving the exposed class (incubation ≈4 d) [illustrative] (per day)
  gamma = 1 / 5,     # rate of leaving infectiousness (≈5 d) [illustrative] (per day)
  cfr = 0.95,        # case fatality (ASF genotype II is near-uniformly lethal) [unverified] (dimensionless)
  decay_c = 1 / 90,  # carcass decay/removal rate (seasonally variable; much slower in winter) [illustrative] (per day)
  remove_c = 0,      # active carcass-removal rate (the main control lever) [illustrative] (per day)
  bb = 0.004,        # boar birth rate (strongly seasonal in reality) [illustrative] (per day)
  mub = 0.002,       # boar natural death rate [illustrative] (per day)
  hunt = 0,          # hunting/culling rate [illustrative] (per day)
  Kb = 2000          # carrying capacity [illustrative] (boar)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 2000,  # susceptible wild boar
  E = 0,     # exposed (infected, not yet infectious)
  Ii = 2,    # infectious wild boar
  R = 0,     # recovered/immune survivors
  Cr = 0     # infectious carcasses in the environment
)

# ---- Model equations -------------------------------------------------
asf_wild_boar_carcass_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + E + Ii + R

    # Derived quantities
    N <- S + E + Ii + R                          # living boar population
    lambda <- beta_dir * Ii / N + beta_car * Cr  # total force of infection (frequency-dependent direct term plus density-dependent carcass term)
    dens <- bb * (1 - N / Kb)                    # density-dependent per-capita birth rate

    # Rates of change
    dS <- pmax(dens, 0) * N - lambda * S - (mub + hunt) * S
    dE <- lambda * S - sigma * E
    dIi <- sigma * E - gamma * cfr * Ii - gamma * (1 - cfr) * Ii - (mub + hunt) * Ii
    dR <- gamma * (1 - cfr) * Ii
    dCr <- gamma * cfr * Ii - (decay_c + remove_c) * Cr

    list(c(dS, dE, dIi, dR, dCr))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = asf_wild_boar_carcass_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", E = "#E8C468", Ii = "#B71C1C", R = "#4E8A3E", Cr = "#8E0E0E")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "African swine fever in wild boar with carcass-mediated environmental transmission")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Chronic wasting disease in deer with environmental prion accumulation

Animal & wildlife
SIiEv

Chronic wasting disease (prion), mule deer / white-tailed deer. Only 3 compartments, which makes this the cleanest demonstration of an environmental-reservoir model: with beta_env = 0 the epidemic is density-dependent and self-limiting, while with slow prion decay the environmental term creates a ratche Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.

Miller MW, Hobbs NT, Tavener SJ (2006). Dynamics of prion disease transmission in mule deer. Ecological Applications 16(6):2208–2214 ↗

Show R code
## Chronic wasting disease in deer with environmental prion accumulation
## Chronic wasting disease (prion), mule deer / white-tailed deer.
## Only 3 compartments, which makes this the cleanest demonstration of an environmental-reservoir model: with beta_env = 0 the epidemic is density-dependent and self-limiting, while with slow prion decay the environmental term creates a ratche
## Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.
## Reference: Miller MW, Hobbs NT, Tavener SJ (2006). Dynamics of prion disease transmission in mule deer. Ecological Applications 16(6):2208–2214
## Source: https://doi.org/10.1890/1051-0761(2006
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ii, Ev
## Flows (time unit: years):
##   (outside) -> S   : births
##   S -> Ii          : lambda * S
##   S -> (outside)   : (mu + harvest) * S
##   Ii -> (outside)  : (mu + alpha + harvest) * Ii
##   (outside) -> Ev  : (shed * Ii + carc * alpha * Ii)
##   Ev -> (outside)  : decay_e * Ev

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 100, by = 0.1)  # years

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_dir = 0.0008,  # direct deer-to-deer transmission coefficient (density-dependent) [illustrative] (per deer per year)
  beta_env = 0.0002,  # environment-to-deer transmission coefficient [illustrative] (per prion unit per year)
  alpha = 0.55,       # CWD-induced mortality rate (mean time from infection to death ≈2 years) [unverified] (per year)
  mu = 0.15,          # natural (non-CWD) mortality rate [illustrative] (per year)
  bdeer = 0.35,       # maximum per-capita birth rate [illustrative] (per year)
  Kd = 1500,          # carrying capacity [illustrative] (deer)
  shed = 1.0,         # shedding from live infected animals (saliva, urine, faeces) [illustrative] (prion units per infected deer per year)
  carc = 10.0,        # prion deposited by a carcass [illustrative] (prion units per CWD death)
  decay_e = 0.05,     # loss of environmental infectivity (prions persist for years; this is the parameter the literature identifies as pivotal) [illustrative] (per year)
  harvest = 0         # hunter harvest rate (control lever; can be applied preferentially to Ii) [illustrative] (per year)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 1000,  # susceptible deer
  Ii = 1,    # infected deer (long subclinical + clinical infection, assumed infectious throughout)
  Ev = 0     # environmental prion load (infectious units; E avoided for clarity)
)

# ---- Model equations -------------------------------------------------
cwd_deer_environmental_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + Ii

    # Derived quantities
    N <- S + Ii                                # deer population
    lambda <- beta_dir * Ii + beta_env * Ev    # force of infection (direct + indirect, additive)
    births <- bdeer * N * pmax(1 - N / Kd, 0)  # logistic recruitment
    prev <- Ii / pmax(N, 1e-9)                 # prevalence

    # Rates of change
    dS <- births - lambda * S - (mu + harvest) * S
    dIi <- lambda * S - (mu + alpha + harvest) * Ii
    dEv <- (shed * Ii + carc * alpha * Ii) - decay_e * Ev

    list(c(dS, dIi, dEv))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = cwd_deer_environmental_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ii = "#B71C1C", Ev = "#E8C468")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (years)", ylab = "Number of individuals",
        main = "Chronic wasting disease in deer with environmental prion accumulation")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Amphibian chytridiomycosis with free-living zoospores

Animal & wildlife
SIiZs

Batrachochytrium dendrobatidis in a common toad (Bufo bufo) breeding population. Structurally an Anderson & May "model G" host–free-living-stage system. Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.

Mitchell KM, Churcher TS, Garner TWJ, Fisher MC (2008). Persistence of the emerging pathogen Batrachochytrium dendrobatidis outside the amphibian host greatly increases the probability of host extinction. Proc R Soc B 275(1632):329–334 ↗

Show R code
## Amphibian chytridiomycosis with free-living zoospores
## Batrachochytrium dendrobatidis in a common toad (Bufo bufo) breeding population.
## Structurally an Anderson & May "model G" host–free-living-stage system.
## Parameter values: 0 of 11 checked against the source; the rest are recalled or illustrative.
## Reference: Mitchell KM, Churcher TS, Garner TWJ, Fisher MC (2008). Persistence of the emerging pathogen Batrachochytrium dendrobatidis outside the amphibian host greatly increases the probability of host extinction. Proc R Soc B 275(1632):329–334
## Source: https://doi.org/10.1098/rspb.2007.1356
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: S, Ii, Zs
## Flows (time unit: days):
##   (outside) -> S   : bz * N * pmax(1 - N / Kz, 0)
##   S -> Ii          : lambda * S
##   Ii -> S          : rec * Ii
##   S -> (outside)   : mu_z * S
##   Ii -> (outside)  : (mu_z + alpha_z) * Ii
##   (outside) -> Zs  : (prod * Ii + sapro)
##   Zs -> (outside)  : clear_z * Zs

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 3650, by = 5)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta_hh = 1e-4,   # host-to-host transmission coefficient [illustrative] (per toad per day)
  beta_z = 1e-7,    # zoospore-to-host transmission coefficient [illustrative] (per zoospore per day)
  alpha_z = 0.02,   # disease-induced host death rate [illustrative] (per day)
  mu_z = 1 / 2920,  # natural host death rate (8-year life span) [illustrative] (per day)
  bz = 1 / 2920,    # host birth rate [illustrative] (per day)
  Kz = 1000,        # host carrying capacity [illustrative] (toads)
  prod = 100,       # zoospore release by infected hosts [illustrative] (zoospores per infected host per day)
  rz = 0.1,         # maximum saprobic (host-independent) zoospore reproduction rate (per day) [illustrative]
  phi = 1e-5,       # density-dependence of saprobic reproduction; appears as exp(-phi Zs) [verified: the paper uses a density-dependent term of the form exp(−φZ(t))] (per zoospore)
  clear_z = 0.1,    # zoospore death/loss rate (the survival time this implies is the key parameter the authors vary) [illustrative] (per day)
  rec = 0           # host recovery rate (set 0 for the baseline; chytrid clearance is limited) [illustrative] (per day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  S = 1000,  # susceptible toads
  Ii = 5,    # infected toads
  Zs = 0     # free-living zoospores in water (Z written Zs for clarity)
)

# ---- Model equations -------------------------------------------------
chytrid_amphibian_zoospore_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N_total <- S + Ii

    # Derived quantities
    N <- S + Ii                           # host population
    lambda <- beta_hh * Ii + beta_z * Zs  # force of infection (direct + zoospore pool)
    sapro <- rz * Zs * exp(-phi * Zs)     # density-limited saprobic zoospore reproduction

    # Rates of change
    dS <- bz * N * pmax(1 - N / Kz, 0) - lambda * S + rec * Ii - mu_z * S
    dIi <- lambda * S - rec * Ii - (mu_z + alpha_z) * Ii
    dZs <- (prod * Ii + sapro) - clear_z * Zs

    list(c(dS, dIi, dZs))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = chytrid_amphibian_zoospore_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(S = "#333333", Ii = "#B71C1C", Zs = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Amphibian chytridiomycosis with free-living zoospores")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

HIV-1 target-cell-limited model with antiretroviral therapy

Within-host
TcIiV

HIV-1 within-host viral dynamics. The canonical three-equation target-cell-limited system. Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Perelson AS, Neumann AU, Markowitz M, Leonard JM, Ho DD (1996). HIV-1 dynamics in vivo: virion clearance rate, infected cell life-span, and viral generation time. Science 271(5255):1582–1586 ↗

Show R code
## HIV-1 target-cell-limited model with antiretroviral therapy
## HIV-1 within-host viral dynamics.
## The canonical three-equation target-cell-limited system.
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
## Reference: Perelson AS, Neumann AU, Markowitz M, Leonard JM, Ho DD (1996). HIV-1 dynamics in vivo: virion clearance rate, infected cell life-span, and viral generation time. Science 271(5255):1582–1586
## Source: https://doi.org/10.1126/science.271.5255.1582
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Tc, Ii, V
## Flows (time unit: days):
##   (outside) -> Tc  : lam
##   Tc -> (outside)  : dT * Tc
##   Tc -> Ii         : beta * (1 - er) * V * Tc
##   Ii -> (outside)  : delta * Ii
##   (outside) -> V   : p * (1 - ep) * Ii
##   V -> (outside)   : cV * V

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 60, by = 0.1)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  lam = 10,       # target-cell production rate [illustrative] (cells µL⁻¹ day⁻¹)
  dT = 0.01,      # natural death rate of uninfected target cells [illustrative] (per day)
  beta = 2.4e-5,  # infection rate constant [illustrative] (mL copies⁻¹ day⁻¹)
  delta = 0.5,    # death rate of productively infected cells; the authors' estimate implies an infected-cell half-life of ≈1.6 days [unverified; 0.5/day is the value conventionall
  p = 1000,       # virion production rate per infected cell [illustrative] (virions cell⁻¹ day⁻¹)
  cV = 3.07,      # virion clearance rate from plasma (c is reserved, hence cV) [unverified; 3.07/day is the value conventionally cited from this paper] (per day)
  eps_rt = 0,     # efficacy of a reverse-transcriptase inhibitor (blocks new infection) [illustrative] (dimensionless, 0–1)
  eps_pi = 0,     # efficacy of a protease inhibitor (blocks production of infectious virions) [illustrative] (dimensionless, 0–1)
  tau_rx = 30     # time therapy starts [illustrative] (days)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Tc = 1000,  # uninfected target cells (CD4+ T cells, per µL)
  Ii = 0,     # productively infected cells (per µL)
  V = 1e-3    # free virions (copies per mL)
)

# ---- Model equations -------------------------------------------------
hiv_target_cell_therapy_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Derived quantities
    on_rx <- ifelse(t < tau_rx, 0, 1)            # therapy switch
    er <- eps_rt * on_rx                         # effective RT-inhibitor efficacy at time t
    ep <- eps_pi * on_rx                         # effective PI efficacy at time t
    R0v <- beta * p * (lam / dT) / (cV * delta)  # basic reproductive ratio of the virus within host
    Tc_star <- cV * delta / (beta * p)           # infected steady-state target-cell level

    # Rates of change
    dTc <- lam - dT * Tc - beta * (1 - er) * V * Tc
    dIi <- beta * (1 - er) * V * Tc - delta * Ii
    dV <- p * (1 - ep) * Ii - cV * V

    list(c(dTc, dIi, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hiv_target_cell_therapy_model, parms = parameters, hmax = 0.1)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Tc = "#333333", Ii = "#B71C1C", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "HIV-1 target-cell-limited model with antiretroviral therapy")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Influenza A target-cell-limited model with an eclipse phase (TEIV)

Within-host
TcI1I2V

Influenza A within-host dynamics in experimentally infected humans. Four compartments, no target-cell regeneration and no host death terms — over a 7–10 day infection, target-cell depletion alone terminates the infection, which is the model's central claim. Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.

Baccam P, Beauchemin C, Macken CA, Hayden FG, Perelson AS (2006). Kinetics of influenza A virus infection in humans. Journal of Virology 80(15):7590–7599 ↗

Show R code
## Influenza A target-cell-limited model with an eclipse phase (TEIV)
## Influenza A within-host dynamics in experimentally infected humans.
## Four compartments, no target-cell regeneration and no host death terms — over a 7–10 day infection, target-cell depletion alone terminates the infection, which is the model's central claim.
## Parameter values: 0 of 6 checked against the source; the rest are recalled or illustrative.
## Reference: Baccam P, Beauchemin C, Macken CA, Hayden FG, Perelson AS (2006). Kinetics of influenza A virus infection in humans. Journal of Virology 80(15):7590–7599
## Source: https://doi.org/10.1128/JVI.01623-05
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Tc, I1, I2, V
## Flows (time unit: days):
##   Tc -> I1         : beta * V * Tc
##   I1 -> I2         : k * I1
##   I2 -> (outside)  : delta * I2
##   (outside) -> V   : p * I2
##   V -> (outside)   : cV * V

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 10, by = 0.01)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  beta = 2.7e-5,  # infection rate constant [unverified (mL TCID50⁻¹ day⁻¹)
  k = 4.0,        # rate of transition from eclipse to productively infected [unverified] (per day)
  delta = 2.4,    # death rate of productively infected cells (a secondary source reports the original estimate as 0.167 per hour ≈ 4.0 per day and notes it implies an implausibly
  p = 1.2e-2,     # virion production rate per productively infected cell [unverified; secondary source reports ≈9.9e4 and ≈5e4 for differently scaled versions] (TCID50 mL⁻¹ day⁻¹
  cV = 3.0,       # viral clearance rate (secondary source: 0.125 per hour = 3.0 per day) [unverified] (per day)
  Tc0 = 4e8       # initial value of Tc
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Tc = 4e8,   # uninfected target (epithelial) cells
  I1 = 0,     # infected cells in the eclipse phase (not yet producing virus)
  I2 = 0,     # productively infected cells
  V = 7.5e-2  # infectious virus titre (TCID50 per mL of nasal wash)
)

# ---- Model equations -------------------------------------------------
influenza_within_host_teiv_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Derived quantities
    R0v <- beta * p * Tc0 / (cV * delta)  # within-host basic reproductive ratio, with Tc0 the initial target-cell count (this expression for R0 is confirmed in the derived literature)
    logV <- log(pmax(V, 1e-12))           # log10-scale output is what the data are fitted on (divide by log(10))

    # Rates of change
    dTc <- -beta * V * Tc
    dI1 <- beta * V * Tc - k * I1
    dI2 <- k * I1 - delta * I2
    dV <- p * I2 - cV * V

    list(c(dTc, dI1, dI2, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = influenza_within_host_teiv_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Tc = "#333333", I1 = "#B71C1C", I2 = "#B71C1C", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Influenza A target-cell-limited model with an eclipse phase (TEIV)")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Hepatitis C within-host dynamics under interferon-α therapy

Within-host
TcIiV

Hepatitis C virus within-host dynamics. Structurally identical to the HIV target-cell model — the scientific content is in how therapy enters. Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.

Neumann AU, Lam NP, Dahari H, Gretch DR, Wiley TE, Layden TJ, Perelson AS (1998). Hepatitis C viral dynamics in vivo and the antiviral efficacy of interferon-α therapy. Science 282(5386):103–107 ↗

Show R code
## Hepatitis C within-host dynamics under interferon-α therapy
## Hepatitis C virus within-host dynamics.
## Structurally identical to the HIV target-cell model — the scientific content is in how therapy enters.
## Parameter values: 0 of 10 checked against the source; the rest are recalled or illustrative.
## Reference: Neumann AU, Lam NP, Dahari H, Gretch DR, Wiley TE, Layden TJ, Perelson AS (1998). Hepatitis C viral dynamics in vivo and the antiviral efficacy of interferon-α therapy. Science 282(5386):103–107
## Source: https://doi.org/10.1126/science.282.5386.103
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Tc, Ii, V
## Flows (time unit: days):
##   (outside) -> Tc  : sh
##   Tc -> (outside)  : dh * Tc
##   Tc -> Ii         : beta * (1 - ei) * V * Tc
##   Ii -> (outside)  : delta * Ii
##   (outside) -> V   : p * (1 - en) * Ii
##   V -> (outside)   : cV * V

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 28, by = 0.05)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  sh = 1e5,      # hepatocyte production rate [illustrative] (cells mL⁻¹ day⁻¹)
  dh = 0.01,     # death rate of uninfected hepatocytes [illustrative] (per day)
  beta = 1e-7,   # infection rate constant [illustrative] (mL virion⁻¹ day⁻¹)
  delta = 0.14,  # death/loss rate of infected hepatocytes; the authors estimated a wide range across patients (≈0.01–0.5 per day), corresponding to infected-cell half-lives of ≈1
  p = 100,       # virion production rate per infected cell [illustrative] (virions cell⁻¹ day⁻¹)
  cV = 6.2,      # virion clearance rate, implying a plasma virion half-life of ≈2.7 hours [unverified; 6.2/day is the headline estimate conventionally attributed to this paper] (
  eta = 0.95,    # interferon efficacy in blocking virion production (dimensionless, 0–1) [recalled]
  eps = 0,       # efficacy in blocking new infections (found to be the minor mechanism) [illustrative] (dimensionless, 0–1)
  tau_rx = 0,    # therapy start time [illustrative] (days)
  tau_lag = 0.3  # pharmacological delay before the drug acts (the authors needed a delay to fit the first hours) [illustrative] (days)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Tc = 1e7,  # uninfected target hepatocytes (cells per mL)
  Ii = 1e6,  # productively infected hepatocytes (cells per mL)
  V = 1e6    # free virions (IU or copies per mL)
)

# ---- Model equations -------------------------------------------------
hcv_neumann_interferon_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Total population
    N <- Tc + Ii + V

    # Derived quantities
    on_rx <- ifelse(t < tau_rx + tau_lag, 0, 1)  # therapy switch including the delay
    en <- eta * on_rx                            # effective production-blocking efficacy
    ei <- eps * on_rx                            # effective infection-blocking efficacy
    R0v <- beta * p * (sh / dh) / (cV * delta)   # within-host reproductive ratio

    # Rates of change
    dTc <- sh - dh * Tc - beta * (1 - ei) * V * Tc
    dIi <- beta * (1 - ei) * V * Tc - delta * Ii
    dV <- p * (1 - en) * Ii - cV * V

    list(c(dTc, dIi, dV))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hcv_neumann_interferon_model, parms = parameters, hmax = 0.05)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Tc = "#333333", Ii = "#B71C1C", V = "#2457C5")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Hepatitis C within-host dynamics under interferon-α therapy")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")

Virus dynamics with a CTL immune response

Within-host
TcIiVZc

Within-host virus–immune-response dynamics (formulated for HIV/HTLV-1 and LCMV). Four compartments, and the only nonlinearity beyond standard mass action is the bilinear CTL proliferation term cz Ii Zc, which in the generator's grammar is an absolute inflow to Zc (the rate is not per-capita in a single source compartmen Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.

Nowak MA, Bangham CRM (1996). Population dynamics of immune responses to persistent viruses. Science 272(5258):74–79 ↗

Show R code
## Virus dynamics with a CTL immune response
## Within-host virus–immune-response dynamics (formulated for HIV/HTLV-1 and LCMV).
## Four compartments, and the only nonlinearity beyond standard mass action is the bilinear CTL proliferation term cz Ii Zc, which in the generator's grammar is an absolute inflow to Zc (the rate is not per-capita in a single source compartmen
## Parameter values: 0 of 9 checked against the source; the rest are recalled or illustrative.
## Reference: Nowak MA, Bangham CRM (1996). Population dynamics of immune responses to persistent viruses. Science 272(5258):74–79
## Source: https://doi.org/10.1126/science.272.5258.74
##
## Generated from a compartmental model diagram (model format v1).
## Regenerating from the same diagram always gives this exact file.
##
## Compartments: Tc, Ii, V, Zc
## Flows (time unit: days):
##   (outside) -> Tc  : lam
##   Tc -> (outside)  : dT * Tc
##   Tc -> Ii         : beta * V * Tc
##   Ii -> (outside)  : (aI + pz * Zc) * Ii
##   (outside) -> V   : kv * Ii
##   V -> (outside)   : uV * V
##   (outside) -> Zc  : cz * Ii * Zc
##   Zc -> (outside)  : bz * Zc

library(deSolve)

# ---- Time ------------------------------------------------------------
times <- seq(0, 200, by = 0.2)  # days

# ---- Parameters ------------------------------------------------------
parameters <- c(
  lam = 10,      # target-cell production [illustrative] (cells day⁻¹)
  dT = 0.1,      # target-cell death rate [illustrative] (per day)
  beta = 0.002,  # infection rate constant [illustrative] (per virus per day)
  aI = 0.5,      # infected-cell death rate in the absence of CTL [illustrative] (per day)
  pz = 1.0,      # rate of CTL-mediated killing of infected cells (pz because p is used for production and c is reserved) [illustrative] (per CTL per day)
  kv = 50,       # virus production per infected cell [illustrative] (virions cell⁻¹ day⁻¹)
  uV = 5,        # virus clearance rate [illustrative] (per day)
  cz = 0.1,      # rate of CTL proliferation driven by antigen [illustrative] (per infected cell per day)
  bz = 0.05      # CTL death rate [illustrative] (per day)
)

# ---- Initial conditions ----------------------------------------------
state <- c(
  Tc = 1000,  # uninfected target cells
  Ii = 0,     # infected cells
  V = 1e-3,   # free virus
  Zc = 1      # virus-specific cytotoxic T lymphocytes (CTL)
)

# ---- Model equations -------------------------------------------------
hiv_ctl_nowak_bangham_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    # Derived quantities
    R0v <- beta * kv * (lam / dT) / (uV * aI)  # reproductive ratio without immune control
    Istar_imm <- bz / cz                       # infected-cell abundance at the immune-controlled equilibrium: a key published insight is that this level is set entirely by CTL parameters,
    Zstar <- (beta * Tc * V / Ii - aI) / pz    # equilibrium CTL level (evaluate symbolically, not in the ODE)

    # Rates of change
    dTc <- lam - dT * Tc - beta * V * Tc
    dIi <- beta * V * Tc - (aI + pz * Zc) * Ii
    dV <- kv * Ii - uV * V
    dZc <- cz * Ii * Zc - bz * Zc

    list(c(dTc, dIi, dV, dZc))
  })
}

# ---- Solve -----------------------------------------------------------
out <- ode(y = state, times = times, func = hiv_ctl_nowak_bangham_model, parms = parameters)
out <- as.data.frame(out)

# ---- Plot ------------------------------------------------------------
compartment_cols <- c(Tc = "#333333", Ii = "#B71C1C", V = "#2457C5", Zc = "#333333")
matplot(out$time, out[, names(compartment_cols)], type = "l", lty = 1, lwd = 2,
        col = compartment_cols, xlab = "Time (days)", ylab = "Number of individuals",
        main = "Virus dynamics with a CTL immune response")
legend("topright", legend = names(compartment_cols), col = compartment_cols,
       lty = 1, lwd = 2, bty = "n")