Introduction

In this lesson, we will extend the classic SIR model by first introducing vital dynamics—the processes of birth and natural death. After establishing the fundamentals of incorporating these dynamics, we’ll further refine our model to include infection-induced deaths. This progression will allow us to understand the effects of both natural and infection-specific mortality on disease dynamics.

Today, you will learn how to modify the SIR model to incorporate both types of mortality and how to implement these modifications in R.

Let’s get started!

Learning Objectives

By the end of this lesson, you should be able to:

  • Understand the role of natural birth and death rates (\(\mu\)) in the SIR model.
  • Extend the SIR model to include infection-induced deaths (\(\alpha\)).
  • Implement the modified SIR models in R.
  • Analyze the differences between the classic SIR model, the model with vital dynamics, and the model including infection-induced deaths.

Packages

This lesson will require the following packages to be installed and loaded:

# Load packages 
if(!require(pacman)) install.packages("pacman")
pacman::p_load(deSolve, # package to solve differential equations
               tidyverse, # package that includes ggplot2
               here)

1 Introduction to Vital Dynamics

The classic SIR model assumes a closed population with no births, deaths, or migration. To model real-world scenarios more accurately, we introduce vital dynamics—the inclusion of birth and natural death rates.

1.1 Why Include Natural Birth and Death Rates?

Including natural birth and death rates in the SIR model provides a more realistic representation of population dynamics, especially over longer time periods. This modification is crucial when:

  • The epidemic spans several years.
  • Birth and death rates significantly impact the susceptible population over time.

The Role of Migration in Vital Dynamics

In addition to birth and death rates, migration—which includes both immigration (inflow) and emigration (outflow)—plays a crucial role in shaping population dynamics.

Migration can introduce or remove individuals from any of the compartments: susceptible (S), infected (I), or recovered (R). This means that population movement can bring in or take away susceptible individuals, active infections, or recovered cases, significantly influencing the spread and progression of an epidemic. By incorporating both inflow and outflow, the model more accurately captures real-world scenarios where migration affects transmission rates and population structure over time.

1.1.1 Model Behavior and Implications of Vital Dynamics

Adding birth and death rates to the SIR model affects the behavior of the system in several important ways, especially when modeling populations over long periods of time. Here’s what happens when we incorporate vital dynamics:

  1. Endemic Equilibrium: The continuous inflow of newborns into the susceptible compartment keeps the disease circulating in the population, potentially leading to an endemic equilibrium. This is where the number of infected individuals stabilizes over time, and the disease remains present at a low level without completely dying out.

  2. Disease Persistence: In the basic SIR model, the disease might fade out after a large portion of the population has recovered. However, with births replenishing the susceptible population, the disease can persist indefinitely. This is particularly important for modeling diseases that never fully disappear, like seasonal flu or endemic infections.

  3. Cyclical Patterns: In populations where the birth rate is high, the disease may exhibit cyclical patterns. New susceptible individuals enter the population regularly, and once they accumulate to a certain threshold, outbreaks may occur again. This leads to cycles of infection over time, which more accurately reflects long-term disease dynamics in the real world.

  4. Population Turnover: By including natural deaths, we introduce population turnover—the replacement of individuals through birth and death. Infected individuals who would otherwise remain in the infected or recovered compartments are regularly removed from the population due to natural mortality, balancing the system and preventing any one compartment from indefinitely growing.

1.1.2 Key Insights from Vital Dynamics

Incorporating vital dynamics into the SIR model introduces the following critical insights:

  • Long-Term Disease Dynamics: Over long time periods, this extended model captures how diseases behave in populations with continuous births and deaths. It accounts for both the inflow of susceptible individuals and the gradual removal of infected and recovered individuals through natural deaths.

  • More Realistic Epidemic Predictions: The model with vital dynamics provides a more realistic depiction of diseases that persist in populations over time, especially for endemic diseases. It also allows for the exploration of long-term interventions and public health strategies to manage such diseases.

1.2 Modifying the SIR Model with Vital Dynamics

