-
Notifications
You must be signed in to change notification settings - Fork 0
/
Predictive Modeling (NFL Attendance).Rmd
154 lines (120 loc) · 3.62 KB
/
Predictive Modeling (NFL Attendance).Rmd
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
---
title: "Predictive Modeling (NFL Attendance)"
author: Ng Wei Keat, 20793486
output: github_document
---
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(cache = TRUE, warning = FALSE, message = FALSE, echo = TRUE, dpi = 180, fig.width = 8, fig.height = 5)
library(tidyverse)
```
I'll be following and stepping through Julia Silge's video on Predictive Modeling about NFL attendance to build a simple model!
## Explore Data
```{r}
library(tidyverse)
attendance <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-04/attendance.csv')
standings <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-04/standings.csv')
attendance_joined <- attendance %>%
left_join(standings, by = c("year", "team_name", "team"))
```
```{r}
attendance_joined %>%
filter(!is.na(weekly_attendance)) %>%
ggplot(aes(fct_reorder(team_name, weekly_attendance),
weekly_attendance, fill = playoffs)) +
geom_boxplot(outlier.alpha = 0.5) +
coord_flip()
```
```{r}
attendance_joined %>%
distinct(team_name, year, margin_of_victory, playoffs) %>%
ggplot(aes(margin_of_victory, fill = playoffs)) +
geom_histogram(position = "identity", alpha = 0.7)
```
```{r}
attendance_joined %>%
mutate(week = factor(week)) %>%
ggplot(aes(week, weekly_attendance, fill = week)) +
geom_boxplot(show.legend = FALSE, outlier.alpha = 0.4)
```
```{r}
attendance_df <- attendance_joined %>%
filter(!is.na(weekly_attendance)) %>%
select(weekly_attendance, team_name, year, week,
margin_of_victory, strength_of_schedule, playoffs)
attendance_df
```
## Train Model
```{r}
library(tidymodels)
set.seed(20793486)
attendance_split <- attendance_df %>%
initial_split(strata = playoffs)
nfl_train <- training(attendance_split)
nfl_test <- testing(attendance_split)
```
```{r}
lm_spec <- linear_reg() %>%
set_engine(engine = "lm")
lm_fit <- lm_spec %>%
fit(weekly_attendance ~., data = nfl_train)
```
```{r}
rf_spec <- rand_forest(mode = "regression") %>%
set_engine(engine = "ranger")
rf_fit <- rf_spec %>%
fit(weekly_attendance ~., data = nfl_train)
```
## Evaluate Model
```{r}
results_train <- lm_fit %>%
predict(new_data = nfl_train) %>%
mutate(truth = nfl_train$weekly_attendance, model = "lm") %>%
bind_rows(rf_fit %>%
predict(new_data = nfl_train) %>%
mutate(truth = nfl_train$weekly_attendance, model = "rf"))
results_test <- lm_fit %>%
predict(new_data = nfl_test) %>%
mutate(truth = nfl_test$weekly_attendance, model = "lm") %>%
bind_rows(rf_fit %>%
predict(new_data = nfl_test) %>%
mutate(truth = nfl_test$weekly_attendance, model = "rf"))
```
```{r}
results_train %>%
group_by(model) %>%
rmse(truth = truth, estimate = .pred)
results_test %>%
group_by(model) %>%
rmse(truth = truth, estimate = .pred)
```
```{r}
results_test %>%
mutate(train = "testing") %>%
bind_rows(results_train %>%
mutate(train = "training")) %>%
ggplot(aes(truth, .pred, color = model)) +
geom_abline(lty = 2, color = "gray80", size = 1.5) +
geom_point(alpha = 0.5) +
facet_wrap(~train)
```
## Resampling to improve estimates
```{r}
set.seed(20793486)
nfl_folds <- vfold_cv(nfl_train, strata = playoffs)
rf_result <- fit_resamples(
weekly_attendance ~.,
rf_spec,
nfl_folds,
control = control_resamples(save_pred = TRUE)
)
rf_result %>%
collect_metrics()
```
```{r}
rf_result %>%
unnest(.predictions) %>%
ggplot(aes(weekly_attendance, .pred, color = id)) +
geom_abline(lty = 2, color = "gray80", size = 1.5) +
geom_point(alpha = 0.5)
```