<- function(time, state_var, pars) {
SIR_model # Extract state variables
<- state_var["S"]
S <- state_var["I"]
I <- state_var["R"]
R <- S + I + R
N # Extract model parameters
<- pars["beta"]
beta <- pars["gamma"]
gamma # The differential equations
<- -beta * S * I / N
dS <- beta * S * I / N - gamma * I
dI <- gamma * I
dR # Return the equations as a list
<- list(c(dS, dI, dR))
sol return(sol)
}
# What are our parameter values?
<- c(beta = 1.2, gamma = 1 / 10)
pars
# Define time to solve equations
<- seq(from = 0, to = 45, by = 0.1)
times
# What are the initial values (or conditions) of the state variables?
<- 1
I0 <- 100
N <- (N - I0)
S0 <- 0
R0
<- c(S = S0, I = I0, R = R0)
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, func = SIR_model,
solution parms = pars, method = rk4))
Using events : vaccination
The ‘events’ function can be used to implement changes in the model at fixed times. In this example, we illustrate how to add vaccination at fixed time points in an SIR model.
SIR model dynamics
In this example, we assume an infection is spreading in a population represented by the Susceptible-Infected-Recovered (SIR) model.The SIR model is described by the following system of differential equations,
\[ \begin{aligned} \frac{dS}{dt} & = \beta S I/N\\ \frac{dI}{dt} &= \beta S I/N - \gamma I \\ \frac{dR}{dt} &=\gamma I \ \end{aligned} \] where \(S\), \(I\) and \(R\) denote the number of susceptible, infectious and recovered individuals respectively. The total population size is \(N = S + I + R\).The model parameters are the transmission rate \(\beta\) and the recovery rate \(\gamma\).
We assume a daily transmission rate of \(\beta = 1.2\), and a recovery rate of \(\gamma = 1/10\). The model solution for given one infected individual in a population of 100 is shown below.
The epidemic peak occurs at time 6.5 with 71.04 infected individuals.
We wish to see the effects of vaccination at fixed time points on the dynamics of infection. To do this we will use ‘events’ in the R package deSolve.
Events
First, we must write a function that describes what happens if an event is triggered. The inputs must be the current time of the integration, the current estimated of the state variables and the parameter vector.
In this example, an event is vaccination. We implement a simplified vaccination process where a proportion \(p\) of the susceptible population is vaccinated and immediately moved to the recovered class.
<- function(times, state_var, parms) {
event_vaccinate # Extract needed state variables
<- state_var["S"]
S <- state_var["R"]
R
# Extract needed model parameters
<- parms["p"]
p
# Changes to the state variable due to vaccination
"S"] <- S - S * p
state_var["R"] <- R + S * p
state_var[
return(state_var)
}
In the function ode
we must specify the event function and the time points at which the event should occur.
Example
Assuming that 20% of the susceptible population is vaccinated on day 1, 5, 7, 10 the we find the solution as follows:
<- c(beta = 1.2, gamma = 1 / 10, p = 0.2)
pars
<- as.data.frame(ode(y = state_var, times = times, func = SIR_model,
solution parms = pars,
events = list(func = event_vaccinate,
time = c(1, 5, 7, 10))))
The epidemic peak now occurs at time 7 with 48.24 infected individuals.
Let’s compare this to a much higher proportion of vaccination and set \(p=0.8\).
The epidemic peak now occurs at time 5 with 4.64 infected individuals.
Events can also be triggered by separate functions instead of occurring at fixed time points, or events can be specified in data frames. For more examples and further reading: