<- function(time, state_var, pars) {
SIRS_demography_model # Extract state variables
<- state_var["S"]
S <- state_var["I"]
I <- state_var["Z"]
R <- S + I + R
N # Extract model parameters
<- pars["beta"]
beta <- pars["gamma"]
gamma <- pars["mu"]
mu # The differential equations
<- mu * N + omega * R - beta * S * I / N - mu * S
dS <- beta * S * I / N - gamma * I - mu * I
dI <- gamma * I - mu * R - omega * R
dR # Return the equations as a list
<- list(c(dS, dI, dR))
sol return(sol)
}
# What are our parameter values?
<- c(omega = 0.01, beta = 0.6, gama = 0.14, mu = 0.01)
pars
# Define time to solve equations
<- seq(from = 0, to = 250, by = 1)
times
# What are the initial values (or conditions) of the state variables?
<- c(S = 99, I = 1, R = 0)
state_var
# Solve the Susceptible Infected equations over the vector of times , time
# with initial conditions
<- as.data.frame(ode(y = state_var, times = times,
solution func = SIRS_demography_model, parms = pars,
method = rk4))
plot(solution$time, solution$S, col = "darkblue", lwd = 2,
type = "l", ylim = c(0, 100), ylab = "Number", xlab = "Time")
lines(solution$time, solution$I, col = "darkred", lwd = 2, lty = 2)
lines(solution$time, solution$R, col = "darkgreen", lwd = 2, lty = 3)
legend("topright", c("Susceptible", "Infected", "Recovered"),
col = c("darkblue", "darkred", "darkgreen"),
lwd = 2, lty = c(1, 2, 3), bty = "n")
Test your understanding
So far, we have learnt how to translate assumptions about how an infection spreads into ODE models. As well as formulating your own models, you may also be given model equations which you need to understand.
We have been given the following equations, let’s try to figure out assumptions this model includes using only the equations.
\[ \begin{aligned} \frac{dS}{dt} & = \mu N + \omega R - \beta S I/N - \mu S\\ \frac{dI}{dt} &= \beta S I/N - \gamma I - \mu I \\ \frac{dR}{dt} &=\gamma I - \mu R - \omega R\\ \end{aligned} \] By writing out the model equations in words, drawing a flow diagram, or both, can you infer what the model assumptions are?
Solution
There are three equations representing the rate of change in compartments \(S\), \(I\) and \(R\). The processes are formulated similarly to what we have seen before, but with the addition of one process described by some removal from \(R\) that moves to \(S\) at rate \(\omega R\).
We can write out the model equations in words, and refer to our additional process as ‘?’.
\[ \begin{aligned} \mbox{susceptible population rate of change} & = \mbox{births} + \mbox{?} - \mbox{infections} - \mbox{deaths} \\ \mbox{infected population rate of change} &= + \mbox{infections} - \mbox{recoveries} - \mbox{deaths}\\ \mbox{recovered population rate of change} &= + \mbox{recoveries} -\mbox{deaths} -\mbox{?} \end{aligned} \]
In flow diagram form this is,
What biological process would result in individuals leaving the recovered class and moving to the susceptible class?
- loss of immunity
The model equations represent a Susceptible - Infected - Recovered - Susceptible (SIRS) model. In this model, individuals can recover from infection, but also lose their immunity after a period of time - and therefore become susceptible again.
Understanding code
We have also been given the following R code to find the numerical solution, but when we try to run the code, it doesn’t work. Can you fix the code and use the output to answer the questions in the next quiz?
Solution
The errors in the code were :
- within the model function, we need to extract
R <- state_var["R"]
notR <- state_var["Z"]
. This looks like a copy and paste mistake from a previous model. - within the model function, we need to extract
omega
from the list of parameters. This line of code was missing, we add the lineomega <- pars["omega"]
to the model code. - The name of the parameter \(\gamma\) did not match in the the parameter vector
pars
and the model function. It was spelledgama
inpars
. We change this isgamma
.
The fixed code is :
<- function(time, state_var, pars) {
SIRS_demography_model # Extract state variables
<- state_var["S"]
S <- state_var["I"]
I <- state_var["R"]
R <- S + I + R
N # Extract model parameters
<- pars["omega"]
omega <- pars["beta"]
beta <- pars["gamma"]
gamma <- pars["mu"]
mu # The differential equations
<- mu * N + omega * R - beta * S * I / N - mu * S
dS <- beta * S * I / N - gamma * I - mu * I
dI <- gamma * I - mu * R - omega * R
dR # Return the equations as a list
<- list(c(dS, dI, dR))
sol return(sol)
}
# What are our parameter values?
<- c(omega = 0.01, beta = 0.6, gamma = 0.14, mu = 0.01)
pars
# Define time to solve equations
<- seq(from = 0, to = 250, by = 1)
times
# What are the initial values (or conditions) of the state variables?
<- c(S = 99, I = 1, R = 0)
state_var
# Solve the Susceptible Infected equations over the vector of times , time
# with initial conditions
<- as.data.frame(ode(y = state_var, times = times,
solution func = SIRS_demography_model, parms = pars,
method = rk4))
plot(solution$time, solution$S, col = "darkblue", lwd = 2,
type = "l", ylim = c(0, 100), ylab = "Number", xlab = "Time")
lines(solution$time, solution$I, col = "darkred", lwd = 2, lty = 2)
lines(solution$time, solution$R, col = "darkgreen", lwd = 2, lty = 3)
legend("topright", c("Susceptible", "Infected", "Recovered"),
col = c("darkblue", "darkred", "darkgreen"), lwd = 2, lty = c(1, 2, 3),
bty = "n")
In the SIRS model solution, after the epidemic peak, the recovered individuals lose their immunity and become susceptible again, resulting in a secondary peak followed by endemic infection in the population.
We can find the endemic equilibrium values using runsteady
.
<- runsteady(y = state_var,
equilibrium_state func = SIRS_demography_model, parms = pars)
print(round(equilibrium_state$y, 2))
S I R
25.00 9.37 65.63
If you increase the rate of loss of immunity, what is the effect on the number of infectious individuals at the endemic equilibrium?
<- c(omega = 0.05, beta = 0.6, gamma = 0.14, mu = 0.01)
pars
<- runsteady(y = state_var,
equilibrium_state func = SIRS_demography_model, parms = pars)
print(round(equilibrium_state$y, 2))
S I R
25.0 22.5 52.5
The number of infectious individuals at endemic equilibrium increases.