In real-world populations, individuals are born and die continuously. Over long periods, it’s essential to incorporate these vital dynamics into our model to capture realistic disease dynamics.

In the basic SIR model, we assumed a closed population, meaning that the population does not change over time. This assumption is reasonable for short-term outbreaks, but over longer periods, populations change through births and deaths, which can significantly influence disease transmission patterns. In this section, we’ll extend the SIR model to include natural births and deaths, creating a more comprehensive model.

1.2.1 Incorporating Births & Deaths

In our extended model, newborns enter the population as susceptible individuals because they are not infected at birth. Therefore, the birth rate directly increases the S compartment. We assume that the birth rate is proportional to the total population size, and that newborns are immediately susceptible to the disease.

Deaths can occur in any of the three compartments—susceptible, infected, or recovered. This means that individuals may die regardless of their disease status. Therefore, we apply the same death rate, \(\mu\), across all compartments.

For simplicity, let’s also assume that the total population size will remain constant, at \(N\). This is not always the case - if we, for example, model a disease over decades in a setting with high birth rate, the birth rate can be higher than death rate.

1.2.2 Conceptualizing Vital Dynamics

Let’s try to create a conceptual diagram that incorporates births and accounts for deaths in our SIR model. For this we will build on from our SIR model’s conceptual diagram.

The SIR Model

The SIR model consists of three compartments:

  • Susceptible (S): Individuals who can become infected.
  • Infected (I): Individuals who are infected and can transmit the disease.
  • Recovered (R): Individuals who have recovered and are no longer infectious. They are immune to reinfection.

And two critical parameters:

  • Transmission coefficient (β): The rate at which susceptible individuals become infected.
  • Recovery rate (γ): The rate at which infected individuals recover.

Let’s explore the SIR model with births and natural deaths!

where:

  • \(\mu\) is the per capita birth and natural death rate (assumed to be equal to maintain a constant total population size).

This extended SIR model divides the population into three compartments:

  • Susceptible (S): Individuals who can contract the disease-causing pathogen.
  • Infectious (I): Individuals who have contracted the pathogen and can transmit it to others.
  • Recovered (R): Individuals who have recovered from the disease and have immunity against it.

And three critical parameters:

  • Transmission coefficient (β): The rate at which susceptible individuals become infected.
  • Recovery rate (γ): The rate at which infected individuals recover.
  • Birth and natural death rate (μ): The per capita rate at which individuals are born and die.

1.2.3 Updated Differential Equations

To mathematically represent the model, we modify the system of differential equations for each compartment:

  • Susceptible individuals can increase through births and decrease by becoming infected or through natural deaths.

    \[ \frac{dS}{dt} = \mu N - \beta SI - \mu S \]

  • Infected individuals increase when susceptible individuals become infected, but they decrease through recovery or natural deaths.

    \[ \frac{dI}{dt} = \beta SI - \gamma I - \mu I \]

  • Recovered individuals increase when infected individuals recover, but they also decrease through natural deaths.

    \[ \frac{dR}{dt} = \gamma I - \mu R \]

where:

  • \(\mu\) is the per capita birth and natural death rate (assumed to be equal to maintain a constant total population size),
  • \(\beta\) is the transmission coefficient, representing the rate at which susceptible individuals become infected,
  • \(\gamma\) is the recovery rate, representing the rate at which infected individuals recover,
  • \(N = S + I + R\) is the total population size.
  • The transmission is density-dependent

ODEs for Frequency-dependent Transmission

To mathematically represent the model for a frequency-dependent transmission, we modify the differential equations:

\[ \frac{dS}{dt} = \mu N - \frac{\beta SI}{N} - \mu S \]

\[ \frac{dI}{dt} = \frac{\beta SI}{N} - \gamma I - \mu I \]

\[ \frac{dR}{dt} = \gamma I - \mu R \]

where:

  • \(\mu\) is the per capita birth and natural death rate (assumed to be equal to maintain a constant total population size),
  • \(\beta\) is the transmission coefficient, representing the rate at which susceptible individuals become infected,
  • \(\gamma\) is the recovery rate, representing the rate at which infected individuals recover,
  • \(N = S + I + R\) is the total population size.
  • The transmission is frequency-dependent

