# Load in the deSolve package
library(deSolve)
# If the package is not installed, install using the install.packages() function
# Define model function
<- function(times, state, parms){
SIR_metapop_model ## Define variables
<- state["S1"]
S1 <- state["I1"]
I1 <- state["R1"]
R1 <- state["C1"]
C1 <- S1 + I1 + R1
N1 <- state["S2"]
S2 <- state["I2"]
I2 <- state["R2"]
R2 <- state["C2"]
C2 <- S2 + I2 + R2
N2 # Extract parameters
<- parms["beta"]
beta <- parms["gamma"]
gamma <- parms["alpha"]
alpha <- (beta * I1 / N1 + alpha * beta * I2 / N2)
lambda1 <- (beta * I2 / N2 + alpha * beta * I1 / N1)
lambda2 # Define differential equations
<- - lambda1 * S1
dS1 <- lambda1 * S1 - gamma * I1
dI1 <- gamma * I1
dR1 <- lambda1 * S1
dC1 <- - lambda2 * S2
dS2 <- lambda2 * S2 - gamma * I2
dI2 <- gamma * I2
dR2 <- lambda2 * S2
dC2 <- list(c(dS1, dI1, dR1, dC1, dS2, dI2, dR2, dC2))
res return(res)
}
# Define parameters
<- c( beta = 0.4, gamma = 0.1, alpha = 1)
parameters
# Define time to run model
<- seq(from = 0, to = 50, by = 1)
times
# Define initial conditions
<- 1000; N2 <- 1000
N1 <- 1; I2_0 <- 0
I1_0 <- 0; R2_0 <- 0
R1_0 <- 0; C2_0 <- 0
C1_0 <- N1 - I1_0
S1_0 <- N2 - I2_0
S2_0 <- c(S1 = S1_0, I1 = I1_0, R1 = R1_0, C1 = C1_0,
state S2 = S2_0, I2 = I2_0, R2 = R2_0, C2 = C2_0)
# Solve equations
<- ode(y = state,
output_raw times = times,
func = SIR_metapop_model,
parms = parameters)
# Convert to data frame for easy extraction of columns
<- as.data.frame(output_raw)
output
# Plot output
par(mfrow = c(1, 1))
plot(output$time, output$I1, type = "l", col = 4, lwd = 2, ylim = c(0, N1),
xlab = "Time", ylab = "Number", main = "")
lines(output$time, output$I2, lwd = 2, col = 2, type = "l")
legend("topright",
legend = c("Infected in population 1",
"Infected in population 2"),
lty = rep(1, 2), col = c(4, 2), lwd = 2, bty = "n")
05. Metapopulations with ODEs
The code below has been written to solve a Susceptible-Infected-Recovered model with two populations. Familiarise yourself with the expanded model before moving onto the activities that follow. Note: the compartments C1 and C2 reflect the cumulative numbers of people infected. This will be used later on.
Question A
When you simulate the above model, you’ll notice that currently the epidemics are nearly identical in the two populations. Update the model parameters so the transmission rate between the two populations is equal to 5% of the transmission rate within each population. What happens to the size and timing of the epidemics?
Question B
What happens if the epidemic starts with 10 people infected in both populations? Why does this happen?
Question C
The model is currently set up to record the number of cumulative cases in each population (i.e. C1 and C2). The below code will plot these cumulative numbers of cases. Update the code so you are plotting incidence, i.e. new cases appearing over time, rather than cumulative cases.
par(mfrow = c(1, 1))
plot(output$time, output$C1, type = "l", col = 4, lwd = 2, ylim = c(0, N1+100),
xlab = "Time", ylab = "Number", main = "")
lines( output$time, output$C2, lwd = 2, col = 2, type = "l")
legend("topright",
legend = c("Cumulative cases in population 1",
"Cumulative cases in population 2"),
lty = rep(1, 2), col = c(4, 2), lwd = 2, bty = "n")
Hint: Create a new variable that calculates the difference between adjacent timesteps, i.e. C1[2:t] - C1[1:(t-1)]
Question D
What does the incidence look like if only 50% of the cases in population 2 are reported?
Hint: There are several ways to do this - some are easier than others.
Question E
If you have time, expand the model to include three populations (denoted 1, 2, 3). How would you model an epidemic where:
mixing between population 1 and population 2 is 5% of the rate of mixing within these populations
mixing between population 1 and population 3 is 10% of the rate of mixing within these populations
there is no mixing between population 2 and population 3
Solutions to this practical can be accessed here.