Using k-Means Clustering to Recruit New NBA Players

Problem Statement

You are a scout for the worst team in the NBA, probably the Wizards. Your general manager just heard about Data Science and thinks it can solve all the teams problems!!! She wants you to figure out a way to find players that are high performing but maybe not highly paid that you can steal to get the team to the playoffs!

Details

  • Determine a way to use clustering to estimate based on performance if players are under or over paid, generally.
  • Then select three players you believe would be best for your team and explain why.
  • Provide a well commented and clean (knitted) report of your findings that can be presented to your GM. Include a rationale for variable selection, details on your approach and a overview of the results with supporting visualizations.

Setting Up Workspace, Including Libraries

Import Datasets, Convert Salary Column to Numeric

getwd( )
## [1] "/Users/vorgelectronics/R/DS3001/my_assignments"
#Import Data Sets, Merge Data
salaries <- read_csv("~/R/DS3001/data/nba_salaries_22.csv")
## Rows: 479 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Player, Salary
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
nba <- read_csv("~/R/DS3001/data/NBA_Perf_22.csv")
## Rows: 812 Columns: 29
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): Player, Pos, Tm
## dbl (26): Age, G, GS, MP, FG, FGA, FG%, 3P, 3PA, 3P%, 2P, 2PA, 2P%, eFG%, FT...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
df <- merge(salaries, nba, by="Player")

# Convert the salary column to numeric
df$Salary <- gsub("[,$]", "", df$Salary) 
df$Salary <- as.numeric(df$Salary)

# Get rid of extraneous characters
df$Player <- gsub("[ '.]", "", df$Player)

# Remove NA Values
df <- df[complete.cases(df), ]
str(df)
## 'data.frame':    466 obs. of  30 variables:
##  $ Player: chr  "AaronGordon" "AaronHoliday" "AaronHoliday" "AaronHoliday" ...
##  $ Salary: num  19690909 1836090 1836090 1836090 3804360 ...
##  $ Pos   : chr  "PF" "PG" "PG" "PG" ...
##  $ Age   : num  26 25 25 25 22 23 35 30 20 27 ...
##  $ Tm    : chr  "DEN" "WAS" "TOT" "PHO" ...
##  $ G     : num  75 41 63 22 52 50 69 81 61 41 ...
##  $ GS    : num  75 14 15 1 3 35 69 44 12 18 ...
##  $ MP    : num  31.7 16.2 16.2 16.3 11 24.2 29.1 28.6 20.2 28 ...
##  $ FG    : num  5.8 2.4 2.4 2.3 1.4 3.1 3.9 3.5 3 2.5 ...
##  $ FGA   : num  11.1 5.2 5.4 5.6 3.5 6.7 8.2 9 7.5 6.2 ...
##  $ FG%   : num  0.52 0.467 0.447 0.411 0.396 0.463 0.467 0.391 0.408 0.398 ...
##  $ 3P    : num  1.2 0.6 0.6 0.7 0.6 0.8 1.3 1.9 0.9 1 ...
##  $ 3PA   : num  3.5 1.6 1.6 1.6 2.2 2.8 3.8 4.8 3.2 3.1 ...
##  $ 3P%   : num  0.335 0.343 0.379 0.444 0.27 0.304 0.336 0.404 0.289 0.333 ...
##  $ 2P    : num  4.6 1.9 1.8 1.6 0.8 2.3 2.6 1.6 2.1 1.5 ...
##  $ 2PA   : num  7.7 3.6 3.7 4 1.3 4 4.4 4.2 4.2 3.2 ...
##  $ 2P%   : num  0.605 0.524 0.477 0.398 0.612 0.573 0.582 0.378 0.498 0.462 ...
##  $ eFG%  : num  0.573 0.521 0.504 0.476 0.481 0.525 0.546 0.499 0.47 0.48 ...
##  $ FT    : num  2.3 0.7 0.9 1.4 0.4 1.2 1.2 2.7 0.6 1.4 ...
##  $ FTA   : num  3.1 0.9 1.1 1.5 0.5 1.7 1.4 3.3 0.8 1.8 ...
##  $ FT%   : num  0.743 0.8 0.868 0.939 0.808 0.729 0.842 0.822 0.7 0.795 ...
##  $ ORB   : num  1.7 0.2 0.4 0.7 0.3 1 1.6 0.6 1.2 0.8 ...
##  $ DRB   : num  4.2 1.4 1.6 1.8 1.4 2.5 6.1 4.3 4 2.8 ...
##  $ TRB   : num  5.9 1.6 1.9 2.5 1.7 3.6 7.7 4.9 5.2 3.6 ...
##  $ AST   : num  2.5 1.9 2.4 3.4 0.4 1.4 3.4 3 2.1 4 ...
##  $ STL   : num  0.6 0.6 0.7 0.8 0.4 0.6 0.7 1 0.6 1.7 ...
##  $ BLK   : num  0.6 0.2 0.1 0 0.1 0.2 1.3 0.3 0.6 0.4 ...
##  $ TOV   : num  1.8 1 1.1 1.3 0.6 1.1 0.9 1.1 1.5 1.4 ...
##  $ PF    : num  2 1.5 1.5 1.5 1.3 1.9 1.9 2.7 1.4 2.6 ...
##  $ PTS   : num  15 6.1 6.3 6.8 3.8 8.3 10.2 11.7 7.6 7.4 ...

