From c57a54c08748cc09c436e49c93f3d2d9e1a99a93 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Fri, 14 Nov 2025 13:28:48 +0000 Subject: [PATCH 1/3] fix prereq --- episodes/simulating-transmission.Rmd | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/episodes/simulating-transmission.Rmd b/episodes/simulating-transmission.Rmd index a2cbdb50..cbc98cce 100644 --- a/episodes/simulating-transmission.Rmd +++ b/episodes/simulating-transmission.Rmd @@ -5,16 +5,11 @@ exercises: 30 # exercise time in minutes --- ```{r setup, echo= FALSE, message = FALSE, warning = FALSE} -library(ggplot2) -library(dplyr) -library(tidyverse) library(DiagrammeR) library(webshot) -library(epidemics) webshot::install_phantomjs(force = TRUE) ``` - :::::::::::::::::::::::::::::::::::::: questions - How do I simulate disease spread using a mathematical model? @@ -34,14 +29,31 @@ webshot::install_phantomjs(force = TRUE) ::::::::::::::::::::::::::::::::::::: prereq ++ Complete tutorial on [Contact matrices](../episodes/contact-matrices.md). + Learners should familiarise themselves with following concept dependencies before working through this tutorial: **Mathematical Modelling** : [Introduction to infectious disease models](https://doi.org/10.1038/s41592-020-0856-2), [state variables](../learners/reference.md#state), [model parameters](../learners/reference.md#parsode), [initial conditions](../learners/reference.md#initial), [differential equations](../learners/reference.md#ordinary). **Epidemic theory** : [Transmission](https://doi.org/10.1155/2011/267049), [Reproduction number](https://doi.org/10.3201/eid2501.171901). + +**R packages installed**: `{epidemics}`, `{socialmixr}`, `{tidyverse}`. + ::::::::::::::::::::::::::::::::: +:::::::::: spoiler +Install packages if their are not already installed + +```{r, eval = FALSE} +if(!require("pak")) install.packages("pak") +pak::pak(c("epidemics", "socialmixr", "tidyverse")) +``` + +If you have any error message, +go to the [main setup page](../learners/setup.md#software-setup). + +:::::::::: ## Introduction From 6a40b7495f3158303beca670cc74a6344db01510 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Fri, 14 Nov 2025 15:49:11 +0000 Subject: [PATCH 2/3] make contact matrix a challenge --- episodes/simulating-transmission.Rmd | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/episodes/simulating-transmission.Rmd b/episodes/simulating-transmission.Rmd index cbc98cce..b5a65e84 100644 --- a/episodes/simulating-transmission.Rmd +++ b/episodes/simulating-transmission.Rmd @@ -45,8 +45,8 @@ Learners should familiarise themselves with following concept dependencies befor Install packages if their are not already installed -```{r, eval = FALSE} -if(!require("pak")) install.packages("pak") +```r +if (!base::require("pak")) install.packages("pak") pak::pak(c("epidemics", "socialmixr", "tidyverse")) ``` @@ -266,37 +266,38 @@ A contact matrix represents the average number of contacts between individuals i ## Load contact and population data -Using the R package `socialmixr`, run the following lines of R code to obtain the contact matrix for the United Kingdom for the year age bins: +Using the R package `socialmixr`, obtain the contact matrix for the United Kingdom for the year age bins: + age between 0 and 20 years, + age between 20 and 40, + 40 years and over. -```r -polymod <- socialmixr::polymod -contact_data <- socialmixr::contact_matrix( - survey = polymod, - countries = "United Kingdom", - age.limits = c(0, 20, 40), - symmetric = TRUE -) -# prepare contact matrix -contact_matrix <- t(contact_data$matrix) -contact_matrix -``` +Use the survey available at `socialmixr::polymod` + +:::::::::::::::: hint + ++ Complete tutorial on [Contact matrices](../episodes/contact-matrices.md). + +:::::::::::::::: :::::::::::::::::::::::: solution -```{r polymod_uk, echo = FALSE, message = FALSE} +```{r polymod_uk, echo = TRUE, message = FALSE} +# Access the contact survey data polymod <- socialmixr::polymod + +# Generate the contact matrix contact_data <- socialmixr::contact_matrix( polymod, countries = "United Kingdom", age.limits = c(0, 20, 40), symmetric = TRUE ) + # prepare contact matrix contact_matrix <- t(contact_data$matrix) + +# print contact_matrix ``` From a42b1256123f0bcae119084eadbb5518f16dde77 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Fri, 14 Nov 2025 16:26:15 +0000 Subject: [PATCH 3/3] add guides to tutors --- episodes/simulating-transmission.Rmd | 134 ++++++++------------------- 1 file changed, 41 insertions(+), 93 deletions(-) diff --git a/episodes/simulating-transmission.Rmd b/episodes/simulating-transmission.Rmd index b5a65e84..2a78fca9 100644 --- a/episodes/simulating-transmission.Rmd +++ b/episodes/simulating-transmission.Rmd @@ -67,103 +67,16 @@ library(socialmixr) library(tidyverse) ``` +:::::::::::::::::::::: instructor -```{r traj, echo = FALSE, message = FALSE, fig.width = 10, eval = TRUE} -# load contact and population data from socialmixr::polymod -polymod <- socialmixr::polymod -contact_data <- socialmixr::contact_matrix( - polymod, - countries = "United Kingdom", - age.limits = c(0, 20, 40), - symmetric = TRUE -) - -# prepare contact matrix -contact_matrix <- t(contact_data$matrix) - -# prepare the demography vector -demography_vector <- contact_data$demography$population -names(demography_vector) <- rownames(contact_matrix) - -# initial conditions: one in every 1 million is infected -initial_i <- 1e-6 -initial_conditions_inf <- c( - S = 1 - initial_i, E = 0, I = initial_i, R = 0, V = 0 -) - -initial_conditions_free <- c( - S = 1, E = 0, I = 0, R = 0, V = 0 -) - -# build for all age groups -initial_conditions <- rbind( - initial_conditions_inf, - initial_conditions_free, - initial_conditions_free -) -rownames(initial_conditions) <- rownames(contact_matrix) - -# prepare the population to model as affected by the epidemic -uk_population <- epidemics::population( - name = "UK", - contact_matrix = contact_matrix, - demography_vector = demography_vector, - initial_conditions = initial_conditions -) - -# time periods -preinfectious_period <- 3.0 -infectious_period <- 7.0 -basic_reproduction <- 1.46 - -# rates -infectiousness_rate <- 1.0 / preinfectious_period -recovery_rate <- 1.0 / infectious_period -transmission_rate <- basic_reproduction / infectious_period - -# run an epidemic model using `epidemic()` -output_plot <- epidemics::model_default( - population = uk_population, - transmission_rate = transmission_rate, - infectiousness_rate = infectiousness_rate, - recovery_rate = recovery_rate, - time_end = 600, increment = 1.0 -) - -output_plot %>% - filter(compartment %in% c("exposed", "infectious")) %>% - ggplot() + - geom_line( - aes( - x = time, - y = value, - colour = demography_group, - linetype = compartment - ), - linewidth = 1.2 - ) + - scale_y_continuous( - labels = scales::comma - ) + - scale_colour_brewer( - palette = "Dark2", - name = "Age group" - ) + - theme_bw() + - labs( - x = "Simulation time (days)", - linetype = "Compartment", - y = "Individuals" - ) -``` +Use slides to introduce the topics of: +- Scenario modelling and +- Contact matrix. -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: instructor - -By the end of this tutorial, learners should be able to replicate the above image on their own computers. - -:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +Then start with the livecoding. +:::::::::::::::::::::: ## Simulating disease spread @@ -315,6 +228,19 @@ In `{epidemics}` the contact matrix normalisation happens within the function ca :::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::: instructor + +Make a pause. + +Use slides to introduce the topics of: + +- Initial conditions and +- Population structure. + +Then continue with the livecoding. + +:::::::::::::::::::::: + ### 2. Initial conditions The initial conditions are the proportion of individuals in each disease state $S$, $E$, $I$ and $R$ for each age group at time 0. In this example, we have three age groups age between 0 and 20 years, age between 20 and 40 years and over. Let's assume that in the youngest age category, one in a million individuals are infectious, and the remaining age categories are infection free. @@ -377,6 +303,18 @@ uk_population <- population( ``` +:::::::::::::::::::::: instructor + +Make a pause. + +Use slides to introduce the topics of: + +- Model parameters and +- New infections. + +Then continue with the livecoding. + +:::::::::::::::::::::: ### 4. Model parameters @@ -540,6 +478,16 @@ newinfections_bygroup %>% ::::::::::::::::::::::: +:::::::::::::::::::::: instructor + +Stop the livecoding. + +Suggest learners to read the rest of the episode. + +Return to slides. + +:::::::::::::::::::::: + ## Accounting for uncertainty The epidemic model is [deterministic](../learners/reference.md#deterministic), which means it runs like clockwork: the same parameters will always lead to the same trajectory. A deterministic model is one where the outcome is completely determined by the initial conditions and parameters, with no random variation. However, reality is not so predictable. There are two main reasons for this: the transmission process can involve randomness, and we may not know the exact epidemiological characteristics of the pathogen we're interested in. In the next episode, we will consider 'stochastic' models (i.e. models where we can define the process that creates randomness in transmission). In the meantime, we can include uncertainty in the value of the parameters that go into the deterministic model. To account for this, we must run our model for different parameter combinations.