3 Modelling An Infected Cohort
3 Modelling An Infected Cohort
3 Modelling An Infected Cohort
dI
= −γI (1)
dt
dR
= γI (2)
dt
Load the packages which you need for this etivity, by running the following cell:
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:
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.
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.
# 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 [ ]:
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
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.
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#
# Now, copy-paste your plot code from above here to visualise the output.
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 [ ]: