forked from garysutton/statisticsplaybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CH03.R
375 lines (337 loc) · 13.1 KB
/
CH03.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
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
library(tidyverse)
library(networkD3)
library(patchwork)
packages <- c("tidyverse", "networkD3", "patchwork")
lapply(packages, library, character.only = TRUE)
draft2 <- read_csv("/Users/garysutton/Library/Mobile Documents/com~apple~CloudDocs/draft2.csv")
glimpse(draft2)
dim(draft2)
mutate(draft2, Pk2 = ifelse(Pk %in% 1:5, "1-5",
ifelse(Pk %in% 6:10, "6-10",
ifelse(Pk %in% 11:15, "11-15",
ifelse(Pk %in% 16:20, "16-20",
ifelse(Pk %in% 21:25, "21-25",
ifelse(Pk %in% 26:30, "26-30", "NA"))))))) -> draft2
draft2$Pk2 <- as.factor(draft2$Pk2)
sumG <- sum(draft2$G)
draft2 %>%
group_by(Pk2) %>%
summarize(mean = mean(G),
median = median(G),
pct = sum(G)/sumG) -> tibble1
print(tibble1)
g1 <- ggplot(tibble1, aes(x = Pk2, y = mean)) +
geom_bar(stat = "identity", width = .8,
fill = "coral", color = "coral4") +
labs(title = "Average Career Games Played",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment",
y = "Average Career Games Played",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = trunc(mean), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 800) +
theme(plot.title = element_text(face = "bold"))
g2 <- ggplot(tibble1, aes(x = Pk2, y = median)) +
geom_bar(stat = "identity", width = .8,
fill = "coral3", color = "coral4") +
labs(title = "Median Career Games Played",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment",
y = "Median Career Games Played",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = trunc(median), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 800) +
theme(plot.title = element_text(face = "bold"))
g1 + g2 + plot_layout(ncol = 1)
sumMP <- sum(draft2$MP)
draft2 %>%
group_by(Pk2) %>%
summarize(mean = mean(MP),
median = median(MP),
pct = sum(MP)/sumMP) -> tibble2
mp1 <- ggplot(tibble2, aes(x = Pk2, y = mean)) +
geom_bar(stat = "identity", width = .8,
fill = "deepskyblue", color = "deepskyblue4") +
labs(title = "Average Minutes Played per Game",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment",
y = "Average Minutes Played per Game",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = trunc(mean), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 30) +
theme(plot.title = element_text(face = "bold"))
mp2 <- ggplot(tibble2, aes(x = Pk2, y = median)) +
geom_bar(stat = "identity", width = .8,
fill = "deepskyblue3", color = "deepskyblue4") +
labs(title = "Median Minutes Played per Game",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment",
y = "Median Minutes Played per Game",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = trunc(median), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 30) +
theme(plot.title = element_text(face = "bold"))
mp1 + mp2 + plot_layout(ncol = 1)
sumWS <- sum(draft2$WS)
draft2 %>%
group_by(Pk2) %>%
summarize(mean = mean(WS),
median = median(WS),
pct = sum(WS)/sumWS) -> tibble3
ws1 <- ggplot(tibble3, aes(x = Pk2, y = mean)) +
geom_bar(stat = "identity", width = .8,
fill = "springgreen", color = "springgreen4") +
labs(title = "Average Career Win Shares",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment",
y = "Average Career Win Shares",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = trunc(mean), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 60) +
theme(plot.title = element_text(face = "bold"))
ws2 <- ggplot(tibble3, aes(x = Pk2, y = median)) +
geom_bar(stat = "identity", width = .8,
fill = "springgreen3", color = "springgreen4") +
labs(title = "Median Career Win Shares",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment", y = "Median Career Win Shares",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = trunc(median), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 70) +
theme(plot.title = element_text(face = "bold"))
ws1 + ws2 + plot_layout(ncol = 1)
sumWS48 <- sum(draft2$WS48)
draft2 %>%
group_by(Pk2) %>%
summarize(mean = mean(WS48),
median = median(WS48),
pct = sum(WS48)/sumWS48) -> tibble4
ws3 <- ggplot(tibble4, aes(x = Pk2, y = mean)) +
geom_bar(stat = "identity", width = .8,
fill = "gold", color = "gold4") +
labs(title = "Average Win Shares per 48 Minutes",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment",
y = "Average Win Shares per 48 Minutes",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = round(mean, 2), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 0.13) +
theme(plot.title = element_text(face = "bold"))
ws4 <- ggplot(tibble4, aes(x = Pk2, y = median)) +
geom_bar(stat = "identity", width = .8,
fill = "gold3", color = "gold4") +
labs(title = "Median Win Shares per 48 Minutes",
subtitle = "First-Round Selections between
2000 and 2009 NBA Drafts",
x = "Segment", y = "Median Win Shares per 48 Minutes",
caption = "regular season games only") +
scale_x_discrete(limits = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30"),
labels = c("1-5", "6-10", "11-15",
"16-20", "21-25", "26-30")) +
geom_text(aes(label = round(median, 2), vjust = -0.3)) +
geom_label(aes(label = trunc(pct*100), vjust = 1.2)) +
ylim(0, 0.13) +
theme(plot.title = element_text(face = "bold"))
ws3 + ws4 + plot_layout(ncol = 1)
nodes <- data.frame(
"name" = c("E-mail", "Text",
"Men", "Women",
"Yes", "No"))
links <- as.data.frame(matrix(c(
0,2,7, 0,3,3,
1,2,14, 1,3,6,
2,4,14, 2,5,7,
3,4,2, 3,5,7),
byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize = 12, nodeWidth = 30)
draft2 %>%
mutate(Age2 = trunc(Age)) -> draft2
draft2 %>%
mutate(draft2, WS2 = trunc(WS)) %>%
mutate(WS3 = case_when(WS2 <= 19 ~ "<20",
WS2 >= 20 & WS2 <= 39 ~ "20-39",
WS2 >= 40 & WS2 <= 59 ~ "40-59",
WS2 >= 60 & WS2 <= 79 ~ "60-79",
WS2 >= 80 & WS2 <= 99 ~ "80-99",
WS2 >= 100 ~ "100+")) -> draft2
nodes <- data.frame(
"name" = c("USA", "World",
"0", "1",
"17", "18", "19", "20", "21", "22", "23", "24", "25",
"Big", "Center", "Forward", "Guard", "Swingman",
"1-5", "6-10", "11-15", "16-20", "21-25", "26-30",
"<20", "20-39", "40-59", "60-79", "80-99", "100+"))
links <- as.data.frame(matrix(c(
0,2,21, 0,3,203,
1,2,51, 1,3,16,
2,4,1, 2,5,20, 2,6,19, 2,7,15, 2,8,12, 2,9,5, 2,10,0, 2,11,0, 2,12,0,
3,4,0, 3,5,3, 3,6,32, 3,7,50, 3,8,58, 3,9,58, 3,10,14, 3,11,3, 3,12,1,
4,13,0, 4,14,0, 4,15,1, 4,16,0, 4,17,0,
5,13,2, 5,14,8, 5,15,6, 5,16,2, 5,17,5,
6,13,11, 6,14,6, 6,15,15, 6,16,14, 6,17,5,
7,13,7, 7,14,12, 7,15,19, 7,16,24, 7,17,3,
8,13,9, 8,14,7, 8,15,19, 8,16,25, 8,17,10,
9,13,5, 9,14,5, 9,15,23, 9,16,24, 9,17,6,
10,13,0, 10,14,1, 10,15,4, 10,16,6, 10,17,3,
11,13,0, 11,14,1, 11,15,2, 11,16,0, 11,17,0,
12,13,0, 12,14,1, 12,15,0, 12,16,0, 12,17,0,
13,18,7, 13,19,6, 13,20,8, 13,21,3, 13,22,2, 13,23,8,
14,18,7, 14,19,6, 14,20,7, 14,21,7, 14,22,6, 14,23,9,
15,18,16, 15,19,18, 15,20,13, 15,21,13, 15,22,13, 15,23,15,
16,18,15, 16,19,13, 16,20,15, 16,21,22, 16,22,18, 16,23,12,
17,18,5, 17,19,6, 17,20,7, 17,21,5, 17,22,3, 17,23,6,
18,24,12, 18,25,9, 18,26,9, 18,27,6, 18,28,2, 18,29,12,
19,24,19, 19,25,15, 19,26,5, 19,27,7, 19,28,3, 19,29,1,
20,24,33, 20,25,9, 20,26,3, 20,27,3, 20,28,1, 20,29,0,
21,24,27, 21,25,12, 21,26,8, 21,27,1, 21,28,2, 21,29,0,
22,24,30, 22,25,10, 22,26,7, 22,27,2, 22,28,1, 22,29,0,
23,24,26, 23,25,10, 23,26,2, 23,27,3, 23,28,0, 23,29,1),
byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize = 12, nodeWidth = 30)
draft2 %>%
count(Born2, College2)
draft2 %>%
count(College2, Age2)
draft2 %>%
count(Age2, Pos2)
draft2 %>%
count(Pos2, Pk2)
draft2 %>%
count(Pk2, WS3)
c(draft2 %>%
filter(Pk < 6 & WS > 100) %>%
tally() / 50,
draft2 %>%
filter(Pk < 6 & WS > 75 & WS < 100) %>%
tally() / 50,
draft2 %>%
filter(Pk < 6 & WS > 50 & WS < 75) %>%
tally() / 50,
draft2 %>%
filter(Pk < 6 & WS > 25 & WS < 50) %>%
tally() / 50,
draft2 %>%
filter(Pk < 6 & WS < 25) %>%
tally() / 50) -> probs1
print(probs1)
c(draft2 %>%
filter(Pk < 6 & WS > 100) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk < 6 & WS > 75 & WS < 100) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk < 6 & WS > 50 & WS < 75) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk < 6 & WS > 25 & WS < 50) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk < 6 & WS < 25) %>%
summarize(med = median(WS))) -> vals1
print(vals1)
sum(as.numeric(probs1) * as.numeric(vals1))
c(draft2 %>%
filter(Pk > 5 & WS > 100) %>%
tally() / 241,
draft2 %>%
filter(Pk > 5 & WS > 75 & WS < 100) %>%
tally() / 241,
draft2 %>%
filter(Pk > 5 & WS > 50 & WS < 75) %>%
tally() / 241,
draft2 %>%
filter(Pk > 5 & WS > 25 & WS < 50) %>%
tally() / 241,
draft2 %>%
filter(Pk > 5 & WS < 25) %>%
tally() / 241) -> probs2
print(probs2)
c(draft2 %>%
filter(Pk > 5 & WS > 100) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk > 5 & WS > 75 & WS < 100) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk > 5 & WS > 50 & WS < 75) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk > 5 & WS > 25 & WS < 50) %>%
summarize(med = median(WS)),
draft2 %>%
filter(Pk > 5 & WS < 25) %>%
summarize(med = median(WS))) -> vals2
print(vals2)
sum(as.numeric(probs2) * as.numeric(vals2))
draft2 %>%
select(Pk, WS) -> draft_clust
draft_clust %>%
group_by(Pk) %>%
summarize(ws = mean(WS)) -> draft_clust_final
head(draft_clust_final, n = 3)
tail(draft_clust_final, n = 3)
euclidean_distance <- sqrt((1 - 3)^2 + (69.6 - 66.9)^2)
euclidean_distance
euclidean_distance <- sqrt((2 - 3)^2 + (51.5 - 66.9)^2)
euclidean_distance
distance_matrix <- dist(draft_clust_final, method = "euclidean")
print(distance_matrix)
hc <- hclust(distance_matrix, method = "complete")
bg = par(bg = "darkseagreen1")
plot(as.dendrogram(hc, cex = 0.6, hang = -1),
main = "Cluster Dendrogram:\nWin Shares by First-Round Selection",
xlab = "First-Round Selection Number\n2000-2009 NBA Drafts",
ylab = "Height (aka Euclidian Distance)")
rect.hclust(hc, k = 2)