Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions episodes/contact-matrices.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ polymod <- socialmixr::polymod
Then we can obtain the contact matrix for the age categories we want by specifying `age.limits`.

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

::::::::::::::::::::::::::::::::::::::::::::::::

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()`
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()`

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

:::::::::::::::::: spoiler

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:

```r
library(socialmixr)
library(tidyverse)

# Get URL for Zambia contact survey data from {socialmixr}
socialmixr::list_surveys() %>%
dplyr::filter(stringr::str_detect(title, "Zambia")) %>%
dplyr::pull(url)
```

::::::::::::::::::


::::::::::::::::::::::::::::::::::::: challenge

## Zambia contact matrix

After downloading the survey, generate a symmetric contact matrix for Zambia using the following age bins:
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}:

+ Get access to the survey from Zambia.
+ Generate a symmetric contact matrix for Zambia using the following age bins:

+ [0,20)
+ 20+

+ [0,20)
+ 20+
+ Get access to the vector of `population` size per age bin from the `demography` dataset inside the contact matrix output.

::::::::::::::::::::: hint

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.

```{r}
# Inspect the countries within the survey object
levels(zambia_sa_survey$participants$country)
```

Similar to the code above, to access vector values within a dataframe, you can use the dollar-sign operator: `$`

:::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::: instructor

```{r polymod_poland}
contact_data_zambia <- contact_matrix(
# Generate the contact matrix for Zambia only
contact_data_zambia <- socialmixr::contact_matrix(
survey = zambia_sa_survey,
countries = "Zambia", # key argument
age.limits = c(0, 20),
symmetric = TRUE
)

# Print the contact matrix for Zambia only
contact_data_zambia

# Print the vector of population size for {epidemics}
contact_data_zambia$demography$population
```
:::::::::::::::::::::::::::::::::

Expand Down Expand Up @@ -271,7 +314,7 @@ max(eigen(basic_reproduction * normalised_matrix)$values)
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:

```{r, message = FALSE}
contact_data_split <- contact_matrix(
contact_data_split <- socialmixr::contact_matrix(
survey = polymod,
countries = "United Kingdom",
age.limits = c(0, 20, 40),
Expand Down
Loading