Run the Clustering Algorithm

#Run the clustering algo with 2 centers

set.seed(1)
clust_data_salary = df[, c("Salary", "Age", "FT", "PTS")]

kmeans_sal = kmeans(clust_data_salary, centers = 2, 
                        algorithm = "Lloyd")

View the Results of the Algorithm

#View the results
kmeans_sal
## K-means clustering with 2 clusters of sizes 367, 99
## 
## Cluster means:
##     Salary      Age       FT      PTS
## 1  5168582 25.44959 1.156131  8.33406
## 2 26512577 28.02020 3.247475 18.15758
## 
## Clustering vector:
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
##   2   1   1   1   1   1   2   1   1   1   1   1   1   1   1   1   2   2   2   1 
##  21  22  23  24  25  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41 
##   1   1   1   1   2   1   1   1   1   2   1   1   2   1   1   1   1   1   1   1 
##  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61 
##   1   2   2   2   1   1   1   1   1   1   1   1   2   2   2   1   1   1   1   1 
##  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81 
##   2   1   1   2   2   2   2   1   1   1   2   1   1   1   1   1   1   2   1   1 
##  82  83  84  85  86  87  88  89  90  91  92  94  95  96  97  98  99 100 101 102 
##   1   2   1   1   1   1   1   2   2   1   1   1   1   1   1   1   1   1   1   2 
## 103 104 105 106 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 
##   1   1   1   2   2   1   2   1   1   1   1   1   2   2   2   1   2   1   1   1 
## 129 130 131 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 
##   1   1   1   1   2   2   2   2   1   1   1   1   1   2   1   1   1   2   1   1 
## 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 
##   1   2   2   1   1   1   1   1   2   1   1   1   1   1   1   2   1   1   2   1 
## 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 
##   2   1   1   1   1   1   2   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 190 193 194 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 
##   1   1   1   1   1   1   2   1   1   1   1   1   1   1   1   2   2   2   1   1 
## 213 214 215 216 217 218 219 220 221 222 223 224 225 226 228 229 230 231 232 233 
##   1   1   2   1   2   1   1   1   2   1   2   1   2   1   1   1   1   1   1   1 
## 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 
##   2   1   2   1   2   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 254 255 256 257 258 259 260 261 262 263 264 265 267 269 270 271 275 276 277 278 
##   1   1   1   1   2   1   1   2   1   1   1   1   1   1   1   1   1   2   1   1 
## 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 
##   1   1   1   1   1   1   1   1   1   1   1   2   1   1   1   1   2   1   1   1 
## 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 
##   2   1   1   2   1   1   2   2   1   1   1   1   1   1   1   2   1   2   1   2 
## 319 320 321 322 324 326 327 328 329 330 334 335 336 337 338 339 340 341 342 343 
##   1   1   1   1   1   1   1   1   2   1   1   2   2   2   1   1   1   1   1   1 
## 344 345 347 348 349 350 351 352 353 354 356 357 358 359 360 361 362 363 364 365 
##   1   1   1   1   1   2   2   2   1   1   1   1   1   1   1   1   1   2   1   1 
## 366 367 368 371 372 373 374 375 377 378 379 380 381 382 383 384 386 387 388 389 
##   1   1   1   1   1   1   2   2   1   1   2   2   2   1   2   1   1   1   1   1 
## 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 
##   2   1   1   1   1   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 410 411 412 413 414 415 416 417 418 419 420 422 423 425 426 427 428 429 430 431 
##   1   1   1   1   1   1   1   1   2   1   2   1   1   1   1   1   1   1   1   1 
## 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 
##   1   1   2   1   2   2   2   1   2   1   2   1   1   1   1   1   1   1   1   1 
## 452 453 454 455 456 457 458 459 460 461 463 464 465 466 467 468 469 470 471 472 
##   2   1   1   1   1   1   1   1   2   2   1   1   1   2   1   1   1   1   1   1 
## 473 474 475 476 477 479 480 481 482 483 485 486 490 491 492 494 495 496 497 498 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 499 500 501 502 503 504 
##   1   1   1   2   1   1 
## 
## Within cluster sum of squares by cluster:
## [1] 5.228073e+15 7.650439e+15
##  (between_SS / total_SS =  73.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
kmeans_sal$betweenss/kmeans_sal$totss
## [1] 0.7339041
party_clusters_sal = as.factor(kmeans_sal$cluster)

