|
| 1 | +# nolint start |
| 2 | + |
| 3 | +# Practical 4 |
| 4 | +# Activity 1 |
| 5 | + |
| 6 | +# step: fill in your room number |
| 7 | +room_number <- #<COMPLETE> replace with 1/2/3/4 |
| 8 | + |
| 9 | +# Load packages ---------------------------------------------------------- |
| 10 | +library(epidemics) |
| 11 | +library(socialmixr) |
| 12 | +library(tidyverse) |
| 13 | + |
| 14 | + |
| 15 | +# (1) Contact matrix ------------------------------------------------------ |
| 16 | + |
| 17 | +# note: all input parameters come from |
| 18 | +# the table of parameters of the practical document |
| 19 | + |
| 20 | +# step: paste the survey link assigned to your room |
| 21 | +# then run the function to download the social contact data |
| 22 | +socialsurvey <- socialmixr::get_survey( |
| 23 | + #<COMPLETE> |
| 24 | +) |
| 25 | + |
| 26 | +socialsurvey |
| 27 | + |
| 28 | +# step: generate the contact matrix by defining |
| 29 | +# - the survey class object just downloaded, |
| 30 | +# - the country name, |
| 31 | +# - the age limits, as in the table of parameters, and |
| 32 | +# - TRUE or FALSE to create a symmetric matrix. |
| 33 | +contact_data <- socialmixr::contact_matrix( |
| 34 | + #<COMPLETE> |
| 35 | +) |
| 36 | + |
| 37 | +contact_data |
| 38 | + |
| 39 | +# run: confirm the symmetry of the matrix |
| 40 | +# Matrix are symmetric for the total number of contacts. |
| 41 | +# The total number of contacts from one group to another is the same in both directions. |
| 42 | +# Check this by multiplying the mean contacts by the population size for each group. |
| 43 | +contact_data$matrix * contact_data$demography$proportion |
| 44 | + |
| 45 | +# run: Prepare contact matrix |
| 46 | +# |
| 47 | +# - {socialmixr} provides a matrix from-to: from-participants -> to-contacts |
| 48 | +# In surveys, participants report their contacts. |
| 49 | +# |
| 50 | +# - {epidemics} expects a matrix to-from: to-contacts <- from-participants |
| 51 | +# Models assume that each susceptible (contact) is exposed to infection based on |
| 52 | +# how often they are contacted (by participants) and how infectious (participatns) are. |
| 53 | +# |
| 54 | +socialcontact_matrix <- t(contact_data$matrix) |
| 55 | + |
| 56 | +socialcontact_matrix |
| 57 | + |
| 58 | +# (2) Initial conditions -------------------------------------------------- |
| 59 | + |
| 60 | +## Infectious population --------- |
| 61 | + |
| 62 | +# step: add the proportion of infectious |
| 63 | +# as given in table of parameter |
| 64 | +initial_i <- #<COMPLETE> |
| 65 | + |
| 66 | +# run: create an infectious vector |
| 67 | +initial_conditions_inf <- c( |
| 68 | + S = 1 - initial_i, |
| 69 | + E = 0, |
| 70 | + I = initial_i, |
| 71 | + R = 0, |
| 72 | + V = 0 |
| 73 | +) |
| 74 | + |
| 75 | +initial_conditions_inf |
| 76 | + |
| 77 | +## Free of infection population --------- |
| 78 | + |
| 79 | +# run: create an infection-free vector |
| 80 | +initial_conditions_free <- c( |
| 81 | + S = 1, |
| 82 | + E = 0, |
| 83 | + I = 0, |
| 84 | + R = 0, |
| 85 | + V = 0 |
| 86 | +) |
| 87 | + |
| 88 | +initial_conditions_free |
| 89 | + |
| 90 | +## Combine initial conditions ------------ |
| 91 | + |
| 92 | +# note: not all the population needs to be infectious. |
| 93 | +# The epidemic can start with infecitous in a specific age range. |
| 94 | + |
| 95 | +# step: Combine the initial conditions |
| 96 | +# Add initial_conditions_inf or initial_conditions_free |
| 97 | +# to the each age group as detailed in table of parameter |
| 98 | +initial_conditions <- base::rbind( |
| 99 | + #<COMPLETE>, # age group 1 |
| 100 | + #<COMPLETE>, # age group 2 |
| 101 | + #<COMPLETE> # age group 3 |
| 102 | +) |
| 103 | + |
| 104 | +# run: Use contact matrix to assign age group names |
| 105 | +rownames(initial_conditions) <- rownames(socialcontact_matrix) |
| 106 | + |
| 107 | +initial_conditions |
| 108 | + |
| 109 | +# (3) Population structure ------------------------------------------------ |
| 110 | + |
| 111 | +# run: Prepare the demography vector |
| 112 | +demography_vector <- contact_data$demography$population |
| 113 | +names(demography_vector) <- rownames(socialcontact_matrix) |
| 114 | + |
| 115 | +# step: Prepare the population to model as affected by the epidemic adding |
| 116 | +# - the name of the country, |
| 117 | +# - the symmetric and transposed contact matrix, |
| 118 | +# - the vector with the population size of each age group |
| 119 | +# - the binded matrix with initial conditions for each age group |
| 120 | +population_object <- epidemics::population( |
| 121 | + #<COMPLETE> |
| 122 | +) |
| 123 | + |
| 124 | +population_object |
| 125 | + |
| 126 | +# (4) Model parameters ---------------------------------------------------- |
| 127 | + |
| 128 | +# step: define the disease-specific parameters: the rates |
| 129 | +# add the values as given in table of parameter |
| 130 | +infectiousness_rate <- 1 / #<COMPLETE> # 1/pre-infectious period |
| 131 | +recovery_rate <- 1 / #<COMPLETE> # 1/infectious period |
| 132 | +transmission_rate <- recovery_rate * #<COMPLETE> # recovery rate * R0 |
| 133 | + |
| 134 | + |
| 135 | +# (5) Run the model -------------------------------------------------------- |
| 136 | + |
| 137 | +# step: in each function argument add |
| 138 | +# - the population object |
| 139 | +# - each of the previously defined disease-specific rates |
| 140 | +# - the total simulation time as given in table of parameter |
| 141 | +simulate_baseline <- epidemics::model_default( |
| 142 | + #<COMPLETE> |
| 143 | +) |
| 144 | + |
| 145 | +simulate_baseline |
| 146 | + |
| 147 | + |
| 148 | +# (6) Plot all compartments ------------------------------------------------ |
| 149 | + |
| 150 | +# run: paste plot in report |
| 151 | + |
| 152 | +# plot with total number of individual per compartment |
| 153 | +# at different points in time |
| 154 | +simulate_baseline %>% |
| 155 | + ggplot(aes( |
| 156 | + x = time, |
| 157 | + y = value, |
| 158 | + color = compartment, |
| 159 | + linetype = demography_group |
| 160 | + )) + |
| 161 | + geom_line() + |
| 162 | + scale_y_continuous( |
| 163 | + breaks = scales::breaks_pretty(n = 10), |
| 164 | + labels = scales::comma |
| 165 | + ) |
| 166 | + |
| 167 | +# (7) Peak of infectious ------------------------------------------------- |
| 168 | + |
| 169 | +# run: paste table output in report |
| 170 | + |
| 171 | +# table of epidemic peak size and time |
| 172 | +# per demographic group |
| 173 | +epidemics::epidemic_peak(data = simulate_baseline) |
| 174 | + |
| 175 | + |
| 176 | +# (8) Plot new infections ------------------------------------------------- |
| 177 | + |
| 178 | +# run: paste plot output in report |
| 179 | + |
| 180 | +# New infections per demographic group in time |
| 181 | +newinfections_bygroup <- epidemics::new_infections(data = simulate_baseline) |
| 182 | + |
| 183 | +# Visualize the spread of the epidemic in terms of new infections |
| 184 | +newinfections_bygroup %>% |
| 185 | + ggplot(aes(time, new_infections, colour = demography_group)) + |
| 186 | + geom_line() + |
| 187 | + scale_y_continuous( |
| 188 | + breaks = scales::breaks_pretty(n = 10), |
| 189 | + labels = scales::comma |
| 190 | + ) |
| 191 | + |
| 192 | +# nolint end |
0 commit comments