
Infectious diseases have been plaguing humans since times immemorial. Often, the transmission is driven by the host which then passes the infection to other individuals. The goal of infectious disease models is to capture these transmission dynamics. They are mathematical descriptions of the spread of infection.
Using the model, it’s possible to :
- Study the effect of interventions on the burden of a disease
- To project the outcome of different interventions
A simple two compartment model
Consider the simplest model with just two compartments -Infected(I) and Recovered(R). This model helps us to answer the following question:
How long does it take for people to move from I to R ? (or the rate of transition from I to R)

Assumption: At any point in time, every individual in compartment I is equally likely to experience recovery.
The transmission dynamics is governed by the following Differential Equations :


The solution to the above equations are:


Here, γ represents the recovery rate. Higher the value of γ, the quicker the recovery.
The time spent in compartment I is distributed as exponential with parameter γ and mean 1/ γ(the mean infectious period)
Building a two compartment model in R
The deSolve package in R contains functions to solve initial value problems of a system of first-order ordinary differential equations (‘ODE’).
Step 1: Define the number of people in the infected and recovered cohort, the recovery rate γ(γ=0.1) and the follow up duration(21 days)
initial_values<-c(I=10000,R=0) #10000 in infected cohort
parameters<-c(gamma=0.1)
time=seq(from=0,t=21,by=1)
Step 2: Specify the model function
In the model function, specify the differential equations. The function has three input arguments :
- time: The time-points at which we want to solve the model at
- state:It stores the number of people in each compartment at every time point
- parameters: The names and values of model parameters
model_cohort<-function(time,state,parameters){
with(as.list(c(state,parameters)),{
dI=-gamma*I
dR=gamma*I
return(list(c(dI,dR)))
}
)
}
Step 3 : Solve the model using ode() in deSolve package
output<-as.data.frame(ode(y=initial_values,func = model_cohort,parms=parameters,times = time))
head(output)

We see that the output is a dataframe with the time points, the number of people in the infected and recovered compartment at each time point.
Step 4: Plot the output using ggplot2
out_long=melt(output,id="time") #To convert the dataframe to long format
ggplot(data = out_long,
aes(x = time, y = value, colour = variable, group = variable)) +
geom_line() +
xlab("Time (days)")+
ylab("No: of people") +
labs(title = paste("No: infected and recovered for gamma = 0.1 days^-1"))+scale_color_discrete(name="State")

From the plot, it’s possible to answer questions about the number infected and recovered at each time point. The mean infectious period will be the inverse of the recovery rate i.e, 1/0.1=10 days
Varying gamma
If γ is increased to say 0.5 , we observe that half of the infected cohort recovers in around 2 days whereas for γ=0.1, it takes around 7 days. Hence, the larger the value of γ, the lesser the time taken for recovery.

This model is the simplest case of much more complex compartmental models which is based on similar principles, say the SIR model. More on it in an upcoming post 🙂
References
- Soetaert, K. E., Petzoldt, T., & Setzer, R. W. (2010). Solving differential equations in R: package deSolve. Journal of statistical software, 33.
- https://www.coursera.org/specializations/infectious-disease-modelling