Skip to content

Instantly share code, notes, and snippets.

@guga31bb
Last active November 8, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guga31bb/b64e875b5bc9e2af4fa4184ab470e9fe to your computer and use it in GitHub Desktop.
Save guga31bb/b64e875b5bc9e2af4fa4184ab470e9fe to your computer and use it in GitHub Desktop.
Fix bugs in nflscrapR EPA
library(nflscrapR)
library(tidyverse)
library(na.tools)


#assumes you're starting with pbp dataframe called 'games'
for_model <- games %>%
  select(half_seconds_remaining, yardline_100, down, ydstogo, goal_to_go, game_id, play_id)

#get the correct EP values
#only works if all the other columns are dropped, otherwise EP comes back wrong
for_model <- calculate_expected_points(for_model, "half_seconds_remaining", "yardline_100", 
              "down", "ydstogo", "goal_to_go") %>%
  rename(new_ep = ep) %>%
  select(game_id, play_id, new_ep)

#join to original data
games <- games %>% left_join(for_model, by=c("game_id", "play_id")) %>%
  filter(!is.na(new_ep)) %>% 
  mutate(f_ep = lead(new_ep, n = 1), f_posteam = lead(posteam, n = 1), f_half=lead(game_half, n = 1), exp_epa=f_ep - new_ep,
         tofix = ifelse(touchdown == 0 & !is.na(epa) & !is.na(down) & posteam== f_posteam & game_half == f_half & 
                          (play_type=="no_play" | play_type=="pass" | play_type=="run"), 1, 0),
         epa = ifelse(tofix == 1 & !is.na(exp_epa), exp_epa, epa))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment