-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from mrcaseb/ben
Add series and series_success
- Loading branch information
Showing
5 changed files
with
73 additions
and
2 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
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,65 @@ | ||
################################################################################ | ||
# Author: Sebastian Carl | ||
# Purpose: Function to add series variables analogue Lee Sharpe's Version | ||
# Code Style Guide: styler::tidyverse_style() | ||
################################################################################ | ||
|
||
## series = | ||
## starts at 1, each new first down increments, numbers shared across both teams | ||
## NA: kickoffs, extra point/two point conversion attempts, non-plays, no posteam | ||
## series_success = | ||
## 1: scored touchdown, gained enough yards for first down | ||
## 0: punt, interception, fumble lost, turnover on downs, 4th down FG attempt | ||
## NA: series is NA, series contains QB spike/kneel, half ended with none of above | ||
|
||
add_series_data <- function(pbp) { | ||
out <- | ||
pbp %>% | ||
dplyr::group_by(game_id) %>% | ||
dplyr::mutate( | ||
# make down numeric | ||
down = as.numeric(down), | ||
# create a first down indicator which marks first down for the offense | ||
# AND first down after change of possesion (-> drivenumber increases) | ||
# we don't want a first down being indicated for XP, 2P, KO | ||
first_down = dplyr::if_else( | ||
(first_down_rush == 1 | first_down_pass == 1 | | ||
first_down_penalty == 1 | drive < dplyr::lead(drive)) & | ||
(extra_point_attempt == 0 & two_point_attempt == 0 & kickoff_attempt == 0), | ||
1, 0 | ||
), | ||
# after setting the first down indicator we modificate it for the end of a half | ||
first_down = dplyr::if_else(game_half != dplyr::lead(game_half), 1, first_down), | ||
# the 'trigger' is being used for calculatung cumsum because we don't want the | ||
# series number to increase in the play the first down occured but in the next play | ||
trigger = dplyr::lag(first_down, 1, 0) | ||
) %>% | ||
# now compute series number with cumsum (for the calculation NA are being relaced with 0) | ||
dplyr::mutate(series = cumsum(tidyr::replace_na(trigger, 0)) + 1) %>% | ||
dplyr::mutate( | ||
# now modificated series number for special cases | ||
series = dplyr::if_else( | ||
kickoff_attempt == 1 | extra_point_attempt == 1 | | ||
two_point_attempt == 1 | is.na(down) | | ||
is.na(posteam), | ||
NA_real_, | ||
series | ||
), | ||
series_success = dplyr::case_when( | ||
is.na(series) | qb_kneel == 1 | qb_spike == 1 ~ NA_real_, | ||
touchdown == 1 | first_down_rush == 1 | first_down_pass == 1 | | ||
first_down_penalty == 1 ~ 1, | ||
punt_attempt == 1 | interception == 1 | fumble_lost == 1 | | ||
fourth_down_failed == 1 | field_goal_attempt == 1 ~ 0, | ||
TRUE ~ 0 | ||
) | ||
) %>% | ||
dplyr::group_by(game_id, series) %>% | ||
# set series_success value for the whole series | ||
dplyr::mutate(series_success = last(series_success)) %>% | ||
dplyr::ungroup() %>% | ||
dplyr::select(-first_down, -trigger) | ||
|
||
message("added series variables") | ||
return(out) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.