Skip to content

Instantly share code, notes, and snippets.

@z-feldman
Last active November 8, 2019 18:59
Show Gist options
  • Save z-feldman/7e2845d476ccf215ebb93830bd72a887 to your computer and use it in GitHub Desktop.
Save z-feldman/7e2845d476ccf215ebb93830bd72a887 to your computer and use it in GitHub Desktop.
Fix EP
library(nflscrapR)
library(tidyverse)
# assumes your data is called pbp
pbp <- pbp %>%
calculate_expected_points("half_seconds_remaining",
"yardline_100",
"down",
"ydstogo",
"goal_to_go")
# change original variables to new calculations, unless new calculations are NA
pbp %<>% mutate(ep = if_else(is.na(ep1), ep, ep1),
no_score_prob = if_else(is.na(no_score_prob1), no_score_prob, no_score_prob1),
opp_fg_prob = if_else(is.na(opp_fg_prob1), opp_fg_prob, opp_fg_prob1),
opp_safety_prob = if_else(is.na(opp_safety_prob1), opp_safety_prob, opp_safety_prob1),
opp_td_prob = if_else(is.na(opp_td_prob1), opp_td_prob, opp_td_prob1),
fg_prob = if_else(is.na(fg_prob1), fg_prob, fg_prob1),
td_prob = if_else(is.na(td_prob1), td_prob, td_prob1),
safety_prob = if_else(is.na(safety_prob1), safety_prob, safety_prob1))
# recalculate epa
pbp %<>% mutate(epa = lead(ep) - ep)
# there are still possible issues with the original ep on plays where the new ep was NA
# filter those plays out with !is.na(ep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment