-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProj_1_NFL.Rmd
More file actions
237 lines (179 loc) · 10.1 KB
/
Proj_1_NFL.Rmd
File metadata and controls
237 lines (179 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
---
title: "Project 1 - NFL Summary Data"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
library(readr)
library(tidyverse)
```
```{r}
# Read data
nfl <- read_csv("nfl_teams_season_summary.csv")
nfl %>% mutate(division2 = substring(division, 1, 3)) -> nfl
```
Theme: Competitiveness
Research Questions:
1. Are any of the divisions getting stronger/more competitive over time?
Group by division (keeping all 8)
Compare w/ AFC vs NFC (not caring about N/S/E/W)
```{r}
afc <- c("AFC East", "AFC North", "AFC South", "AFC West")
nfc <- c("NFC East", "NFC North", "NFC South", "NFC West")
nfl <- nfl %>% mutate(div_general = ifelse(division %in% afc, "AFC", "NFC"))
nfl <- nfl %>% mutate(region =
ifelse(division %in% c("AFC East", "NFC East"), "East",
ifelse(division %in% c("AFC North", "NFC North"), "North",
ifelse(division %in% c("AFC South", "NFC South"), "South",
"West"))))
```
```{r}
nfl <- nfl %>% mutate(WLRatio = wins/losses)
```
```{r}
nfl %>% ggplot(aes(x=WLRatio, col = region)) + geom_density() + facet_wrap(~div_general) + xlim(0,6)
```
Is the distribution of wins/losses tight or spread out?
Pull out most competitive teams for each division over the years
```{r}
# Max win/loss ratio from each division
#nfl %>%
# mutate(wl_ratio = wins/losses) %>%
# group_by(season, division2) %>%
# slice(which.max(wl_ratio)) %>%
# select(season, team, wl_ratio, division2) %>%
# ggplot(aes(x = season, y = wl_ratio, color = division2)) +
# geom_line() +
# theme_bw()
library(gridExtra)
# Total wins for each division
4 * sum(nfl$wins)/nrow(nfl)
nfl %>%
mutate(wl_ratio = wins/losses) %>%
filter(division2 == "NFC") %>%
group_by(season, division) %>%
summarize(total_wins = sum(wins), wl_ratio_avg = mean(wl_ratio)) %>%
ggplot(aes(x = season, y = total_wins)) +
geom_hline(yintercept=31) +
geom_line(color="blue") +
ylim(21, 42) +
facet_wrap(~division) +
theme_bw() -> wins.nfc.plt
nfl %>%
mutate(wl_ratio = wins/losses) %>%
filter(division2 == "AFC") %>%
group_by(season, division) %>%
summarize(total_wins = sum(wins), wl_ratio_avg = mean(wl_ratio)) %>%
ggplot(aes(x = season, y = total_wins)) +
geom_hline(yintercept=31) +
geom_line(color="red") +
ylim(21, 42) +
facet_wrap(~division) +
theme_bw() -> wins.afc.plt
library(ggridges)
library(ggthemes)
library(plotly)
nfl %>%
mutate(wl_ratio = wins/losses) %>%
ggplot(aes(x=wl_ratio, y=as.factor(season))) +
geom_density_ridges() +
geom_vline(xintercept=1) +
facet_wrap(~division2) +
xlim(-1, 5) +
theme_ridges()
grid.arrange(wins.nfc.plt, wins.afc.plt, nrow = 1)
```
```{r}
library(ggridges)
library(ggthemes)
nfl %>%
ggplot(aes(x=WLRatio, y=as.factor(season), fill = div_general)) +
geom_density_ridges(alpha = 0.7) +
xlim(-1, 5) +
geom_vline(xintercept=1) +
labs(x = "Win / Loss Ratio", y = NULL, title = "Division Records Over Time",
fill = "Division") +
theme_gray()
```
#nfl %>% group_by(div_general, team) %>%
# summarize(mean_WL = mean(WLRatio)) %>%
# arrange(desc(mean_WL)) %>%
# head(10)
nfl %>% group_by(div_general, team) %>%
summarize(mean_WL = mean(WLRatio)) %>%
arrange(desc(mean_WL)) %>%
head(10)
```{r}
#top10 <- c("NE", "GB", "CAR", "LA", "NO", "LAC", "PIT", "IND", "DEN", "ATL")
```
```{r}
#topTeams <- nfl %>% filter(team %in% top10)
```
```{r}
top_teams_by_year <- nfl %>%
group_by(season, div_general) %>%
select(season, team, WLRatio) %>% top_n(n=1)
```
```{r}
ggplot(top_teams_by_year, aes(x=season, y=WLRatio, fill = team)) + geom_bar(stat = "identity", position = "dodge", col = "black") +
facet_wrap(~div_general) +
scale_fill_manual(breaks = c("ATL", "CAR", "CIN", "DAL", "DEN", "GB", "IND", "KC", "LA", "LAC", "MIN", "NE", "NO", "PHI", "PIT", "SEA"),
values = c("darkred", "turquoise2", "darkorange2", "gray77", "black", "darkgreen", "darkblue", "red", "goldenrod2", "powderblue", "purple", "blue", "tan", "aquamarine4", "yellow", "chartreuse1"),
labels = c("Atlanta Falcons", "Carolina Panthers", "Cincinnati Bengals", "Dallas Cowboys", "Denver Broncos", "Green Bay Packers", "Indianapolis Colts", "Kansas City Chiefs", "LA Rams", "LA Chargers", "Minnesota Vikings", "New England Patriots", "New Orleans Saints", "Philadelphia Eagles", "Pittsburgh Steelers", "Seattle Seahawks")) + theme(legend.position = "bottom") + labs(x = NULL, y = "Win / Loss Ratio", title = "The Best Performing NFL Teams", subtitle = "The AFC has more consistent top teams than NFC", fill = NULL) + scale_x_continuous(breaks = c(2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018), labels = c("'09", "'10", "'11", "'12", "'13", "'14", "'15", "'16", "'17", "'18"))
```
2. Which works better -- passing or running?
Among the winningest teams in each division, how much do they pass vs run? (as a percentage of total yards)
Wins vs passing yards
Wins vs running yards
```{r}
nfl %>%
mutate(total_yds = pass_off_total_yards_gained + run_off_total_yards_gained) %>%
group_by(season, division2) %>%
summarise(total_yards = sum(total_yds)) %>%
ggplot(aes(x = season, y = total_yards, color = division2)) + geom_line() + geom_smooth(method = "lm", se = F)
nfl_passrun <- nfl %>%
mutate(pass = pass_off_total_yards_gained/(pass_off_total_yards_gained + run_off_total_yards_gained), run = run_off_total_yards_gained/(pass_off_total_yards_gained + run_off_total_yards_gained))
```
```{r}
nfl_passrun <- nfl_passrun %>% filter(team %in% top10) %>% select(team, season, pass, run)
```
```{r}
nfl_passrun %>% gather(key = "type", value = "pct", pass, run) %>%
group_by(team, type) %>%
summarize(pct = mean(pct)) %>%
ggplot(aes(x=team, y=pct, fill = type)) + geom_bar(stat = "identity", position = "fill") + geom_hline(yintercept = 0.331, linetype = "dashed") + xlab(NULL) + ylab("% of Offensive Yards") + labs(title = "What Makes A Top Team? Running or Passing?", subtitle = "The most competitive teams play similarly to each other, but pass more than average", fill = "Play Type")
```
3. Offensive vs defense importance for wins? Which is more impactful?
Look at a sample of the best teams and a sample of the worst teams.
See how many passing/running yards the defense allows.
Look at how many passing/running yards the offense makes.
```{r}
nfl <- nfl %>% mutate(point_ratio = points_scored/points_allowed)
nfl <- nfl %>% mutate(win_majority = ifelse(WLRatio > 0.5, "Win Majority", "Lose Majority"))
nfl <- nfl %>% mutate(games = wins + losses + ties,
avg_scored = points_scored / games,
avg_allowed = points_allowed / games)
nfl <- nfl %>% mutate(offense_strength = ifelse(avg_scored > 25, "High Scoring (>25 Points Scored/Game)", ifelse(avg_scored > 18, "Medium Scoring (>18)", "Low Scoring (<=18)")),
defense_strength = ifelse(avg_allowed < 18, "Strong (<18 Points Allowed/Game)", ifelse(avg_allowed < 25, "Medium (<25)", "Weak (>=25)")))
```
```{r}
nfl$offense_strength <- factor(nfl$offense_strength, levels = c("High Scoring (>25 Points Scored/Game)","Medium Scoring (>18)", "Low Scoring (<=18)"))
nfl$defense_strength <- factor(nfl$defense_strength, levels = c("Strong (<18 Points Allowed/Game)", "Medium (<25)", "Weak (>=25)"))
nfl %>% ggplot(aes(x=point_ratio, y = WLRatio, col = offense_strength)) + geom_point(size = 2, alpha = 0.8) + facet_wrap(~defense_strength) + labs(x = "Points Scored / Points Allowed", y = "Win / Loss Ratio", title = "Offense and Defense Strength", subtitle = "To win, you need a balance", col = "Offense Strength") + ylim(0, 5) + scale_color_manual(breaks = c("High Scoring (>25 Points Scored/Game)","Medium Scoring (>18)", "Low Scoring (<=18)"), values = c("green", "gold", "red")) + theme(legend.position = "bottom") + geom_hline(yintercept = 1, linetype = "dashed") + geom_vline(xintercept = 1.00, linetype = "dashed")
```
```{r}
nfl <- nfl %>% mutate(record = ifelse(WLRatio > 2, "Double Positive", ifelse(WLRatio >= 1, "Positive", "Negative")),
total_offensive_gained = run_off_total_yards_gained + pass_off_total_yards_gained,
total_defensive_allowed = run_def_total_yards_allowed + pass_def_total_yards_allowed)
nfl$record <- factor(nfl$record, levels = c("Double Positive", "Positive", "Negative"))
ggplot(nfl, aes(x=total_defensive_allowed, y=total_offensive_gained, col = record)) + geom_point() + scale_color_manual(breaks = c("Double Positive", "Positive", "Negative"), values = c("green", "gold", "red")) + labs(title = "Offense and Defense Strength", subtitle = "Competitive teams are typically good at both", x = "Total Defensive Yards Allowed in Season", y = "Total Offensive Yards Gained in Season", col = "Win Record")
```
```{r}
ggplot(nfl, aes(x=reorder(team, avg_scored, FUN = median), y = avg_scored, fill = div_general)) + geom_boxplot() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab(NULL) + ylab("Avg Points Per Game") + labs(title = "The Top Teams Aren't Always Consistent", subtitle = "The NFL appears very competitive, with decent upset chances", fill = "Division")
```
```{r}
ggplot(nfl, aes(x=reorder(team, WLRatio, FUN = median), y = WLRatio, fill = div_general)) + geom_boxplot() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab(NULL) + ylab("Win / Loss Ratio") + labs(title = "The Top Teams Aren't Always Consistent", subtitle = "The NFL appears very competitive, with decent upset chances", fill = "Division") + geom_hline(yintercept = 1, linetype = "dashed", color = "red")
```
```{r}
ggplot(nfl, aes(x=reorder(team, WLRatio, FUN = median), y = WLRatio, fill = div_general)) + geom_boxplot() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab(NULL) + ylab("Win / Loss Ratio") + labs(title = "The Top Teams Aren't Always Consistent", subtitle = "The NFL appears very competitive, with decent upset chances", fill = "Division") + geom_hline(yintercept = 1, linetype = "dashed", color = "red") + ylim(0,5)
```