Learn how to use Python to evaluate team strength using nflfastR's EPA model.
This post is a continuation of the last post focused on general NFL-analytics using NflFastPy's EPA model.
The code below will build heavily on the last post so if you'd like to follow along the code it may be necessary to go back and start with the blog post from last week. Last week we used NflFastPy's EPA (Estimated Points Added) model to visualize the best offenses and defenses in the league. Now we will use that information along with schedule data to look at the fantasy fallout.
NflFastPy's play by play data comes with EPA data for each play. EPA is a model that estimates the expected points added per individual play based on starting and ending field position, down, and field goal distance.
Each play has an EPA, and we're going to be finding each team's EPA per play on offense and defense. For offense, it's straight forward. If a play has an EPA of 1.2 on offense, that means the offense moved the ball such that they added an expected 1.2 points to their score. For defense, it's going to be the opposite. If a team is on defense, and the EPA for the play is 1.2, then we'll say the defense gave up or allowed an estimated 1.2 points on the play. Team defenses with more negative EPAs are better defenses, while team defenses with more positive EPAs are worse defenses.
Now that we know the offensive and defensive value of each team we can see how hard a team's schedule has been so far and how hard it will be for the rest of the season. By doing this we are essentially normalizing a team's performance and evaluating with the opposition strength in mind. By looking at the biggest change in schedule strength we can zero in on fantasy players on teams that will have a much easier schedule. We can also fade some players that have performed well so far this year but is about to face some tough teams.
This analysis is very helpful in putting fantasy performances into perspective. I often use it to evaluate current value versus future value which gives an edge when it comes to trading.
Let's get to the code. First things first, load up your Google colab or jupyter notebook and import the libraries we'll need for this post.
Next, we'll load in 2021 play by play data via NflFastPy. We've used this data quite a bit, just as a reminder it is an extensive database detailing every snap that has taken place so far this year.
5 rows × 372 columns
Here, we're making a DataFrame called epa_df which will sum up team EPAs for each play and we'll also count the number of plays. Using this we calculate epa per play. Then we rinse and repeat to add in the defensive epa.
These are the 5 best offenses in the league by EPA per play. Its always good practice to check your results and confirm they are what you expect, or at least make sense. The 5 teams listed above are considered some of the best offenses in the league so this checks out.
These are the 5 worst defenses in the league by EPA per play. Remember, more positive EPAs per play on the defense side are bad. This means the defense is allowing (an estimated amount) of more points per play.
Now we can utilize the information created above in addition to schedule data to get some valuable insights. Most of this code is definitions, transformations, and manipulations. I'll breeze through it fairly quickly, but feel free to reach out if you have specific questions.
First we grab schedule data for all of 2021, and then define a function to extract all opponents for a singular team. Then we associate the EPA's for each list of opponents. And lastly, input the number of weeks played in the season to see the EPA of offenses and defenses already faced compared to the teams the rest of the season.
Now we can use the definitions from above to populate a dataframe with all the EPA information.
Let's interpret the numbers. A positive offensive delta means a team will be facing better offenses in the second half of the season than the first half. A positive defensive delta means a team will be facing worse defenses in the second half of the season than the first half. So in the dataframe above sorted by defensive EPA delta we can see 5 teams that are jumping from easy schedules to hard schedules (denoted by a negative defense EPA delta). These are teams you may want to avoid when streaming players.
Now lets make a similar plot to last post. This time we will plot the delta epa for offense and defense. This metric is effectively the change in strength of schedule from the first half to the second half of the season (for both offense and defense separately).
The best Way to interpret this plot is to look at the x or y axis one at a time. Lets start with the x axis.
The x axis contains information about opposing offenses. If a team is to the right of the origin the team will be playing better offenses in the second half than they did in the first half. If a team is to the left of the origin the team will be playing worse offenses in the second half than they did in the first half. A couple notable teams are the Titans and the Raiders. The Titans played some really great offensive teams in the first half including Cardinals, Chiefs, Bills, Rams, and the Colts twice. In the second half they will face the Jaguars, Dolphins, Steelers, and the Texans twice. So it makes sense we see them pretty far on the left side of the plot since they will be facing easier offenses. This means the Titans defense could be a great pick up down the stretch. The Raiders on the other hand will be facing better offenses. This is valuable information since if you believe the Raiders will get into some shoot outs to keep up with their opponents, Derek Carr is worth monitoring from a fantasy perspective.
The y axis contains information about opposing defenses. If a team is above the origin the team will be playing worse defenses in the second half than they did in the first half. If a team is below the origin the team will be playing better defenses in the second half than they did in the first half. The Bills for example will be facing much better defenses in their final 8 games. They face off against the Saints, Panthers and the Patriots twice. All of these teams are top 10 defenses according to Pro Football Reference. It is also interesting to note the Bills play the Jets (possibly the worst defense in the league) in week 18 and this is baked into our analysis. If you do not play week 18 in your fantasy football leagues then you don't get the points from this easy match up. If you are interested I recommend playing with this analysis and cutting off week 18 to get more specific results for your fantasy football leagues.
As a final note I'd like to add that this analysis does not take into account a couple things. First off, we are looking specifically at how teams have performed in the first half of the season and assuming their performance will be similar in the future. This doesn't account for future injuries (or players returning), home / road splits, or if a team is on a hot streak. Like I mentioned before this is a helpful analysis when trying to evaluate players performances so far this season and if their schedule will lighten up the rest of the way.
I'll leave the full list of defensive EPA deltas below. Thanks for reading!