1.3 Implementing the SIR Model with Vital Dynamics in R

Let’s start by implementing the SIR model with natural birth and death rates in R. For this, we will use a generic example.

1.3.1 Step 1: Setting Initial Conditions and Parameters

We begin by defining the initial conditions and parameters for the model:

  • Total population (N): 100
  • Initial susceptible (S): 99
  • Initial infected (I): 1
  • Initial recovered (R): 0
  • Transmission rate (β): 0.1
  • Recovery rate (γ): 0.014
  • Natural birth/death rate (μ): 0.01
  • Time span: 400 days

Let’s define this in R:

# Define initial conditions
initial_state <- c(S = 99, I = 1, R = 0)

# Define parameters including birth and death rate
parameters <- c(beta = 0.002,  # Transmission rate
                gamma = 0.1,    # Recovery rate
                mu = 0.02)      # Natural Birth/Death rate

# Time span for simulation
times <- seq(0, 400, by = 1)

1.3.2 Step 2: Writing the SIR Model Function with Vital Dynamics

Next, we define the SIR model function that incorporates vital dynamics:

sir_model_vital <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    
    # Calculate total population size
    N <- S + I + R
    
    # Rate of change with vital dynamics
    dS <- mu * N - beta * S * I - mu * S
    dI <- beta * S * I - gamma * I - mu * I
    dR <- gamma * I - mu * R
    
    # Return the rate of change
    list(c(dS, dI, dR))
  })
}

1.3.3 Step 3: Solving the Differential Equations

We then use the ode function from the {deSolve} package to solve the differential equations:

# Solve the modified SIR model with vital dynamics
sir_vital_out <- ode(
  y = initial_state,
  times = times,
  func = sir_model_vital, 
  parms = parameters
)

1.3.4 Step 4: Converting the Output to a Data Frame and Visualizing

We now need to convert the output to a data frame and visualize the dynamics:

# Convert to dataframe
sir_vital_out <- as.data.frame(sir_vital_out)

# Plot the results
ggplot(sir_vital_out, aes(x = time)) +
  geom_line(aes(y = S, color = "Susceptible")) +
  geom_line(aes(y = I, color = "Infectious")) +
  geom_line(aes(y = R, color = "Recovered")) +
  labs(title = "SIR Model with Births and Deaths", x = "Time (days)", y = "Proportion of Population") +
  scale_color_manual(values = c("Susceptible" = "#2F5597", "Infectious" = "#C00000", "Recovered" = "#548235")) +
  theme_minimal() +
  theme(legend.title = element_blank())

In this model, with \(\mu = 0.02\), we introduce a natural birth and death process:

  • The susceptible population (S) decreases initially as people get infected but eventually stabilizes because new susceptible individuals are born at the same rate as deaths occur, maintaining a continuous influx into the susceptible category.
  • The infected population (I) reaches a peak and then fluctuates around an equilibrium level due to the ongoing process of new births and deaths, preventing the disease from dying out completely.
  • The recovered population (R) stabilizes as well, balancing the births and deaths within the system.

The introduction of a birth/death rate ensures that the disease becomes endemic, meaning it persists over time, rather than disappearing entirely, as in the no-birth/death case.

Exercise 1

Modify the SIR model with vital dynamics to include an Exposed (E) compartment, creating an SEIR model. The model should also incorporate natural births and deaths (i.e., include the birth/death rate \(\mu\)). Use the following parameters and conceptual diagram:

  • Initial population: 1000 (with 990 susceptible, 5 exposed, 4 infected, and 1 recovered)
  • Transmission rate (\(\beta\)): 0.002
  • Progression rate from exposed to infected (\(\sigma\)): 0.1
  • Recovery rate (\(\gamma\)): 0.05
  • Birth/death rate (\(\mu\)): 0.01
  • Transmission is density-dependent

Tasks:

  1. Define the initial conditions and parameters for the SEIR model in R.
  2. Write an R function to represent the SEIR model with natural births and deaths.
  3. Solve the model over 200 days and plot the results using ggplot2.

