Skip to content

Commit

Permalink
Correctly aggregate conversion rates
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb committed Oct 20, 2023
1 parent dccfba6 commit f707044
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions R/calculate_series_conversion_rates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------------------

Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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
}

0 comments on commit f707044

Please sign in to comment.