labeldf <- data.frame(df$Player, party_clusters_sal)

Plot the Cluster Output

#Visualize the output
ggplot(df, aes(x = PTS, y = Salary, shape = party_clusters_sal, label = labeldf$df.Player)) + 
  geom_point(size = 6) +
  geom_text(hjust=-0.5, vjust=0, size=1) + 
  ggtitle("Salary vs Total Number of Points Scored") +
  xlab("Total Number of Points") +
  ylab("Salary") +
  scale_shape_manual(name = "Cluster", 
                     labels = c("Cluster 1", "Cluster 2"),
                     values = c("1", "2")) +
  theme_light()

Evaluate the Quality by Calculating Variance

#Evaluate the quality of the clustering 
num_money = kmeans_sal$betweenss

# Total variance, "totss" is the sum of the distances
# between all the points in the data set.
denom_money = kmeans_sal$totss

# Variance accounted for by clusters.
(var_exp_money = num_money / denom_money)
## [1] 0.7339041
# Variance was found to be 0.734

Run Multiple Cluster Numbers

#Use the function we created to evaluate several different number of clusters
set.seed(1)
# The function explained_variance wraps our code for calculating 
# the variance explained by clustering.
explained_variance = function(data_in, k){
  
  # Running the kmeans algorithm.
  set.seed(1)
  kmeans_obj = kmeans(data_in, centers = k, algorithm = "Lloyd", iter.max = 50)
  
  # Variance accounted for by clusters:
  # var_exp = intercluster variance / total variance
  var_exp = kmeans_obj$betweenss / kmeans_obj$totss
  var_exp  
}

explained_var_money = sapply(1:10, explained_variance, data_in = clust_data_salary)

# Variance for 50 iterations
explained_var_money
##  [1] 9.091284e-15 7.339041e-01 8.984976e-01 9.408733e-01 9.540181e-01
##  [6] 9.547446e-01 9.609756e-01 9.627686e-01 9.633565e-01 9.847053e-01

###Create an Elbow Plot of the Output

#Create a elbow chart of the output 
elbow_data_money = data.frame(k = 1:10, explained_var_money)
ggplot(elbow_data_money, 
       aes(x = k,  
           y = explained_var_money)) + 
  geom_point(size = 4) +           #<- sets the size of the data points
  geom_line(size = 1) +            #<- sets the thickness of the line
  xlab('k') + 
  ylab('Inter-cluster Variance / Total Variance') + 
  theme_light()

The results of the elbow graph show that k=2 is the recommended number of clusters. The bend in the graph occurs around k=2.

Running NbClust to Find the Ideal Number of Clusters

#Use NbClust to select a number of clusters

