13  Clustering

One common effort in sports is to classify teams and players – who are this players peers? What teams are like this one? Who should we compare a player to? Truth is, most sports commentators use nothing more sophisticated that looking at a couple of stats or use the “eye test” to say a player is like this or that.

There’s better ways.

In this chapter, we’re going to use a method that sounds advanced but it really quite simple called k-means clustering. It’s based on the concept of the k-nearest neighbor algorithm. You’re probably already scared. Don’t be.

Imagine two dots on a scatterplot. If you took a ruler out and measured the distance between those dots, you’d know how far apart they are. In math, that’s called the Euclidean distance. It’s just the space between them in numbers. Where k-nearest neighbor comes in, you have lots of dots and you want measure the distance between all of them. What does k-means clustering do? It lumps them into groups based on the average distance between them. Players who are good on offense but bad on defense are over here, good offense good defense are over here. And using the Euclidean distance between them, we can decide who is in and who is out of those groups.

For this exercise, I want to look at Cam Mack, Nebraska’s point guard and probably the most interesting player on Fred Hoiberg’s first team. This was Mack’s first – only? – year in major college basketball. I believe Mack could have been one of the best players Nebraska ever had, but it didn’t work out. So who does Cam Mack compare to?

To answer this, we’ll use k-means clustering.

First thing we do is load some libraries and set a seed, so if we run this repeatedly, our random numbers are generated from the same base. If you don’t have the cluster library, just add it on the console with install.packages("cluster")

library(tidyverse)
library(cluster)

set.seed(1234)

I’ve gone and scraped stats for every player in that season.

For this walkthrough:

Now load that data.

players <- read_csv("data/players20.csv")
Rows: 5452 Columns: 57
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (8): Team, Player, Class, Pos, Height, Hometown, High School, Summary
dbl (49): #, Weight, Rk.x, G, GS, MP, FG, FGA, FG%, 2P, 2PA, 2P%, 3P, 3PA, 3...

ℹ 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.

To cluster this data properly, we have some work to do.

First, it won’t do to have players who haven’t played, so we can use filter to find anyone with greater than 0 minutes played. Next, Cam Mack is a guard, so let’s just look at guards. Third, we want to limit the data to things that make sense to look at for Cam Mack – things like shooting, three point shooting, assists, turnovers and points.

playersselected <- players |> 
  filter(MP>0) |> filter(Pos == "G") |> 
  select(Player, Team, Pos, MP, `FG%`, `3P%`, AST, TOV, PTS) |> 
  na.omit() 

Now, k-means clustering doesn’t work as well with data that can be on different scales. So comparing a percentage to a count metric – shooting percentage to points – would create chaos because shooting percentages are a fraction of 1 and points, depending on when they are in the season, could be quite large. So we have to scale each metric – put them on a similar basis using the distance from the max value as our guide. Also, k-means clustering won’t work with text data, so we need to create a dataframe that’s just the numbers, but scaled. We can do that with another select, and using mutate_all with the scale function. The na.omit() means get rid of any blanks, because they too will cause errors.

playersscaled <- playersselected |> 
  select(MP, `FG%`, `3P%`, AST, TOV, PTS) |> 
  mutate_all(scale) |> 
  na.omit()

With k-means clustering, we decide how many clusters we want. Most often, researchers will try a handful of different cluster numbers and see what works. But there are methods for finding the optimal number. One method is called the Elbow method. One implementation of this, borrowed from the University of Cincinnati’s Business Analytics program, does this quite nicely with a graph that will help you decide for yourself.

All you need to do in this code is change out the data frame – playersscaled in this case – and run it.

# function to compute total within-cluster sum of square 
wss <- function(k) {
  kmeans(playersscaled, k, nstart = 10 )$tot.withinss
}

# Compute and plot wss for k = 1 to k = 15
k.values <- 1:15

# extract wss for 2-15 clusters
wss_values <- map_dbl(k.values, wss)
Warning: did not converge in 10 iterations

Warning: did not converge in 10 iterations

Warning: did not converge in 10 iterations

Warning: did not converge in 10 iterations

Warning: did not converge in 10 iterations

Warning: did not converge in 10 iterations

