-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproject.Rmd
400 lines (349 loc) · 13.9 KB
/
project.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Jess, Anna and Seth Project
# 12/6/19
# project r markdown document
```{r}
source('packages.R')
source('styleguide.R')
source('helpers.R')
source('cleaner.R')
source('models.R')
```
# Get the Data
```{r}
# Read in Clean DF
df.clean <- add_time("complete_data_clean.csv")
df.clean <- add_coach_change(df.clean)
df.tourney <- add_time("tourney_data_clean.csv")
df.tourney <- add_coach_change(df.tourney)
```
```{r}
# Check dimensions - len(unique schools) * len(unique years) must equal # of rows
dim_checker(df.clean)
dim_checker(df.tourney)
get_newprop = cbind(df.tourney$School, get_prop_df(df.tourney))
```
```{r}
# gets the minimum slope
sorted_alpha_school = df.tourney[order(df.tourney$School),]
sorted_alpha_prop = get_newprop[order(get_newprop$`df.tourney$School`),]
sorted_alpha_school$winner = (sorted_alpha_school$W.L. >= 0.5) * 1
```
```{r}
total_wins <- aggregate(sorted_alpha_school$winner, by=list(sorted_alpha_school$School), FUN=sum)[2]
total_3s_made <- aggregate(sorted_alpha_prop$X3P, by=list(sorted_alpha_prop$`df.tourney$School`), FUN=mean)[2]
total_3s_attempted <- aggregate(sorted_alpha_prop$X3PA, by=list(sorted_alpha_prop$`df.tourney$School`), FUN=mean)[2]
total_sos <- aggregate(sorted_alpha_school$SOS, by=list(sorted_alpha_school$School), FUN=sum)[2]
```
```{r}
idx_min <- which.min(coef(lmer3d)$School[,2])
school_list <- sort(unique(df.tourney$School)) # alphabetically sort the list
school_list[idx_min]
coef(lmer3d)$School[idx_min, 2]
new_df <- data.frame(school = school_list, slope = coef(lmer3d)$School[,2], wins = total_wins, threes_m = total_3s_made, sos = total_sos)
new_df = new_df[order(new_df$slope),]
```
# Find the Schools Most Prone and Least Prone by taking the Absolute value (slopes closer to 0 will be least impacted by the change) and we define most affected as a larger absolute value
```{r}
# check for with absolute value
abs_df <- new_df
abs_df$slope = abs(abs_df$slope)
abs_df = abs_df[order(abs_df$slope),]
abs_df
close_zero_least <- abs_df[1:50,]
close_zero
close_zero_most <- abs_df[181:230,]
```
```{r}
# histograms - teams least prone
ggplot(close_zero_least, aes(x = x)) +
geom_histogram() +
geom_vline(aes(xintercept=mean(total_wins$x)),
color="red", size=1) +
geom_vline(aes(xintercept=mean(close_zero_least$x)),
color="blue",linetype="dashed", size=1) +
geom_vline(aes(xintercept=mean(close_zero_most$x)),
color="green",linetype="dashed", size=1) +
theme_hodp() +
labs(title="Teams Least Prone to 3PAr Rate Change") +
xlab("Wins in 15 years") +
ylab("Frequency")
cols <- c("All Teams"="red","Most Prone"="green", "Least Prone"="blue")
ggplot(close_zero_least, aes(x = x.1)) +
geom_histogram() +
geom_vline(aes(xintercept=mean(total_3s_made$x), color="All Teams"), size=1) +
geom_vline(aes(xintercept=mean(close_zero_least$x.1), color="Least Prone"), linetype="dashed", size=1) +
geom_vline(aes(xintercept=mean(close_zero_most$x.1), color="Most Prone"), linetype="dashed", size=1) +
theme_hodp() +
labs(title="Teams Least Prone to 3PAr Rate Change") +
theme(legend.position = "right") +
scale_color_manual("Means", values = cols) +
xlab("3 Pointers Made") +
ylab("Frequency")
```
```{r}
# histogram teams most prone
ggplot(close_zero_most, aes(x = x)) +
geom_histogram() +
geom_vline(aes(xintercept=mean(total_wins$x)),
color="red", size=1) +
geom_vline(aes(xintercept=mean(close_zero_most$x)),
color="green",linetype="dashed", size=1) +
geom_vline(aes(xintercept=mean(close_zero_least$x)),
color="blue",linetype="dashed", size=1) +
theme_hodp() +
labs(title="Teams Most Prone to 3PAr Rate Change") +
xlab("Wins in 15 years") +
ylab("Frequency")
ggplot(close_zero_most, aes(x = x.1)) +
geom_histogram() +
geom_vline(aes(xintercept=mean(total_3s_made$x)),
color="red", size=1) +
geom_vline(aes(xintercept=mean(close_zero_most$x.1)),
color="green",linetype="dashed", size=1) +
geom_vline(aes(xintercept=mean(close_zero_least$x.1)),
color="blue",linetype="dashed", size=1) +
theme_hodp() +
labs(title="Teams Most Prone to 3PAr Rate Change") +
theme(legend.position = c(0.8, 0.2)) +
xlab("3 Pointers Made") +
ylab("Frequency")
```
# Comparing the Means with formal hypothesis testing
```{r}
# between the most and least affected teams - games
t.test(close_zero_least$x, close_zero_most$x)
# between the msot and least affected - 3 pointers made per game
t.test(close_zero_least$x.1, close_zero_most$x.1)
```
# Conducting a similar method on the piecewise linear model
```{r}
# more tests on the piece slopes
piece.slopes = coef(lmer9a)$School
piece.slopes$time = abs(piece.slopes$time)
piece.slopes = piece.slopes[order(piece.slopes$time),]
era.zeroslopes = piece.slopes[, 2]
era.oneslopes = era.zeroslopes + piece.slopes[, 5]
era.twoslopes = era.zeroslopes + piece.slopes[, 6]
piece_df <- data.frame(school = school_list, slope0 = era.zeroslopes, slope1 = era.oneslopes, slope2 = era.twoslopes, wins = total_wins, threes_m = total_3s_made)
piece_df0 = piece_df[order(piece_df$slope0),]
piece_df1 = piece_df[order(piece_df$slope1),]
piece_df2 = piece_df[order(piece_df$slope2),]
```
# Comparing the Means for piecewise with formal hypothesis testing
```{r}
piece_df0_least <- piece_df0[1:50, ]
piece_df0_most <- piece_df0[(nrow(piece_df0) - 50):nrow(piece_df0), ]
piece_df1_least <- piece_df1[1:50, ]
piece_df1_most <- piece_df1[(nrow(piece_df1) - 50):nrow(piece_df1), ]
piece_df2_least <- piece_df2[1:50, ]
piece_df2_most <- piece_df2[(nrow(piece_df2) - 50):nrow(piece_df2), ]
# t tests for wins
mean(total_wins$x)
t.test(piece_df0_least$x, piece_df0_most$x)
t.test(piece_df1_least$x, piece_df1_most$x)
t.test(piece_df2_least$x, piece_df2_most$x)
# t tests for 3 pointers made, p value 0.05
mean(total_3s_made$x)
t.test(piece_df0_least$x.1, piece_df0_most$x.1)
t.test(piece_df1_least$x.1, piece_df1_most$x.1)
t.test(piece_df2_least$x.1, piece_df2_most$x.1)
```
# Comparisons between Schools
```{r}
#eda correlation
data <- read.csv('data/full_data_raw.csv')
wl <- data %>% select(TeamW, TeamL, W.L., ConfW, ConfL, HomeW, HomeL, AwayW, AwayL)
cor <- round(cor(wl), 1)
p <- ggcorrplot(cor) +
labs(title='Corr Plot for W-L Vars') +
xlab('') + ylab('') +
theme_hodp() +
theme(axis.text.x=element_text(angle=60))
p
```
```{r}
#look at effects for individual teams
uva = df.tourney[df.tourney$School == 'Virginia',]
nova = df.tourney[df.tourney$School == 'Villanova',]
data_teams = rbind(uva, nova)
#plot villanova and overall linear fit
p <- ggplot(nova, aes(x = time + 2003, y = X3PAr)) +
geom_point() +
stat_smooth(method = "lm", col = '#EE3838', se = F) +
labs(title="3PAr Over Time - Pooled") +
xlab("Year") +
ylab("3PAr") +
#ylim(c(0,0.6)) +
theme_hodp()
p
```
```{r}
#plot villanova and uva by color
p <- ggplot(data_teams, aes(x = time + 2003, y = X3PAr, color = School)) +
geom_point(aes(color = School))
p
```
```{r}
#interaction between time and school
lmer9 <- lmer(X3PAr ~ time*era + (1 + time|School), data=data_teams) # fails to converge
lm2 <- lm(X3PAr ~ time*School, data_teams)
summary(lm2)
names(df.clean)
```
```{r}
#interaction between school and time and time and era
data_teams$era <- as.factor((data_teams$year > 2006) + (data_teams$year > 2012))
lm3 <- lm(X3PAr ~ time*(School + era), data_teams)
lmer10 <- lmer(X3PAr ~ time*era + (1 + time|School), data=data_teams) # fails to converge
```
```{r}
# Compare mixed model to lm
summary(lm3)
summary(lmer10)
AIC(lm3)
AIC(lmer10)
```
```{r}
# lm outperforms mixed model
intercept.0 <- summary(lm3)$coef[1,1]t
slope.0 <- summary(lm3)$coef[2,1]
intercept.virgina <- summary(lm3)$coef[3,1]
intercept.1 <- summary(lm3)$coef[4,1]
intercept.2 <- summary(lm3)$coef[5,1]
slope.virginia <- summary(lm3)$coef[6,1]
slope.1 <- summary(lm3)$coef[7,1]
slope.2 <- summary(lm3)$coef[8,1]
```
```{r}
#plot lm3
year <- 2003:2017
fnnova <- function(year, era1, era2) {
return(intercept.0 + (year - 2003) * slope.0 +
intercept.1*era1 + slope.1 * (year - 2003) * era1 +
intercept.2*era2 + slope.2 * (year - 2003) * era2)
}
fnuva <- function(year, era1, era2) {
return(intercept.0 + intercept.virgina + (year - 2003 + slope.virgina) * slope.0 +
intercept.1*era1 + slope.1 * (year - 2003) * era1 +
intercept.2*era2 + slope.2 * (year - 2003) * era2)
}
```
```{r}
#plot interaction between school and time and time and era for just uva
p <- ggplot(data_teams, aes(x = time + 2003, y = X3PAr)) +
geom_point() +
geom_segment(aes(x = 2003, y = fnuva(2003,0,0), xend = 2006, yend = fnuva(2006,0,0),
colour = '#EE3838'),
data = data_teams) +
geom_segment(aes(x = 2007, y = fnuva(2007,1,0), xend = 2012, yend = fnuva(2012,1,0),
colour = '#78C4D4'),
data = data_teams) +
geom_segment(aes(x = 2013, y = fnuva(2013,0,1), xend = 2017, yend = fnuva(2017,0,1),
colour = '#4B5973'),
data = data_teams) +
geom_vline(xintercept = 2012.5) +
geom_vline(xintercept = 2006.5) +
scale_colour_identity(name="Model Type:",
breaks = c('#EE3838','#78C4D4','#4B5973'),
labels = c("2003-2006", "2007-2012", "2012-2013"),
guide = "legend") +
annotate(geom="label", x = 2012.5, y = 0, label = "NBA Revolution", fill ="#F2F2F2", color = "black") +
annotate(geom="label", x = 2006.5, y = 0, label = "NCAA Rule Change", fill ="#F2F2F2", color = "black") +
labs(title="3PAr Over Time - Villanova") +
xlab("Year") +
ylab("3PAr") +
theme_hodp()
p
```
```{r}
#plot interaction between school and time and time and era for both schools
p <- ggplot(data_teams, aes(x = time + 2003, y = X3PAr, color = School)) +
geom_point(aes(color = School)) +
scale_color_manual(values = c("#0000FF", "#00FFFF", "#0000FF", "#00FFFF"))+
#geom_point(color = c('#EE3838', '#78C4D4')) +
geom_segment(aes(x = 2003, y = fnnova(2003,0,0), xend = 2006, yend = fnnova(2006,0,0),
colour = 'Villanova'),
data = data_teams) +
geom_segment(aes(x = 2007, y = fnnova(2007,1,0), xend = 2012, yend = fnnova(2012,1,0),
colour = 'Villanova'),
data = data_teams) +
geom_segment(aes(x = 2013, y = fnnova(2013,0,1), xend = 2017, yend = fnnova(2017,0,1),
colour = 'Villanova'),
data = data_teams) +
geom_segment(aes(x = 2003, y = fnuva(2003,0,0), xend = 2006, yend = fnuva(2006,0,0),
colour = 'Virginia'),
data = data_teams) +
geom_segment(aes(x = 2007, y = fnuva(2007,1,0), xend = 2012, yend = fnuva(2012,1,0),
colour = 'Virginia'),
data = data_teams) +
geom_segment(aes(x = 2013, y = fnuva(2013,0,1), xend = 2017, yend = fnuva(2017,0,1),
colour = 'Virginia'),
data = data_teams) +
geom_vline(xintercept = 2012.5) +
geom_vline(xintercept = 2006.5) +
annotate(geom="label", x = 2012.5, y = 0, label = "NBA Revolution", fill ="#F2F2F2", color = "black") +
annotate(geom="label", x = 2006.5, y = 0, label = "NCAA Rule Change", fill ="#F2F2F2", color = "black") +
labs(title="3PAr Over Time By School") +
xlab("Year") +
ylab("3PAr") +
theme_hodp()
p
```
```{r}
#interaction between school and time and time and era and school and era
data_teams$era <- as.factor((data_teams$year > 2006) + (data_teams$year > 2012))
lm4 <- lm(X3PAr ~ time*(School + era) +School*era , data_teams)
summary(lm4)
intercept.0 <- summary(lm4)$coef[1,1]
slope.0 <- summary(lm4)$coef[2,1]
intercept.virgina <- summary(lm4)$coef[3,1]
intercept.1 <- summary(lm4)$coef[4,1]
intercept.2 <- summary(lm4)$coef[5,1]
slope.virginia <- summary(lm4)$coef[6,1]
slope.1 <- summary(lm4)$coef[7,1]
slope.2 <- summary(lm4)$coef[8,1]
intercept.1.virginia <- summary(lm4)$coef[9,1]
intercept.2.virginia <- summary(lm4)$coef[10,1]
year <- 2003:2017
fnnova <- function(year, era1, era2) {
return(intercept.0 + (year - 2003) * slope.0 +
intercept.1*era1 + slope.1 * (year - 2003) * era1 +
intercept.2*era2 + slope.2 * (year - 2003) * era2)
}
fnuva <- function(year, era1, era2) {
return(intercept.0 + intercept.virgina + (year - 2003 + slope.virgina) * slope.0 +
(intercept.1 + intercept.1.virginia) *era1 + slope.1 * (year - 2003) * era1 +
(intercept.2 + intercept.2.virginia) *era2 + slope.2 * (year - 2003) * era2)
}
p <- ggplot(data_teams, aes(x = time + 2003, y = X3PAr, color = School)) +
geom_point(aes(color = School)) +
scale_color_manual(values = c("#EE3838", "#0000FF", "#EE3838", "#0000FF"))+
#geom_point(color = c('#EE3838', '#78C4D4')) +
geom_segment(aes(x = 2003, y = fnnova(2003,0,0), xend = 2006, yend = fnnova(2006,0,0),
colour = 'Villanova'),
data = data_teams) +
geom_segment(aes(x = 2007, y = fnnova(2007,1,0), xend = 2012, yend = fnnova(2012,1,0),
colour = 'Villanova'),
data = data_teams) +
geom_segment(aes(x = 2013, y = fnnova(2013,0,1), xend = 2017, yend = fnnova(2017,0,1),
colour = 'Villanova'),
data = data_teams) +
geom_segment(aes(x = 2003, y = fnuva(2003,0,0), xend = 2006, yend = fnuva(2006,0,0),
colour = 'Virginia'),
data = data_teams) +
geom_segment(aes(x = 2007, y = fnuva(2007,1,0), xend = 2012, yend = fnuva(2012,1,0),
colour = 'Virginia'),
data = data_teams) +
geom_segment(aes(x = 2013, y = fnuva(2013,0,1), xend = 2017, yend = fnuva(2017,0,1),
colour = 'Virginia'),
data = data_teams) +
geom_vline(xintercept = 2012.5) +
geom_vline(xintercept = 2006.5) +
annotate(geom="label", x = 2012.5, y = 0, label = "NBA Revolution", fill ="#F2F2F2", color = "black") +
annotate(geom="label", x = 2006.5, y = 0, label = "NCAA Rule Change", fill ="#F2F2F2", color = "black") +
labs(title="3PAr Over Time By School") +
xlab("Year") +
ylab("3PAr") +
theme_hodp()
p
```