The Susceptible-Exposed-Infected-Recovered (SEIR) model is a natural extension of the SIR Model, accounting for a fourth category of disease state, Exposure. For this ThuRsday Tutorial, we’ll cover how to not only make a quick SEIR model but also how to graph the results. If you’d like a decent background on what all goes into the SIR model conceptually and mathematically, please check out our Epi Explained article for Understanding SIR Models , or to see the more simplistic model in action you can also check out the previous SIR model ThuRsday Tutorial article. As usual, if you’d like to play with the code on your own, you can download it through Cody’s Github.
Step 1: Installing and Loading Necessary Packages
Just like last time, we’ll be using two primary packages in this tutorial. The two packages we’ll be using are deSolve
and plotly
.
deSolve
is a package for solving differential equations in R, which we’ll use to solve the SEIR model equations.plotly
allows for creating interactive plots, which will enable us to visualize the dynamics of the SEIR model effectively.
Step 2: Defining the SEIR Model
Incorporating the Exposed compartment modifies our approach from the original SIR model slightly. Now, we have four states: Susceptible (S), Exposed (E), Infected (I), and Recovered (R). The transitions now include a movement from Susceptible to Exposed before individuals become Infectious. Here’s how we set up the SEIR model in R:
Now on first look this might be a bit strange even to experienced R users as the function seems to do very little with what we’re inputting, but in reality this is because we’re actually going to use this function in yet another function in Step 3. For now though, let’s do a quick run-through of what we’re just set up.
The function takes three arguments:
time
: The current time point. While not directly used in the differential equations, it is required by theode
function from thedeSolve
package.variables
: A vector containing the current numbers of susceptible, infected, and recovered individuals (S, E, I, R).parameters
: A vector of parameters for the model, specificallybeta
,gamma
,sigma
, andN
(the total population).
Within the function, we use the with
function to combine variables
and parameters
into a single list, which simplifies accessing these values. The with
function allows us to refer to elements of the list directly by their names (e.g., S
, E
, I
, R
, beta
, gamma
, sigma
, N
) within its code block.
The rates of change for each compartment are then calculated according to the SEIR model equations:
dS
: The rate of change of susceptible individuals is the negative product of the infection rate (beta
), the number of susceptible individuals (S
), and the number of infected individuals (I
), divided by the total population (N
). This represents the number of new infections per time unit.dE
represents the rate of change for the Exposed population, transitioning from Susceptible due to infection and moving towards Infectious as the incubation period ends. The parametersigma
controls the rate at which Exposed individuals become Infectious, adding a new dynamic to the model.dI
: The rate of change of infected individuals is the number of new infections (as calculated fordS
) minus the number of recoveries, which is the product of the recovery rate (gamma
) and the number of infected individuals (I
).dR
: The rate of change of recovered individuals is simply the number of recoveries, as calculated in thedI
equation.
Finally, the function returns a list containing the rates of change for each compartment, which is then used by the ode
function to solve the model over time.
Step 3: Setting Initial Conditions and Parameters, Then Solving
Before solving the model, we need to specify the initial conditions (i.e., the starting numbers of susceptible, infected, and recovered individuals) and the parameters (beta
, sigma
, and gamma
) which control the disease transmission, incubation period and recovery rates, respectively. Here, we start with a population of 1000 individuals, with only one infected person as the outbreak begins. The parameter values are hypothetical and can be adjusted based on the disease’s characteristics you’re modeling (the incubation period is seen as an inverse, so we have an incubation period of five days, thus a parameter value of 1/5). With the function we already made and initial setup ready, we can now solve the differential equations using deSolve
‘s ode
function, which will return the solutions to every step from day 0 to day 50.
Step 4: Visualizing the Results with Plotly
Finally, to understand the dynamics of the disease through time, we’ll visualize the results using plotly
. This step transforms the output into a dynamic and interactive plot that can is much easier to understand, and can often wow your desired audience.
This is a fairly simply plotly graph, but it’s worth noting that for each class, we need to create a separate trace for each line, which allows them to act independently of one another.
The output can be seen here:
Conclusion
Through this tutorial, you’ve learned how to implement and visualize the SEIR model in R using deSolve
and plotly
. This model as mentioned earlier is a natural extension of the SIR compartmental model, though by no means should be seen as the last one. In fact, there are dozens of potential compartmental models that include things like vaccination, spatial relationships, and much more.
Humanities Moment
This ThuRsday Tutorial’s featured image is Cat Family by Sophie Sperlich (German, 1863-1906). An artist from Munich, Sperlich is famed for paintings of both cats and dogs in particular, and while not much is known of her life outside of her work, she has gained renewed popularity in recent years. In fact, since 2008, 38 of her works have been sold at auction, for anywhere between $100 and $1800.