Skip to content

Instantly share code, notes, and snippets.

Team Strength Exclusion Bias in Expected Points Models

@nflscrapR's Expected Points (EP) is a popular metric among analysts doing public research of play in the NFL. Detailed in the creators' research paper, the metric is derived from a model that was built as a part of a larger system designed to calculate individual wins above replacement values for offensive skill players.

The authors very graciously made public all of their data (nflscrapR-data) and code (nflWAR, nflscrapR-models, nflscrapR) for this project, including the code used to build the EP model. In the init_ep_fg_models.R file of the nflscrapR-models repository, we can see that the following variables are used

@guga31bb
guga31bb / fix_fumbles.R
Created February 19, 2020 17:59
give credit for EPA prior to fumble
#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
@guga31bb
guga31bb / epa_fix.mD
Last active November 8, 2019 18:00
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)
@guga31bb
guga31bb / goal_line.md
Last active May 16, 2021 22:34
Investigate rush and pass TD% at goal line

Investigate rush and pass TD% at goal line

Need to change locations of files and output below.

library(tidyverse)
library(ggimage)
library(ggrepel)
library(ggplot2)
library(ggbeeswarm)
@guga31bb
guga31bb / box_score.md
Last active May 16, 2021 22:35
Make pretty box scores

Make box scores with EPA and other stuff

Code below. To get the game you want, need to choose season, type (regular, postseason, etc), home team, away team, and the rest will go on itself.

Code from @benbbaldwin

library(nflscrapR)
library(tidyverse)
library(na.tools)

Running back targets

Code used for running back targets piece.

library(tidyverse)
library(na.tools)
library(ggrepel)
library(ggimage)
library(hrbrthemes)
@guga31bb
guga31bb / timelines.md
Last active May 16, 2021 22:36
A decade of QB and non-QB efficiency in a chart

First

You need to have the roster data and rush-pass play-by-play data saved on your computer.

How to create and save those files is shown in this file here.

Example image:

Image

@guga31bb
guga31bb / master create play-by-play.md
Last active May 16, 2021 22:42
Put together play-by-play data

Run this code, making sure all the packages are installed (install.packages("package") if you don't).

Make sure to replace the instances of FILENAME where you want to save your data.

library(tidyverse)
library(dplyr)
library(na.tools)

first <- 2009 #first season to grab. min available=2009

The accumulation of QB hits vs passing efficiency

Ben Baldwin

In a follow-up to his excellent piece on the value of the run game in The Athletic (great website, highly recommended), Ted Nguyen shared the following:

"In-house NFL analytics crews track QB hits and the results of the accumulation of hits and how it affects offensive performance over the course of a game."

Does the accumulation of hits affect offensive performance over the game? Is this finally a feather in the cap for the run game defenders?

@guga31bb
guga31bb / nflscrapr.md
Last active August 18, 2023 07:45
Simple guide for using nflscrapR

THIS IS OUTDATED. PLEASE FOLLOW THE FOLLOWING LINK

--> A beginner's guide to nflfastR <--

Basic nflscrapR tutorial

I get a lot of questions about how to get nflscrapR up and running. This guide is intended to help new users build interesting tables or charts from the ground up, taking the raw nflscrapR data.

Quick word if you're new to programming: all of this is happening in R. Obviously, you need to install R on your computer to do any of this. Make sure you save what you're doing in a script (in R, File --> New script) so you can save your work and run multiple lines of code at once. To run code from a script, highlight what you want, right click, and select Run line. As you go through your R journey, you might get stuck and have to google a bunch of things, but that's totally okay and normal. That's how I wrote this thing!