-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
4,652 additions
and
885 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
Type: Package | ||
Package: nflfastR | ||
Title: Functions to Efficiently Scrape NFL Play by Play and | ||
Roster Data | ||
Version: 1.1.10 | ||
Title: Functions to Efficiently Scrape NFL Play by Play Data | ||
Version: 2.0.0 | ||
Authors@R: | ||
c(person(given = "Sebastian", | ||
family = "Carl", | ||
|
@@ -12,11 +11,10 @@ Authors@R: | |
role = c("cre", "aut"), | ||
email = "[email protected]")) | ||
Description: nflfastR is a set of functions to efficiently | ||
scrape NFL play-by-play and roster data. | ||
scrape NFL play-by-play data. | ||
License: MIT + file LICENSE | ||
Depends: | ||
R (>= 3.5.0), | ||
nflscrapR | ||
R (>= 3.5.0) | ||
Imports: | ||
dplyr, | ||
glue, | ||
|
@@ -26,12 +24,12 @@ Imports: | |
lubridate, | ||
magrittr, | ||
mgcv, | ||
progressr, | ||
purrr, | ||
stringr, | ||
tibble, | ||
tidyr, | ||
tidyselect, | ||
xml2 | ||
tidyselect | ||
Suggests: | ||
furrr, | ||
future | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export("%>%") | ||
export(add_qb_epa) | ||
export(calculate_expected_points) | ||
export(clean_pbp) | ||
export(fast_scraper) | ||
export(fast_scraper_clips) | ||
export(fast_scraper_roster) | ||
export(fast_scraper_schedules) | ||
export(fix_fumbles) | ||
importFrom(magrittr,"%>%") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#' Compute expected points | ||
#' | ||
#' for provided plays. Returns the data with | ||
#' probabilities of each scoring event and EP added. The following columns | ||
#' must be present: season, week, home_team, posteam, roof (coded as 'open', | ||
#' 'closed', or 'retractable'), half_seconds_remaining, yardline_100, | ||
#' ydstogo, posteam_timeouts_remaining, defteam_timeouts_remaining | ||
#' | ||
#' @param pbp_data Play-by-play dataset to estimate expected points for. | ||
#' @details Computes expected points for provided plays. Returns the data with | ||
#' probabilities of each scoring event and EP added. The following columns | ||
#' must be present: season, week, home_team, posteam, roof (coded as 'open', | ||
#' 'closed', or 'retractable'), half_seconds_remaining, yardline_100, | ||
#' ydstogo, posteam_timeouts_remaining, defteam_timeouts_remaining | ||
#' @return The original pbp_data with the following columns appended to it: | ||
#' \itemize{ | ||
#' \item{ep} - expected points. | ||
#' \item{no_score_prob} - probability of no more scoring this half. | ||
#' \item{opp_fg_prob} - probability next score opponent field goal this half. | ||
#' \item{opp_safety_prob} - probability next score opponent safety this half. | ||
#' \item{opp_td_prob} - probability of next score opponent touchdown this half. | ||
#' \item{fg_prob} - probability next score field goal this half. | ||
#' \item{safety_prob} - probability next score safety this half. | ||
#' \item{td_prob} - probability text score touchdown this half. | ||
#' } | ||
#' @export | ||
|
||
calculate_expected_points <- function(pbp_data) { | ||
|
||
suppressWarnings( | ||
model_data <- pbp_data %>% | ||
#drop existing values of ep and the probs before making new ones | ||
dplyr::select(-one_of(drop.cols)) %>% | ||
make_model_mutations() %>% | ||
ep_model_select() | ||
) | ||
|
||
|
||
preds <- as.data.frame( | ||
matrix(stats::predict(ep_model, as.matrix(model_data)), ncol=7, byrow=TRUE) | ||
) | ||
|
||
colnames(preds) <- c("td_prob","opp_td_prob","fg_prob","opp_fg_prob", | ||
"safety_prob","opp_safety_prob","no_score_prob") | ||
|
||
preds <- preds %>% | ||
dplyr::mutate( | ||
ep = | ||
(-3 * opp_fg_prob) + | ||
(-2 * opp_safety_prob) + | ||
(-7 * opp_td_prob) + | ||
(3 * fg_prob) + | ||
(2 * safety_prob) + | ||
(7 * td_prob) | ||
) %>% | ||
dplyr::bind_cols(pbp_data) | ||
|
||
return(preds) | ||
|
||
} | ||
|
||
|
||
drop.cols <- c( | ||
'ep', 'td_prob', 'opp_td_prob', 'fg_prob', 'opp_fg_prob', | ||
'safety_prob', 'opp_safety_prob', 'no_score_prob' | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.