2 Introducing Infection-Induced Deaths

In some infectious diseases, the mortality caused directly by the infection can significantly influence the disease’s dynamics. This concept is known as infection-induced deaths, where individuals succumb to the disease rather than dying from natural causes.

2.1 Why Focus on Infection-Induced Deaths?

Infections like Ebola or severe strains of influenza lead to high mortality rates among those infected. In such scenarios, it’s crucial to model the epidemic by considering the infection-induced death rate (\(\alpha\)) to understand the true impact of the disease on the population. This is particularly relevant when:

  • The disease has a significant mortality rate, making infection-induced deaths a primary driver of population changes.
  • Short-term epidemic behavior is the focus, where birth and natural death rates are less significant.

2.1.1 Key Implications of Incorporating Infection-Induced Deaths

By incorporating only infection-induced deaths into the SIR model, we can observe the following effects:

  1. Direct Impact on Infected Population: The infection-induced death rate (\(\alpha\)) specifically reduces the number of infected individuals, representing the direct fatalities caused by the disease.

  2. Altered Recovery and Disease Progression: As some infected individuals die, fewer people recover, which alters the disease’s progression through the population.

  3. Simplified Model: Without the birth and natural death rates, the model focuses purely on disease transmission, recovery, and infection-induced mortality, making it clearer and easier to analyze.

2.2 Modifying the SIR Model to Include Infection-Induced Deaths

To incorporate infection-induced deaths into the SIR model, we introduce the parameter \(\alpha\). This modification gives us a more straightforward view of how the disease affects the population.

2.2.1 Conceptual Model

Let’s explore the SIR model with infection-induced deaths:

The simplified SIR model includes three compartments:

  • Susceptible (S): Individuals who can be infected.
  • Infected (I): Individuals who are currently infected and can either recover or die due to the disease.
  • Recovered (R): Individuals who have recovered and gained immunity.

And three critical parameters:

  • Transmission coefficient (β): The rate at which susceptible individuals become infected.
  • Recovery rate (γ): The rate at which infected individuals recover.
  • Infection-induced deaths (α): The rate at which infected individuals die as a direct consequence of the disease.

2.2.2 Updated Differential Equations

The differential equations for this modified SIR model are:

  • Susceptible (S): Individuals in this compartment decrease as they become infected.

\[ \frac{dS}{dt} = -\beta SI \]

  • Infected (I): The infected population increases as susceptible individuals become infected and decreases as infected individuals either recover or die from the disease.

\[ \frac{dI}{dt} = \beta SI - \gamma I - \alpha I \]

  • Recovered (R): Recovered individuals increase as infected individuals recover.

\[ \frac{dR}{dt} = \gamma I \]

where:

  • \(\beta\) is the transmission rate (rate at which susceptible individuals become infected),

  • \(\gamma\) is the recovery rate (rate at which infected individuals recover),

  • \(\alpha\) is the infection-induced death rate (rate at which infected individuals die from the disease).

  • The transmission is density-dependent.

ODEs for Frequency-dependent Transmission

To mathematically represent the model for a frequency-dependent transmission, we modify the differential equations:

\[ \frac{dS}{dt} = \mu N - \frac{\beta SI}{N} - \mu S \]

\[ \frac{dI}{dt} = \frac{\beta SI}{N} - \gamma I - \alpha I \]

\[ \frac{dR}{dt} = \gamma I - \mu R \]

where:

  • \(\mu\) is the per capita birth and natural death rate (assumed to be equal to maintain a constant total population size),
  • \(\beta\) is the transmission coefficient, representing the rate at which susceptible individuals become infected,
  • \(\gamma\) is the recovery rate, representing the rate at which infected individuals recover,
  • \(N = S + I + R\) is the total population size.
  • The transmission is frequency-dependent.

2.3 Implementing the Modified SIR Model in R

To implement this model, we will once again use the same generic example as before.

2.3.1 Step 1: Defining Initial Conditions and Parameters

