The Susceptible-Infected-Recovered (SIR) model is a fundamental concept in epidemiology, offering insights into how diseases spread and recede in populations over time through a relatively simply set of functions. For this ThuRsday Tutorial, we’ll cover how to not only make a quick SIR 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. Now, let’s get to coding. 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

Before diving into the modeling process, you must ensure that the required R packages are installed and loaded into your workspace. 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 SIR model equations.`plotly`

allows for creating interactive plots, which will enable us to visualize the dynamics of the SIR model effectively.

### Step 2: Defining the SIR Model

The SIR model is made up of three compartments: Susceptible (S), Infected (I), and Recovered (R). Individuals transition from being susceptible to infected and then to recovered over time, modeled through a set of differential equations. Here’s how to define these equations 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 the`ode`

function from the`deSolve`

package.`variables`

: A vector containing the current numbers of susceptible, infected, and recovered individuals (S, I, R).`parameters`

: A vector of parameters for the model, specifically`beta`

,`gamma`

, and`N`

(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`

, `I`

, `R`

, `beta`

, `gamma`

, `N`

) within its code block.

The rates of change for each compartment are then calculated according to the SIR 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.`dI`

: The rate of change of infected individuals is the number of new infections (as calculated for`dS`

) 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 the`dI`

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`

and `gamma`

) which control the disease transmission and recovery rates, respectively. Here, we start with a population of 1000 individuals, with only one infected person as the outbreak begins. The `beta`

and `gamma`

values are hypothetical and can be adjusted based on the disease’s characteristics you’re modeling. 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 SIR model in R using `deSolve`

and `plotly`

. This model serves as a basic framework for understanding disease dynamics in populations, providing a foundation for more complex epidemiological modeling. It’s worth noting again that the SIR model is highly simplified; It assumes no population movement, random interactions, and other factors. Later on we’ll cover things like SEIR and MEM models that are a bit more complex. But for now, thanks for reading this ThuRsday Tutorial!

## Humanities Moment

This ThuRsday Tutorial’s featured image is An Allegory Of Water by Pauwels Franck (Flemish, 1540-1596). Pauwels Franck, also known as Paolo Fiammingo in Italy, was a Flemish painter who made a significant impact on the Venetian art scene after training in Antwerp. Active mainly in Venice, he is celebrated for his landscapes adorned with mythological, allegorical, and religious scenes, showcasing a Mannerist style influenced by notable artists like Tintoretto, Veronese, and Bassano. Though details about his early life are sparse, it’s believed he was born around 1540 in Antwerp and later joined the city’s Guild of Saint Luke in 1561. His journey to Italy remains largely undocumented, but by the 1560s, he was likely working in Tintoretto’s workshop in Venice, where he spent the majority of his career and earned acclaim under the name Paolo Fiammingo. Franck’s Venice studio attracted commissions from across Europe, including significant projects for the Fugger family, highlighting his expertise in allegorical and mythological themes. His work, which includes the celebrated Allegories of Love and various landscapes, blends Flemish precision with Venetian grace, anticipating the Italianate Flemish school.