Skip to content

Commit 955df8f

Browse files
authored
Merge pull request #208 from idem-lab/fix-case-when-issue-i207
Fix case when issue i207
2 parents 3ccda97 + 1c05971 commit 955df8f

7 files changed

Lines changed: 55 additions & 63 deletions

R/fit_single_contact_model.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,11 @@
131131
#' age_from <= 20
132132
#' )
133133
#'
134-
#' my_mod <- fit_single_contact_model(
135-
#' contact_data = example_contact_20,
136-
#' population = example_population
137-
#' )
138-
#'
139134
#' # you can specify your own population data for school and work demographics
140135
#' my_mod_diff_data <- fit_single_contact_model(
141136
#' contact_data = example_contact_20,
142137
#' population = example_population,
138+
#' # optional arguments
143139
#' school_demographics = conmat_original_school_demographics,
144140
#' work_demographics = conmat_original_work_demographics
145141
#' )

R/get_polymod_contact_data.R

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
#' number of participants in that row.
3030
#' @examples
3131
#' get_polymod_contact_data()
32-
#' get_polymod_contact_data(setting = "home")
33-
#' get_polymod_contact_data(countries = "Belgium")
34-
#' get_polymod_contact_data(countries = c("Belgium", "Italy"))
35-
#' get_polymod_contact_data(ages = 0:50)
36-
#' get_polymod_contact_data(contact_age_imputation = "sample")
37-
#' get_polymod_contact_data(contact_age_imputation = "mean")
38-
#' get_polymod_contact_data(contact_age_imputation = "remove_participant")
32+
#' get_polymod_contact_data(
33+
#' setting = "home",
34+
#' countries = c("Belgium", "Italy"),
35+
#' ages = 0:50,
36+
#' contact_age_imputation = "mean"
37+
#' )
3938
#' @export
4039
get_polymod_contact_data <- function(
4140
setting = c("all", "home", "work", "school", "other"),
@@ -110,17 +109,7 @@ get_polymod_contact_data <- function(
110109
!missing_any_contact_setting
111110
)
112111

113-
# get contacts by setting (keeping 0s, so we can record 0 contacts for some individuals)
114-
contact_data_setting <- contact_data_filtered %>%
115-
dplyr::mutate(
116-
contacted = dplyr::case_when(
117-
setting == "all" ~ 1L,
118-
setting == "home" ~ cnt_home,
119-
setting == "school" ~ cnt_school,
120-
setting == "work" ~ cnt_work,
121-
setting == "other" ~ pmax(cnt_transport, cnt_leisure, cnt_otherplace),
122-
)
123-
)
112+
contact_data_setting <- add_contacted_setting(contact_data_filtered, setting)
124113

125114
# collapse down number of contacts per participant and contact age
126115
contact_data_setting %>%
@@ -151,3 +140,39 @@ get_polymod_contact_data <- function(
151140
.before = dplyr::everything()
152141
)
153142
}
143+
144+
# helper function to replace deprecated usage of case_when
145+
add_contacted_setting <- function(
146+
contact_data_filtered,
147+
setting = c("all", "home", "school", "work", "other")
148+
) {
149+
setting <- rlang::arg_match(setting)
150+
151+
if (setting == "all") {
152+
contact_data_setting <- contact_data_filtered |>
153+
dplyr::mutate(
154+
contacted = 1L
155+
)
156+
} else if (setting == "home") {
157+
contact_data_setting <- contact_data_filtered |>
158+
dplyr::mutate(
159+
contacted = cnt_home
160+
)
161+
} else if (setting == "school") {
162+
contact_data_setting <- contact_data_filtered |>
163+
dplyr::mutate(
164+
contacted = cnt_school
165+
)
166+
} else if (setting == "work") {
167+
contact_data_setting <- contact_data_filtered |>
168+
dplyr::mutate(
169+
contacted = cnt_work
170+
)
171+
} else if (setting == "other") {
172+
contact_data_setting <- contact_data_filtered |>
173+
dplyr::mutate(
174+
contacted = pmax(cnt_transport, cnt_leisure, cnt_otherplace)
175+
)
176+
}
177+
contact_data_setting
178+
}

R/partial-prediction-helpers.R

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@
2424
#' @name partial-prediction
2525
#' @examples
2626
#' partials_home <- partial_effects(
27-
#' polymod_setting_models$home,
28-
#' ages = 1:99
29-
#' )
30-
#' # partial effects for all settings
31-
#' partials_setting <- partial_effects(
32-
#' polymod_setting_models,
27+
#' polymod_setting_models$home, # Do for all models by omitting $home
3328
#' ages = 1:99
3429
#' )
3530
#' @export
@@ -103,16 +98,9 @@ partial_effects.setting_contact_model <- function(model, ages, ...) {
10398
#' the coefficients for that age bracket.
10499
#' @name partial-prediction-sum
105100
#' @examples
106-
#' # Summed up partial effects (y-hat) for a single setting
107-
#' partials_summed_home <- partial_effects_sum(
108-
#' polymod_setting_models$home,
109-
#' ages = 1:99
110-
#' )
111-
#'
112-
#' autoplot(partials_summed_home)
113101
#' # summed up partial effects (y-hat) for all settings
114102
#' partials_summed_setting <- partial_effects_sum(
115-
#' polymod_setting_models,
103+
#' polymod_setting_models, # can also do for one setting with $home
116104
#' ages = 1:99
117105
#' )
118106
#' autoplot(partials_summed_setting)

man/fit_single_contact_model.Rd

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_polymod_contact_data.Rd

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/partial-prediction-sum.Rd

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/partial-prediction.Rd

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)