Let’s start by setting our initial conditions, similarily as we did in our previous example:

# Define initial conditions
initial_state <- c(S = 99, I = 1, R = 0)

# Parameters for the model
parameters <- c(beta = 0.002,  # Transmission rate
                gamma = 0.1, # Recovery rate
                alpha = 0.02) # Infection-induced death rate

# Time points
times <- seq(0, 400, by = 1)

2.3.2 Step 2: Writing the SIR Model Function with Infection-Induced Deaths

The next step is to define the SIR model function that incorporates only infection-induced deaths:

sir_model_infection_deaths <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    
    # Rate of change without birth/natural death rates
    dS <- -beta * S * I
    dI <- beta * S * I - gamma * I - alpha * I
    dR <- gamma * I
    
    # Return the rate of change
    list(c(dS, dI, dR))
  })
}

2.3.3 Step 3: Solving the Differential Equations

We solve the differential equations using the ode function from the {deSolve} package.

# Solve the modified SIR model with only infection-induced deaths
sir_infection_deaths_out <- ode(
  y = initial_state,
  times = times,
  func = sir_model_infection_deaths, 
  parms = parameters
)

2.3.4 Step 4: Converting the Output to a Data Frame and Visualizing

We convert the output to a data frame and visualize it using ggplot2:

# Convert the output to a data frame
sir_infection_deaths_out <- as.data.frame(sir_infection_deaths_out)

# Plot the results
ggplot(sir_infection_deaths_out, aes(x = time)) +
  geom_line(aes(y = S, color = "Susceptible")) +
  geom_line(aes(y = I, color = "Infectious")) +
  geom_line(aes(y = R, color = "Recovered")) +
  labs(title = "SIR Model with Infection-Induced Deaths", 
       x = "Time (days)", y = "Proportion of Population") +
  scale_color_manual(values = c("Susceptible" = "#2F5597", "Infectious" = "#C00000", "Recovered" = "#548235")) +
  theme_minimal() +
  theme(legend.title = element_blank())

In this model, you will notice several key differences:

  • Slower Spread of the Disease: With an infection-induced death rate (\(\alpha = 0.02\)), infected individuals are more likely to die from the disease, reducing the overall number of infections over time. This leads to a smaller peak in the infected population.
  • Lower Infection Peak: The maximum number of infected individuals decreases because deaths reduce the number of individuals who can continue to spread the disease.
  • Quicker Decline in Infected Population: As infected individuals are removed from the population faster through disease-induced deaths, the infected population decreases faster.

Since there’s no natural birth or death process (\(\mu = 0\)), the total population (N) decreases over time, contributing to the eventual disappearance of the disease.

Exercise 2

Modify the SIR model with vital dynamics to create an SI model that only includes Susceptible (S) and Infected (I) compartments. The model should incorporate infection-induced deaths (i.e., include the infection-induced rate \(\alpha\)) with a frequency-dependent transmission. Use the following parameters and the conceptual diagram below:

  • Initial population: 1000 (with 990 susceptible and 10 infected)
  • Transmission rate (\(\beta\)): 0.1
  • Infection-induced death rate (\(\alpha\)): 0.02

Tasks:

  1. Define the initial conditions and parameters for the SI model in R.
  2. Write an R function to represent the SI model with infection-induced deaths and frequency-dependent transmission.
  3. Solve the model over 200 days and plot the results using ggplot2.

3 SIR Model Simulation

Now that you are familiar with the concepts of vital dynamics, let’s explore how these elaborations to the SIR model impact the spread and dynamic of a disease!

The simulation below allows you to explore how changes in the parameters \(\beta\), \(\gamma\), \(\mu\), and \(\alpha\) affect the dynamics of the SIR model. Adjust the values to see how the susceptible (S), infected (I), and recovered (R) populations change over time.

To view the live version of the simulation, click here!

Exercise 3

