diff --git a/episodes/compare-interventions.Rmd b/episodes/compare-interventions.Rmd index 88175e44..3815019e 100644 --- a/episodes/compare-interventions.Rmd +++ b/episodes/compare-interventions.Rmd @@ -8,6 +8,9 @@ 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) +library(tidyverse) # hidden seed for stable stochastic output in lesson set.seed(33) @@ -18,12 +21,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 +354,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..39e31528 100644 --- a/episodes/contact-matrices.Rmd +++ b/episodes/contact-matrices.Rmd @@ -34,12 +34,16 @@ 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(wpp2024) +library(tidyverse) ``` ## The contact matrix @@ -96,15 +100,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 `{socialmixr}`. ```{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 +206,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 @@ -315,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 ) diff --git a/episodes/disease-burden.Rmd b/episodes/disease-burden.Rmd index d394788e..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) ``` @@ -91,12 +92,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..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) ``` @@ -93,13 +94,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..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) ``` @@ -183,13 +184,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 @@ -301,6 +309,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 diff --git a/episodes/vaccine-comparisons.Rmd b/episodes/vaccine-comparisons.Rmd index f7a29d07..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) ``` @@ -74,12 +75,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 ) 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 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) 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",