# Run NbClust.
(nbclust_obj_money = NbClust(data = clust_data_salary, method = "kmeans"))

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 6 proposed 2 as the best number of clusters 
## * 5 proposed 3 as the best number of clusters 
## * 1 proposed 4 as the best number of clusters 
## * 2 proposed 5 as the best number of clusters 
## * 2 proposed 7 as the best number of clusters 
## * 1 proposed 8 as the best number of clusters 
## * 6 proposed 10 as the best number of clusters 
## * 1 proposed 14 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
## $All.index
##         KL       CH Hartigan     CCC    Scott      Marriot       TrCovW
## 2   3.1105 1279.732 752.4095 12.4307 2434.280 5.625979e+26 4.146400e+31
## 3   3.4838 2049.234 331.8298 12.7080 2914.814 4.513778e+26 6.033197e+30
## 4   3.7200 2450.578 132.2850 11.2438 3177.704 4.564720e+26 2.047205e+30
## 5   1.9305 2392.074   7.4721  7.7172 3311.841 5.348398e+26 1.237245e+30
## 6   1.0607 1941.949  73.2181  2.0092 3324.391 7.497044e+26 1.198092e+30
## 7   3.6572 1883.973  22.0652 -0.6172 3390.112 8.862053e+26 8.916533e+29
## 8   0.4475 1691.920   7.3789 -4.1312 3420.514 1.084388e+27 8.117336e+29
## 9   0.0750 1501.918 637.9977 -7.6108 3435.916 1.327808e+27 7.861965e+29
## 10 32.2712 3262.560   4.1057  2.5358 3841.630 6.863418e+26 1.369421e+29
## 11  0.9098 2956.654   1.2011 -0.3648 3852.697 8.109836e+26 1.345091e+29
## 12  1.7408 2689.148  29.6642 -3.0974 3874.291 9.214344e+26 1.338017e+29
## 13  0.8875 2622.800   4.9509 -4.6789 3902.143 1.018664e+27 1.178923e+29
## 14  0.7830 2442.483   0.9154 -6.8801 3912.562 1.155289e+27 1.153571e+29
## 15  0.9776 2267.651   0.0490 -9.0576 3928.042 1.282893e+27 1.148913e+29
##          TraceW Friedman    Rubin Cindex     DB Silhouette      Duda  Pseudot2
## 2  1.287851e+16  78.3095   7.1648 0.1422 0.5887     0.7197    0.6559  218.2913
## 3  4.912515e+15  93.6946  18.7829 0.1314 0.5305     0.6803    0.6054  232.7235
## 4  2.861612e+15 107.5628  32.2446 0.0958 0.5322     0.6491    0.6809  106.4002
## 5  2.224631e+15 116.0731  41.4772 0.0676 0.5513     0.6218    1.8017  -52.0627
## 6  2.189148e+15 117.8742  42.1495 0.0663 0.5455     0.5636 1086.3894  -54.9494
## 7  1.888548e+15 124.1486  48.8584 0.0547 0.5119     0.6024   63.0259 -151.5566
## 8  1.801926e+15 126.5411  51.2071 0.0502 0.5182     0.6108    2.2342  -84.5202
## 9  1.773355e+15 128.3393  52.0321 0.0512 0.5148     0.6143    1.3862  -18.3883
## 10 7.401140e+14 203.9378 124.6719 0.0668 0.5284     0.6219    4.5531 -111.5926
## 11 7.335098e+14 206.2804 125.7944 0.0707 0.5469     0.5938    0.8515   16.5624
## 12 7.315786e+14 208.1662 126.1264 0.0750 0.5454     0.5792    0.9702    1.5335
## 13 6.867093e+14 218.3149 134.3675 0.0688 0.5538     0.5823   30.3640  -56.0898
## 14 6.792853e+14 220.2647 135.8360 0.0689 0.5492     0.5931    1.7829  -22.3957
## 15 6.779125e+14 222.2880 136.1111 0.0740 0.5302     0.6158   14.0250  -19.5027
##      Beale Ratkowsky         Ball Ptbiserial      Frey McClain   Dunn Hubert
## 2   1.2634    0.4217 6.439256e+15     0.7755    2.4327  0.1065 0.0257      0
## 3   1.5687    0.3855 1.637505e+15     0.7107    2.5548  0.1645 0.0078      0
## 4   1.1272    0.3459 7.154030e+14     0.6203    3.5420  0.2170 0.0081      0
## 5  -1.0681    0.3132 4.449261e+14     0.4897    8.7860  0.3272 0.0033      0
## 6  -2.1927    0.2877 3.648580e+14     0.4702    1.4103  0.3566 0.0030      0
## 7  -2.1383    0.2666 2.697926e+14     0.4450    4.4082  0.3563 0.0028      0
## 8  -1.3244    0.2506 2.252407e+14     0.4054  -22.8431  0.4159 0.0043      0
## 9  -0.6597    0.2382 1.970394e+14     0.3547    0.0360  0.5720 0.0024      0
## 10 -1.8677    0.2270 7.401140e+13     0.3595   -5.8754  0.4184 0.0045      0
## 11  0.4165    0.2174 6.668271e+13     0.3258   -2.6814  0.5455 0.0014      0
## 12  0.0732    0.2107 6.096488e+13     0.3068    1.1502  0.6537 0.0034      0
## 13 -2.2513    0.2022 5.282379e+13     0.2962 -108.4415  0.6576 0.0034      0
## 14 -1.0194    0.1955 4.852038e+13     0.2858   -1.9968  0.7117 0.0034      0
## 15 -2.1402    0.1902 4.519417e+13     0.2666   -1.6728  0.8762 0.0033      0
##    SDindex    Dindex   SDbw
## 2        0 4119559.6 0.7005
## 3        0 2619422.6 0.3223
## 4        0 1855307.1 0.2722
## 5        0 1416725.3 0.2536
## 6        0 1394815.8 0.2432
## 7        0 1173716.6 0.2049
## 8        0 1061168.3 0.1913
## 9        0  995041.3 0.1779
## 10       0  772123.9 0.1129
## 11       0  755604.4 0.1160
## 12       0  743141.6 0.1326
## 13       0  692446.4 0.1145
## 14       0  668522.3 0.0959
## 15       0  656394.4 0.1338
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.7354           149.6977       0.2824
## 3          0.7259           134.8018       0.1803
## 4          0.7148            90.5674       0.3422
## 5          0.6877            53.1396       1.0000
## 6          0.2576           158.5314       1.0000
## 7          0.2316           510.8316       1.0000
## 8          0.6731            74.3232       1.0000
## 9          0.5633            51.1654       1.0000
## 10         0.6540            75.6382       1.0000
## 11         0.6357            54.4432       0.7967
## 12         0.6230            30.2610       0.9902
## 13         0.4656            66.5583       1.0000
## 14         0.4520            61.8325       1.0000
## 15         0.4195            29.0549       1.0000
## 
## $Best.nc
##                      KL      CH Hartigan    CCC    Scott      Marriot
## Number_clusters 10.0000   10.00   10.000  3.000   3.0000 1.000000e+01
## Value_Index     32.2712 3262.56  633.892 12.708 480.5349 7.661075e+26
##                      TrCovW       TraceW Friedman    Rubin Cindex     DB
## Number_clusters 3.00000e+00 3.000000e+00  10.0000  10.0000 8.0000 7.0000
## Value_Index     3.54308e+31 5.915095e+15  75.5985 -71.5173 0.0502 0.5119
##                 Silhouette   Duda PseudoT2  Beale Ratkowsky         Ball
## Number_clusters     2.0000 5.0000   5.0000 2.0000    2.0000 3.000000e+00
## Value_Index         0.7197 1.8017 -52.0627 1.2634    0.4217 4.801751e+15
##                 PtBiserial   Frey McClain   Dunn Hubert SDindex Dindex    SDbw
## Number_clusters     2.0000 7.0000  2.0000 2.0000      0       4      0 14.0000
## Value_Index         0.7755 4.4082  0.1065 0.0257      0       0      0  0.0959
## 
## $Best.partition
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
##   2   1   1   1   1   1   2   1   1   1   1   1   1   1   1   1   2   2   2   1 
##  21  22  23  24  25  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41 
##   1   1   1   1   2   1   1   1   1   2   1   1   2   1   1   1   1   1   1   1 
##  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61 
##   1   2   2   2   1   1   1   1   1   1   1   1   2   2   2   1   1   1   1   1 
##  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81 
##   2   1   1   2   2   2   2   1   1   1   2   1   1   1   1   1   1   2   1   1 
##  82  83  84  85  86  87  88  89  90  91  92  94  95  96  97  98  99 100 101 102 
##   1   2   1   1   1   1   1   2   2   1   1   1   1   1   1   1   1   1   1   2 
## 103 104 105 106 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 
##   1   1   1   2   2   1   2   1   1   1   1   1   2   2   2   1   2   1   1   1 
## 129 130 131 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 
##   1   1   1   1   2   2   2   2   1   1   1   1   1   2   1   1   1   2   1   1 
## 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 
##   1   2   2   1   1   1   1   1   2   1   1   1   1   1   1   2   1   1   2   1 
## 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 
##   2   1   1   1   1   1   2   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 190 193 194 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 
##   1   1   1   1   1   1   2   1   1   1   1   1   1   1   1   2   2   2   1   1 
## 213 214 215 216 217 218 219 220 221 222 223 224 225 226 228 229 230 231 232 233 
##   1   1   2   1   2   1   1   1   2   1   2   1   2   1   1   1   1   1   1   1 
## 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 
##   2   1   2   1   2   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 254 255 256 257 258 259 260 261 262 263 264 265 267 269 270 271 275 276 277 278 
##   1   1   1   1   2   1   1   2   1   1   1   1   1   1   1   1   1   2   1   1 
## 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 
##   1   1   1   1   1   1   1   1   1   1   1   2   1   1   1   1   2   1   1   1 
## 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 
##   2   1   1   2   1   1   2   2   1   1   1   1   1   1   1   2   1   2   1   2 
## 319 320 321 322 324 326 327 328 329 330 334 335 336 337 338 339 340 341 342 343 
##   1   1   1   1   1   1   1   1   2   1   1   2   2   2   1   1   1   1   1   1 
## 344 345 347 348 349 350 351 352 353 354 356 357 358 359 360 361 362 363 364 365 
##   1   1   1   1   1   2   2   2   1   1   1   1   1   1   1   1   1   2   1   1 
## 366 367 368 371 372 373 374 375 377 378 379 380 381 382 383 384 386 387 388 389 
##   1   1   1   1   1   1   2   2   1   1   2   2   2   1   2   1   1   1   1   1 
## 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 
##   2   1   1   1   1   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 410 411 412 413 414 415 416 417 418 419 420 422 423 425 426 427 428 429 430 431 
##   1   1   1   1   1   1   1   1   2   1   2   1   1   1   1   1   1   1   1   1 
## 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 
##   1   1   2   1   2   2   2   1   2   1   2   1   1   1   1   1   1   1   1   1 
## 452 453 454 455 456 457 458 459 460 461 463 464 465 466 467 468 469 470 471 472 
##   2   1   1   1   1   1   1   1   2   2   1   1   1   2   1   1   1   1   1   1 
## 473 474 475 476 477 479 480 481 482 483 485 486 490 491 492 494 495 496 497 498 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 499 500 501 502 503 504 
##   1   1   1   2   1   1
# View the output of NbClust.
nbclust_obj_money
## $All.index
##         KL       CH Hartigan     CCC    Scott      Marriot       TrCovW
## 2   3.1105 1279.732 752.4095 12.4307 2434.280 5.625979e+26 4.146400e+31
## 3   3.4838 2049.234 331.8298 12.7080 2914.814 4.513778e+26 6.033197e+30
## 4   3.7200 2450.578 132.2850 11.2438 3177.704 4.564720e+26 2.047205e+30
## 5   1.9305 2392.074   7.4721  7.7172 3311.841 5.348398e+26 1.237245e+30
## 6   1.0607 1941.949  73.2181  2.0092 3324.391 7.497044e+26 1.198092e+30
## 7   3.6572 1883.973  22.0652 -0.6172 3390.112 8.862053e+26 8.916533e+29
## 8   0.4475 1691.920   7.3789 -4.1312 3420.514 1.084388e+27 8.117336e+29
## 9   0.0750 1501.918 637.9977 -7.6108 3435.916 1.327808e+27 7.861965e+29
## 10 32.2712 3262.560   4.1057  2.5358 3841.630 6.863418e+26 1.369421e+29
## 11  0.9098 2956.654   1.2011 -0.3648 3852.697 8.109836e+26 1.345091e+29
## 12  1.7408 2689.148  29.6642 -3.0974 3874.291 9.214344e+26 1.338017e+29
## 13  0.8875 2622.800   4.9509 -4.6789 3902.143 1.018664e+27 1.178923e+29
## 14  0.7830 2442.483   0.9154 -6.8801 3912.562 1.155289e+27 1.153571e+29
## 15  0.9776 2267.651   0.0490 -9.0576 3928.042 1.282893e+27 1.148913e+29
##          TraceW Friedman    Rubin Cindex     DB Silhouette      Duda  Pseudot2
## 2  1.287851e+16  78.3095   7.1648 0.1422 0.5887     0.7197    0.6559  218.2913
## 3  4.912515e+15  93.6946  18.7829 0.1314 0.5305     0.6803    0.6054  232.7235
## 4  2.861612e+15 107.5628  32.2446 0.0958 0.5322     0.6491    0.6809  106.4002
## 5  2.224631e+15 116.0731  41.4772 0.0676 0.5513     0.6218    1.8017  -52.0627
## 6  2.189148e+15 117.8742  42.1495 0.0663 0.5455     0.5636 1086.3894  -54.9494
## 7  1.888548e+15 124.1486  48.8584 0.0547 0.5119     0.6024   63.0259 -151.5566
## 8  1.801926e+15 126.5411  51.2071 0.0502 0.5182     0.6108    2.2342  -84.5202
## 9  1.773355e+15 128.3393  52.0321 0.0512 0.5148     0.6143    1.3862  -18.3883
## 10 7.401140e+14 203.9378 124.6719 0.0668 0.5284     0.6219    4.5531 -111.5926
## 11 7.335098e+14 206.2804 125.7944 0.0707 0.5469     0.5938    0.8515   16.5624
## 12 7.315786e+14 208.1662 126.1264 0.0750 0.5454     0.5792    0.9702    1.5335
## 13 6.867093e+14 218.3149 134.3675 0.0688 0.5538     0.5823   30.3640  -56.0898
## 14 6.792853e+14 220.2647 135.8360 0.0689 0.5492     0.5931    1.7829  -22.3957
## 15 6.779125e+14 222.2880 136.1111 0.0740 0.5302     0.6158   14.0250  -19.5027
##      Beale Ratkowsky         Ball Ptbiserial      Frey McClain   Dunn Hubert
## 2   1.2634    0.4217 6.439256e+15     0.7755    2.4327  0.1065 0.0257      0
## 3   1.5687    0.3855 1.637505e+15     0.7107    2.5548  0.1645 0.0078      0
## 4   1.1272    0.3459 7.154030e+14     0.6203    3.5420  0.2170 0.0081      0
## 5  -1.0681    0.3132 4.449261e+14     0.4897    8.7860  0.3272 0.0033      0
## 6  -2.1927    0.2877 3.648580e+14     0.4702    1.4103  0.3566 0.0030      0
## 7  -2.1383    0.2666 2.697926e+14     0.4450    4.4082  0.3563 0.0028      0
## 8  -1.3244    0.2506 2.252407e+14     0.4054  -22.8431  0.4159 0.0043      0
## 9  -0.6597    0.2382 1.970394e+14     0.3547    0.0360  0.5720 0.0024      0
## 10 -1.8677    0.2270 7.401140e+13     0.3595   -5.8754  0.4184 0.0045      0
## 11  0.4165    0.2174 6.668271e+13     0.3258   -2.6814  0.5455 0.0014      0
## 12  0.0732    0.2107 6.096488e+13     0.3068    1.1502  0.6537 0.0034      0
## 13 -2.2513    0.2022 5.282379e+13     0.2962 -108.4415  0.6576 0.0034      0
## 14 -1.0194    0.1955 4.852038e+13     0.2858   -1.9968  0.7117 0.0034      0
## 15 -2.1402    0.1902 4.519417e+13     0.2666   -1.6728  0.8762 0.0033      0
##    SDindex    Dindex   SDbw
## 2        0 4119559.6 0.7005
## 3        0 2619422.6 0.3223
## 4        0 1855307.1 0.2722
## 5        0 1416725.3 0.2536
## 6        0 1394815.8 0.2432
## 7        0 1173716.6 0.2049
## 8        0 1061168.3 0.1913
## 9        0  995041.3 0.1779
## 10       0  772123.9 0.1129
## 11       0  755604.4 0.1160
## 12       0  743141.6 0.1326
## 13       0  692446.4 0.1145
## 14       0  668522.3 0.0959
## 15       0  656394.4 0.1338
## 
## $All.CriticalValues
##    CritValue_Duda CritValue_PseudoT2 Fvalue_Beale
## 2          0.7354           149.6977       0.2824
## 3          0.7259           134.8018       0.1803
## 4          0.7148            90.5674       0.3422
## 5          0.6877            53.1396       1.0000
## 6          0.2576           158.5314       1.0000
## 7          0.2316           510.8316       1.0000
## 8          0.6731            74.3232       1.0000
## 9          0.5633            51.1654       1.0000
## 10         0.6540            75.6382       1.0000
## 11         0.6357            54.4432       0.7967
## 12         0.6230            30.2610       0.9902
## 13         0.4656            66.5583       1.0000
## 14         0.4520            61.8325       1.0000
## 15         0.4195            29.0549       1.0000
## 
## $Best.nc
##                      KL      CH Hartigan    CCC    Scott      Marriot
## Number_clusters 10.0000   10.00   10.000  3.000   3.0000 1.000000e+01
## Value_Index     32.2712 3262.56  633.892 12.708 480.5349 7.661075e+26
##                      TrCovW       TraceW Friedman    Rubin Cindex     DB
## Number_clusters 3.00000e+00 3.000000e+00  10.0000  10.0000 8.0000 7.0000
## Value_Index     3.54308e+31 5.915095e+15  75.5985 -71.5173 0.0502 0.5119
##                 Silhouette   Duda PseudoT2  Beale Ratkowsky         Ball
## Number_clusters     2.0000 5.0000   5.0000 2.0000    2.0000 3.000000e+00
## Value_Index         0.7197 1.8017 -52.0627 1.2634    0.4217 4.801751e+15
##                 PtBiserial   Frey McClain   Dunn Hubert SDindex Dindex    SDbw
## Number_clusters     2.0000 7.0000  2.0000 2.0000      0       4      0 14.0000
## Value_Index         0.7755 4.4082  0.1065 0.0257      0       0      0  0.0959
## 
## $Best.partition
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
##   2   1   1   1   1   1   2   1   1   1   1   1   1   1   1   1   2   2   2   1 
##  21  22  23  24  25  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41 
##   1   1   1   1   2   1   1   1   1   2   1   1   2   1   1   1   1   1   1   1 
##  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61 
##   1   2   2   2   1   1   1   1   1   1   1   1   2   2   2   1   1   1   1   1 
##  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81 
##   2   1   1   2   2   2   2   1   1   1   2   1   1   1   1   1   1   2   1   1 
##  82  83  84  85  86  87  88  89  90  91  92  94  95  96  97  98  99 100 101 102 
##   1   2   1   1   1   1   1   2   2   1   1   1   1   1   1   1   1   1   1   2 
## 103 104 105 106 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 
##   1   1   1   2   2   1   2   1   1   1   1   1   2   2   2   1   2   1   1   1 
## 129 130 131 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 
##   1   1   1   1   2   2   2   2   1   1   1   1   1   2   1   1   1   2   1   1 
## 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 
##   1   2   2   1   1   1   1   1   2   1   1   1   1   1   1   2   1   1   2   1 
## 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 
##   2   1   1   1   1   1   2   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 190 193 194 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 
##   1   1   1   1   1   1   2   1   1   1   1   1   1   1   1   2   2   2   1   1 
## 213 214 215 216 217 218 219 220 221 222 223 224 225 226 228 229 230 231 232 233 
##   1   1   2   1   2   1   1   1   2   1   2   1   2   1   1   1   1   1   1   1 
## 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 
##   2   1   2   1   2   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 254 255 256 257 258 259 260 261 262 263 264 265 267 269 270 271 275 276 277 278 
##   1   1   1   1   2   1   1   2   1   1   1   1   1   1   1   1   1   2   1   1 
## 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 
##   1   1   1   1   1   1   1   1   1   1   1   2   1   1   1   1   2   1   1   1 
## 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 
##   2   1   1   2   1   1   2   2   1   1   1   1   1   1   1   2   1   2   1   2 
## 319 320 321 322 324 326 327 328 329 330 334 335 336 337 338 339 340 341 342 343 
##   1   1   1   1   1   1   1   1   2   1   1   2   2   2   1   1   1   1   1   1 
## 344 345 347 348 349 350 351 352 353 354 356 357 358 359 360 361 362 363 364 365 
##   1   1   1   1   1   2   2   2   1   1   1   1   1   1   1   1   1   2   1   1 
## 366 367 368 371 372 373 374 375 377 378 379 380 381 382 383 384 386 387 388 389 
##   1   1   1   1   1   1   2   2   1   1   2   2   2   1   2   1   1   1   1   1 
## 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 
##   2   1   1   1   1   2   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 410 411 412 413 414 415 416 417 418 419 420 422 423 425 426 427 428 429 430 431 
##   1   1   1   1   1   1   1   1   2   1   2   1   1   1   1   1   1   1   1   1 
## 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 
##   1   1   2   1   2   2   2   1   2   1   2   1   1   1   1   1   1   1   1   1 
## 452 453 454 455 456 457 458 459 460 461 463 464 465 466 467 468 469 470 471 472 
##   2   1   1   1   1   1   1   1   2   2   1   1   1   2   1   1   1   1   1   1 
## 473 474 475 476 477 479 480 481 482 483 485 486 490 491 492 494 495 496 497 498 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 499 500 501 502 503 504 
##   1   1   1   2   1   1
# View the output that shows the number of clusters each method recommends.
str(nbclust_obj_money$Best.nc)
##  num [1:2, 1:26] 10 32.3 10 3262.6 10 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : chr [1:2] "Number_clusters" "Value_Index"
##   ..$ : chr [1:26] "KL" "CH" "Hartigan" "CCC" ...

