Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ffverse/ffexpectedpoints in…
Browse files Browse the repository at this point in the history
…to main
  • Loading branch information
JoeSydlowski committed May 12, 2022
2 parents ecfa348 + 4173afb commit b8eeaf2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 121 deletions.
2 changes: 1 addition & 1 deletion R/ep_load.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ep_load <- function(season = nflreadr:::most_recent_season(),
p <- NULL
if (is_installed("progressr")) p <- progressr::progressor(along = season)
out <- purrr::map_dfr(urls, nflreadr::progressively(rds_from_url, p))
ts <- nflreadr::raw_from_url("https://github.com/ffverse/ffopportunity/releases/download/latest-data/timestamp.txt") %>%
ts <- nflreadr::raw_from_url(glue::glue("https://github.com/ffverse/ffopportunity/releases/download/{version}-data/timestamp.txt")) %>%
rawToChar() %>%
as.POSIXct()

Expand Down
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
11 changes: 11 additions & 0 deletions pkgdown/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ h4.author,h4.date {
font-weight: 700;
color: #333;
}
.row > main {
max-width: 100%;
}

@media only screen and (min-width: 640px) {
main + .col-md-3 {
margin-left: unset;
padding-left: 5rem;
max-width: 75%;
}
}
8 changes: 4 additions & 4 deletions update/ep_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
# zip(file.path(folder_path,paste0(version,".zip")),files = file.path(version))
# zip(file.path(folder_path,"latest.zip"), files = file.path(version))
#
# try(piggyback::pb_new_release(repo = "ffverse/ffexpectedpoints",tag = paste0(version, "-model")))
# try(piggyback::pb_new_release(repo = "ffverse/ffopportunity",tag = paste0(version, "-model")))
# piggyback::pb_upload(file = file.path(folder_path,paste0(version,".zip")),
# repo = "ffverse/ffexpectedpoints",
# repo = "ffverse/ffopportunity",
# tag = paste0(version, "-model"))
#
# try(piggyback::pb_new_release(repo = "ffverse/ffexpectedpoints",tag = "latest-model"))
# try(piggyback::pb_new_release(repo = "ffverse/ffopportunity",tag = "latest-model"))
# piggyback::pb_upload(file = file.path(folder_path,"latest.zip"),
# repo = "ffverse/ffexpectedpoints",
# repo = "ffverse/ffopportunity",
# tag = "latest-model")
#
# setwd(wd)
13 changes: 8 additions & 5 deletions update/ep_update.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pkgload::load_all()

save_ep_data <- function(season, folder_path, version){

ep_object <- ffexpectedpoints::ep_build(season,version = version)
ep_object <- ffopportunity::ep_build(season,version = version)

# rds
saveRDS(ep_object$ep_weekly, file.path(folder_path, glue::glue('ep_weekly_{season}.rds')))
Expand All @@ -25,18 +25,21 @@ save_ep_data <- function(season, folder_path, version){

upload_ep_data <- function(folder_path, version){
list.files(folder_path, pattern = "csv$|rds$|parquet$|txt$", full.names = TRUE) %>%
purrr::walk(piggyback::pb_upload, repo = "ffverse/ffexpectedpoints", tag = "latest-data", overwrite = TRUE) %>%
purrr::walk(piggyback::pb_upload, repo = "ffverse/ffexpectedpoints", tag = glue::glue("{version}-data"), overwrite = TRUE)
piggyback::pb_upload(repo = "ffverse/ffopportunity", tag = "latest-data", overwrite = TRUE)

list.files(folder_path, pattern = "csv$|rds$|parquet$|txt$", full.names = TRUE) %>%
piggyback::pb_upload(repo = "ffverse/ffopportunity", tag = glue::glue("{version}-data"), overwrite = TRUE)

cli::cli_alert_success("Completed ep upload! {Sys.time()}")
}

update_ep <- function(season, version = "v1.0.0", folder_path){
try(piggyback::pb_new_release(repo = "ffverse/ffexpectedpoints", tag = "latest-data"))
try(piggyback::pb_new_release(repo = "ffverse/ffexpectedpoints", tag = glue::glue("{version}-data")))
purrr::walk(season, save_ep_data, folder_path = folder_path, version = version)
upload_ep_data(folder_path, version)
invisible(NULL)
}
try(piggyback::pb_new_release(repo = "ffverse/ffopportunity", tag = "latest-data"))
try(piggyback::pb_new_release(repo = "ffverse/ffopportunity", tag = glue::glue("{version}-data")))
temp <- tempdir()
update_ep(nflreadr:::most_recent_season(), version = "latest", folder_path = temp)
update_ep(nflreadr:::most_recent_season(), version = "v1.0.0", folder_path = temp)
Expand Down

0 comments on commit b8eeaf2

Please sign in to comment.