Created
February 19, 2020 17:59
-
-
Save guga31bb/12b8cb97709998fb4d3bcb577aaad1ec to your computer and use it in GitHub Desktop.
give credit for EPA prior to fumble
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
#give credit for where fumble happened in EPA like how yards gained works | |
fix_fumbles <- function(d) { | |
n <- d %>% filter(complete_pass == 1 & fumble_lost == 1 & !is.na(epa)) %>% | |
select(desc, game_id, play_id, epa, posteam, half_seconds_remaining, yardline_100, down, ydstogo, yards_gained, goal_to_go, ep) %>% | |
mutate( | |
#save old stuff for testing/checking | |
down_old = down, ydstogo_old = ydstogo, epa_old = epa, | |
#update yard line, down, yards to go from play result | |
yardline_100 = yardline_100 - yards_gained, down = ifelse(yards_gained >= ydstogo, 1, down + 1), | |
#if the fumble spot would have resulted in turnover on downs, need to give other team the ball and fix | |
change = ifelse(down == 5, 1, 0), down = ifelse(down == 5 , 1, down), | |
#yards to go is 10 if its a first down, update otherwise | |
ydstogo = ifelse(down == 1, 10, ydstogo - yards_gained), | |
#fix yards to go for goal line (eg can't have 1st & 10 inside opponent 10 yard line) | |
ydstogo = ifelse(yardline_100 < ydstogo, yardline_100, ydstogo), | |
#10 yards to go if possession change | |
ydstogo = ifelse(change == 1 , 10, ydstogo), | |
#flip field for possession change | |
yardline_100 = ifelse(change == 1, 100 - yardline_100, yardline_100), | |
goal_to_go = ifelse(yardline_100 == ydstogo, 1, 0), | |
ep_old = ep) %>% | |
select(-ep, -epa) | |
t2 <- calculate_expected_points(n, "half_seconds_remaining", "yardline_100", | |
"down", "ydstogo", "goal_to_go") %>% | |
mutate(ep = ifelse(change == 1, -ep, ep), fixed_epa = ep - ep_old) %>% | |
select(game_id, play_id, fixed_epa) | |
d <- d %>% | |
left_join(t2, by=c("game_id", "play_id")) %>% | |
mutate(epa = ifelse(!is.na(fixed_epa), fixed_epa, epa)) %>% | |
select(-fixed_epa) | |
return(d) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment