Skip to content

Commit

Permalink
Push nflfastR 2.2.0.9001
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcaseb committed Aug 10, 2020
1 parent f3179be commit a268308
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: nflfastR
Title: Functions to Efficiently Scrape NFL Play by Play Data
Version: 2.2.0.9000
Version: 2.2.0.9001
Authors@R:
c(person(given = "Sebastian",
family = "Carl",
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# nflfastR (development version)

* Fix `add_xyac()` breaking with some old packages
* Fix `add_xyac()` calculations being wrong for some failed 4th downs
* Fix `add_xyac()` and `add_qb_epa()` calculations being wrong for some failed 4th downs
* Updated Readme with ep and cp model plots
* Updated `vignette("examples")` with the new `add_xyac()` function
* Added xYAC model to `vignette("nflfastR-models")`
Expand Down
7 changes: 4 additions & 3 deletions R/helper_add_xyac.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,19 @@ add_xyac <- function(pbp) {
turnover = dplyr::if_else(.data$down == 4 & .data$gain < .data$ydstogo, as.integer(1), as.integer(0)),
down = dplyr::if_else(.data$gain >= .data$ydstogo, 1, .data$down + 1),
ydstogo = dplyr::if_else(.data$gain >= .data$ydstogo, 10, .data$ydstogo - .data$gain),
# ydstogo can't be bigger than yardline
ydstogo = dplyr::if_else(.data$ydstogo >= .data$yardline_100, as.integer(.data$yardline_100), as.integer(.data$ydstogo)),
# possession change if 4th down failed
down = dplyr::if_else(.data$turnover == 1, as.integer(1), as.integer(.data$down)),
ydstogo = dplyr::if_else(.data$turnover == 1, as.integer(10), as.integer(.data$ydstogo)),
# flip yardline_100 and timeouts for turnovers
yardline_100 = dplyr::if_else(.data$turnover == 1, as.integer(100 - .data$yardline_100), as.integer(.data$yardline_100)),
posteam_timeouts_remaining = dplyr::if_else(.data$turnover == 1,
.data$defeam_timeouts_pre,
.data$posteam_timeouts_pre),
defteam_timeouts_remaining = dplyr::if_else(.data$turnover == 1,
.data$posteam_timeouts_pre,
.data$defeam_timeouts_pre)
.data$defeam_timeouts_pre),
# ydstogo can't be bigger than yardline
ydstogo = dplyr::if_else(.data$ydstogo >= .data$yardline_100, as.integer(.data$yardline_100), as.integer(.data$ydstogo))
) %>%
dplyr::ungroup() %>%
nflfastR::calculate_expected_points() %>%
Expand Down
26 changes: 21 additions & 5 deletions R/helper_additional_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,37 @@ add_qb_epa <- function(d) {
fumbles_df <- d %>%
dplyr::filter(.data$complete_pass == 1 & .data$fumble_lost == 1 & !is.na(.data$epa) & !is.na(.data$down)) %>%
dplyr::mutate(
half_seconds_remaining = dplyr::if_else(
.data$half_seconds_remaining <= 6,
0,
.data$half_seconds_remaining - 6
),
down = as.numeric(.data$down),
# save old stuff for testing/checking
down_old = .data$down, ydstogo_old = .data$ydstogo, epa_old = .data$epa,
posteam_timeouts_pre = .data$posteam_timeouts_remaining,
defeam_timeouts_pre = .data$defteam_timeouts_remaining,
down_old = .data$down,
ydstogo_old = .data$ydstogo,
epa_old = .data$epa,
# update yard line, down, yards to go from play result
yardline_100 = .data$yardline_100 - .data$yards_gained, down = dplyr::if_else(.data$yards_gained >= .data$ydstogo, 1, .data$down + 1),
yardline_100 = .data$yardline_100 - .data$yards_gained,
down = dplyr::if_else(.data$yards_gained >= .data$ydstogo, 1, .data$down + 1),
# if the fumble spot would have resulted in turnover on downs, need to give other team the ball and fix
change = dplyr::if_else(.data$down == 5, 1, 0), down = dplyr::if_else(.data$down == 5, 1, .data$down),
# yards to go is 10 if its a first down, update otherwise
ydstogo = dplyr::if_else(.data$down == 1, 10, .data$ydstogo - .data$yards_gained),
# fix yards to go for goal line (eg can't have 1st & 10 inside opponent 10 yard line)
ydstogo = dplyr::if_else(.data$yardline_100 < .data$ydstogo, .data$yardline_100, .data$ydstogo),
# 10 yards to go if possession change
ydstogo = dplyr::if_else(.data$change == 1, 10, .data$ydstogo),
# flip field for possession change
# flip field and timeouts for possession change
yardline_100 = dplyr::if_else(.data$change == 1, 100 - .data$yardline_100, .data$yardline_100),
posteam_timeouts_remaining = dplyr::if_else(.data$change == 1,
.data$defeam_timeouts_pre,
.data$posteam_timeouts_pre),
defteam_timeouts_remaining = dplyr::if_else(.data$change == 1,
.data$posteam_timeouts_pre,
.data$defeam_timeouts_pre),
# fix yards to go for goal line (eg can't have 1st & 10 inside opponent 10 yard line)
ydstogo = dplyr::if_else(.data$yardline_100 < .data$ydstogo, .data$yardline_100, .data$ydstogo),
ep_old = .data$ep
) %>%
dplyr::select(
Expand Down
2 changes: 1 addition & 1 deletion vignettes/beginners_guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ data <- readRDS(url('https://raw.githubusercontent.com/guga31bb/nflfastR-data/ma

### Dimensions

Before moving forward, here are a few ways to get a sense of what's in a dataframe. We can check the **dim**ensions of the data, and this tells us that there are 48,034 rows (i.e., plays) in the data and 322 columns (variables):
Before moving forward, here are a few ways to get a sense of what's in a dataframe. We can check the **dim**ensions of the data, and this tells us that there are 48,034 rows (i.e., plays) in the data and 328 columns (variables):
``` {r}
dim(data)
```
Expand Down
3 changes: 2 additions & 1 deletion vignettes/nflfastR-models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ round(with(cp_cv_cal_error, weighted.mean(weight_cal_error, n_complete)), 4)

By now, the process should be familiar.
``` {r xyac-setup, results = 'hide'}
pbp <- readRDS(url('https://github.com/guga31bb/nflfastR-data/blob/master/models/cal_data.rds?raw=true'))
pbp_data <- readRDS(url('https://github.com/guga31bb/nflfastR-data/blob/master/models/cal_data.rds?raw=true'))
# pbp_data <- readRDS('../../nflfastR-data/models/cal_data.rds')
model_data <- pbp_data %>%
make_model_mutations() %>%
Expand Down

0 comments on commit a268308

Please sign in to comment.