Skip to content

Commit 8d5ce7f

Browse files
avallecamclaude
andcommitted
apply contactsurveys/socialmixr pattern to all downstream episodes
Replace socialmixr::polymod and socialmixr::get_survey() with contactsurveys::download_survey() + socialmixr::load_survey() using the Zenodo DOI. Add return_demography = TRUE and standardize object names (contacts_byage, contacts_byage_matrix) across: - simulating-transmission.Rmd - modelling-interventions.Rmd - compare-interventions.Rmd - disease-burden.Rmd - vaccine-comparisons.Rmd - template.Rmd Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent bfe07cc commit 8d5ce7f

6 files changed

Lines changed: 170 additions & 110 deletions

File tree

episodes/compare-interventions.Rmd

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,26 @@ library(epidemics)
1212
# hidden seed for stable stochastic output in lesson
1313
set.seed(33)
1414
15-
polymod <- socialmixr::polymod
16-
contact_data <- socialmixr::contact_matrix(
17-
polymod,
15+
survey_files <- contactsurveys::download_survey(
16+
survey = "https://doi.org/10.5281/zenodo.3874557",
17+
verbose = FALSE
18+
)
19+
survey_load <- socialmixr::load_survey(files = survey_files)
20+
21+
contacts_byage <- socialmixr::contact_matrix(
22+
survey = survey_load,
1823
countries = "United Kingdom",
1924
age_limits = c(0, 15, 65),
20-
symmetric = TRUE
25+
symmetric = TRUE,
26+
return_demography = TRUE
2127
)
2228
2329
# prepare contact matrix
24-
contact_matrix <- t(contact_data$matrix)
30+
contacts_byage_matrix <- t(contacts_byage$matrix)
2531
2632
# prepare the demography vector
27-
demography_vector <- contact_data$demography$population
28-
names(demography_vector) <- rownames(contact_matrix)
33+
demography_vector <- contacts_byage$demography$population
34+
names(demography_vector) <- rownames(contacts_byage_matrix)
2935
3036
# initial conditions: one in every 1 million is infected
3137
initial_i <- 1e-6
@@ -35,15 +41,15 @@ initial_conditions <- c(
3541
3642
# build for all age groups
3743
initial_conditions <- matrix(
38-
rep(initial_conditions, dim(contact_matrix)[1]),
44+
rep(initial_conditions, dim(contacts_byage_matrix)[1]),
3945
ncol = 5, byrow = TRUE
4046
)
41-
rownames(initial_conditions) <- rownames(contact_matrix)
47+
rownames(initial_conditions) <- rownames(contacts_byage_matrix)
4248
4349
# prepare the population to model as affected by the epidemic
4450
uk_population <- epidemics::population(
4551
name = "UK",
46-
contact_matrix = contact_matrix,
52+
contact_matrix = contacts_byage_matrix,
4753
demography_vector = demography_vector,
4854
initial_conditions = initial_conditions
4955
)
@@ -355,19 +361,25 @@ output <- epidemics::model_vacamole(
355361
1. Run the model
356362

357363
```{r}
358-
polymod <- socialmixr::polymod
359-
contact_data <- socialmixr::contact_matrix(
360-
survey = polymod,
364+
survey_files_2 <- contactsurveys::download_survey(
365+
survey = "https://doi.org/10.5281/zenodo.3874557",
366+
verbose = FALSE
367+
)
368+
survey_load_2 <- socialmixr::load_survey(files = survey_files_2)
369+
370+
contacts_byage_2 <- socialmixr::contact_matrix(
371+
survey = survey_load_2,
361372
countries = "United Kingdom",
362373
age_limits = c(0, 20, 40),
363-
symmetric = TRUE
374+
symmetric = TRUE,
375+
return_demography = TRUE
364376
)
365377
# prepare contact matrix
366-
contact_matrix <- t(contact_data$matrix)
378+
contacts_byage_matrix_2 <- t(contacts_byage_2$matrix)
367379
368380
# extract demography vector
369-
demography_vector <- contact_data$demography$population
370-
names(demography_vector) <- rownames(contact_matrix)
381+
demography_vector <- contacts_byage_2$demography$population
382+
names(demography_vector) <- rownames(contacts_byage_matrix_2)
371383
372384
# prepare initial conditions
373385
initial_i <- 1e-6
@@ -385,12 +397,12 @@ initial_conditions_vacamole <- rbind(
385397
initial_conditions_vacamole,
386398
initial_conditions_vacamole
387399
)
388-
rownames(initial_conditions_vacamole) <- rownames(contact_matrix)
400+
rownames(initial_conditions_vacamole) <- rownames(contacts_byage_matrix_2)
389401
390402
# prepare population object
391403
uk_population_vacamole <- epidemics::population(
392404
name = "UK",
393-
contact_matrix = contact_matrix,
405+
contact_matrix = contacts_byage_matrix_2,
394406
demography_vector = demography_vector,
395407
initial_conditions = initial_conditions_vacamole
396408
)

episodes/disease-burden.Rmd

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ In this tutorial, we'll focus on the separated approach, where we first run an e
4747
```{r, warning = FALSE, message = FALSE}
4848
library(epiparameter)
4949
library(epidemics)
50+
library(contactsurveys)
5051
library(socialmixr)
5152
library(tidyverse)
5253
```
@@ -83,21 +84,27 @@ We'll use `{epiparameter}` to define these delay distributions. The Gamma distri
8384
:::::::::::::::: instructor
8485

8586
```{r, echo = TRUE, message = FALSE}
86-
# load contact and population data from socialmixr::polymod
87-
polymod <- socialmixr::polymod
88-
contact_data <- socialmixr::contact_matrix(
89-
polymod,
87+
# download and load contact and population data from Zenodo
88+
survey_files <- contactsurveys::download_survey(
89+
survey = "https://doi.org/10.5281/zenodo.3874557",
90+
verbose = FALSE
91+
)
92+
survey_load <- socialmixr::load_survey(files = survey_files)
93+
94+
contacts_byage <- socialmixr::contact_matrix(
95+
survey = survey_load,
9096
countries = "United Kingdom",
9197
age_limits = c(0, 20, 40),
92-
symmetric = TRUE
98+
symmetric = TRUE,
99+
return_demography = TRUE
93100
)
94101
95102
# prepare contact matrix
96-
contact_matrix <- t(contact_data$matrix)
103+
contacts_byage_matrix <- t(contacts_byage$matrix)
97104
98105
# prepare the demography vector
99-
demography_vector <- contact_data$demography$population
100-
names(demography_vector) <- rownames(contact_matrix)
106+
demography_vector <- contacts_byage$demography$population
107+
names(demography_vector) <- rownames(contacts_byage_matrix)
101108
102109
# initial conditions: one in every 1 million is infected
103110
initial_i <- 1e-6
@@ -115,12 +122,12 @@ initial_conditions <- rbind(
115122
initial_conditions_free,
116123
initial_conditions_free
117124
)
118-
rownames(initial_conditions) <- rownames(contact_matrix)
125+
rownames(initial_conditions) <- rownames(contacts_byage_matrix)
119126
120127
# prepare the population to model as affected by the epidemic
121128
uk_population <- epidemics::population(
122129
name = "UK",
123-
contact_matrix = contact_matrix,
130+
contact_matrix = contacts_byage_matrix,
124131
demography_vector = demography_vector,
125132
initial_conditions = initial_conditions
126133
)

episodes/modelling-interventions.Rmd

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ In this tutorial, we will learn how to use `{epidemics}` to model interventions
5757

5858
```{r,message=FALSE,warning=FALSE}
5959
library(epidemics)
60+
library(contactsurveys)
6061
library(socialmixr)
6162
library(tidyverse)
6263
```
@@ -85,23 +86,28 @@ We will investigate the effect of interventions on a COVID-19 outbreak using an
8586
The SEIR model divides the population into four compartments: Susceptible (S), Exposed (E), Infectious (I), and Recovered (R). We will set the following parameters for our model: $R_0 = 2.7$ (basic reproduction number), latent period or pre-infectious period $= 4$ days, and the infectious period $= 5.5$ days (parameters adapted from [Davies et al. (2020)](https://doi.org/10.1016/S2468-2667(20)30133-X)). We adopt a contact matrix with age bins 0-18, 18-65, 65 years and older using `{socialmixr}`, and assume that one in every 1 million individuals in each age group is infectious at the start of the epidemic.
8687

8788
```{r model_setup, echo = TRUE, message = FALSE}
88-
# load survey data
89-
survey_data <- socialmixr::polymod
89+
# download and load survey data
90+
survey_files <- contactsurveys::download_survey(
91+
survey = "https://doi.org/10.5281/zenodo.3874557",
92+
verbose = FALSE
93+
)
94+
survey_load <- socialmixr::load_survey(files = survey_files)
9095
9196
# generate contact matrix
92-
cm_results <- socialmixr::contact_matrix(
93-
survey = survey_data,
97+
contacts_byage <- socialmixr::contact_matrix(
98+
survey = survey_load,
9499
countries = "United Kingdom",
95100
age_limits = c(0, 15, 65),
96-
symmetric = TRUE
101+
symmetric = TRUE,
102+
return_demography = TRUE
97103
)
98104
99105
# transpose contact matrix
100-
cm_matrix <- t(cm_results$matrix)
106+
contacts_byage_matrix <- t(contacts_byage$matrix)
101107
102108
# prepare the demography vector
103-
demography_vector <- cm_results$demography$population
104-
names(demography_vector) <- rownames(cm_matrix)
109+
demography_vector <- contacts_byage$demography$population
110+
names(demography_vector) <- rownames(contacts_byage_matrix)
105111
106112
# initial conditions: one in every 1 million is infected
107113
initial_i <- 1e-6
@@ -119,12 +125,12 @@ initial_conditions <- base::rbind(
119125
initial_conditions,
120126
initial_conditions
121127
)
122-
rownames(initial_conditions) <- rownames(cm_matrix)
128+
rownames(initial_conditions) <- rownames(contacts_byage_matrix)
123129
124130
# prepare the population to model as affected by the epidemic
125131
uk_population <- epidemics::population(
126132
name = "UK",
127-
contact_matrix = cm_matrix,
133+
contact_matrix = contacts_byage_matrix,
128134
demography_vector = demography_vector,
129135
initial_conditions = initial_conditions
130136
)
@@ -176,7 +182,7 @@ The first NPI we will consider is the effect of school closures on reducing the
176182
To include an intervention in our model we must create an `intervention` object. The inputs are the name of the intervention (`name`), the type of intervention (`contacts` or `rate`), the start time (`time_begin`), the end time (`time_end`) and the reduction (`reduction`). The values of the reduction matrix are specified in the same order as the age groups in the contact matrix.
177183

178184
```{r}
179-
rownames(cm_matrix)
185+
rownames(contacts_byage_matrix)
180186
```
181187

182188
Therefore, we specify `reduction = matrix(c(0.5, 0.01, 0.01))`. We assume that the school closures start on day 50 and continue to be in place for a further 100 days. Therefore our intervention object is:
@@ -433,8 +439,8 @@ Here we will assume all age groups are vaccinated at the same rate 0.01 and that
433439
# prepare a vaccination object
434440
vaccinate <- epidemics::vaccination(
435441
name = "vaccinate all",
436-
time_begin = matrix(40, nrow(cm_matrix)),
437-
time_end = matrix(40 + 150, nrow(cm_matrix)),
442+
time_begin = matrix(40, nrow(contacts_byage_matrix)),
443+
time_end = matrix(40 + 150, nrow(contacts_byage_matrix)),
438444
nu = matrix(c(0.01, 0.01, 0.01))
439445
)
440446
```

episodes/simulating-transmission.Rmd

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ In this tutorial we are going to learn how to use the `{epidemics}` package to s
6363

6464
```{r,message=FALSE,warning=FALSE}
6565
library(epidemics)
66+
library(contactsurveys)
6667
library(socialmixr)
6768
library(tidyverse)
6869
```
@@ -185,7 +186,7 @@ Using the R package `socialmixr`, obtain the contact matrix for the United Kingd
185186
+ age between 20 and 40,
186187
+ 40 years and over.
187188

188-
Use the survey available at `socialmixr::polymod`
189+
Use the POLYMOD survey available at `https://doi.org/10.5281/zenodo.3874557`
189190

190191
:::::::::::::::: hint
191192

@@ -196,30 +197,35 @@ Use the survey available at `socialmixr::polymod`
196197
:::::::::::::::::::::::: solution
197198

198199
```{r polymod_uk, echo = TRUE, message = FALSE}
199-
# Access the contact survey data
200-
polymod <- socialmixr::polymod
200+
# Download and load the contact survey data
201+
survey_files <- contactsurveys::download_survey(
202+
survey = "https://doi.org/10.5281/zenodo.3874557",
203+
verbose = FALSE
204+
)
205+
survey_load <- socialmixr::load_survey(files = survey_files)
201206
202207
# Generate the contact matrix
203-
contact_data <- socialmixr::contact_matrix(
204-
polymod,
208+
contacts_byage <- socialmixr::contact_matrix(
209+
survey = survey_load,
205210
countries = "United Kingdom",
206211
age_limits = c(0, 20, 40),
207-
symmetric = TRUE
212+
symmetric = TRUE,
213+
return_demography = TRUE
208214
)
209215
210216
# prepare contact matrix
211-
socialcontact_matrix <- t(contact_data$matrix)
217+
contacts_byage_matrix <- t(contacts_byage$matrix)
212218
213219
# print
214-
socialcontact_matrix
220+
contacts_byage_matrix
215221
```
216222

217223
Remember that the matrix satisfies the `symmetric = TRUE` condition at the level of total number of contacts.
218224

219-
The total number of contacts between groups $i$ and $j$ is calculated as the mean number of contacts (`contact_data$matrix`) multiplied by the number of individuals in group $i$ (`contact_data$demography$population`)
225+
The total number of contacts between groups $i$ and $j$ is calculated as the mean number of contacts (`contacts_byage$matrix`) multiplied by the number of individuals in group $i$ (`contacts_byage$demography$population`)
220226

221227
```{r}
222-
contact_data$matrix * contact_data$demography$population
228+
contacts_byage$matrix * contacts_byage$demography$population
223229
```
224230

225231
:::::::::::::::::::::::::::::::::
@@ -284,22 +290,22 @@ initial_conditions <- rbind(
284290
)
285291
286292
# use contact matrix to assign age group names
287-
rownames(initial_conditions) <- rownames(socialcontact_matrix)
293+
rownames(initial_conditions) <- rownames(contacts_byage_matrix)
288294
initial_conditions
289295
```
290296

291297

292298

293299

294300
### 3. Population structure
295-
The population object requires a vector containing the demographic structure of the population. The demographic vector must be a named vector containing the number of individuals in each age group of our given population. In this example, we can extract the demographic information from the `contact_data` object that we obtained using the `socialmixr` package.
301+
The population object requires a vector containing the demographic structure of the population. The demographic vector must be a named vector containing the number of individuals in each age group of our given population. In this example, we can extract the demographic information from the `contacts_byage` object that we obtained using the `socialmixr` package.
296302

297303
```{r demography}
298304
# extract the demography vector
299-
demography_vector <- contact_data$demography$population
305+
demography_vector <- contacts_byage$demography$population
300306
301307
# use contact matrix to assign age group names
302-
names(demography_vector) <- rownames(socialcontact_matrix)
308+
names(demography_vector) <- rownames(contacts_byage_matrix)
303309
demography_vector
304310
```
305311

@@ -310,7 +316,7 @@ library(epidemics)
310316
311317
uk_population <- epidemics::population(
312318
name = "UK",
313-
contact_matrix = socialcontact_matrix,
319+
contact_matrix = contacts_byage_matrix,
314320
demography_vector = demography_vector,
315321
initial_conditions = initial_conditions
316322
)

0 commit comments

Comments
 (0)