Skip to content

Commit

Permalink
Update epi_slide_{sum,mean} examples w/ naming options, cleanup
Browse files Browse the repository at this point in the history
- Show naming options, including with multi-column selections when we have
  accommodating example data sets
- Select away the pre-existing 7d aggregations in the example data set
- Ungroup output
  • Loading branch information
brookslogan committed Nov 14, 2024
1 parent 79ed2f3 commit e5b66bd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 18 deletions.
49 changes: 40 additions & 9 deletions R/slide.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
#' },
#' .window_size = 7
#' ) %>%
#' ungroup() %>%
#' dplyr::select(geo_value, time_value, cases, cases_7sd, cases_7dav)
#'
#' # Use the geo_value or the ref_time_value in the slide computation
Expand Down Expand Up @@ -605,7 +606,8 @@ get_before_after_from_window <- function(window_size, align, time_type) {
#' # Compute a 7-day trailing average on cases.
#' cases_deaths_subset %>%
#' group_by(geo_value) %>%
#' epi_slide_opt(cases, .f = data.table::frollmean, .window_size = 7)
#' epi_slide_opt(cases, .f = data.table::frollmean, .window_size = 7) %>%
#' ungroup()
#'
#' # Same as above, but adjust `frollmean` settings for speed, accuracy, and
#' # to allow partially-missing windows.
Expand All @@ -615,7 +617,8 @@ get_before_after_from_window <- function(window_size, align, time_type) {
#' cases,
#' .f = data.table::frollmean, .window_size = 7,
#' algo = "exact", hasNA = TRUE, na.rm = TRUE
#' )
#' ) %>%
#' ungroup()
epi_slide_opt <- function(
.x, .col_names, .f, ...,
.window_size = NULL, .align = c("right", "center", "left"),
Expand Down Expand Up @@ -919,20 +922,34 @@ epi_slide_opt <- function(
#'
#' @export
#' @examples
#' # Compute a 7-day trailing average on cases.
#' cases_deaths_subset %>%
#' # Compute a 7-day trailing average of case rates.
#' covid_case_death_rates_extended %>%
#' group_by(geo_value) %>%
#' epi_slide_mean(cases, .window_size = 7)
#' epi_slide_mean(case_rate, .window_size = 7) %>%
#' ungroup()
#'
#' # Same as above, but adjust `frollmean` settings for speed, accuracy, and
#' # to allow partially-missing windows.
#' cases_deaths_subset %>%
#' covid_case_death_rates_extended %>%
#' group_by(geo_value) %>%
#' epi_slide_mean(
#' cases,
#' case_rate,
#' .window_size = 7,
#' na.rm = TRUE, algo = "exact", hasNA = TRUE
#' )
#' ) %>%
#' ungroup()
#'
#' # Compute a 7-day trailing average of case rates and death rates, with custom
#' # output column names:
#' covid_case_death_rates_extended %>%
#' group_by(geo_value) %>%
#' epi_slide_mean(c(case_rate, death_rate), .window_size = 7,
#' .new_col_names = c("smoothed_case_rate", "smoothed_death_rate")) %>%
#' ungroup()
#' covid_case_death_rates_extended %>%
#' group_by(geo_value) %>%
#' epi_slide_mean(c(case_rate, death_rate), .window_size = 7, .suffix = "_{.n}{.time_unit_abbr}_avg") %>%
#' ungroup()
epi_slide_mean <- function(
.x, .col_names, ...,
.window_size = NULL, .align = c("right", "center", "left"),
Expand Down Expand Up @@ -995,8 +1012,22 @@ epi_slide_mean <- function(
#' @examples
#' # Compute a 7-day trailing sum on cases.
#' cases_deaths_subset %>%
#' select(geo_value, time_value, cases) %>%
#' group_by(geo_value) %>%
#' epi_slide_sum(cases, .window_size = 7) %>%
#' ungroup()
#'
#' # Specify output column names and/or naming scheme:
#' cases_deaths_subset %>%
#' select(geo_value, time_value, cases) %>%
#' group_by(geo_value) %>%
#' epi_slide_sum(cases, .window_size = 7, .new_col_names = "case_sum") %>%
#' ungroup()
#' cases_deaths_subset %>%
#' select(geo_value, time_value, cases) %>%
#' group_by(geo_value) %>%
#' epi_slide_sum(cases, .window_size = 7)
#' epi_slide_sum(cases, .window_size = 7, .prefix = "sum_") %>%
#' ungroup()
epi_slide_sum <- function(
.x, .col_names, ...,
.window_size = NULL, .align = c("right", "center", "left"),
Expand Down
1 change: 1 addition & 0 deletions man/epi_slide.Rd

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

48 changes: 39 additions & 9 deletions man/epi_slide_opt.Rd

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

0 comments on commit e5b66bd

Please sign in to comment.