-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatcast modeling.R
294 lines (221 loc) · 12.7 KB
/
Statcast modeling.R
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# functions ---------------------------------------------------------------
source("./define_functions.R")
# prepare data frames -----------------------------------------------------
print("Preparing data frames...")
# .rds file is updated by update_data_files.R
if (!exists("original_batted") || !is.data.frame(get("original_batted"))) {
tryCatch({original_batted <- readRDS("./data/completed_seasons_statcast_batted_balls.rds")},
error=function(err) {
print("Missing Statcast data file.")
})
}
batted <- format_data_frame(original_batted, lw_year=2015:2017)
# fit OLS model --------------------------------------------------------
# lmod <- lm(linear_weight ~ launch_speed + launch_angle + spray_angle, data=batted)
# batted$lm_linear_weight <- predict(lmod, newdata=batted)
# Note: OLS model only predicts linear weight (no class probabilities) and isn't very good (as expected)
# fit multinomial logistic regression model ----------------------------------------
print("Fitting multinomial...")
library(nnet)
# mod.multinom <- multinom(class ~ launch_speed + launch_angle + spray_angle, data=batted)
# saveRDS(mod.multinom, file="./models/multinom.rds")
mod.multinom <- readRDS("./models/multinom.rds")
probs.multinom <- predict(mod.multinom, newdata=batted, type="prob")
batted <- add_preds_from_probs(batted, "multinom", probs.multinom)
# Note: additional features (e.g., speed scores) did not improve the multinomial model
# fit random forest models ------------------------------------------------
print("Fitting random forest...")
library(randomForest)
# here's how the models were trained:
# set.seed(1)
# which.train <- sample(1:dim(batted)[1], 2e5)
# train <- batted[which.train,]
# rf <- randomForest(class ~ launch_speed + launch_angle + spray_angle + Spd + home_team, data=train)
rf <- readRDS("./models/rf.rds")
probs.rf <- predict(rf, newdata=batted, type="prob")
batted <- add_preds_from_probs(batted, "rf", probs.rf)
# Note: I tried adding defensive shifts, but it didn't make much difference (actually slightly worse)
# rf.shift <- randomForest(class ~ launch_speed + launch_angle + spray_angle + Spd + shift, data=train)
rf.old <- readRDS("./models/rf.old.rds")
probs.rf.old <- predict(rf.old, newdata=batted, type="prob")
batted <- add_preds_from_probs(batted, "rf.old", probs.rf.old)
# rf.simple <- randomForest(class ~ launch_speed + launch_angle + spray_angle, data=train)
# saveRDS(rf.simple, "./models/rf.simple.rds")
rf.simple <- readRDS("./models/rf_simple.rds")
probs.rf.simple <- predict(rf.simple, newdata=batted, type="prob")
batted <- add_preds_from_probs(batted, "rf.simple", probs.rf.simple)
# fit kNN model -----------------------------------------------------------
# print("Fitting kNN...")
# library(caret)
# tune k with 'knn cross validation.R'
# k=60 works well
# here's how the model was trained:
# currently fitting model on half the data
# could fit on all data, but would need to re-tune k and it won't make much difference
# knnmod <- fit_knn_model(batted, k=60, trainSize=0.5, seed=1)
knnmod <- readRDS("./models/knn.rds")
probs.knn <- predict(knnmod,newdata=batted,type="prob")
probs.knn <- as.matrix(probs.knn) # it was returning a list, which caused problems with the next function
batted <- add_preds_from_probs(batted, "knn", probs.knn)
# fit other models --------------------------------------------------------
# potentially try more models in the future
print("Done fitting models.")
# confusion matrix --------------------------------------------------------
print("Building confusion matrix...")
preds.multinom <- predict(mod.multinom, newdata=batted)
preds.rf <- predict(rf, newdata=batted)
preds.knn <- predict(knnmod, newdata=batted)
preds.rf.old <- predict(rf.old, newdata=batted)
preds.rf.simple <- predict(rf.simple, newdata=batted)
order <- c("out","single","double","triple","home_run")
batted$class <- factor(batted$class, levels=order)
preds.multinom <- factor(preds.multinom, levels=order)
preds.rf <- factor(preds.rf, levels=order)
preds.knn <- factor(preds.knn, levels=order)
preds.rf.old <- factor(preds.rf.old, levels=order)
preds.rf.simple <- factor(preds.rf.simple, levels=order)
mx.multinom <- caret::confusionMatrix(preds.multinom, batted$class)
mx.rf <- caret::confusionMatrix(preds.rf, batted$class)
mx.knn <- caret::confusionMatrix(preds.knn, batted$class)
mx.rf.old <- caret::confusionMatrix(preds.rf.old, batted$class)
mx.rf.simple <- caret::confusionMatrix(preds.rf.simple, batted$class)
# group linear weights by player ------------------------------------------
# these are the defaulst in the next function (just leaving here for easy access)
lw.prefixes <- get_prefixes(batted, type="lw")
full.prefixes <- get_prefixes(batted, type="full")
weights.df <- group_weights_by_year(batted)
# weights_by_month.df <- group_weights_by_year(batted, by_month=TRUE)
# add wOBA to Lahman database ---------------------------------------------
batting.df <- add_preds_to_yearly_data(weights.df)
# need to do the same for monthly data if I'm going to do month-to-month correlations
AB_cutoff <- 100
sub.oneyear <- batting.df %>%
filter(AB >= AB_cutoff)
batting_lagged <- lag_yearly_data(batting.df)
sub.lag <- batting_lagged %>%
filter(AB >= AB_cutoff & AB.prev >= AB_cutoff)
# get Marcel projections --------------------------------------------------
print("Creating Marcel projections...")
# 2018 projections
# eval.df.2018 <- get_marcel_eval_df(2018, lw_years=2015:2017, pred_df=batting.df, include_true_stats=FALSE)
eval.df.2018 <- get_marcel_eval_df(2018, lw_years=2016:2018, pred_df=batting.df, AB_cutoff=AB_cutoff)
steamer.2018 <- read_csv("./projections/Steamer projections 2018.csv", col_types=cols()) %>%
rename("X1B" = "1B", "X2B" = "2B", "X3B" = "3B",
"key_mlbam" = "mlbamid")
eval.df.2018 <- add_steamer_to_eval_df(eval.df.2018, steamer.2018)
# 2017 projections (for evaluation)
steamer.2017 <- read_csv("./projections/Steamer projections 2017.csv", col_types=cols()) %>%
rename("X1B" = "1B", "X2B" = "2B", "X3B" = "3B",
"key_mlbam" = "mlbamid")
eval.df.2017 <- get_marcel_eval_df(2017, lw_years=2015:2017, pred_df=batting.df, AB_cutoff=AB_cutoff)
eval.df.2017 <- add_steamer_to_eval_df(eval.df.2017, steamer.2017)
marcel_eval_plot(eval.df.2017, model_desc="Marcel")
marcel_eval_plot(eval.df.2017, model_prefix="rf", model_desc="RF")
marcel_eval_plot(eval.df.2017, model_prefix="knn", model_desc="kNN")
marcel_eval_plot(eval.df.2017, model_prefix="multinom", model_desc="multinom")
marcel_eval_plot(eval.df.2017, model_prefix="steamer", model_desc="Steamer")
marcel_eval_plot(eval.df.2017, model_prefix="rf.old", model_desc="RF old")
marcel_eval_plot(eval.df.2017, model_prefix="rf.simple", model_desc="RF simple")
eval.df.full <- bind_rows(eval.df.2017, eval.df.2018)
# 2016 projections
# eval.df.2016 <- get_marcel_eval_df(2016, lw_years=2015:2017, pred_df=batting.df, AB_cutoff=AB_cutoff)
# marcel_eval_plot(eval.df.2016, model_desc="Marcel")
# marcel_eval_plot(eval.df.2016, model_prefix="rf", model_desc="RF")
# marcel_eval_plot(eval.df.2016, model_prefix="knn", model_desc="kNN")
# 2007 projections
# match the analysis here:
# https://web.archive.org/web/20080111231423/http://www.baseballprospectus.com/unfiltered/?p=564
# (Nate Silver got 0.591 correlation with OPS for Marcel projections)
# (Not sure why the correlation is so much higher than for 2017. Is Marcel becoming less reliable
# in the new hitting environment? Are other projection systems also becoming less reliable? Was 2016
# just a particularly difficult year?)
# eval.df.2007 <- get_marcel_eval_df(2007, AB_cutoff=100)
# marcel_eval_plot(eval.df.2007, model_desc="Marcel")
# evaluate Marcel projections ---------------------------------------------
summary.2017.wOBA <- create_eval_summary(eval.df.2017)
summary.2017.OPS <- create_eval_summary(eval.df.2017, stat="OPS")
summary.2017.OBP <- create_eval_summary(eval.df.2017, stat="OBP")
summary.2017.SLG <- create_eval_summary(eval.df.2017, stat="SLG")
summary.2018.wOBA <- create_eval_summary(eval.df.2018)
summary.2018.OPS <- create_eval_summary(eval.df.2018, stat="OPS")
summary.2018.OBP <- create_eval_summary(eval.df.2018, stat="OBP")
summary.2018.SLG <- create_eval_summary(eval.df.2018, stat="SLG")
summary.full.wOBA <- create_eval_summary(eval.df.full)
summary.full.OPS <- create_eval_summary(eval.df.full, stat="OPS")
summary.full.OBP <- create_eval_summary(eval.df.full, stat="OBP")
summary.full.SLG <- create_eval_summary(eval.df.full, stat="SLG")
# projection system comparison --------------------------------------------
# visualize correlation, MAE, and RMSE in a plot
p <- plot_projection_summary(summary.2017.wOBA,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
# which=c("marcel", "steamer", full.prefixes),
# plot.title="Relative Accuracy of\nProjections",
# subtitle=NULL
); print(p)
p <- plot_projection_summary(summary.2017.OPS,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel")); print(p)
p <- plot_projection_summary(summary.2017.OBP,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel")); print(p)
p <- plot_projection_summary(summary.2017.SLG,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel")); print(p)
# 2018
p <- plot_projection_summary(summary.2018.wOBA,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
# which=c("marcel", "steamer", full.prefixes),
# plot.title="Relative Accuracy of\nProjections",
# subtitle=NULL
marcel_at_zero=TRUE
# ,scale=FALSE
# ,metrics=c("mae", "rmse")
); print(p)
p <- plot_projection_summary(summary.2018.OPS,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
marcel_at_zero=FALSE
); print(p)
p <- plot_projection_summary(summary.2018.OBP,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
marcel_at_zero=FALSE
); print(p)
p <- plot_projection_summary(summary.2018.SLG,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
marcel_at_zero=FALSE
); print(p)
# both years
p <- plot_projection_summary(summary.full.wOBA,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
# which=c("marcel", "steamer", full.prefixes),
# plot.title="Relative Accuracy of\nProjections",
subtitle=("(2017-18 wOBA)"),
point_size=3.5,
text_size=16,
title_size=20,
marcel_at_zero=TRUE
); print(p)
p <- plot_projection_summary(summary.full.OPS,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
marcel_at_zero=FALSE
); print(p)
p <- plot_projection_summary(summary.full.OBP,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
marcel_at_zero=FALSE
); print(p)
p <- plot_projection_summary(summary.full.SLG,
which=c("marcel", "steamer", "multinom", "rf"),
names=c("Marcel", "Steamer", "MLR Marcel", "RF Marcel"),
marcel_at_zero=FALSE
); print(p)
library(knitr)
kable(summary.2017.wOBA, digits=3)
kable(scale_eval_summary(summary.2017.wOBA), digits=3)
# kable(summary.2017.wOBA, digits=3, format="latex")