Using the SIR model simulator, investigate how different values of \(\alpha\) (infection-induced death rate) impact the disease dynamics. Use the following parameters:

  • Initial population: 100 (with 99 susceptible, 1 infected, and 0 recovered)
  • Transmission rate (\(\beta\)): 0.1
  • Recovery rate (\(\gamma\)): 0.014
  • Birth/death rate (\(\mu\)): 0.01
  • Infection-Induced death rate (\(\alpha\)): 0.01
  • Transmission Type: Frequency Dependent

Vary \(\alpha\) from 0.01 to 0.05 in increments of 0.01.

3.1 Understanding the Impact of Vital Dynamics

Let’s explore how the vital dynamic parameters (\(\mu\) and \(\alpha\)) influence the spread of a disease using a practical example and using the SIR Model Simulation.

Assume the following initial settings for our population of 100 individuals over the course of 1 year:

  • Transmission rate: \(\beta = 0.003\)
  • Recovery rate: \(\gamma = 0.1\)
  • Natural birth/death rate: \(\mu = 0\)
  • Infection-induced death rate: \(\alpha = 0\)

This graph shows the classic SIR (Susceptible-Infected-Recovered) model with no vital dynamics (no births or deaths). At the start:

  • The susceptible population (S) starts at 99 and decreases over time as individuals become infected.
  • The infected population (I) begins with 1 individual and increases to a peak before declining as people either recover or move out of the infected category.
  • The recovered population (R) starts at 0 and gradually increases as infected individuals recover.

Since there are no births, natural deaths, or infection-induced deaths, the total population (N) remains constant, and the dynamics only depend on transmission and recovery rates.

3.1.1 Vital Dynamics and Infection-Induced Deaths

We have previously seen how \(\mu\) and \(\alpha\) individually affect the SIR model; however, we have not explored their effect when both are introduced. Let’s now see what happens in that case!

For this, let’s set \(\mu = 0.02\) and \(\alpha = 0.02\).

In this combined scenario:

  • Births and Deaths Create a Dynamic Equilibrium: The natural birth/death rate (\(\mu = 0.02\)) continues to replenish the susceptible population, ensuring the disease doesn’t disappear entirely.
  • Infection-Induced Deaths Lower the Peak and Stabilize Infections: The infection-induced death rate (\(\alpha = 0.02\)) prevents the infected population from growing too large, leading to a lower peak and steady decline in the infected individuals over time.

Overall, this model balances the effects of both vital dynamics and infection-induced deaths, showing a sustained yet lower-level presence of the disease over time. The system reaches a steady state where births maintain the population, while infection-induced deaths prevent uncontrolled outbreaks.

Exercise 4

Write an R function called seir_vital_dynamics that represents a frequency-dependent SIR model that includes natural births/deaths and infection-induced deaths. Use the SIR conceptual diagram below as a reference.

4 Conclusion

In this lesson, we first extended the classic SIR model to include vital dynamics, accounting for natural births and deaths. We then introduced infection-induced deaths to further refine the model, providing a more realistic view of disease impact. These modifications help us understand the complex interplay between natural and infection-specific mortality and the spread of disease in a population.

By implementing these models in R, we gained practical insights into modeling disease dynamics and the importance of considering different mortality factors.

Answer Key

Exercise 1

Defining Initial Conditions and Parameters

# Initial conditions
initial_state <- c(S = 990, E = 5, I = 4, R = 1)

# Define parameters
parameters <- c(beta = 0.002, # Transmission rate
                sigma = 0.1,  # Progression rate
                gamma = 0.05, # Recovery rate
                mu = 0.01)    # Birth/Death rate

SEIR Model Function with Births/Deaths (\(\mu\))

# Define the SEIR model function with birth/death rate (mu)
seir_model_vital <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    
    # Total population
    N <- S + E + I + R
    
    # Rate of change
    dS <- mu * N - beta * S * I - mu * S
    dE <- beta * S * I  - sigma * E - mu * E
    dI <- sigma * E - gamma * I - mu * I
    dR <- gamma * I - mu * R
    
    # Return the rate of change
    list(c(dS, dE, dI, dR))
  })
}

Solving the SEIR Model

# Time span for the simulation
times <- seq(0, 200, by = 1)

