Skip to content

Commit d0cf210

Browse files
authored
add elements to access contact data
1 parent f81d2b2 commit d0cf210

1 file changed

Lines changed: 49 additions & 8 deletions

File tree

episodes/contact-matrices.Rmd

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ polymod <- socialmixr::polymod
8989
Then we can obtain the contact matrix for the age categories we want by specifying `age.limits`.
9090

9191
```{r polymod_uk, echo = TRUE}
92-
contact_data <- contact_matrix(
92+
contact_data <- socialmixr::contact_matrix(
9393
survey = polymod,
9494
countries = "United Kingdom",
9595
age.limits = c(0, 20, 40),
@@ -121,34 +121,75 @@ If `symmetric` is set to TRUE, the `contact_matrix()` function will internally u
121121

122122
::::::::::::::::::::::::::::::::::::::::::::::::
123123

124-
The example above uses the POLYMOD survey. There are a number of surveys available in `socialmixr`, to list the available surveys use `list_surveys()`. To download a survey, we can use `get_survey()`
124+
The example above uses the POLYMOD survey. There are a number of surveys available in `socialmixr`. To list the available surveys, use `socialmixr::list_surveys()`. To download a survey, we can use `socialmixr::get_survey()`
125125

126126
```{r, message = FALSE, warning = FALSE}
127-
zambia_sa_survey <- get_survey("https://doi.org/10.5281/zenodo.3874675")
127+
# Access the contact survey data from Zenodo
128+
zambia_sa_survey <- socialmixr::get_survey("https://doi.org/10.5281/zenodo.3874675")
128129
```
129130

131+
:::::::::::::::::: spoiler
132+
133+
You can explore all the available surveys from the Zenodo repository at <https://zenodo.org/communities/social_contact_data/>. If you are interested in accessing to a specific URL within R, you can try:
134+
135+
```r
136+
library(socialmixr)
137+
library(tidyverse)
138+
139+
# Get URL for Zambia contact survey data from {socialmixr}
140+
socialmixr::list_surveys() %>%
141+
dplyr::filter(stringr::str_detect(title, "Zambia")) %>%
142+
dplyr::pull(url)
143+
```
144+
145+
::::::::::::::::::
130146

131147

132148
::::::::::::::::::::::::::::::::::::: challenge
133149

134150
## Zambia contact matrix
135151

136-
After downloading the survey, generate a symmetric contact matrix for Zambia using the following age bins:
152+
The R package {socialmixr} contains functions which can estimate contact matrices from POLYMOD and other surveys. Outputs include demographic information like population size and number of participants in the study. Using {socialmixr}:
153+
154+
+ Get access to the survey from Zambia.
155+
+ Generate a symmetric contact matrix for Zambia using the following age bins:
156+
157+
+ [0,20)
158+
+ 20+
159+
160+
+ Get access to the vector of `population` size per age bin from the `demography` dataset inside the contact matrix output.
137161

138-
+ [0,20)
139-
+ 20+
162+
::::::::::::::::::::: hint
163+
164+
The survey object `zambia_sa_survey` contains data from two countries. If you need to estimate the social contact matrix from data of the specific country of Zambia, identify what argument in `socialmixr::contact_matrix()` you need for this.
165+
166+
```{r}
167+
# Inspect the countries within the survey object
168+
levels(zambia_sa_survey$participants$country)
169+
```
170+
171+
Similar to the code above, to access vector values within a dataframe, you can use the dollar-sign operator: `$`
172+
173+
:::::::::::::::::::::
140174

141175
::::::::::::::::::::::::::::::::::::::::::::::::
142176

143177
:::::::::::::::::::::::: instructor
144178

145179
```{r polymod_poland}
146-
contact_data_zambia <- contact_matrix(
180+
# Generate the contact matrix for Zambia only
181+
contact_data_zambia <- socialmixr::contact_matrix(
147182
survey = zambia_sa_survey,
183+
countries = "Zambia", # key argument
148184
age.limits = c(0, 20),
149185
symmetric = TRUE
150186
)
187+
188+
# Print the contact matrix for Zambia only
151189
contact_data_zambia
190+
191+
# Print the vector of population size for {epidemics}
192+
contact_data_zambia$demography$population
152193
```
153194
:::::::::::::::::::::::::::::::::
154195

@@ -271,7 +312,7 @@ max(eigen(basic_reproduction * normalised_matrix)$values)
271312
Normalisation can be performed by the function `contact_matrix()` in `{socialmixr}`. To obtain the normalised matrix we must specify that we want to split out the different components of the contact matrix using the argument `split = TRUE`. Then we can obtain the normalised matrix as follows:
272313

273314
```{r, message = FALSE}
274-
contact_data_split <- contact_matrix(
315+
contact_data_split <- socialmixr::contact_matrix(
275316
survey = polymod,
276317
countries = "United Kingdom",
277318
age.limits = c(0, 20, 40),

0 commit comments

Comments
 (0)