From 50793b857402c891f34ffa3553370b6d8da4b83e Mon Sep 17 00:00:00 2001 From: Tan-DESKPC Date: Fri, 28 Jan 2022 09:07:52 -0500 Subject: [PATCH] killing time, ocd style - skip check, skip pkgdown, skip covr --- R/ep_summarize.R | 189 +++++++++++++++++++---------------------------- 1 file changed, 78 insertions(+), 111 deletions(-) diff --git a/R/ep_summarize.R b/R/ep_summarize.R index adf00ae..9e08f79 100644 --- a/R/ep_summarize.R +++ b/R/ep_summarize.R @@ -20,9 +20,7 @@ #' #' @export -ep_summarize <- function( - predicted_pbp, - stat_type = c("all", "expected_points", "team_stats")){ +ep_summarize <- function(predicted_pbp, stat_type = c("all", "expected_points", "team_stats")){ stat_type <- rlang::arg_match(stat_type) @@ -42,30 +40,22 @@ ep_summarize <- function( .data$posteam, player_type = "rush", attempt = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$rush_attempt), - #already 0 for 2pt attempts - yards_gained = .data$rushing_yards, - yards_gained_exp = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$rush_yards_exp), + yards_gained = .data$rushing_yards, #already 0 for 2pt attempts + yards_gained_exp = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$rush_yards_exp), touchdown = dplyr::if_else(.data$rush_touchdown == "1", 1L, 0L), - touchdown_exp = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$rush_touchdown_exp), + touchdown_exp = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$rush_touchdown_exp), two_point_conv = .data$two_point_converted, - two_point_conv_exp = dplyr::if_else( - .data$two_point_attempt == 1, .data$two_point_conv_exp, 0), + two_point_conv_exp = dplyr::if_else(.data$two_point_attempt == 1, .data$two_point_conv_exp, 0), first_down = .data$first_down_rush, - first_down_exp = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$rush_first_down_exp), + first_down_exp = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$rush_first_down_exp), fantasy_points = - 6.0*.data$touchdown + - 2.0*.data$two_point_converted + - 0.1*.data$rushing_yards - - 2.0*.data$fumble_lost, + 6.0 * .data$touchdown + + 2.0 * .data$two_point_converted + + 0.1 * .data$rushing_yards + + -2.0 * .data$fumble_lost, fantasy_points_exp = 0.1*.data$rush_yards_exp + - dplyr::if_else( - .data$two_point_attempt == 1, - 2*.data$two_point_conv_exp, - 6*.data$rush_touchdown_exp), + dplyr::if_else(.data$two_point_attempt == 1, 2*.data$two_point_conv_exp, 6*.data$rush_touchdown_exp), .data$fumble_lost) pass_df <- @@ -84,102 +74,83 @@ ep_summarize <- function( rec.full_name = .data$receiver_full_name, rec.position = .data$receiver_position, .data$posteam, - attempt = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$pass_attempt), - air_yards = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$air_yards), + attempt = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$pass_attempt), + air_yards = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$air_yards), complete_pass = dplyr::if_else(.data$complete_pass == "1", 1L, 0L), - complete_pass_exp = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$pass_completion_exp), + complete_pass_exp = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$pass_completion_exp), #already 0 for 2pt attempts - yards_gained = dplyr::if_else( - is.na(.data$receiving_yards), 0, .data$receiving_yards), - yards_gained_exp = - .data$complete_pass_exp * - (.data$yards_after_catch_exp + .data$air_yards), + yards_gained = dplyr::if_else(is.na(.data$receiving_yards), 0, .data$receiving_yards), + yards_gained_exp = .data$complete_pass_exp * (.data$yards_after_catch_exp + .data$air_yards), touchdown = dplyr::if_else(.data$pass_touchdown == "1", 1L, 0L), - touchdown_exp = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$pass_touchdown_exp), + touchdown_exp = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$pass_touchdown_exp), two_point_conv = .data$two_point_converted, - two_point_conv_exp = dplyr::if_else( - .data$two_point_attempt == 1, .data$two_point_conv_exp, 0), + two_point_conv_exp = dplyr::if_else(.data$two_point_attempt == 1, .data$two_point_conv_exp, 0), first_down = .data$first_down_pass, - first_down_exp = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$pass_first_down_exp), + first_down_exp = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$pass_first_down_exp), interception = dplyr::if_else(.data$interception == "1", 1L, 0L), - interception_exp = dplyr::if_else( - .data$two_point_attempt == 1, 0, .data$pass_interception_exp), - .data$fumble_lost) %>% - tidyr::pivot_longer(cols = c(.data$pass.player_id, - .data$pass.full_name, - .data$pass.position, - .data$rec.player_id, - .data$rec.full_name, - .data$rec.position), - names_to = c("player_type", ".value"), - names_sep = "\\.") %>% - dplyr::mutate(fantasy_points_exp = - dplyr::if_else(.data$player_type == "rec", - 0.1*.data$yards_gained_exp + - .data$complete_pass_exp + - 6*.data$touchdown_exp + - 2*.data$two_point_conv_exp, - 0.04*.data$yards_gained_exp - - 2*.data$interception_exp + - 4*.data$touchdown_exp + - 2*.data$two_point_conv_exp), - fantasy_points = - dplyr::if_else(.data$player_type == "rec", - 6*.data$touchdown + - 2*.data$two_point_conv + - 0.1*.data$yards_gained - - 2*.data$fumble_lost + - .data$complete_pass, - 4*.data$touchdown + - 2*.data$two_point_conv + - 0.04*.data$yards_gained - - # Haven't included sack fumbles yet - # 2*.data$fumble_lost - - 2*.data$interception)) + interception_exp = dplyr::if_else(.data$two_point_attempt == 1, 0, .data$pass_interception_exp), + .data$fumble_lost + ) %>% + tidyr::pivot_longer( + cols = c(.data$pass.player_id, + .data$pass.full_name, + .data$pass.position, + .data$rec.player_id, + .data$rec.full_name, + .data$rec.position), + names_to = c("player_type", ".value"), + names_sep = "\\.") %>% + dplyr::mutate( + fantasy_points_exp = dplyr::if_else(.data$player_type == "rec", + 0.1 * .data$yards_gained_exp + + 1 * .data$complete_pass_exp + + 6 * .data$touchdown_exp + + 2 * .data$two_point_conv_exp, + 0.04 * .data$yards_gained_exp + + - 2 * .data$interception_exp + + 4 * .data$touchdown_exp + + 2 *.data$two_point_conv_exp), + fantasy_points = dplyr::if_else(.data$player_type == "rec", + 6 * .data$touchdown + + 2 * .data$two_point_conv + + 0.1 * .data$yards_gained + + -2 * .data$fumble_lost + + 1 * .data$complete_pass, + 4 * .data$touchdown + + 2 * .data$two_point_conv + + 0.04 * .data$yards_gained + + -2 *.data$interception + # Haven't included sack fumbles yet + # 2*.data$fumble_lost - + )) combined_df <- pass_df %>% dplyr::bind_rows(rush_df) %>% - tidyr::pivot_wider(id_cols = c(.data$season, - .data$posteam, - .data$week, - .data$game_id, - .data$player_id, - .data$full_name, - .data$position), - names_from = .data$player_type, - names_glue = "{player_type}_{.value}", - values_fn = sum, - values_from = c(where(is.numeric), - -.data$week)) %>% + tidyr::pivot_wider( + id_cols = c(.data$season, + .data$posteam, + .data$week, + .data$game_id, + .data$player_id, + .data$full_name, + .data$position), + names_from = .data$player_type, + names_glue = "{player_type}_{.value}", + values_fn = sum, + values_from = c(where(is.numeric),-.data$week)) %>% janitor::remove_empty(which = "cols") %>% - dplyr::mutate(dplyr::across(.cols = where(is.numeric), - .fns = ~tidyr::replace_na(.x, 0)), - dplyr::across(.cols = where(is.numeric), - .fns = ~round(.x, 2))) %>% + dplyr::mutate(dplyr::across(.cols = where(is.numeric), .fns = ~tidyr::replace_na(.x, 0) %>% round(2))) %>% dplyr::rowwise() %>% dplyr::mutate( - total_yards_gained = - sum(dplyr::across(tidyselect::ends_with("yards_gained"), sum)), - total_yards_gained_exp = - sum(dplyr::across(tidyselect::ends_with("yards_gained_exp"), sum)), - total_touchdown = - sum(dplyr::across(tidyselect::ends_with("touchdown"), sum)), - total_touchdown_exp = - sum(dplyr::across(tidyselect::ends_with("touchdown_exp"), sum)), - total_first_down = - sum(dplyr::across(tidyselect::ends_with("first_down"), sum)), - total_first_down_exp = - sum(dplyr::across(tidyselect::ends_with("first_down_exp"), sum)), - total_fantasy_points = - sum(dplyr::across(tidyselect::ends_with("fantasy_points"), sum)), - total_fantasy_points_exp = - sum(dplyr::across(tidyselect::ends_with("fantasy_points_exp"), sum))) %>% + total_yards_gained =sum(dplyr::across(tidyselect::ends_with("yards_gained"), sum)), + total_yards_gained_exp = sum(dplyr::across(tidyselect::ends_with("yards_gained_exp"), sum)), + total_touchdown = sum(dplyr::across(tidyselect::ends_with("touchdown"), sum)), + total_touchdown_exp = sum(dplyr::across(tidyselect::ends_with("touchdown_exp"), sum)), + total_first_down = sum(dplyr::across(tidyselect::ends_with("first_down"), sum)), + total_first_down_exp = sum(dplyr::across(tidyselect::ends_with("first_down_exp"), sum)), + total_fantasy_points = sum(dplyr::across(tidyselect::ends_with("fantasy_points"), sum)), + total_fantasy_points_exp = sum(dplyr::across(tidyselect::ends_with("fantasy_points_exp"), sum))) %>% dplyr::ungroup() %>% dplyr::rename( pass_completions = .data$pass_complete_pass, @@ -198,19 +169,15 @@ ep_summarize <- function( stringr::str_remove_all("_exp") for(f in exp_fields) { - combined_df[paste0(f,"_diff")] <- - combined_df[f]-combined_df[paste0(f,"_exp")] + combined_df[paste0(f,"_diff")] <- combined_df[f]-combined_df[paste0(f,"_exp")] } team_df <- combined_df %>% dplyr::group_by(.data$season, .data$posteam, .data$week, .data$game_id) %>% dplyr::summarise( - dplyr::across( - .cols = where(is.numeric), - .fns = sum, - .names = "{col}_team") - ) %>% + dplyr::across(.cols = where(is.numeric), .fns = sum, .names = "{col}_team") + ) %>% dplyr::ungroup() player_team_df <-