Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

3 Modelling An Infected Cohort

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Mod_1_notebook_1_Modelling_an_Infected_Cohort

November 11, 2021

1 Modelling an infected cohort


In this etivity, you will code your first simple model and use it to answer a research question. To
get used to the format for models in R, we’ll start by giving you some parts of the code, and you
need to fill in the missing numbers or code blocks by replacing the #YOUR CODE# placeholders.
At the end of each etivity, we’ll provide a solution file that contains the full code, annotated with
comments. Make sure you understand each line, and soon you’ll be able to write your own model
from scratch!
Your task is to find out how long it takes for a cohort of infected people to recover. As you
saw in the video, to answer this question you need to keep track of 2 populations: those that
are infected (compartment I), and those that have recovered (compartment R). Infected people
recover at a rate γ (gamma). The differential equations describing this are:

dI
= −γI (1)
dt
dR
= γI (2)
dt
Load the packages which you need for this etivity, by running the following cell:

1.0.1 Run this cell as it is


In [7]: library(deSolve) # package to solve the model
library(reshape2) # package to change the shape of the model output
library(ggplot2) # package for plotting

To start, it is useful to code what we know about the situation we want to model. We are
looking at a cohort of 106 currently infected people, and no one has recovered so far. The average
duration of infection is 10 days. The question we want to answer is how many people will recover
from the infection over a 4-week period.
Given this data, fill in the correct values to the following variables, and run the cell:

In [ ]: initial_number_infected <- 1000000 # the initial infected population


# size
initial_number_recovered <- #YOUR CODE# # the initial number of people in
# the recovered state
recovery_rate <- #YOUR CODE# # the rate of recovery gamma,

1
# in units of days^-1
follow_up_duration <- #YOUR CODE# # the duration to run the model for,
# in units of days

# Hint: the units of the recovery rate and the follow-up duration should be
# consistent.

In [4]: initial_number_infected <- 1000000


initial_number_recovered <-0
recovery_rate <-1/10
follow_up_duration <-4*7

Now, we combine this data into objects that are recognised by the deSolve package as model
input. To do this, again run the code below.

In [10]: # The initial state values are stored as a vector


# and each value is assigned a name.
initial_state_values <- c(I = initial_number_infected,
R = initial_number_recovered)

# Parameters are also stored as a vector with assigned names and values.
parameters <- c(gamma = recovery_rate)
# In this case we only have one parameter, gamma.

In [ ]:

Think about: what kind of information is stored in the initial_state_values and


parameters vectors?
Additionally, we need to specify the time we want the model to run for. This depends on the
question we want to answer. In the cell below, the duration you specified earlier is automatically
filled in when you run it.

In [11]: # The times vector creates a sequence of timepoints at which we want to


# calculate the number of people in the I and R compartment.
times <- seq(from = 0, to = follow_up_duration, by = 1)

Think about: what kind of information is stored in the times vector?


Check your answers by having a look at each of these vectors to familiarise yourself with the
structure: in the following code cell we have typed the object names, so you just need to press
“Run” to see what each of them contains.

In [12]: initial_state_values
parameters
times

I 1e+06 R 0
gamma: 0.1
1. 0 2. 1 3. 2 4. 3 5. 4 6. 5 7. 6 8. 7 9. 8 10. 9 11. 10 12. 11 13. 12 14. 13 15. 14 16. 15 17. 16 18. 17
19. 18 20. 19 21. 20 22. 21 23. 22 24. 23 25. 24 26. 25 27. 26 28. 27 29. 28
The next step is specifying the model. Using the example code in the introductory document,
complete the following model function with the differential equations above.

2
In [24]: cohort_model <- function(time, state, parameters) {

with(as.list(c(state, parameters)), {

dI <- -gamma * I
dR <- gamma * I

return(list(c(dI, dR)))
})

Now all there’s left to do is solving this set of equations using the deSolve package. Fill in the
following command, which calculates and stores the number of infected and recovered people at
each timestep in the output dataframe. Don’t forget to run it!

In [23]: # Hint: if you can't remember what those arguments correspond to,
# just look up the ode help file:
?ode

In [25]: output <- as.data.frame(ode(y = initial_state_values,


times = times,
func = cohort_model,
parms = parameters))

Printing the model output returns a dataframe with columns time (containing our times vec-
tor), I (containing the number of infected people at each timestep) and R (containing the number
of recovered people at each timestep):

In [ ]: output

1.0.2 Question: Based on the output, how many people have recovered after 4 weeks? What
proportion of the total population does this correspond to?
Now, plot your model output in the following cell, with time on the x axis and the number of
infected and recovered people on the y axis. You can use the introductory document for help with
this.

In [22]: # First turn the output dataset into a long format,


# so that the number in each compartment at each timestep
# are all in the same column
output_long <- melt(as.data.frame(output), id = "time")

# Plot the number of people in each compartment over time


ggplot(data = output_long, # specify object containing data to plot
aes(aes(x = time,)) + # assign columns to axes and groups
geom_line() + # represent data as lines
xlab("Time (days)")+ # add label for x axis
ylab("Number of people") # add label for y axis

3
Error in parse(text = x, srcfile = src): <text>:12:0: unexpected end of input
10: xlab("Time (days)")+ # add label for x axis
11: ylab("Number of people") # add label for y axis
ˆ
Traceback:

1.0.3 Question: Based on the plot, at what timepoint were infected and recovered individuals
equal in number?
For the last part of the etivity, try varying γ to see how it affects the output. For example, in the
cell below change gamma (γ) to correspond to an average infectious period of:
a) 2 days
b) 20 days.
What is the recovery rate in these 2 cases?
In [1]: parameters <- c(gamma = #YOUR CODE#)
output <- #COPY-PASTE CODE FROM ABOVE#

# Since the initial number in each compartment and


# the timesteps of interest haven't changed,
# these are the only parts of the code we need to rerun.

# Now, copy-paste your plot code from above here to visualise the output.

Error in parse(text = x, srcfile = src): <text>:9:0: unexpected end of input


7:
8: # Now, copy-paste your plot code from above here to visualise the output.
ˆ
Traceback:

1.0.4 Question: What changes do you observe in the transition to the recovered compartment
if γ is higher or lower? For example, how long does it take for everyone to recover in
both cases?
1.1 Well done on writing your first model code. Now, check the solutions!
After completing an exercise, you should always carefully compare your answers and code with
the solutions! It is especially important to get the coding right from the start, because we are
building on the same modelling framework throughout the course. Additionally, the solution
files sometimes contain further information that will help you deepen your understanding of the
lesson.
To make the most of the learning experience, we always recommend trying to complete the whole
etivity before checking the solutions - but if you are stuck at any point, they can also help you to
move on to the next part.

4
1.1.1 Question: What changes do you observe in the transition to the recovered compartment
if γ is higher or lower? For example, how long does it take for everyone to recover in
both cases?
1.2 Well done on writing your first model code. Now, check the solutions!
After completing an exercise, you should always carefully compare your answers and code with
the solutions! It is especially important to get the coding right from the start, because we are
building on the same modelling framework throughout the course. Additionally, the solution
files sometimes contain further information that will help you deepen your understanding of the
lesson.
To make the most of the learning experience, we always recommend trying to complete the whole
etivity before checking the solutions - but if you are stuck at any point, they can also help you to
move on to the next part.

In [ ]:

You might also like