Warning: did not converge in 10 iterations
plot(k.values, wss_values,
       type="b", pch = 19, frame = FALSE, 
       xlab="Number of clusters K",
       ylab="Total within-clusters sum of squares")

The Elbow method – so named because you’re looking for the “elbow” where the line flattens out. In this case, it looks like a K of 5 is ideal. So let’s try that. We’re going to use the kmeans function, saving it to an object called k5. We just need to tell it our dataframe name, how many centers (k) we want, and we’ll use a sensible default for how many different configurations to try.

k5 <- kmeans(playersscaled, centers = 5, nstart = 25)

Let’s look at what we get.

k5
K-means clustering with 5 clusters of sizes 242, 58, 495, 989, 863

Cluster means:
          MP         FG%         3P%        AST        TOV        PTS
1 -1.3602270 -1.98012908 -1.65325745 -0.9392522 -1.1172744 -1.1417360
2 -1.3785814  3.22934178  3.92207534 -0.9251265 -1.1485250 -1.1077564
3  1.2279089  0.26624900  0.17613521  1.5255303  1.5159849  1.4306790
4  0.5451701  0.14083415  0.12854961  0.1392254  0.2539075  0.3248641
5 -0.8549890  0.02411493 -0.04833668 -0.7090093 -0.7700257 -0.7982928

Clustering vector:
   [1] 3 4 4 4 4 5 5 5 5 4 4 3 4 5 5 5 3 4 5 5 1 1 3 4 4 4 4 4 5 2 4 4 3 4 4 5 1
  [38] 3 3 4 4 5 4 5 5 3 4 4 4 5 5 4 3 4 4 4 4 5 5 1 4 3 5 4 4 5 5 5 5 5 4 4 3 4
  [75] 4 5 1 5 1 3 4 4 4 5 5 5 5 3 4 4 4 4 5 1 3 4 4 5 5 5 3 4 5 4 5 5 5 5 2 4 4
 [112] 5 5 5 4 5 1 3 4 4 4 5 5 5 3 4 4 5 1 1 3 4 4 5 5 5 3 4 4 4 4 5 5 3 3 4 4 4
 [149] 4 1 3 4 4 4 5 5 5 1 3 4 4 4 4 3 5 1 3 3 4 4 4 4 5 5 4 4 4 4 5 5 5 5 5 5 3
 [186] 4 1 1 3 4 4 4 5 5 1 5 3 3 4 4 5 5 5 2 1 3 4 4 3 4 4 5 5 5 1 3 4 5 5 5 1 1
 [223] 3 3 4 4 4 4 4 1 3 4 4 3 5 3 4 3 4 4 5 5 5 4 4 4 4 4 5 1 3 3 3 4 4 4 5 5 1
 [260] 1 3 3 4 4 4 5 5 5 1 3 3 4 4 4 5 5 5 3 4 4 4 1 3 3 4 4 5 1 4 3 3 4 3 4 5 5
 [297] 3 4 4 4 5 5 5 5 5 3 4 4 5 1 5 1 1 3 3 4 4 5 5 3 3 4 4 4 5 5 4 4 4 4 5 5 5
 [334] 3 3 4 4 4 4 5 3 3 4 5 5 5 1 3 4 4 4 4 5 1 3 3 4 5 4 5 5 1 1 3 4 4 4 4 5 5
 [371] 5 3 3 4 4 5 5 5 5 5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 4 4 5 5 5 2 3 4 4 4 5 5 1 3 4 4 4 5 5 4 4 4 4 5 4 5 5 3 4 4 5 5 5
