You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
update contact-matrices.Rmd with contactsurveys/socialmixr pattern
- Add library(contactsurveys) and replace socialmixr::polymod loading
with contactsurveys::download_survey() + socialmixr::load_survey()
using the Zenodo DOI for POLYMOD and Zambia surveys
- Add return_demography = TRUE to all contact_matrix() calls
- Standardize object names: contacts_byage, contacts_byage_matrix
- Add callout warning users to always specify countries = argument
- Add text explaining return_demography requirement for {epidemics}
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: episodes/contact-matrices.Rmd
+47-24Lines changed: 47 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,7 @@ Some groups of individuals have more contacts than others; the average schoolchi
38
38
39
39
40
40
```{r,message=FALSE,warning=FALSE}
41
+
library(contactsurveys)
41
42
library(socialmixr)
42
43
```
43
44
@@ -80,29 +81,47 @@ For a contact matrix with rows $i$ and columns $j$:
80
81
81
82
Contact matrices are commonly estimated from studies that use diaries to record interactions. For example, the POLYMOD survey measured contact patterns in 8 European countries using data on the location and duration of contacts reported by the study participants [(Mossong et al. 2008)](https://doi.org/10.1371/journal.pmed.0050074).
82
83
83
-
The R package `{socialmixr}` contains functions which can estimate contact matrices from POLYMOD and other surveys. We can load the POLYMOD survey data:
84
+
The R package `{socialmixr}` contains functions which can estimate contact matrices from POLYMOD and other surveys. We can download and load the POLYMOD survey data directly from Zenodo using `{contactsurveys}` and `{socialmixr}`:
A single survey file can contain data from multiple countries. You can inspect the available countries with:
85
99
86
-
```{rpolymod_, echo = TRUE}
87
-
polymod <- socialmixr::polymod
100
+
```{rpolymod_countries, echo = TRUE}
101
+
levels(survey_load$participants$country)
88
102
```
89
103
90
-
Then we can obtain the contact matrix for the age categories we want by specifying `age_limits`.
104
+
Always pass the `countries =` argument to `contact_matrix()` to make sure you use data from the intended country only.
105
+
106
+
::::::::::::::::::::::::::::::::::::::::::::::::
107
+
108
+
Then we can obtain the contact matrix for the age categories we want by specifying `age_limits`. We also add `return_demography = TRUE` to include demographic information in the output, which is required when using the contact matrix with `{epidemics}`.
91
109
92
110
```{r polymod_uk, echo = TRUE}
93
-
contact_data <- socialmixr::contact_matrix(
94
-
survey = polymod,
111
+
contacts_byage <- socialmixr::contact_matrix(
112
+
survey = survey_load,
95
113
countries = "United Kingdom",
96
114
age_limits = c(0, 20, 40),
97
-
symmetric = TRUE
115
+
symmetric = TRUE,
116
+
return_demography = TRUE
98
117
)
99
-
contact_data
118
+
contacts_byage
100
119
```
101
120
102
121
103
122
104
-
**Note: although the contact matrix `contact_data$matrix` is not itself mathematically symmetric, it satisfies the condition that the total number of contacts of one group with another is the same as the reverse. In other words:
**Note: although the contact matrix `contacts_byage$matrix` is not itself mathematically symmetric, it satisfies the condition that the total number of contacts of one group with another is the same as the reverse. In other words:
For the mathematical explanation see [the corresponding section in the socialmixr documentation](https://epiforecasts.io/socialmixr/articles/socialmixr.html#symmetric-contact-matrices).**
107
126
108
127
@@ -122,13 +141,16 @@ If `symmetric` is set to TRUE, the `contact_matrix()` function will internally u
122
141
123
142
::::::::::::::::::::::::::::::::::::::::::::::::
124
143
125
-
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()`
144
+
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 from Zenodo and load it, we use `contactsurveys::download_survey()` followed by `socialmixr::load_survey()`:
126
145
127
146
```{r, message = FALSE, warning = FALSE}
128
-
# Access the contact survey data from Zenodo
129
-
zambia_sa_survey <- socialmixr::get_survey(
130
-
"https://doi.org/10.5281/zenodo.3874675"
147
+
# Download and load the contact survey data for Zambia from Zenodo
# Print the vector of population size for {epidemics}
195
-
contact_data_zambia$demography$population
218
+
contacts_byage_zambia$demography$population
196
219
```
197
220
:::::::::::::::::::::::::::::::::
198
221
@@ -290,12 +313,12 @@ When simulating an epidemic, we often want to ensure that the average number of
290
313
291
314
Rather than just using the raw number of contacts, we can instead normalise the contact matrix to make it easier to work in terms of $R_0$. In particular, we normalise the matrix by scaling it so that if we were to calculate the average number of secondary cases based on this normalised matrix, the result would be 1 (in mathematical terms, we are scaling the matrix so the largest eigenvalue is 1). This transformation scales the entries but preserves their relative values.
292
315
293
-
In the case of the above model, we want to define $\beta C_{i,j}$ so that the model has a specified valued of $R_0$. If the entry of the contact matrix $C[i,j]$ represents the contacts of population $i$ with $j$, it is equivalent to `contact_data$matrix[i,j]`, and the maximum eigenvalue of this matrix represents the typical magnitude of contacts, not typical magnitude of transmission. We must therefore normalise the matrix $C$ so the maximum eigenvalue is one; we call this matrix $C_{normalised}$. Because the rate of recovery is $\gamma$, individuals will be infectious on average for $1/\gamma$ days. So $\beta$ as a model input is calculated from $R_0$, the scaling factor and the value of $\gamma$ (i.e. mathematically we use the fact that the dominant eigenvalue of the matrix $R_0 \times C_{normalised}$ is equal to $\beta / \gamma$).
316
+
In the case of the above model, we want to define $\beta C_{i,j}$ so that the model has a specified valued of $R_0$. If the entry of the contact matrix $C[i,j]$ represents the contacts of population $i$ with $j$, it is equivalent to `contacts_byage$matrix[i,j]`, and the maximum eigenvalue of this matrix represents the typical magnitude of contacts, not typical magnitude of transmission. We must therefore normalise the matrix $C$ so the maximum eigenvalue is one; we call this matrix $C_{normalised}$. Because the rate of recovery is $\gamma$, individuals will be infectious on average for $1/\gamma$ days. So $\beta$ as a model input is calculated from $R_0$, the scaling factor and the value of $\gamma$ (i.e. mathematically we use the fact that the dominant eigenvalue of the matrix $R_0 \times C_{normalised}$ is equal to $\beta / \gamma$).
0 commit comments