Deterministic SIR model with R

Federica Gazzelloni

April 22, 2023

Materials

All materials we need:

sir_pkgs <- c("tidyverse","deSolve")
install.packages(sir_pkgs)

GitHub Repo: https://github.com/Fgazzelloni/sir-model-with-R/

Introduction

In this presentation, we will explore how to implement the deterministic SIR model in R, including how to simulate the model, plot the results, and interpret the output.

We will also discuss the assumptions and limitations of the model and how to extend it to more complex scenarios.

So, let’s dive in and learn more about the deterministic SIR model in R!

What is a SIR model?

  • The SIR model is a mathematical model used to describe the spread of infectious diseases in a population.

  • The model divides the population into three compartments:

    • susceptible
    • infected
    • recovered

The deterministic version of the SIR model assumes that the transmission rate and recovery rate are constant over time and that the population is homogeneous.

Modeling infectious diseases

Mathematical transmission model

\(N\) is the population, \(S\) the susceptible, \(I\) the infected, \(R\) the recovered. \(\beta\) and \(\gamma\) are the parameters, the transmission rate and the recovery rate respectively, expressed in \(day^-1\).

\[\frac{dS}{dt}=-\beta S \frac{I}{N}\] \[\frac{dI}{dt}= \beta S \frac{I}{N} -\gamma I\] \[\frac{dR}{dt}= \gamma I\]

Define the parameters

N <- 1e3  # Total population
beta <- 0.5  # Transmission rate
gamma <- 0.1  # Recovery rate

dS <- -beta * S * I / N
dI <- beta * S * I / N - gamma * I
dR <- gamma * I

Set the initial values and parameters

init_state <- c(S = 999, I = 1, R = 0)
parameters <- c(beta = 0.5, 
                gamma = 0.1, 
                N = sum(init_state))

Define the SIR model

sir_model <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    dS <- -beta * S * I / N
    dI <- beta * S * I / N - gamma * I
    dR <- gamma * I
    return(list(c(dS, dI, dR)))})}

Simulate the model

times <- seq(0, 100, by = 0.1)
sir_out <- ode(y = init_state, 
               times = times, 
               func = sir_model, 
               parms = parameters)

Here are some sources to look at for improving your knowledge about how to make a deterministic SIR model in R:

  1. The R Epidemics Consortium (RECON) is a group of researchers and practitioners who develop and apply computational models to understand and control infectious disease outbreaks. They have a GitHub repository with R code for simulating and analyzing SIR models.

  2. The {EpiModel} package is an R package for simulating and analyzing stochastic and deterministic epidemic models, including the SIR model. The package includes functions for parameter estimation, sensitivity analysis, and visualization.

  3. The {deSolve} package is an R package for solving differential equations, which are commonly used to model dynamic systems such as the SIR model. The package includes functions for solving ordinary differential equations (ODEs) and partial differential equations (PDEs).

  4. The Epidemic Modeling with R (EMR) book provides a comprehensive introduction to epidemic modeling with R, including the SIR model. It covers both deterministic and stochastic models, as well as parameter estimation, sensitivity analysis, and model validation.

More to know

For example, the SIR (Susceptible-Infected-Recovered) model is a deterministic model that describes the dynamics of the spread of a virus, but it also includes stochastic elements such as random interactions between individuals.