[2332] 4 3 4 4 4 4 5 5 5 5 3 4 3 4 5 5 5 2 3 3 3 5 1 3 3 4 5 5 2 1 4 3 4 4 4 4 4
[2369] 4 4 4 1 1 3 3 5 5 3 3 4 4 5 1 3 4 4 5 5 5 3 3 4 4 4 1 5 3 4 5 5 5 5 1 1 3
[2406] 3 4 4 4 5 5 5 2 3 4 4 4 1 5 5 4 4 3 4 4 4 5 5 5 4 3 4 4 4 5 5 5 1 3 3 4 5
[2443] 5 3 3 4 4 4 5 5 2 3 4 4 4 5 5 5 3 4 4 4 4 4 5 2 3 3 4 4 5 5 5 5 3 4 3 4 4
[2480] 5 1 1 3 3 4 4 5 5 1 3 3 4 3 4 4 5 5 5 3 3 5 5 5 1 5 3 3 4 4 5 5 1 5 1 3 4
[2517] 4 4 4 3 3 4 4 4 4 1 1 5 5 4 5 5 5 5 5 1 3 4 4 4 5 5 5 1 3 4 4 4 5 5 3 4 4
[2554] 5 5 5 5 3 4 4 5 5 5 1 1 1 3 4 4 4 5 5 1 1 3 3 4 4 5 5 5 1 3 3 4 4 4 5 1 5
[2591] 3 3 4 4 5 5 3 4 4 4 4 5 5 5 5 5 5 3 3 3 5 5 5 5 1 4 4 3 4 1 5 1 1 3 3 4 5
[2628] 1 1 3 4 4 4 5 5 5 5 5 5 3 4 4 4 5 5 5 5