Displaying NBClust Results

#Display the results visually 
freq_k = nbclust_obj_money$Best.nc[1,]
freq_k = data.frame(freq_k)
view(freq_k)

nbclust_obj_money$Best.nc
##                      KL      CH Hartigan    CCC    Scott      Marriot
## Number_clusters 10.0000   10.00   10.000  3.000   3.0000 1.000000e+01
## Value_Index     32.2712 3262.56  633.892 12.708 480.5349 7.661075e+26
##                      TrCovW       TraceW Friedman    Rubin Cindex     DB
## Number_clusters 3.00000e+00 3.000000e+00  10.0000  10.0000 8.0000 7.0000
## Value_Index     3.54308e+31 5.915095e+15  75.5985 -71.5173 0.0502 0.5119
##                 Silhouette   Duda PseudoT2  Beale Ratkowsky         Ball
## Number_clusters     2.0000 5.0000   5.0000 2.0000    2.0000 3.000000e+00
## Value_Index         0.7197 1.8017 -52.0627 1.2634    0.4217 4.801751e+15
##                 PtBiserial   Frey McClain   Dunn Hubert SDindex Dindex    SDbw
## Number_clusters     2.0000 7.0000  2.0000 2.0000      0       4      0 14.0000
## Value_Index         0.7755 4.4082  0.1065 0.0257      0       0      0  0.0959
# Check the maximum number of clusters suggested.
max(freq_k)
## [1] 14
# Plot as a histogram.
ggplot(freq_k,
       aes(x = freq_k)) +
  geom_bar() +
  scale_x_continuous(breaks = seq(0, 15, by = 1)) +
  scale_y_continuous(breaks = seq(0, 12, by = 1)) +
  labs(x = "Number of Clusters",
       y = "Number of Votes",
       title = "Cluster Analysis")

### Compare the output to the elbow chart method to recommended clusters, assuming it’s different. In this case, both the nbclust() analysis and the elbow chart method showed that two clusters was the best option. The two methods differ, because the elbow chart method only considers variance. NbClust is a more hollistic approach, because it obtains the best clustering scheme by varying combinations of clusters, methods, and distance measures.

What differences and similarities did you see between how the clustering worked for the datasets?

The choice of variable alongside Salary was the most important part of the clustering method. Variables such as Age were not as descriptive, because there were no distinct clusters when examining those variables. One additional analysis I would do would be to redo the process but look at either number of freethrows scored or percentage of freethrows made.

Conclusion

The players that the team should select are Ja Morant, Darius Garland, and Anthony Edwards, since their total number of scored points is high, yet their salaries are low.