-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNBAClustering.Rmd
123 lines (88 loc) · 3 KB
/
NBAClustering.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
---
title: "NBAClustering"
author: "Eric Drew"
date: "2022-10-12"
output: pdf_document
---
```{r setup, include=FALSE}
library(tidyverse)
library(datasets)
library(arules)
library(arulesViz)
library(ggplot2)
library(dplyr)
library(rpart)
library(rpart.plot)
library(TH.data)
library(ISLR2)
library(lattice)
library(stats)
library(rattle)
library(RColorBrewer)
#library(caret)
library(ROCR)
library(cluster)
library(factoextra)
library(gridExtra)
library(NbClust)
library(dendextend)
library(class)
library(ClustOfVar)
library(MASS)
library(kableExtra)
library(partykit)
library(dbscan)
```
```{r data}
#https://www.basketball-reference.com/leagues/NBA_2022_per_game.html
#per game Stats Data
pergame <- read.csv("C:\\Users\\ericd\\OneDrive - North Carolina State University\\Desktop\\PersonalProjects\\NBA-Clustering\\perGameStats.csv")
#Remove the "total" observation(for now)
pergame = pergame[order(pergame[,'Player'],-pergame[,'G']),]
pergame = pergame[!duplicated(pergame$Player),]
pergame <- pergame[,-c(2,3,4)]
```
```{r clean}
#missing values check
sum(is.na(pergame))
#remove rows with missing values
pergame <- na.omit(pergame)
#set player name to the row names and drop column
row.names(pergame) <- pergame$Player
pergame <- pergame[,-1]
pergame <- as.data.frame(pergame)
#filter to players who play more than 15mpg
pergame <- pergame %>% filter(pergame$PTS >= 13)
labelvec <- c('LeBron James','Joel Embiid','Kevin Durant','Luka Dončić','Rudy Gobert','Bam Adebayo',
'Anthony Davis', 'Montrezl Harrell','Aaron Gordon','Josh Hart','Reggie Jackson',
'LaMelo Ball','Stephen Curry','Carmelo Anthony','Kyle Kuzma', 'Deandre Ayton', 'James Harden',
'Jimmy Butler','Bradley Beal','Jalen Green')
```
```{r cluster}
scaleData <- scale(pergame)
clus2=kmeans(scaleData,centers=3,nstart = 10000)
clus2
fviz_nbclust(scaleData, kmeans, method = "wss",k.max = 9)
fviz_nbclust(scaleData, kmeans, method = "silhouette",k.max = 9)
fviz_cluster(clus2,data = scaleData,geom=c('text','point'), pointsize=.8,labelsize = 12,label.select=labelvec,
main='Cluster Plot of NBA Players(min. 13PPG)')
#bind the clusters onto each players observation
profile.kmeans=cbind(pergame,clus2$cluster)
#create the summary statistics for the important per game stats
all.k=profile.kmeans %>% group_by(clus2$cluster) %>%
summarise('Points/Game'=round(mean(PTS),2),'Rebs/Game'=round(mean(TRB),2),'AST/Game'=round(mean(AST),2),
'Blocks/Game'=round(mean(BLK),2))
#rename clusters to what they are categorized as
all.k$`clus2$cluster` <- as.character(all.k$`clus2$cluster`)
all.k[1,1] = 'Role Players'
all.k[2,1] = 'Stars'
all.k[3,1] = 'Big Men'
names(all.k)[names(all.k) == 'clus2$cluster'] <- 'Player Category'
#print table with ordering the stars first
all.k[c(2,3,1),]
#league averages for the stats to be highlighted(stars:points. big men:blocks/rebounds)
mean(pergame$PTS) #18.8
mean(pergame$BLK) #0.6
mean(pergame$TRB) #5.9
NbClust(scaleData,method="kmeans",min.nc=2,max.nc = 4)
```