Within cluster sum of squares by cluster:
[1]  319.4278  250.5515 1094.8283 1341.0495 1430.7781
 (between_SS / total_SS =  72.1 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
[6] "betweenss"    "size"         "iter"         "ifault"      

Interpreting this output, the very first thing you need to know is that the cluster numbers are meaningless. They aren’t ranks. They aren’t anything. After you have taken that on board, look at the cluster sizes at the top. Clusters 1 and 2 are pretty large compared to others. That’s notable. Then we can look at the cluster means. For reference, 0 is going to be average. So group 1 is below average on minutes played. Group 2 is slightly above, group 5 is well above.

So which group is Cam Mack in? Well, first we have to put our data back together again. In K5, there is a list of cluster assignments in the same order we put them in, but recall we have no names. So we need to re-combine them with our original data. We can do that with the following:

playercluster <- data.frame(playersselected, k5$cluster) 

Now we have a dataframe called playercluster that has our player names and what cluster they are in. The fastest way to find Cam Mack is to double click on the playercluster table in the environment and use the search in the top right of the table. Because this is based on some random selections of points to start the groupings, these may change from person to person, but Mack is in Group 1 in my data.

We now have a dataset and can plot it like anything else. Let’s get Cam Mack and then plot him against the rest of college basketball on assists versus minutes played.

cm <- playercluster |> filter(Player == "Cam Mack")

cm
    Player                 Team Pos  MP  FG.  X3P. AST TOV PTS k5.cluster
1 Cam Mack Nebraska Cornhuskers   G 931 0.39 0.339 172  71 324          3

So Cam’s in cluster 3, which if you look at our clusters, puts him in the cluster with all above average metrics. What does that look like? We know Cam was an assist machine, so where do group 5 people grade out on assists?

ggplot() + 
  geom_point(data=playercluster, aes(x=MP, y=AST, color=k5.cluster)) + 
  geom_point(data=cm, aes(x=MP, y=AST), color="red")

Not bad, not bad. But who are Cam Mack’s peers? If we look at the numbers in Group 3, there’s 495 of them. So let’s limit them to just Big Ten guards. Unfortunately, my scraper didn’t quite work and in the place of Conference is the coach’s name. So I’m going to have to do this the hard way and make a list of Big Ten teams and filter on that. Then I’ll sort by minutes played.

big10 <- c("Nebraska Cornhuskers", "Iowa Hawkeyes", "Minnesota Golden Gophers", "Illinois Fighting Illini", "Northwestern Wildcats", "Wisconsin Badgers", "Indiana Hoosiers", "Purdue Boilermakers", "Ohio State Buckeyes", "Michigan Wolverines", "Michigan State Spartans", "Penn State Nittany Lions", "Rutgers Scarlet Knights", "Maryland Terrapins")

playercluster |> filter(k5.cluster == 3) |> filter(Team %in% big10) |> arrange(desc(MP))
            Player                     Team Pos   MP   FG.  X3P. AST TOV PTS
1      Marcus Carr Minnesota Golden Gophers   G 1004 0.377 0.341 181  76 419
2    Anthony Cowan       Maryland Terrapins   G  965 0.379 0.331 133  59 454
3         Cam Mack     Nebraska Cornhuskers   G  931 0.390 0.339 172  71 324
4  Eric Hunter Jr.      Purdue Boilermakers   G  907 0.419 0.373  77  57 297
5   Zavier Simpson      Michigan Wolverines   G  907 0.472 0.354 213  85 351
6      Ayo Dosunmu Illinois Fighting Illini   G  891 0.483 0.295  86  72 443
7   D'Mitrik Trice        Wisconsin Badgers   G  883 0.397 0.390 116  50 289
8  Cassius Winston  Michigan State Spartans   G  868 0.432 0.409 157  85 497
9        CJ Walker      Ohio State Buckeyes   G  795 0.420 0.338  94  47 227
10     Pat Spencer    Northwestern Wildcats   G  787 0.458 0.250 103  63 286
11       Geo Baker  Rutgers Scarlet Knights   G  736 0.389 0.261  91  45 273
   k5.cluster
1           3
2           3
3           3
4           3
5           3
6           3
7           3
8           3
9           3
10          3
11          3

So there are the 11 guards most like Cam Mack in the Big Ten. Safe to say, these are the 11 best guards in the conference.

13.1 Advanced metrics

How much does this change if we change the metrics? I used pretty standard box score metrics above. What if we did it using Player Efficiency Rating, True Shooting Percentage, Point Production, Assist Percentage, Win Shares Per 40 Minutes and Box Plus Minus (you can get definitions of all of them by hovering over the stats on Nebraksa’s stats page).

We’ll repeat the process. Filter out players who don’t play, players with stats missing, and just focus on those stats listed above.

playersadvanced <- players |> 
  filter(MP>0) |> 
  filter(Pos == "G") |> 
  select(Player, Team, Pos, PER, `TS%`, PProd, `AST%`, `WS/40`, BPM) |> 
  na.omit() 

Now to scale them.

playersadvscaled <- playersadvanced |> 
  select(PER, `TS%`, PProd, `AST%`, `WS/40`, BPM) |> 
  mutate_all(scale) |> 
  na.omit()

Let’s find the optimal number of clusters.

# function to compute total within-cluster sum of square 
wss <- function(k) {
  kmeans(playersadvscaled, k, nstart = 10 )$tot.withinss
}

# Compute and plot wss for k = 1 to k = 15
k.values <- 1:15

# extract wss for 2-15 clusters
wss_values <- map_dbl(k.values, wss)

plot(k.values, wss_values,
       type="b", pch = 19, frame = FALSE, 
       xlab="Number of clusters K",
       ylab="Total within-clusters sum of squares")

Looks like 5 again.

advk5 <- kmeans(playersadvscaled, centers = 5, nstart = 25)

What do we have here?

advk5
K-means clustering with 5 clusters of sizes 104, 1253, 9, 632, 766

Cluster means:
         PER        TS%      PProd       AST%      WS/40        BPM
1 -2.6922417 -2.6802561 -1.1497380 -1.0109009 -2.8398288 -2.8455990
2  0.1594366  0.3741799 -0.2279088 -0.2650562  0.2130658  0.2118878
3  8.9269330  4.3359455 -1.1326482  2.2478231  8.0858448  6.3926472
4 -0.6089002 -0.6736304 -0.8322766 -0.2324539 -0.5651578 -0.6493960
5  0.5022214  0.2566711  1.2288970  0.7362004  0.4083263  0.5004325

Clustering vector:
   [1] 5 2 2 2 2 4 2 4 2 5 2 5 2 2 4 2 5 2 2 2 4 4 2 1 5 2 2 2 2 4 2 2 5 5 5 2 2
  [38] 2 4 5 5 2 2 2 2 2 4 4 5 2 2 2 2 2 2 1 5 5 2 2 5 2 2 2 2 5 5 5 2 2 2 4 4 4
  [75] 4 4 5 2 5 2 5 4 1 2 4 5 2 2 2 2 4 2 4 5 5 2 2 2 4 4 5 5 2 4 4 4 4 5 2 2 2
 [112] 2 2 4 4 2 5 5 2 2 2 2 4 4 5 5 2 4 2 4 4 5 5 2 2 4 2 3 1 5 2 2 2 4 2 5 2 2
 [149] 2 2 4 2 1 5 5 2 2 5 2 4 5 5 2 4 2 4 3 4 1 5 5 2 2 2 5 2 4 5 5 2 5 2 2 2 4
 [186] 5 5 5 2 2 2 2 2 2 4 5 5 4 4 5 2 2 2 4 2 4 2 5 5 2 2 4 2 2 3 4 5 2 2 5 2 2
 [223] 2 4 2 4 5 2 2 2 2 4 4 5 5 2 2 2 2 2 4 5 2 5 5 2 5 2 5 2 2 2 2 2 4 1 5 5 5
 [260] 2 4 2 1 5 5 5 2 2 2 2 2 4 5 1 1 5 5 2 2 2 2 2 4 4 5 5 2 2 2 2 2 2 2 5 2 2
 [297] 2 5 4 5 5 5 2 4 1 5 5 5 2 5 2 2 2 5 5 2 5 2 2 2 2 2 5 2 4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5 5 2 2 4 2 1 1 5 2 5 4 4 4 4 1
[2739] 5 5 2 4 4 2 4 5 2 2 2 2 2 2 2 4 2 2 5 5 5 4 2 2 2 2

Within cluster sum of squares by cluster:
[1]  737.6141 2624.4456  722.9391 1248.0870 1849.5824
 (between_SS / total_SS =  56.7 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
[6] "betweenss"    "size"         "iter"         "ifault"      

Looks like this time, cluster 1 is all below average and cluster 5 is mostly above. Which cluster is Cam Mack in?

playeradvcluster <- data.frame(playersadvanced, advk5$cluster) 
cmadv <- playeradvcluster |> filter(Player == "Cam Mack")

cmadv
    Player                 Team Pos  PER   TS. PProd AST. WS.40 BPM
1 Cam Mack Nebraska Cornhuskers   G 15.9 0.481   382 36.4 0.081 3.9
  advk5.cluster
1             5

Cluster 2 on my dataset. So in this season, we can say he’s in a big group of players who are all above average on these advanced metrics.

Now who are his Big Ten peers?

playeradvcluster |> 
  filter(advk5.cluster == 2) |> 
  filter(Team %in% big10) |> 
  arrange(desc(PProd))
                  Player                     Team Pos  PER   TS. PProd AST.
1           Dachon Burke     Nebraska Cornhuskers   G 14.3 0.461   294  9.7
2         Gabe Kalscheur Minnesota Golden Gophers   G 10.5 0.491   273  9.2
3          Trent Frazier Illinois Fighting Illini   G 11.5 0.495   251 12.0
4           Brad Davison        Wisconsin Badgers   G 14.3 0.546   250 11.9
5           Franz Wagner      Michigan Wolverines   G 15.5 0.552   242  5.6
6     Izaiah Brockington Penn State Nittany Lions   G 15.5 0.530   232  9.4
7            Jacob Young  Rutgers Scarlet Knights   G 12.3 0.455   227 17.4
8       Sasha Stefanovic      Purdue Boilermakers   G 13.9 0.549   221 11.3
9  Thorir Thorbjarnarson     Nebraska Cornhuskers   G 14.1 0.606   218  8.8
10       Caleb McConnell  Rutgers Scarlet Knights   G 14.2 0.493   213 13.3
11          Alan Griffin Illinois Fighting Illini   G 27.6 0.652   209  6.1
12         Brevin Pritzl        Wisconsin Badgers   G 13.5 0.545   205  5.5
13         Montez Mathis  Rutgers Scarlet Knights   G 12.7 0.451   201 10.0
14         Payton Willis Minnesota Golden Gophers   G 13.4 0.517   193 13.3
15             Kobe King        Wisconsin Badgers   G 14.4 0.513   188 12.2
16        David Dejulius      Michigan Wolverines   G 13.3 0.522   187 12.9
17          Jervay Green     Nebraska Cornhuskers   G 11.5 0.481   185 13.8
18         Nojel Eastern      Purdue Boilermakers   G 11.5 0.434   182 19.3
19       Luther Muhammad      Ohio State Buckeyes   G 12.2 0.557   181 10.9
20          Rocket Watts  Michigan State Spartans   G 10.1 0.473   178 15.5
21      Curtis Jones Jr. Penn State Nittany Lions   G 10.8 0.462   165 12.0
22        Jamari Wheeler Penn State Nittany Lions   G 10.4 0.564   147 19.6
23       Isaiah Thompson      Purdue Boilermakers   G  8.3 0.485   140  6.8
24          Paul Mulcahy  Rutgers Scarlet Knights   G 13.0 0.605   128 18.7
25         Bakari Evelyn            Iowa Hawkeyes   G  5.8 0.485   114 16.9
26       Armaan Franklin         Indiana Hoosiers   G  6.8 0.445   109 16.9
27           Matej Kavas     Nebraska Cornhuskers   G 11.1 0.523   100  7.4
28       Jordan Bohannon            Iowa Hawkeyes   G 12.7 0.475    91 22.6
29          Foster Loyer  Michigan State Spartans   G 16.5 0.662    86 22.7
30     Da'Monte Williams Illinois Fighting Illini   G  8.3 0.427    80 10.1
31           Kyle Ahrens  Michigan State Spartans   G 10.5 0.601    78  7.0
32       Trevor Anderson        Wisconsin Badgers   G  8.3 0.483    66 19.9
33        Anthony Gaines    Northwestern Wildcats   G 13.0 0.505    65 12.2
34          Walt McGrory        Wisconsin Badgers   G 12.9 0.503    17 17.5
35         Conner George  Michigan State Spartans   G 15.8 0.420    17  3.6
36           Cole Bajema      Michigan Wolverines   G 35.2 0.927    14  0.0
37          Danny Hummer      Ohio State Buckeyes   G 11.9 0.411    11 25.7
38         Samari Curtis     Nebraska Cornhuskers   G  9.7 0.639    11  3.7
39            Austin Ash            Iowa Hawkeyes   G 16.1 0.432    11 14.9
40            Reese Mona       Maryland Terrapins   G 20.5 1.017     8 13.4
41           Joey Downes  Rutgers Scarlet Knights   G 35.2 1.013     5  0.0
42       Stephen Beattie Penn State Nittany Lions   G 33.5 0.750     4  0.0
43         Jared Wulbrun      Purdue Boilermakers   G  5.0 0.750     3 21.0
44       Taylor Nussbaum Penn State Nittany Lions   G 24.2 0.256     3 18.4
45         Travis Valmon       Maryland Terrapins   G 19.5 0.678     3  0.0
46       Michael Ballard        Wisconsin Badgers   G 17.5 1.053     2  0.0
47          Cooper Bybee         Indiana Hoosiers   G 37.9 1.500     1  0.0
   WS.40  BPM advk5.cluster
1  0.049  1.1             2
2  0.079  3.8             2
3  0.130  5.2             2
4  0.137  6.7             2
5  0.125  7.0             2
6  0.123  3.4             2
7  0.071  1.8             2
8  0.133  6.8             2
9  0.097  4.3             2
10 0.121  5.1             2
11 0.252 10.6             2
12 0.123  5.7             2
13 0.111  3.7             2
14 0.120  6.0             2
15 0.100  3.3             2
16 0.113  4.5             2
17 0.044  1.6             2
18 0.085  4.5             2
19 0.129  6.4             2
20 0.084  1.3             2
21 0.087  1.5             2
22 0.095  5.8             2
23 0.078  1.5             2
24 0.130  5.7             2
25 0.027  0.6             2
26 0.047  0.3             2
27 0.061 -0.7             2
28 0.098  2.5             2
29 0.183  5.0             2
30 0.091  6.1             2
31 0.110  4.8             2
32 0.075  1.9             2
33 0.078  4.2             2
34 0.144  4.6             2
35 0.156 -1.1             2
36 0.253 11.0             2
37 0.104  6.0             2
38 0.070 -0.8             2
39 0.106  6.0             2
40 0.205 10.3             2
41 0.294  8.6             2
42 0.234  7.8             2
43 0.019 -8.6             2
44 0.200 16.1             2
45 0.187  2.8             2
46 0.227  2.7             2
47 0.227 17.2             2

Sorting on Points Produced, Cam Mack is sixth out of the 53 guards in the Big Ten who land in Cluster 2. Seems advanced metrics take a little bit of the shine off of Cam. But then, so does leaving the program after one suspension-riddled season.