From f7070447d36813ac27af4f79877b9fc30fb53698 Mon Sep 17 00:00:00 2001 From: mrcaseb Date: Fri, 20 Oct 2023 22:52:30 +0200 Subject: [PATCH] Correctly aggregate conversion rates --- R/calculate_series_conversion_rates.R | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/R/calculate_series_conversion_rates.R b/R/calculate_series_conversion_rates.R index b4109057..78fb0b4c 100644 --- a/R/calculate_series_conversion_rates.R +++ b/R/calculate_series_conversion_rates.R @@ -83,8 +83,13 @@ #' } calculate_series_conversion_rates <- function(pbp, weekly = FALSE){ - # For tests - # pbp <- nflreadr::load_pbp() + + if (isTRUE(weekly)){ + grp <- c("season", "team", "week") + } else if (isFALSE(weekly)){ + grp <- c("season", "team") + } + grp_vars <- lapply(grp, as.symbol) # Offense ----------------------------------------------------------------- @@ -103,7 +108,7 @@ calculate_series_conversion_rates <- function(pbp, ) offense <- off_series %>% - dplyr::group_by(.data$season, .data$team, .data$week) %>% + dplyr::group_by(!!!grp_vars) %>% dplyr::summarise( off_n = dplyr::n(), off_scr = mean(.data$conversion), @@ -138,7 +143,7 @@ calculate_series_conversion_rates <- function(pbp, ) defense <- def_series %>% - dplyr::group_by(.data$season, .data$team, .data$week) %>% + dplyr::group_by(!!!grp_vars) %>% dplyr::summarise( def_n = dplyr::n(), def_scr = mean(.data$conversion), @@ -159,19 +164,7 @@ calculate_series_conversion_rates <- function(pbp, # Offense + Defense ------------------------------------------------------- - combined <- dplyr::full_join(offense, defense, by = c("season", "team", "week")) - - if (isFALSE(weekly)){ - combined <- combined %>% - dplyr::select(-"week") %>% - dplyr::group_by(.data$season, .data$team) %>% - dplyr::summarise( - dplyr::across(.cols = dplyr::ends_with("_n"), .fns = ~ sum(.x, na.rm = TRUE)), - dplyr::across(.cols = !dplyr::ends_with("_n"), .fns = ~ mean(.x, na.rm = TRUE)), - .groups = "drop" - ) %>% - dplyr::relocate("def_n", .after = "off_to") - } + combined <- dplyr::full_join(offense, defense, by = grp) combined }