Skip to content

Commit

Permalink
killing time, ocd style - skip check, skip pkgdown, skip covr
Browse files Browse the repository at this point in the history
  • Loading branch information
tanho63 committed Jan 28, 2022
1 parent f94223f commit 50793b8
Showing 1 changed file with 78 additions and 111 deletions.
189 changes: 78 additions & 111 deletions R/ep_summarize.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

0 comments on commit 50793b8

Please sign in to comment.