# Solve the SEIR model
seir_vital_out <- ode(
  y = initial_state,
  times = times,
  func = seir_model_vital, 
  parms = parameters
)

# Convert to a data frame
seir_vital_out <- as.data.frame(seir_vital_out)

# Plot the results
library(ggplot2)
ggplot(seir_vital_out, aes(x = time)) +
  geom_line(aes(y = S, color = "Susceptible")) +
  geom_line(aes(y = E, color = "Exposed")) +
  geom_line(aes(y = I, color = "Infected")) +
  geom_line(aes(y = R, color = "Recovered")) +
  labs(title = "SEIR Model with Natural Births and Deaths", x = "Time (days)", y = "Population Count") +
  scale_color_manual(values = c("Susceptible" = "blue", "Exposed" = "orange", "Infected" = "red", "Recovered" = "green")) +
  theme_minimal()

Exercise 2

Step 1: Defining Initial Conditions and Parameters

# Define initial conditions
initial_state <- c(S = 990, I = 10)

# Define parameters
parameters <- c(beta = 0.1, # Transmission rate
                alpha = 0.02)    # Infection-induced rate

Step 2: Writing the SI Model Function with Births/Deaths and Frequency-Dependent Transmission

# Define the SI model function with natural births/deaths and frequency-dependent transmission
si_model_freq <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    
    # Total population
    N <- S + I
    
    # Rate of change
    dS <- - (beta * S * I / N) 
    dI <- (beta * S * I / N) - alpha * I
    
    # Return the rate of change
    list(c(dS, dI))
  })
}

Step 3: Solving the Model Using the ode Function

# Load the deSolve package
if(!require(deSolve)) install.packages("deSolve")
library(deSolve)

# Time span for the simulation
times <- seq(0, 400, by = 1)

# Solve the SI model
si_freq_out <- ode(
  y = initial_state,
  times = times,
  func = si_model_freq, 
  parms = parameters
)

# Convert the output to a data frame
si_freq_out <- as.data.frame(si_freq_out)

Step 4: Plotting the Results Using ggplot2

# Load the ggplot2 package
if(!require(ggplot2)) install.packages("ggplot2")
library(ggplot2)

# Plot the results
ggplot(si_freq_out, aes(x = time)) +
  geom_line(aes(y = S, color = "Susceptible")) +
  geom_line(aes(y = I, color = "Infected")) +
  labs(title = "SI Model with Births/Deaths and Frequency-Dependent Transmission", 
       x = "Time (days)", y = "Population Count") +
  scale_color_manual(values = c("Susceptible" = "blue", "Infected" = "red")) +
  theme_minimal() +
  theme(legend.title = element_blank())

Exercise 3

Overall Impact of Varying \(\alpha\) on Disease Dynamics:

  • Higher \(\alpha\) Values: Increase the rate at which infected individuals die, thereby reducing the infection’s ability to spread. This results in a shorter outbreak with a lower peak of infected individuals and fewer recovered individuals.

  • Lower \(\alpha\) Values: Allow the disease to spread more extensively, leading to a higher peak of infected individuals and a prolonged epidemic duration.

Exercise 4

# Define the SIR model function for Frequency Dependent (FD)
sir_model_fd <- function(time, state, parameters) {
  with(as.list(c(state, parameters)), {
    
    # Calculate total population size
    N <- S + I + R
    
    # Rate of change with frequency-dependent transmission
    dS <- mu * N - beta * S * I / N - mu * S
    dI <- beta * S * I / N - gamma * I - (mu + alpha) * I
    dR <- gamma * I - mu * R
    
    # Return the rate of change
    list(c(dS, dI, dR))
  })
}

Contributors

The following team members contributed to this lesson:

References

  • Keeling, M. J., & Rohani, P. (2008). Modeling Infectious Diseases in Humans and Animals. Princeton University Press.
  • Soetaert, K., Woodrow Setzer, R., & Petzoldt, T. (2021). R package deSolve. https://desolve.r-forge.r-project.org/

This work is licensed under the Creative Commons Attribution Share Alike license. Creative Commons License