/
nba_project.Rmd
837 lines (711 loc) · 31.4 KB
/
nba_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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
---
title: "NBA Modern Positions"
output: github_document
always_allow_html: true
---
```{r,warning = FALSE, message = FALSE}
#load necessary libraries
library(nbastatR)
library(tidyverse)
library(BBmisc)
library(corrplot)
library(gganimate)
library(plotly)
library(ggrepel)
```
## Abstract
In this project, my group will describe the changes in playstyle types
throughout the NBA's history. We will perform PCA analysis to determine
the essential traits for different players, and then use important principal
components and unsupervised learning techinques to cluster players into
different "types" and see the movement of types over time.
## Introduction
The NBA, initially created in 1946, has seen much change throughout its
history, for example, the introduction of the 3 point line in the 79/80
season and the elimination of hand checking in 1990. These changes have
altered the playstyles of many teams; some players, especially
former players, have critisized these changes, such as the reliance of many
teams on jump shooting. If you look at the Houston Rocket's shotchart,
you may notice huge regions of white space away from the basket and the
3 point line in order to maximize the efficiency of their shots. We expect
to see a steady increase in 3 point shooting from when the 3 point line
was introduced in the NBA. We also expect to see a decrease in big, slow
players that may have trouble defending these shots, and a decrease in
players that specialize in shooting the midrange.
### The data
We obtained our datasets from basketball-reference.com and stats.nba.com.
We used the R package `nbastatR` to obtain the data from those websites.
#### Unit of observation and Variables
Our unit of observation is a player during one season.
Our original dataset consists of 21235 observations and 75 variables, but we've reduced this number to 10223 observations after selecting seasonsafter 1983 and players that have played in at least 600 minutes in the season. We've also taken out some unnecessary variables to make a 11695 observation, 49 variable dataset.
### Variables used:
#### Shooting:
- pctFTRate: The percentage of total field goal attempts that are Free Throws.
- pct3PRate: The percentage of total field goal attempts that are 3-pt shots.
- pctFG: Field Goal Percentage.
- pctFG3: Field Goal 3-pt Percentage.
- pctFG2: Field Goal 2-pt Percentage.
- pctEFG: Effective Field Goal Percentage. (FGM + (0.5 * 3PM)/FGA). This statistic adjusts for the fact that a 3-pt field goal is worth one more point than a regular 2-pt field goal.
- pctFT: Free Throw Percentage.
- fgmPerGame: Field Goal Made per Game.
- fgaPerGame: Field Goal Attempts per game
- fg3mPerGame: Field Goal Made (3-pt) per game.
- fg3aPerGame: Field Goal Attempts (3-pt) per game.
- fg2mPerGame: Field Goal Made (2-pt) per game.
- fg2aPerGame: Field Goal Attempts (2-pt) per game.
- ftmPerGame: Free Throws Made per game.
- ftaPerGame: Free Throw Attempts per game.
- ptsPerGame: Points per game.
#### Rebounds
- pctORB: Offensive Rebound Percentage (Rebound Rate).
- pctTRB: Total Rebound Percentage (Rebound Rate).
- pctDRB: Deffensive Rebound Percentage (Rebound Rate).
- orbPerGame: Offensive Rebounds per game.
- drbPerGame: Deffensive Rebounds per game.
- trbPerGame: Total Rebounds per game.
#### Passing
- pctAST: Assists Percentage.
- pctTOV: Turnover Percentage.
- astPerGame: Assists per game.
- tovPerGame: Turnovers per game.
#### Defense
- pctSTL: Steals percentage.
- pctBLK: Blocks percentage.
- stlPerGame: Steals per game.
- blkPerGame: Blocks per game.
- pfPerGame: Personal Fouls per game.
#### Efficiency stats
- ratioPER: Player Efficiency Rating
- ratioOWS: Offensive Win Shares.
- ratioDWS: Deffensive Win Shares.
- ratioWS: Win Shares.
- ratioWSPer48: Win Shares per 48 minutes.
- ratioOBPM: Offensive Box Plus/Minus.
- ratioDBPM: Deffensive Box Plus/Minus.
- ratioBPM: Box Plus/Minus.
- ratioVORP: Value Over Replacement Player.
- minutesPerGame: Minutes Per Game
- pctUSG: Usage Percentage. (100 * ((FGA + 0.44 * FTA + TOV) * (Tm MP / 5)) / (MP * (Tm FGA + 0.44 * Tm FTA + Tm TOV))
#### Other
- namePlayer: Name of player
- groupPosition: Player Position. Either Guard, Forward, or Center
- yearSeason: Season year
- countGames: Games played during season
- minutes: Minutes played during season
- isAllNBA: Did player make season's all NBA team?
```{r, message = FALSE, warning = FALSE, cache = TRUE}
# Gets player stats from 1984 to 2020.
players_post1984 <- bref_players_stats(seasons = 1984:2020, tables = c("advanced", "per_game"), include_all_nba = TRUE, return_message = FALSE)
# Filters by amount of time they played.
ggplot(players_post1984, mapping = aes(x = minutes)) +
geom_histogram() +
geom_vline(xintercept = 600, color = "tomato")
players_post1984 <- filter(players_post1984, minutes > 600)
# Variables to drop
dropping <-c("slugSeason", "idPlayerNBA", "namePlayerBREF", "urlPlayerBREF",
"slugPlayerBREF", "urlPlayerThumbnail", "urlPlayerHeadshot",
"slugPosition","urlPlayerPhoto", "urlPlayerStats",
"urlPlayerActionPhoto","yearSeasonFirst",
"countTeamsPlayerSeason","countGamesStarted",
"isSeasonCurrent","slugPlayerSeason", "agePlayer",
"slugTeamBREF", "isHOFPlayer", "slugTeamsBREF",
"countTeamsPlayerSeasonPerGame", "groupAllNBA",
"numberAllNBATeam", "isAllNBA2", "isAllNBA1",
"isAllNBA3" )
# Dropping variables
players_post1984 <- players_post1984[,!(names(players_post1984) %in% dropping)]
# Groups players by season, and normalizes by season.
for(year in 1984:2020){
eval(parse(text = paste( "players_",year," <- filter(players_post1984, yearSeason == ",year,")", sep = "" )))
for(i in 6:48){
eval(parse(text = paste( "players_",year,"[,",i,"] <- normalize(players_",year,"[,",i,"],method = 'range', range = c(0,1))", sep = "")))
}
}
# Ungroups players by season.
players_post1984_normalized <- players_1984
for(year in 1985:2020){
eval(parse(text = paste( "players_post1984_normalized <- rbind(players_post1984_normalized, players_",year,")", sep = "")))
}
players_post1984_all_nba_normalized <- players_post1984_normalized %>% filter(isAllNBA == TRUE)
```
## Exploratory Data Analysis
For our exploratory data analysis we ploted our stats variables against season, grouped by "standard" positions.
```{r}
#some summary statistics
summarystatistics1984 <- players_post1984 %>%
group_by(yearSeason, groupPosition) %>%
summarize(meanpctFTRate = mean(pctFTRate),
meanpct3PRate = mean(pct3PRate),
meanpctFG = mean(pctFG),
meanpctFG3 = mean(pctFG3),
meanpctFG2 = mean(pctFG2),
meanpctEFG = mean(pctEFG),
meanpctFT = mean(pctFT),
meanfgmPerGame = mean(fgmPerGame),
meanfgaPerGame = mean(fgaPerGame),
meanfg3mPerGame = mean(fg3mPerGame),
meanfg3aPerGame = mean(fg3aPerGame),
meanfg2mPerGame = mean(fg2mPerGame),
meanfg2aPerGame = mean(fg2aPerGame),
meanftmPerGame = mean(ftmPerGame),
meanftaPerGame = mean(ftaPerGame),
meanptsPerGame = mean(ptsPerGame),
meanpctORB = mean(pctORB),
meanpctTRB = mean(pctTRB),
meanpctDRB = mean(pctDRB),
meanorbPerGame = mean(orbPerGame),
meandrbPerGame = mean (drbPerGame),
meantrbPerGame = mean(trbPerGame),
meanpctAST = mean(pctAST),
meanpctTOV = mean(pctTOV),
meanastPerGame = mean(astPerGame),
meantovPerGame = mean(tovPerGame),
meanpctSTL = mean(pctSTL),
meanpctBLK = mean(pctBLK),
meanstlPerGame = mean(stlPerGame),
meanblkPerGame = mean(blkPerGame),
meanpfPerGame = mean(pfPerGame),
meanratioOWS = mean(ratioOWS),
meanratioDWS = mean(ratioDWS),
meanratioWS = mean(ratioWS),
meanratioWSPer48 = mean(ratioWSPer48),
meanratioOBPM = mean(ratioOBPM),
meanratioDBPM = mean(ratioDBPM),
meanratioBPM = mean(ratioBPM),
meanratioVORP = mean(ratioVORP),
sdpctFTRate = sd(pctFTRate),
sdpct3PRate = sd(pct3PRate),
sdpctFG = sd(pctFG),
sdpctFG3 = sd(pctFG3),
sdpctFG2 = sd(pctFG2),
sdpctEFG = sd(pctEFG),
sdpctFT = sd(pctFT),
sdfgmPerGame = sd(fgmPerGame),
sdfgaPerGame = sd(fgaPerGame),
sdfg3mPerGame = sd(fg3mPerGame),
sdfg3aPerGame = sd(fg3aPerGame),
sdfg2mPerGame = sd(fg2mPerGame),
sdfg2aPerGame = sd(fg2aPerGame),
sdftmPerGame = sd(ftmPerGame),
sdftaPerGame = sd(ftaPerGame),
sdptsPerGame = sd(ptsPerGame),
sdpctORB = sd(pctORB),
sdpctTRB = sd(pctTRB),
sdpctDRB = sd(pctDRB),
sdorbPerGame = sd(orbPerGame),
sddrbPerGame = sd (drbPerGame),
sdtrbPerGame = sd(trbPerGame),
sdpctSTL = sd(pctSTL),
sdpctBLK = sd(pctBLK),
sdstlPerGame = sd(stlPerGame),
sdblkPerGame = sd(blkPerGame),
sdpfPerGame = sd(pfPerGame),
sdratioOWS = sd(ratioOWS),
sdratioDWS = sd(ratioDWS),
sdratioWS = sd(ratioWS),
sdratioWSPer48 = sd(ratioWSPer48),
sdratioOBPM = sd(ratioOBPM),
sdratioDBPM = sd(ratioDBPM),
sdratioBPM = sd(ratioBPM),
sdratioVORP = sd(ratioVORP)
)
head(summarystatistics1984)
ggplot(data = summarystatistics1984, mapping = aes(x = yearSeason, y = meanpctFG, color = groupPosition)) +
geom_line()
#taking a look at some stats over time, grouped by year and position
#fg pct
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanpctFG, color = groupPosition)) + geom_line()
#3FGPct, see the introduction of 3 point line
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanpctFG3,
color = groupPosition)) +
geom_line()
#meanFGM
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanfgmPerGame,
color = groupPosition)) +
geom_line()
#meanFGA
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanfgaPerGame,
color = groupPosition)) +
geom_line()
#mean3FGM
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanfg3mPerGame,
color = groupPosition)) +
geom_line()
#mean3FGA
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanfg3aPerGame, color = groupPosition)) +
geom_line()
#meanFTA
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanftaPerGame,
color = groupPosition)) +
geom_line()
#meanFTM
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanftmPerGame,
color = groupPosition)) +
geom_line()
#meanORB
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanorbPerGame,
color = groupPosition)) +
geom_line()
#meanDRB
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meandrbPerGame,
color = groupPosition)) +
geom_line()
#meanAST
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanastPerGame,
color = groupPosition)) +
geom_line()
#meanSTL
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanstlPerGame,
color = groupPosition)) +
geom_line()
#meanBLK
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanblkPerGame,
color = groupPosition)) +
geom_line()
#meanTOV
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meantovPerGame,
color = groupPosition)) +
geom_line()
#meanPTS
ggplot(data = summarystatistics1984,
mapping = aes(x = yearSeason, y = meanptsPerGame,
color = groupPosition)) +
geom_line()
#also a correlation matrix plot
rcorr <- round(cor(players_post1984_normalized[,c(5:48)]),2)
corrplot(rcorr, method="color")
```
## Modeling
```{r, cache = TRUE}
pca <- prcomp(players_post1984_normalized[,-c(1:5, 49)])
d <- as.data.frame(pca$x)
pc1 <- pca$rotation[, 1]
pc1
pc2 <- pca$rotation[, 2]
pc2
pca3 <- pca$rotation[, 3]
d$namePlayer <- players_post1984_normalized$namePlayer
d$isAllNBA <- players_post1984_normalized$isAllNBA
d$yearSeason <- players_post1984_normalized$yearSeason
pcaplot <- ggplot(d, aes(x = PC1, y = PC2)) +
geom_point(size = .5, alpha = .7) +
ylab("Shooters vs Baseline") +
xlab("Usage vs Non-Usage")
pcaplot
d1 <- tibble(PC = 1:43,
PVE = pca$sdev^2 /
sum(pca$sdev^2))
ggplot(d1, aes(x = PC, y = PVE)) +
geom_line() +
geom_point()
#some PCA exploration
pca_rotations <- data.frame(pca$rotation)
pca_rotations$variables <- rownames(pca_rotations)
PC1graph <-
ggplot(data = pca_rotations, mapping = aes(x = variables, y = PC1,
fill = variables)) +
geom_col() +
coord_flip() +
theme(legend.position = "none") +
labs(title = "Usage (-) vs Non-Usage (+)")
PC1graph
#note important characteristics: tov, pts, minutes, fgm, fga
PC2graph <-
ggplot(data = pca_rotations, mapping = aes(x = variables, y = PC2,
fill = variables)) +
geom_col() +
coord_flip() +
theme(legend.position = "none")+
labs(title = "Shooters (-) vs Baseline(+)")
PC2graph
#note important characteristics: (+) rebounding stats, (-) 3pt shooting, asts
PC3graph <-
ggplot(data = pca_rotations, mapping = aes(x = variables, y = PC3,
fill = variables)) +
geom_col() +
coord_flip() +
theme(legend.position = "none") +
labs(title = "Outside the Arc (-) vs Inside the Arc (+)")
PC3graph
#important characteristics: (+) turnovers, usage, field goals worth 2
# (-) True shooting, EFG, 3 pointers
#lets now create a plot with some players that we will recognize
currentplayersplot <- ggplot(d, aes(x = PC1, y = PC2)) +
geom_point(size = .5, alpha = .1) +
geom_text_repel(data = subset(d, isAllNBA == TRUE &
yearSeason %in% 2017:2019 ),
aes(label = namePlayer)) +
geom_point(data = subset(d, isAllNBA == TRUE &
yearSeason %in% 2017:2019),
mapping = aes(x = PC1, y = PC2),
size = .5, alpha = .7, color = "purple2") +
ylab("Shooters vs Baseline") +
xlab("Usage vs Non-Usage")
currentplayersplot
famousplayersplot <- ggplot(d, aes(x = PC1, y = PC2)) +
geom_point(size = .5, alpha = .1) +
geom_text_repel(data = subset(d, namePlayer %in% c("Lebron James",
"Michael Jordan",
"Stephen Curry",
"Kobe Bryant",
"Tim Duncan",
"Magic Johnson",
"Kevin Durant")),
aes(label = namePlayer)) +
geom_point(color = "purple",
data = subset(d, namePlayer %in% c("Lebron James",
"Michael Jordan",
"Stephen Curry",
"Kobe Bryant",
"Tim Duncan",
"Magic Johnson",
"Kevin Durant"),
size = .5, alpha = .6)) +
ylab("Shooters vs Baseline") +
xlab("Usage vs Non-Usage")
famousplayersplot
#animated plot, allstars throughout the seasons
allnbaplayerspca <- d %>%
filter(isAllNBA == TRUE)
#staticplot
staticplot <- ggplot(data = allnbaplayerspca,
mapping = aes(x = PC1, y = PC2)) +
geom_point(alpha = .8, color = "purple") +
geom_text_repel(data = allnbaplayerspca,
mapping = aes(label = namePlayer)) +
geom_point(data = d, mapping = aes(x = PC1, y = PC2), alpha = .1) +
ylab("Shooters vs Baseline") +
xlab("Usage vs Non-Usage")
staticplot
#byseason
byseasonplot <- staticplot + transition_time(yearSeason) +
labs(title = "Season: {frame_time}")
animate(byseasonplot, renderer = gifski_renderer(), nframes = 37, fps = 2)
```
```{r, cache = TRUE}
set.seed(13)
n_pcas <- as_tibble(pca$x[,1:3])
#choosing k for kmeans
n_clusters_kmeans1<-kmeans(n_pcas, centers = 1, nstart = 20)
n_clusters_kmeans2<-kmeans(n_pcas, centers = 2, nstart = 20)
n_clusters_kmeans3<-kmeans(n_pcas, centers = 3, nstart = 20)
n_clusters_kmeans4<-kmeans(n_pcas, centers = 4, nstart = 20)
n_clusters_kmeans5<-kmeans(n_pcas, centers = 5, nstart = 20)
n_clusters_kmeans6<-kmeans(n_pcas, centers = 6, nstart = 20)
kmeansvariation <- tibble("K" = 1:6,
"SS" = c(n_clusters_kmeans1$tot.withinss,
n_clusters_kmeans2$tot.withinss,
n_clusters_kmeans3$tot.withinss,
n_clusters_kmeans4$tot.withinss,
n_clusters_kmeans5$tot.withinss,
n_clusters_kmeans6$tot.withinss))
kmeansvariationplot <-
ggplot(data = kmeansvariation, mapping = aes(x = K, y = SS)) +
geom_line() +
geom_point() +
labs(title = "K means scree plot")
kmeansvariationplot
#does not seem to be a dinstinct elbow. We will decide on K=3, as it is
#near the middle of complete aggregation and no aggregation, and because
#generally, there seem to be three types of players
n_pcasdist <- dist(n_pcas)
distmatrix <- as.matrix(n_pcasdist)
n_clusters_hier<-hclust(dist(n_pcas))
n_clusters_hier_plot<-plot(n_clusters_hier,
xlab = "", ylab = "", sub = "",
main = "Complete Linkage")
n_clusters_hier_plot
abline(h = 3.7, col = "tomato")
n_clusters_hier_cut <- cutree(n_clusters_hier, 3)
n_pcas_kmeans <- n_pcas %>% mutate(cluster = n_clusters_kmeans3$cluster)
n_pcas_hier <- n_pcas %>% mutate(cluster = n_clusters_hier_cut)
ggplot(n_pcas_kmeans, aes(x = PC1, y = PC2,
color = as.factor(cluster))) +
geom_point(alpha = .6) +
labs(title = "kmeans clustering")
ggplot(n_pcas_hier, aes(x = PC1, y = PC2,
color = as.factor(cluster))) +
geom_point(alpha = .6) +
labs(title = "hierarchical clustering")
#plot_ly(n_pcas_kmeans, x = ~PC1, y = ~PC2, z = ~PC3, color = ~cluster)
players_post1984_pca_clusters <- players_post1984 %>%
mutate(PC1 = pca$x[,1], PC2 = pca$x[,2], PC3 = pca$x[,3],
hier_cluster = n_clusters_hier_cut, kmeans_cluster = n_clusters_kmeans3$cluster)
players_post1984_pca_clusters_year <- players_post1984_pca_clusters %>%
group_by(yearSeason, kmeans_cluster) %>% count
players_post1984_pca_year <- players_post1984_pca_clusters %>%
group_by(yearSeason) %>% count
players_post1984_pca_clusters_year_1 <- players_post1984_pca_clusters_year %>%
filter(kmeans_cluster == 1)
players_post1984_pca_clusters_year_2 <- players_post1984_pca_clusters_year %>%
filter(kmeans_cluster == 2)
players_post1984_pca_clusters_year_3 <- players_post1984_pca_clusters_year %>%
filter(kmeans_cluster == 3)
cluster_proportions_1 = players_post1984_pca_clusters_year_1$n/players_post1984_pca_year$n
cluster_proportions_2 = players_post1984_pca_clusters_year_2$n/players_post1984_pca_year$n
cluster_proportions_3 = players_post1984_pca_clusters_year_3$n/players_post1984_pca_year$n
cluster_proportions <- tibble(year = c(1984:2020), one = cluster_proportions_1, two = cluster_proportions_2, three = cluster_proportions_3)
clusterdataframe <- data.frame(cluster1 = cluster_proportions_1,
cluster2 = cluster_proportions_2,
cluster3 = cluster_proportions_3,
year = c(1984:2020))
clusterdf <- data.frame(year = rep(c(1984:2020), 3),
proportion = c(cluster_proportions_1,
cluster_proportions_2,
cluster_proportions_3),
cluster = c(rep("Cluster 1", 37),
rep("Cluster 2", 37),
rep("Cluster 3", 37)))
#maybe use this graph instead?
ggplot(clusterdf, mapping = aes(x = year, y = proportion, color = cluster)) +
geom_line()
#ggplot(cluster_proportions, aes(x = year, y = one), color = "red")+geom_line()+
# geom_line(aes(x = year, y = two), color = "blue")+
#geom_line(aes(x = year, y = three), color = "yellow")
players_post1984_pca_clusters_year
```
```{r}
#what clusters do all-nba players end up in?
allnbaclusterssummary <- players_post1984_pca_clusters%>%
filter(isAllNBA == TRUE) %>%
group_by(hier_cluster) %>%
summarize(n = n())
allnbahierchdata <- players_post1984_pca_clusters%>%
filter(isAllNBA == TRUE)
allnbaclusterstatic <- ggplot(data = allnbahierchdata,
mapping = aes(x = PC1, y = PC2, color = as.factor(hier_cluster))) +
geom_point() +
geom_point(data = n_pcas_hier,
mapping = aes(x = PC1, y = PC2,
color = as.factor(cluster)),
alpha = .1)
allnbabyseasonplot <- allnbaclusterstatic+
transition_time(yearSeason) +
labs(title = "Season: {frame_time}")
animate(allnbabyseasonplot,
renderer = gifski_renderer(), nframes = 36, fps = 2)
#now for all-nba pca analysis
pca_all_nba <- prcomp(players_post1984_all_nba_normalized[,-c(1:5, 49)])
d_all_nba <- as.data.frame(pca_all_nba$x)
ggplot(d_all_nba, aes(x = PC1, y = PC2)) +
geom_point(size = .5, alpha = .7)
d2 <- tibble(PC = 1:43,
PVE = pca$sdev^2 /
sum(pca$sdev^2))
d3 <- tibble(PC = 1:43,
PVE = pca_all_nba$sdev^2 / sum(pca_all_nba$sdev^2))
ggplot(d2, aes(x = PC, y = PVE)) +
geom_line() +
geom_point() +
labs(title = "Skree Plot for NBA")
ggplot(d3, aes(x = PC, y = PVE)) +
geom_line() +
geom_point() +
labs(title = "Skree Plot for All-NBA")
pca_all_nba_rotations <- data.frame(pca_all_nba$rotation)
pca_all_nba_rotations$variables <- rownames(pca_all_nba_rotations)
PC1_all_nba_graph <-
ggplot(data = pca_all_nba_rotations, mapping = aes(x = variables, y = PC1,
fill = variables)) +
geom_col()+
coord_flip()+
theme(legend.position = "none") +
labs(title = "Rebounders(-) vs Shooters(+)")
PC1_all_nba_graph
PC2_all_nba_graph <-
ggplot(data = pca_all_nba_rotations, mapping = aes(x = variables, y = PC2,
fill = variables)) +
geom_col()+
coord_flip()+
theme(legend.position = "none") +
labs(title = "Efficient (-) vs Not Efficient (+) ")
PC2_all_nba_graph
PC3_all_nba_graph <-
ggplot(data = pca_all_nba_rotations, mapping = aes(x = variables, y = PC3,
fill = variables)) +
geom_col()+
coord_flip()+
theme(legend.position = "none") +
labs(title = "Team (-) vs Scoring (+)")
PC3_all_nba_graph
PC4_all_nba_graph <-
ggplot(data = pca_all_nba_rotations, mapping = aes(x = variables, y = PC4,
fill = variables)) +
geom_col()+
coord_flip()+
theme(legend.position = "none")
PC4_all_nba_graph
```
```{r}
n_pcas_all_nba <- as_tibble(pca_all_nba$x[,1:3])
n_clusters_kmeans_all_nba <-kmeans(n_pcas_all_nba, centers = 3)
n_clusters_hier_all_nba <-hclust(dist(n_pcas_all_nba))
n_clusters_hier_plot_all_nba<-plot(n_clusters_hier_all_nba)
abline(h = 2.7, col = "tomato")
n_clusters_hier_cut_all_nba <- cutree(n_clusters_hier_all_nba, 3)
n_pcas_kmeans_all_nba <- n_pcas_all_nba %>% mutate(cluster = n_clusters_kmeans_all_nba$cluster)
n_pcas_hier_all_nba <- n_pcas_all_nba %>% mutate(cluster = n_clusters_hier_cut_all_nba)
ggplot(n_pcas_kmeans_all_nba, aes(x = PC1, y = PC2,
color = as.factor(cluster))) +
geom_point() +
labs(title = "all-nba kmeans clustering")
ggplot(n_pcas_hier_all_nba, aes(x = PC1, y = PC2,
color = as.factor(cluster))) +
geom_point() +
labs(title = "all-nba hierarchical clustering")
#plot_ly(n_pcas_hier_all_nba, x = ~PC1, y = ~PC2, z = ~PC3, color = ~cluster)
players_post1984_pca_clusters_all_nba <- players_post1984_all_nba_normalized %>%
mutate(PC1 = pca_all_nba$x[,1], PC2 = pca_all_nba$x[,2], PC3 = pca_all_nba$x[,3],
hier_cluster = n_clusters_hier_cut_all_nba, kmeans_cluster = n_clusters_kmeans_all_nba$cluster)
players_post1984_pca_clusters_year_all_nba <- players_post1984_pca_clusters_all_nba %>%
group_by(yearSeason, hier_cluster) %>% count
players_post1984_pca_year_all_nba <- players_post1984_pca_clusters_all_nba %>%
group_by(yearSeason) %>% count
players_post1984_pca_clusters_year_1_all_nba <- players_post1984_pca_clusters_year_all_nba %>%
filter(hier_cluster == 1)
players_post1984_pca_clusters_year_2_all_nba <- players_post1984_pca_clusters_year_all_nba %>%
filter(hier_cluster == 2)
players_post1984_pca_clusters_year_3_all_nba <- players_post1984_pca_clusters_year_all_nba %>%
filter(hier_cluster == 3)
cluster_proportions_1_all_nba = players_post1984_pca_clusters_year_1_all_nba$n/players_post1984_pca_year_all_nba$n
cluster_proportions_2_all_nba = players_post1984_pca_clusters_year_2_all_nba$n/players_post1984_pca_year_all_nba$n
cluster_proportions_3_all_nba = players_post1984_pca_clusters_year_3_all_nba$n/players_post1984_pca_year_all_nba$n
cluster_proportions_all_nba <- tibble(year = c(1984:2019), one = cluster_proportions_1_all_nba, two = cluster_proportions_2_all_nba, three = cluster_proportions_3_all_nba)
#Red: first cluster, Blue: second cluster, Green: third cluster
#ggplot(cluster_proportions_all_nba, aes(x = year, y = one))+geom_line(color = "red")+
# geom_line(aes(x = year, y = two), color = "blue")+
#geom_line(aes(x = year, y = three), color = "green") +
#labs(y ="Proportion")
#new graph:
allnbaclusterdf <- data.frame(year = rep(c(1984:2019), 3),
proportion = c(cluster_proportions_1_all_nba,
cluster_proportions_2_all_nba,
cluster_proportions_3_all_nba),
cluster = c(rep("Cluster 1", 36),
rep("Cluster 2", 36),
rep("Cluster 3", 36)))
ggplot(allnbaclusterdf, mapping = aes(x = year, y = proportion,
color = cluster)) +
geom_line()
```
```{r}
#some more animated graphs
#counts
nbaclusters <- tibble(n_pcas_kmeans_all_nba$cluster,
n_pcas_hier_all_nba$cluster,
allnbaplayerspca$yearSeason) %>%
mutate(kmeanscluster=n_pcas_kmeans_all_nba$cluster ,
hierarchicalcluster = n_pcas_hier_all_nba$cluster,
yearSeason= allnbaplayerspca$yearSeason)
nbaclusters <- nbaclusters[,-c(1:3)]
clustercountkmeansallnba <- nbaclusters %>%
group_by(yearSeason, kmeanscluster) %>%
summarize(count = n())
clustercounthierchallnba <- nbaclusters %>%
group_by(yearSeason, hierarchicalcluster) %>%
summarize(count = n())
#allnba kmean and hierarchical clusters over time
kmeansclustergraphallnba <-
ggplot(data = clustercountkmeansallnba,
mapping = aes(x = yearSeason, y = count, color =
as.factor(kmeanscluster))) +
geom_point() +
ylim(-.5,7)
kmeansclustergraphallnba
hierarchicalgraphallnba <-
ggplot(data = clustercounthierchallnba,
mapping = aes(x = yearSeason, y = count,
color = as.factor(hierarchicalcluster))) +
geom_point() +
ylim(-.5,7)
hierarchicalgraphallnba
#all-nba player's clusters over time
n_pcas_kmeans_all_nba <- n_pcas_kmeans_all_nba %>%
mutate(yearSeason = allnbaplayerspca$yearSeason)
n_pcas_hier_all_nba <- n_pcas_hier_all_nba %>%
mutate(yearSeason = allnbaplayerspca$yearSeason)
allnbakmeans <-
ggplot(n_pcas_kmeans_all_nba, aes(x = PC1, y = PC2,
color = as.factor(cluster)))+
geom_point()+
ylab("Shooters vs Baseline") +
xlab("Usage vs Non-Usage") +
labs(title = "All NBA teams over time - kmeans")
allnbakmeans
allnbahierch <-
ggplot(n_pcas_hier_all_nba, aes(x = PC1, y = PC2,
color = as.factor(cluster))) +
geom_point()+
ylab("Shooters vs Baseline") +
xlab("Usage vs Non-Usage") +
labs(title = "All NBA teams over time - hierarchical")
allnbahierch
byseasonplotkmeans <- allnbakmeans + transition_time(yearSeason) +
labs(title = "Season: {frame_time}")
animate(byseasonplotkmeans,
renderer = gifski_renderer(), nframes = 36, fps = 2)
byseasonplothierch <- allnbahierch + transition_time(yearSeason) +
labs(title = "Season: {frame_time}")
animate(byseasonplothierch,
renderer = gifski_renderer(), nframes = 36, fps = 2)
```
## Discussion
### Looking at PCA (Principal Component Analysis)
PC1 seems to be a case of an "Usage" (-) vs "Non-Usage" (+) battle.
In the negative side, we see characteristics such as ptsPerGame,
fgmPerGame, fg2mPerGame, fg2aPerGame, minutesPerGame. The other types
of variables do not tend to take on positive values, but the ones
that are include:
pctBLK, pctSTL, pctTOV.
Looking at PC2, I would describe this as "Shooters" (-) vs "Baseline" (+).
We see positive values for shooting characteristics, such as pct3PRate,
fg3mPerGame, fg3aPerGame, and negative values for characteristics
typical for tall, big players:pctDRB, pctTRB, blkPerGame.
PC3 is the distinction between "Outside the Arc" (-) and "Inside the Arc" (+)
Extreme negative values appear for pctTrueShooting, pctEFG, pct3PRate. On
the positive side, we see tovPerGame, pctUSG, pctBLK, and fg2aPerGame.
### Clustering
Using Hierarchical clustering, we were able to break down players into 3
"types":
Cluster 1: high usage, shooters, spread for outside/inside the arc
Cluster 2: low usage, spread between shooters/baseline and outside/inside the
arc
Cluster 3: high usage, baseline players, spread for outside/inside arc
### Looking at All-NBA
When we narrow our observations to just players that made the all-nba team,
our first PC is the distinction between "Rebounders" (-) and "Shooters" (+)
Negative variables include trbPerGame, pctTRB, pctDRB, orbPerGame.
I would classify our second PCA as a battle between "Efficient" (-) and
"Not Efficient" (+). We don't have many variables for the non-efficient
side, mainly turnovers and blocks, but for the efficient aspect of the PC,
high magnitude variables include ptsPerGame, VORP, ftmPerGame, and
ftaPerGame. Our third PC, I would classify as "Team" (-) vs "Scoring" (+)
players. "Team" stats include ratioVORP, ratioBPM, pctAST, and astPerGame,
whereas the "Scoring" variables include pstPerGAme, pctUSG, fgmPerGame,
and fg2aPerGame.
### Clustering for All-NBA
It seems that all of the clusters have some spread in PC3, the "Team" vs
"Scoring" principle component. Cluster 2 does have the more scoring players
however. The different types:
Cluster 1: Shooters, mixed efficiency, generally Scorers
Cluster 2: Pretty diverse set of Rebounders and Shooters. Shooters tend
to be scorers while Rebounders are Team players. Players are efficient.
Cluster 3: Team Rebounders, however, not very efficient
Remember though, all of these players are All-NBA players, so their stats
are being compared to the best players each year in the NBA.
###
## References