-
Notifications
You must be signed in to change notification settings - Fork 2
/
WNBA Table Manipulation.Rmd
256 lines (182 loc) · 7.72 KB
/
WNBA Table Manipulation.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
---
title: "WNBA Table Manipulation"
output:
html_document: default
pdf_document: default
---
```{r}
# Read in the data and create a base and ridge regressed model
points <- read.csv("C://Users/Brad Hymas/Desktop/School/Senior/Spring 2021/IS 590R/point_diff_2019_updated.csv", header = TRUE)
library(tidyverse)
y <- points$point_diff
x <- points %>% dplyr::select(-point_diff, -game_id, -home_possession) %>% as.matrix()
library(MASS)
ridge.model <- lm.ridge(y~-1+x, lambda = 2000)
model <- lm(y~-1+x)
ridge.coefs <- ridge.model$coef * 100
coefs <- model$coef * 100
# Variance of the coefficients
lm_var <- diag(vcov(model))* 1000
s2 <- 1/(nrow(x) - ncol(x)) * t(y - x %*% matrix(ridge.model$coef)) %*% (y - x %*% matrix(ridge.model$coef))
ridge_var <- solve(t(x) %*% x + 2000* diag(ncol(x))) %*% t(x) %*% x %*% solve(t(x) %*% x + 2000 * diag(ncol(x)))
finalRidgeVar <- as.vector(s2) * diag(ridge_var) * 1000
```
```{r}
# Determining player interactions
test <- as.matrix(points)
transTest<- t(test)
int <- transTest %*% test
finalInt <- int[3:(nrow(int) - 1), 3:(ncol(int) - 1)]
# Create a list of the amount of possessions played by each player
title <- colnames(finalInt)
possessions <- finalInt[1,1]
for (i in 2:nrow(finalInt)) {
possessions <- c(possessions, finalInt[i, i])
}
# Create a data frame for the players and the number of possessions played, sorted decreasing
temp <- data.frame("Player Name" = title, "Possessions Played" = possessions)
index <- order(temp[,2], decreasing = TRUE)
playerPossessions <- temp[index,]
```
```{r}
# Per game stats
player2019 <- read.csv("C://Users/Brad Hymas/Desktop/School/Senior/Spring 2021/IS 590R/Players_2019.csv")
player2019test <- player2019[,c(2, 4:6, 8:length(player2019))]
# Defensive rebounds per player per game
drb <- player2019test[,18] - player2019test[,17]
# Minutes played per game
mppg <- player2019test[,8] / player2019test[,6]
# Field goals made per game
fgpg <- player2019test[,9] / player2019test[,6]
# Field goals attempted per game
fgapg <- player2019test[,10] / player2019test[,6]
# Two-pointers made per game
twoppg <- player2019test[,11] / player2019test[,6]
# Two-pointers attempted per game
twopapg <- player2019test[,12] / player2019test[,6]
# Three-pointers made per game
threeppg <- player2019test[,13] / player2019test[,6]
# Three-pointers attempted per game
threepapg <- player2019test[,14] / player2019test[,6]
# Free throws made per game
ftpg <- player2019test[,15] / player2019test[,6]
# Free throws attempted per game
ftapg <- player2019test[,16] / player2019test[,6]
# Offensive rebounds per game
orbpg <- player2019test[,17] / player2019test[,6]
# Defensive rebounds per game
drbpg <- drb / player2019test[,6]
# Total rebounds per game
trbpg <- player2019test[,18] / player2019test[,6]
# Assists per game
apg <- player2019test[,19] / player2019test[,6]
# Steals per game
spg <- player2019test[,20] / player2019test[,6]
# Blocks per game
bpg <- player2019test[,21] / player2019test[,6]
# Turnovers per game
tpg <- player2019test[,22] / player2019test[,6]
# Personal fouls per game
pfpg <- player2019test[,23] / player2019test[,6]
# Points per game
ppg <- player2019test[,24] / player2019test[,6]
# Possessions per game
pog <- (possessions / player2019test[,6])
```
```{r}
# Per 36 minutes stats
# Defensive rebounds per 36 minutes
drbm <- (drb / player2019test[,8]) * 36
# Field goals made per 36 minutes
fgm <- (player2019test[,9] / player2019test[,8]) * 36
# Field goals attempted per 36 minutes
fgam <- (player2019test[,10] / player2019test[,8]) * 36
# Two-pointers made per 36 minutes
twom <- (player2019test[,11] / player2019test[,8]) * 36
# Two-pointers attempted per 36 minutes
twoam <- (player2019test[,12] / player2019test[,8]) * 36
# Three-pointers made per 36 minutes
threem <- (player2019test[,13] / player2019test[,8]) * 36
# Three-pointers attempted per 36 minutes
threeam <- (player2019test[,14] / player2019test[,8]) * 36
# Free throws made per 36 minutes
ftm <- (player2019test[,15] / player2019test[,8]) * 36
# Free throws attempted per 36 minutes
ftam <- (player2019test[,16] / player2019test[,8]) * 36
# Offensive rebounds per 36 minutes
orbm <- (player2019test[,17] / player2019test[,8]) * 36
# Total rebounds per 36 minutes
trbm <- (player2019test[,18] / player2019test[,8]) * 36
# Assists per 36 minutes
am <- (player2019test[,19] / player2019test[,8]) * 36
# Steals per 36 minutes
sm <- (player2019test[,20] / player2019test[,8]) * 36
# Blocks per 36 minutes
bm <- (player2019test[,21] / player2019test[,8]) * 36
# Turnovers per 36 minutes
tm <- (player2019test[,22] / player2019test[,8]) * 36
# Personal fouls per 36 minutes
pfm <- (player2019test[,23] / player2019test[,8]) * 36
# Points per 36 minutes
pm <- (player2019test[,24] / player2019test[,8]) * 36
# Possessions per 36 minutes
pom <- (possessions / player2019test[,8]) * 36
```
```{r}
# Adding per game data to columns
player2019test['APG'] <- apg
player2019test['BPG'] <- bpg
player2019test['DRB'] <- drb
player2019test['DRBPG'] <- drbpg
player2019test['FGPG'] <- fgpg
player2019test['FGAPG'] <- fgapg
player2019test['FTPG'] <- ftpg
player2019test['FTAPG'] <- ftapg
player2019test['MPG'] <- mppg
player2019test['ORBPG'] <- orbpg
player2019test['PFPG'] <- pfpg
player2019test['PPG'] <- ppg
player2019test['SPG'] <- spg
player2019test['X3PPG'] <- threeppg
player2019test['X3PAPG'] <- threepapg
player2019test['TPG'] <- tpg
player2019test['TRBPG'] <- trbpg
player2019test['X2PPG'] <- twoppg
player2019test['X2PAPG'] <- twopapg
# Adding per 36 minutes data to columns
player2019test['FGPM'] <- fgm
player2019test['FGAPM'] <- fgam
player2019test['X2PPM'] <- twom
player2019test['X2PAPM'] <- twoam
player2019test['X3PPM'] <- threem
player2019test['X3PAPM'] <- threeam
player2019test['FTPM'] <- ftm
player2019test['FTAPM'] <- ftam
player2019test['ORBPM'] <- orbm
player2019test['TRBPM'] <- trbm
player2019test['DRBPM'] <- drbm
player2019test['ASPM'] <- am
player2019test['SPM'] <- sm
player2019test['BPM'] <- bm
player2019test['TPM'] <- tm
player2019test['PFPM'] <- pfm
player2019test['PPM'] <- pm
# Adding player possession data
player2019test['POPG'] <- pog
player2019test['POS'] <- possessions
player2019test['POPM'] <- pom
# Reorganize the columns so the per game columns are next to the corresponding season totals
player2019pg <- player2019test[, c(1:8, 38, 9, 34, 10, 35, 11, 47, 12, 48, 13, 43, 14, 44, 15, 36, 16, 37, 32, 33, 17, 39, 18, 46, 19, 30, 20, 42, 21, 31, 22, 45, 23, 40, 24, 41, 67, 66, 25:29)]
# Reorganize the columns so the per 36 minutes columns are next to the corresponding season totals
player2019pm <- player2019test[, c(1:9, 49, 10, 50, 11, 51, 12, 52, 13, 53, 14, 54, 15, 55, 16, 56, 17, 57, 18, 58, 32, 59, 19, 60, 20, 61, 21, 62, 22, 63, 23, 64, 24, 65, 67, 68, 25:29)]
```
```{r}
# Add the coefficients from the base model and ridge regressed model to the table for both tables
player_2019_info_per_game <- player2019pg %>% arrange(Player) %>% mutate(APM = coefs, RAPM = ridge.coefs)
salaries <- read.csv("C://Users/Brad Hymas/Desktop/School/Senior/Spring 2021/IS 590R/WNBA player salary 20192.csv")
players_2019_per_game <- player_2019_info_per_game %>% left_join(salaries %>% dplyr::select(Player, Salary), by = "Player")
write.csv(players_2019_per_game, 'shiny_data.csv')
player_2019_info_per_minutes <- player2019pm %>% arrange(Player) %>% mutate(APM = coefs, RAPM = ridge.coefs)
players_2019_per_minutes <- player_2019_info_per_minutes %>% left_join(salaries %>% dplyr::select(Player, Salary), by = "Player")
write.csv(players_2019_per_minutes, 'shiny_data.csv')
```