From ed1b547146bc00195418c91d875f8f45f82fa0b5 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Thu, 11 Jun 2026 17:34:15 +0100 Subject: [PATCH 1/7] Replace return_demography with survey_pop from wpp2024 in all episode POLYMOD sections Use explicit UK (and Zambia) population from wpp2024::popAge1dt via survey_pop instead of relying on survey-derived demography from return_demography = TRUE. Co-Authored-By: Claude Sonnet 4.6 --- episodes/compare-interventions.Rmd | 18 ++++++++++++++++-- episodes/contact-matrices.Rmd | 20 +++++++++++++++++--- episodes/disease-burden.Rmd | 9 ++++++++- episodes/modelling-interventions.Rmd | 9 ++++++++- episodes/simulating-transmission.Rmd | 9 ++++++++- episodes/vaccine-comparisons.Rmd | 9 ++++++++- 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/episodes/compare-interventions.Rmd b/episodes/compare-interventions.Rmd index 88175e44..7d5387f9 100644 --- a/episodes/compare-interventions.Rmd +++ b/episodes/compare-interventions.Rmd @@ -18,12 +18,19 @@ survey_files <- contactsurveys::download_survey( ) survey_load <- socialmixr::load_survey(files = survey_files) +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + contacts_byage <- socialmixr::contact_matrix( survey = survey_load, countries = "United Kingdom", age_limits = c(0, 15, 65), symmetric = TRUE, - return_demography = TRUE + survey_pop = uk_pop ) # prepare contact matrix @@ -344,12 +351,19 @@ survey_files_uk <- contactsurveys::download_survey( ) survey_load_uk <- socialmixr::load_survey(files = survey_files_uk) +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + contacts_byage_uk <- socialmixr::contact_matrix( survey = survey_load_uk, countries = "United Kingdom", age_limits = c(0, 20, 40), symmetric = TRUE, - return_demography = TRUE + survey_pop = uk_pop ) # prepare contact matrix contacts_byage_matrix_uk <- t(contacts_byage_uk$matrix) diff --git a/episodes/contact-matrices.Rmd b/episodes/contact-matrices.Rmd index b2d93974..6092894a 100644 --- a/episodes/contact-matrices.Rmd +++ b/episodes/contact-matrices.Rmd @@ -96,15 +96,22 @@ levels(survey_load$participants$country) :::::::::::::::::::::::::::::::::::::::::::::::: -We obtain the contact matrix for the United Kingdom — passing `countries = "United Kingdom"` to select data from the intended country, `age_limits` to define age categories, and `return_demography = TRUE` to include demographic information required by `{epidemics}`. +We obtain the contact matrix for the United Kingdom — passing `countries = "United Kingdom"` to select data from the intended country, `age_limits` to define age categories, and `survey_pop` to supply the population structure from `{wpp2024}` required by `{epidemics}`. ```{r polymod_uk, echo = TRUE} +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + contacts_byage <- socialmixr::contact_matrix( survey = survey_load, countries = "United Kingdom", age_limits = c(0, 20, 40), symmetric = TRUE, - return_demography = TRUE + survey_pop = uk_pop ) contacts_byage ``` @@ -195,13 +202,20 @@ Similar to the code above, to access vector values within a dataframe, you can u :::::::::::::::::::::::: instructor ```{r zambia_solution} +data(popAge1dt, package = "wpp2024") + +zambia_pop <- popAge1dt %>% + dplyr::filter(name == "Zambia", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + # Generate the contact matrix for Zambia only contacts_byage_zambia <- socialmixr::contact_matrix( survey = survey_load_zambia, countries = "Zambia", # key argument age_limits = c(0, 20), symmetric = TRUE, - return_demography = TRUE + survey_pop = zambia_pop ) # Print the contact matrix for Zambia only diff --git a/episodes/disease-burden.Rmd b/episodes/disease-burden.Rmd index d394788e..0d03017a 100644 --- a/episodes/disease-burden.Rmd +++ b/episodes/disease-burden.Rmd @@ -91,12 +91,19 @@ survey_files <- contactsurveys::download_survey( ) survey_load <- socialmixr::load_survey(files = survey_files) +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + contacts_byage <- socialmixr::contact_matrix( survey = survey_load, countries = "United Kingdom", age_limits = c(0, 20, 40), symmetric = TRUE, - return_demography = TRUE + survey_pop = uk_pop ) # prepare contact matrix diff --git a/episodes/modelling-interventions.Rmd b/episodes/modelling-interventions.Rmd index a67283fa..c426fbd6 100644 --- a/episodes/modelling-interventions.Rmd +++ b/episodes/modelling-interventions.Rmd @@ -93,13 +93,20 @@ survey_files <- contactsurveys::download_survey( ) survey_load <- socialmixr::load_survey(files = survey_files) +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + # generate contact matrix contacts_byage <- socialmixr::contact_matrix( survey = survey_load, countries = "United Kingdom", age_limits = c(0, 15, 65), symmetric = TRUE, - return_demography = TRUE + survey_pop = uk_pop ) # transpose contact matrix diff --git a/episodes/simulating-transmission.Rmd b/episodes/simulating-transmission.Rmd index bff01904..78e352b1 100644 --- a/episodes/simulating-transmission.Rmd +++ b/episodes/simulating-transmission.Rmd @@ -183,13 +183,20 @@ survey_files <- contactsurveys::download_survey( ) survey_load <- socialmixr::load_survey(files = survey_files) +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + # Generate the contact matrix contacts_byage <- socialmixr::contact_matrix( survey = survey_load, countries = "United Kingdom", age_limits = c(0, 20, 40), symmetric = TRUE, - return_demography = TRUE + survey_pop = uk_pop ) # prepare contact matrix diff --git a/episodes/vaccine-comparisons.Rmd b/episodes/vaccine-comparisons.Rmd index f7a29d07..2b69e4c3 100644 --- a/episodes/vaccine-comparisons.Rmd +++ b/episodes/vaccine-comparisons.Rmd @@ -74,12 +74,19 @@ survey_files <- contactsurveys::download_survey( ) survey_load <- socialmixr::load_survey(files = survey_files) +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) + contacts_byage <- socialmixr::contact_matrix( survey = survey_load, countries = "United Kingdom", age_limits = c(0, 15, 65), symmetric = TRUE, - return_demography = TRUE + survey_pop = uk_pop ) From 2af7863e3b8049317c9c841cdf53969cf0254367 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Thu, 11 Jun 2026 17:35:27 +0100 Subject: [PATCH 2/7] Fix baseline-interventions-en.R: migrate to contactsurveys pattern and wpp2024 survey_pop Replace deprecated socialmixr::polymod with contactsurveys::download_survey() + socialmixr::load_survey(), fix age.limits -> age_limits, and add wpp2024::popAge1dt population via survey_pop to align with the episode pattern. Co-Authored-By: Claude Sonnet 4.6 --- learners/files/baseline-interventions-en.R | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/learners/files/baseline-interventions-en.R b/learners/files/baseline-interventions-en.R index 2bd417f4..e473e0d0 100644 --- a/learners/files/baseline-interventions-en.R +++ b/learners/files/baseline-interventions-en.R @@ -3,17 +3,30 @@ # load packages library(epidemics) library(socialmixr) +library(contactsurveys) library(tidyverse) # load survey data -survey_data <- socialmixr::polymod +survey_files <- contactsurveys::download_survey( + survey = "https://doi.org/10.5281/zenodo.3874557", + verbose = FALSE +) +survey_load <- socialmixr::load_survey(files = survey_files) + +data(popAge1dt, package = "wpp2024") + +uk_pop <- popAge1dt %>% + dplyr::filter(name == "United Kingdom", year == max(year)) %>% + dplyr::select(lower.age.limit = age, population = pop) %>% + dplyr::mutate(population = population * 1000) # generate contact matrix cm_results <- socialmixr::contact_matrix( - survey = survey_data, + survey = survey_load, countries = "United Kingdom", - age.limits = c(0, 15, 65), - symmetric = TRUE + age_limits = c(0, 15, 65), + symmetric = TRUE, + survey_pop = uk_pop ) # transpose contact matrix From c09e1cc1b1c3a423d4252a05e3acd98c24004d27 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Thu, 11 Jun 2026 18:26:49 +0100 Subject: [PATCH 3/7] add minor text clarifications and checklist --- episodes/contact-matrices.Rmd | 7 +++++-- episodes/simulating-transmission.Rmd | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/episodes/contact-matrices.Rmd b/episodes/contact-matrices.Rmd index 6092894a..410937e2 100644 --- a/episodes/contact-matrices.Rmd +++ b/episodes/contact-matrices.Rmd @@ -34,12 +34,15 @@ webshot::install_phantomjs(force = TRUE) ## Introduction -Some groups of individuals have more contacts than others; the average schoolchild has many more daily contact than the average elderly person, for example. This heterogeneity of contact patterns between different groups can affect disease transmission, because certain groups are more likely to transmit to others within that group, as well as to other groups. The rate at which individuals within and between groups make contact with others can be summarised in a contact matrix. In this tutorial we are going to learn how contact matrices can be used in different analyses and how the `{socialmixr}` package can be used to estimate contact matrices. +Some groups of individuals have more contacts than others; the average schoolchild has many more daily contact than the average elderly person, for example. This heterogeneity of contact patterns between different groups can affect disease transmission, because certain groups are more likely to transmit to others within that group, as well as to other groups. The rate at which individuals within and between groups make contact with others can be summarised in a contact matrix. + +In this tutorial we are going to learn how contact matrices can be used in different analyses, how the package `{contactsurveys}` can be used to access survey data from different countries, and how the `{socialmixr}` package can be used to estimate contact matrices. We'll use `{dplyr}`, `{ggplot2}` and the pipe `%>%` to connect some of their functions, so let's also call to the `{tidyverse}` package: ```{r,message=FALSE,warning=FALSE} library(contactsurveys) library(socialmixr) +library(tidyverse) ``` ## The contact matrix @@ -96,7 +99,7 @@ levels(survey_load$participants$country) :::::::::::::::::::::::::::::::::::::::::::::::: -We obtain the contact matrix for the United Kingdom — passing `countries = "United Kingdom"` to select data from the intended country, `age_limits` to define age categories, and `survey_pop` to supply the population structure from `{wpp2024}` required by `{epidemics}`. +We obtain the contact matrix for the United Kingdom — passing `countries = "United Kingdom"` to select data from the intended country, `age_limits` to define age categories, and `survey_pop` to supply the population structure from `{wpp2024}` required by `{socialmixr}`. ```{r polymod_uk, echo = TRUE} data(popAge1dt, package = "wpp2024") diff --git a/episodes/simulating-transmission.Rmd b/episodes/simulating-transmission.Rmd index 78e352b1..31c63054 100644 --- a/episodes/simulating-transmission.Rmd +++ b/episodes/simulating-transmission.Rmd @@ -308,6 +308,18 @@ uk_population <- epidemics::population( ) ``` +:::::::::::: checklist + +Print the `uk_population` object. + +It must collect all the input information: + +- Population name +- Demography +- Contact matrix +- Initial conditions + +:::::::::::: :::::::::::::::::::::: instructor From de05b7467d3a01514320b236fbf5645b36589121 Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Thu, 11 Jun 2026 18:50:34 +0100 Subject: [PATCH 4/7] Add library(wpp2024) to all episodes; add library(contactsurveys) to compare-interventions Makes wpp2024 dependency explicit in every episode that loads popAge1dt, and fills the missing contactsurveys library call in compare-interventions.Rmd setup chunk. Co-Authored-By: Claude Sonnet 4.6 --- episodes/compare-interventions.Rmd | 2 ++ episodes/contact-matrices.Rmd | 1 + episodes/disease-burden.Rmd | 1 + episodes/modelling-interventions.Rmd | 1 + episodes/simulating-transmission.Rmd | 1 + episodes/vaccine-comparisons.Rmd | 1 + 6 files changed, 7 insertions(+) diff --git a/episodes/compare-interventions.Rmd b/episodes/compare-interventions.Rmd index 7d5387f9..786a42ea 100644 --- a/episodes/compare-interventions.Rmd +++ b/episodes/compare-interventions.Rmd @@ -8,6 +8,8 @@ exercises: 30 # exercise time in minutes ```{r setup, echo= FALSE, message = FALSE, warning = FALSE} webshot::install_phantomjs(force = TRUE) library(epidemics) +library(contactsurveys) +library(wpp2024) # hidden seed for stable stochastic output in lesson set.seed(33) diff --git a/episodes/contact-matrices.Rmd b/episodes/contact-matrices.Rmd index 410937e2..2274e062 100644 --- a/episodes/contact-matrices.Rmd +++ b/episodes/contact-matrices.Rmd @@ -42,6 +42,7 @@ In this tutorial we are going to learn how contact matrices can be used in diffe ```{r,message=FALSE,warning=FALSE} library(contactsurveys) library(socialmixr) +library(wpp2024) library(tidyverse) ``` diff --git a/episodes/disease-burden.Rmd b/episodes/disease-burden.Rmd index 0d03017a..1f1c5f4e 100644 --- a/episodes/disease-burden.Rmd +++ b/episodes/disease-burden.Rmd @@ -49,6 +49,7 @@ library(epiparameter) library(epidemics) library(contactsurveys) library(socialmixr) +library(wpp2024) library(tidyverse) ``` diff --git a/episodes/modelling-interventions.Rmd b/episodes/modelling-interventions.Rmd index c426fbd6..395cf0f3 100644 --- a/episodes/modelling-interventions.Rmd +++ b/episodes/modelling-interventions.Rmd @@ -59,6 +59,7 @@ In this tutorial, we will learn how to use `{epidemics}` to model interventions library(epidemics) library(contactsurveys) library(socialmixr) +library(wpp2024) library(tidyverse) ``` diff --git a/episodes/simulating-transmission.Rmd b/episodes/simulating-transmission.Rmd index 31c63054..a1eea3ba 100644 --- a/episodes/simulating-transmission.Rmd +++ b/episodes/simulating-transmission.Rmd @@ -65,6 +65,7 @@ In this tutorial we are going to learn how to use the `{epidemics}` package to s library(epidemics) library(contactsurveys) library(socialmixr) +library(wpp2024) library(tidyverse) ``` diff --git a/episodes/vaccine-comparisons.Rmd b/episodes/vaccine-comparisons.Rmd index 2b69e4c3..49a60722 100644 --- a/episodes/vaccine-comparisons.Rmd +++ b/episodes/vaccine-comparisons.Rmd @@ -42,6 +42,7 @@ library(ggplot2) library(epidemics) library(contactsurveys) library(socialmixr) +library(wpp2024) library(dplyr) library(purrr) ``` From 6b5ddc1f6da4c2da149090d5d944aef8c29b902c Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Thu, 11 Jun 2026 18:50:50 +0100 Subject: [PATCH 5/7] Add contactsurveys and wpp2024 to setup.md install and verify sections contactsurveys is on CRAN; wpp2024 installs from PPgp/wpp2024 on GitHub. Co-Authored-By: Claude Sonnet 4.6 --- learners/setup.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/learners/setup.md b/learners/setup.md index 5e077557..6c72297f 100644 --- a/learners/setup.md +++ b/learners/setup.md @@ -173,6 +173,8 @@ if(!require("pak")) install.packages("pak") new_packages <- c( "socialmixr", + "contactsurveys", + "PPgp/wpp2024", "finalsize", "epiverse-trace/epidemics", "epiparameter", @@ -247,6 +249,8 @@ When the installation has finished, you can try to load the packages by pasting ```r library(socialmixr) +library(contactsurveys) +library(wpp2024) library(finalsize) library(epidemics) library(epiparameter) From 88a8b59bb16f15cd3d7e217cf3075943e913de3e Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Thu, 11 Jun 2026 19:00:05 +0100 Subject: [PATCH 6/7] add new package --- renv/profiles/lesson-requirements/renv.lock | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/renv/profiles/lesson-requirements/renv.lock b/renv/profiles/lesson-requirements/renv.lock index 9fdb9130..64806ffd 100644 --- a/renv/profiles/lesson-requirements/renv.lock +++ b/renv/profiles/lesson-requirements/renv.lock @@ -5354,6 +5354,32 @@ "NeedsCompilation": "no", "Repository": "CRAN" }, + "wpp2024": { + "Package": "wpp2024", + "Version": "1.1-3", + "Source": "GitHub", + "Date": "2024-11-26", + "Title": "World Population Prospects 2024", + "Author": "United Nations Population Division", + "Maintainer": "Hana Sevcikova ", + "Depends": [ + "R (>= 2.14.2)", + "data.table" + ], + "Suggests": [], + "Description": "Provides data from the United Nation's World Population Prospects 2024.", + "License": "file LICENSE", + "URL": "http://population.un.org/wpp", + "NeedsCompilation": "no", + "LazyData": "no", + "RemoteType": "github", + "RemoteHost": "api.github.com", + "RemoteRepo": "wpp2024", + "RemoteUsername": "PPgp", + "RemotePkgRef": "PPgp/wpp2024", + "RemoteRef": "HEAD", + "RemoteSha": "2da7768ae64fc74105d3f9e98f9a74d37b62f99a" + }, "xfun": { "Package": "xfun", "Version": "0.58", From 6437d6dfb844bbc96af3b014e4bd48800c0be09a Mon Sep 17 00:00:00 2001 From: Andree Valle Campos Date: Thu, 11 Jun 2026 19:29:07 +0100 Subject: [PATCH 7/7] add tidyverse and survey pop for matrix split --- episodes/compare-interventions.Rmd | 1 + episodes/contact-matrices.Rmd | 1 + 2 files changed, 2 insertions(+) diff --git a/episodes/compare-interventions.Rmd b/episodes/compare-interventions.Rmd index 786a42ea..3815019e 100644 --- a/episodes/compare-interventions.Rmd +++ b/episodes/compare-interventions.Rmd @@ -10,6 +10,7 @@ webshot::install_phantomjs(force = TRUE) library(epidemics) library(contactsurveys) library(wpp2024) +library(tidyverse) # hidden seed for stable stochastic output in lesson set.seed(33) diff --git a/episodes/contact-matrices.Rmd b/episodes/contact-matrices.Rmd index 2274e062..39e31528 100644 --- a/episodes/contact-matrices.Rmd +++ b/episodes/contact-matrices.Rmd @@ -333,6 +333,7 @@ contact_data_split <- socialmixr::contact_matrix( countries = "United Kingdom", age_limits = c(0, 20, 40), symmetric = TRUE, + survey_pop = uk_pop, split = TRUE )