Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions R/fit_single_contact_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,11 @@
#' 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,
#' # optional arguments
#' school_demographics = conmat_original_school_demographics,
#' work_demographics = conmat_original_work_demographics
#' )
Expand Down
61 changes: 43 additions & 18 deletions R/get_polymod_contact_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@
#' number of participants in that row.
#' @examples
#' get_polymod_contact_data()
#' get_polymod_contact_data(setting = "home")
#' get_polymod_contact_data(countries = "Belgium")
#' get_polymod_contact_data(countries = c("Belgium", "Italy"))
#' get_polymod_contact_data(ages = 0:50)
#' get_polymod_contact_data(contact_age_imputation = "sample")
#' get_polymod_contact_data(contact_age_imputation = "mean")
#' get_polymod_contact_data(contact_age_imputation = "remove_participant")
#' get_polymod_contact_data(
#' setting = "home",
#' countries = c("Belgium", "Italy"),
#' ages = 0:50,
#' contact_age_imputation = "mean"
#' )
#' @export
get_polymod_contact_data <- function(
setting = c("all", "home", "work", "school", "other"),
Expand Down Expand Up @@ -110,17 +109,7 @@ get_polymod_contact_data <- function(
!missing_any_contact_setting
)

# get contacts by setting (keeping 0s, so we can record 0 contacts for some individuals)
contact_data_setting <- contact_data_filtered %>%
dplyr::mutate(
contacted = dplyr::case_when(
setting == "all" ~ 1L,
setting == "home" ~ cnt_home,
setting == "school" ~ cnt_school,
setting == "work" ~ cnt_work,
setting == "other" ~ pmax(cnt_transport, cnt_leisure, cnt_otherplace),
)
)
contact_data_setting <- add_contacted_setting(contact_data_filtered, setting)

# collapse down number of contacts per participant and contact age
contact_data_setting %>%
Expand Down Expand Up @@ -151,3 +140,39 @@ get_polymod_contact_data <- function(
.before = dplyr::everything()
)
}

# helper function to replace deprecated usage of case_when
add_contacted_setting <- function(
contact_data_filtered,
setting = c("all", "home", "school", "work", "other")
) {
setting <- rlang::arg_match(setting)

if (setting == "all") {
contact_data_setting <- contact_data_filtered |>
dplyr::mutate(
contacted = 1L
)
} else if (setting == "home") {
contact_data_setting <- contact_data_filtered |>
dplyr::mutate(
contacted = cnt_home
)
} else if (setting == "school") {
contact_data_setting <- contact_data_filtered |>
dplyr::mutate(
contacted = cnt_school
)
} else if (setting == "work") {
contact_data_setting <- contact_data_filtered |>
dplyr::mutate(
contacted = cnt_work
)
} else if (setting == "other") {
contact_data_setting <- contact_data_filtered |>
dplyr::mutate(
contacted = pmax(cnt_transport, cnt_leisure, cnt_otherplace)
)
}
contact_data_setting
}
16 changes: 2 additions & 14 deletions R/partial-prediction-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
#' @name partial-prediction
#' @examples
#' partials_home <- partial_effects(
#' polymod_setting_models$home,
#' ages = 1:99
#' )
#' # partial effects for all settings
#' partials_setting <- partial_effects(
#' polymod_setting_models,
#' polymod_setting_models$home, # Do for all models by omitting $home
#' ages = 1:99
#' )
#' @export
Expand Down Expand Up @@ -103,16 +98,9 @@ partial_effects.setting_contact_model <- function(model, ages, ...) {
#' the coefficients for that age bracket.
#' @name partial-prediction-sum
#' @examples
#' # Summed up partial effects (y-hat) for a single setting
#' partials_summed_home <- partial_effects_sum(
#' polymod_setting_models$home,
#' ages = 1:99
#' )
#'
#' autoplot(partials_summed_home)
#' # summed up partial effects (y-hat) for all settings
#' partials_summed_setting <- partial_effects_sum(
#' polymod_setting_models,
#' polymod_setting_models, # can also do for one setting with $home
#' ages = 1:99
#' )
#' autoplot(partials_summed_setting)
Expand Down
6 changes: 1 addition & 5 deletions man/fit_single_contact_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions man/get_polymod_contact_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions man/partial-prediction-sum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions man/partial-prediction.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.