View source: R/fit_single_contact_model.R
fit_single_contact_model | R Documentation |
This is the workhorse of the conmat
package, and is typically
used inside fit_setting_contacts()
. It predicts the contact rate between
all age bands (the contact rate between ages 0 and 1, 0 and 2, 0 and 3,
and so on), for a specified setting, with specific terms being added for
given settings. See "details" for further information.
fit_single_contact_model(
contact_data,
population,
symmetrical = TRUE,
school_demographics = NULL,
work_demographics = NULL
)
contact_data |
dataset with columns |
population |
|
symmetrical |
whether to enforce symmetrical terms in the model.
Defaults to TRUE. See |
school_demographics |
(optional) defaults to census average proportion
at school. You can provide a dataset with columns, "age" (numeric), and
"school_fraction" (0-1), if you would like to specify these
details. See |
work_demographics |
(optional) defaults to census average proportion
employed. You can provide a dataset with columns, "age" (numeric), and
"work_fraction", if you would like to specify these details. See
|
The model fit is a Generalised Additive Model (GAM). We provide two "modes" for model fitting. Either using "symmetric" or "non-symmetric" model predictor terms with the logical variance "symmetrical", which is set to TRUE by default. We recommend using the "symmetrical" terms as it reflects the fact that contacts are symmetric - person A having contact with person B means person B has had contact with person A. We've included a variety of terms to account for assortativity with age, where people of similar ages have more contact with each other. And included terms to account for intergenerational contact patterns, where parents and grandparents will interact with their children and grand children. These terms are fit with a smoothing function. Specifically, the relevant code looks like this:
# abs(age_from - age_to) s(gam_age_offdiag) + # abs(age_from - age_to)^2 s(gam_age_offdiag_2) + # abs(age_from * age_to) s(gam_age_diag_prod) + # abs(age_from + age_to) s(gam_age_diag_sum) + # pmax(age_from, age_to) s(gam_age_pmax) + # pmin(age_from, age_to) s(gam_age_pmin)
We also include predictors for the probability of attending school, and
attending work. These are computed as the probability that a person goes
to the same school/work, proportional to the increase in contacts due to
attendance. These terms are calculated from estimated proportion of
people in age groups attending school and work. See
add_modelling_features()
for more details.
Finally, we include two offset terms so that we estimate the contact rate,
that is the contacts per capita, instead of the number of contacts. These
offset terms are log(contactable_population)
, and
log(contactable_population_school)
when the model is fit to a school
setting. The contactable population is estimated as the interpolated
1 year ages from the data. For schools this is the contactable population
weighted by the proportion of the population attending school.
This leaves us with a model that looks like so:
mgcv::bam( formula = contacts ~ # abs(age_from - age_to) s(gam_age_offdiag) + # abs(age_from - age_to)^2 s(gam_age_offdiag_2) + # abs(age_from * age_to) s(gam_age_diag_prod) + # abs(age_from + age_to) s(gam_age_diag_sum) + # pmax(age_from, age_to) s(gam_age_pmax) + # pmin(age_from, age_to) s(gam_age_pmin) + school_probability + work_probability + offset(log_contactable_population) + # or for school settings # offset(log_contactable_population_school) family = stats::poisson, offset = log(participants), data = population_data )
But if the term symmetrical = FALSE
is used, you get:
mgcv::bam( formula = contacts ~ s(age_to) + s(age_from) + s(abs(age_from - age_to)) + s(abs(age_from - age_to), age_from) + school_probability + work_probability + offset(log_contactable_population) + # or for school settings # offset(log_contactable_population_school) family = stats::poisson, offset = log(participants), data = population_data )
single model
example_contact <- get_polymod_contact_data(setting = "home")
example_contact
example_population <- get_polymod_population()
library(dplyr)
example_contact_20 <- example_contact %>%
filter(
age_to <= 20,
age_from <= 20
)
my_mod <- fit_single_contact_model(
contact_data = example_contact_20,
population = example_population
)
# you can specify your own population data for school and work demographics
my_mod_diff_data <- fit_single_contact_model(
contact_data = example_contact_20,
population = example_population,
school_demographics = conmat_original_school_demographics,
work_demographics = conmat_original_work_demographics
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.