-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdisc analysis.Rmd
More file actions
188 lines (152 loc) · 5.57 KB
/
pdisc analysis.Rmd
File metadata and controls
188 lines (152 loc) · 5.57 KB
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
---
title: "bootcamp final project"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
warning = FALSE
```
```{r packages}
#install.packages("dplyr")
#install.packages("tidyverse")
#install.packages("pracma")
library(dplyr)
library(tidyverse)
library(baseballr)
library(ggplot2)
library(kableExtra)
library(pracma)
```
```{r data reading}
# read in data
setwd("~/carolinabb1")
pdisc <- read.csv(file = "platedisc.csv", header = TRUE, sep = ",")
names(pdisc)[1] <- "Name"
pdiscavg <- read.csv(file = "pdiscavg.csv", header = TRUE, sep = ",")
full <- read.csv(file = "clutch leaderboard.csv", header = TRUE, sep =",")
names(full)[1]<-"Name"
full <- select(full, Name, Team, Clutch, playerid, WPA)
```
```{r percents formatting}
# format percentages as decimals
for (i in 3:11){
pdisc[,i] = as.numeric(gsub("[\\%,]", "", pdisc[,i]))
pdisc[,i] = pdisc[,i]*0.01
}
for (i in 2:10){
pdiscavg[,i] = as.numeric(gsub("[\\%,]", "", pdiscavg[,i]))
pdiscavg[,i] = pdiscavg[,i]*0.01
}
```
```{r standard dev. calc}
pdisctemp <- c()
for (i in 3:11){
sdtemp <- sd(pdisc[,i])
pdisctemp <- c(pdisctemp,sdtemp)
}
# move values into a csv file
pdiscsd <- read.csv(file = "pdiscsd.csv", header = TRUE, sep = ",")
```
```{r disc rating}
# numbers subbed in from the pdiscavg dataset
pdisc$dRating <- ((pdisc$O.Swing.-.316)/.0595) + ((pdisc$Z.Swing.-.685)/0.0584) + ((pdisc$Swing.- .470)/0.0496) - ((pdisc$O.Contact.-.627)/0.08) - ((pdisc$Z.Contact.-.849)/0.0443) - ((pdisc$Contact.-.762)/0.0529) - ((pdisc$Zone.-.418)/0.0202)+ ((pdisc$F.Strike.-.609)/0.0338)+ ((pdisc$SwStr.-.112)/0.0289)
```
```{r creation of dataset}
# creation of dataset with disc ratings
pdiscRatings <- select(pdisc, Name, Team, dRating, playerid)
arrange(pdiscRatings,desc(dRating))
# matchup with clutch data from full dataset
tempclutch <- c()
tempWPA <- c()
for(i in 1:135){
tempID <- pdiscRatings[i,4]
for (j in 1:135){
if (full[j,4] == tempID){
tempclutch <- c(tempclutch,full[j,3])
tempWPA <- c(tempWPA, full[j,5])
} else {
next
}
}
}
# creating new pretty dataset with disc ratings and clutch values
pdiscRatings$clutch <- tempclutch
pdiscRatings$WPA <- tempWPA
```
```{r average addition}
# creation of dataset with disc ratings
avg <- read.csv(file = "averageData.csv", header = TRUE, sep = ",")
# matchup with average data from secondary dataset
tempBA <- c()
for(i in 1:135){
tempID <- pdiscRatings[i,4]
for (j in 1:135){
if (avg[j,23] == tempID){
tempBA <- c(tempBA,avg[j,22])
} else {
next
}
}
}
pdiscRatings$BA <- tempBA
```
```{r BA vs. Disc}
options(scipen = 999)
data("pdiscRatings", package = "ggplot2")
theme_set(theme_bw())
theme_update(plot.title = element_text(hjust = 0.5))
ggplot(data = pdiscRatings, mapping = aes(x = dRating, y= BA)) + geom_point() + geom_smooth(method = "loess", se = F) + labs(title = "Disc Rating vs. BA of Individual Players", x = "Disc Rating", y = "BA")
powerMod <- lm(BA~dRating, data = pdiscRatings)
summary(powerMod)
```
```{r comparison between drating & clutch}
options(scipen = 999)
data("pdiscRatings", package = "ggplot2")
theme_set(theme_bw())
theme_update(plot.title = element_text(hjust = 0.5))
ggplot(data = pdiscRatings, mapping = aes(x = dRating, y= clutch)) + geom_point() + geom_smooth(method = "loess", se = F) + labs(title = "Disc Rating vs. Clutch of Individual Players", x = "Disc Rating", y = "Clutch")
powerMod <- lm(clutch~dRating, data = pdiscRatings)
summary(powerMod)
```
```{r comparison of WPA vs. Disc Rating}
options(scipen = 999)
data("pdiscRatings", package = "ggplot2")
theme_set(theme_bw())
theme_update(plot.title = element_text(hjust = 0.5))
ggplot(data = pdiscRatings, mapping = aes(y = WPA, x= dRating)) + geom_point() + geom_smooth(method = "loess", se = F) + labs(title = "Disc Rating vs. WPA of Individual Players", x = "Disc Rating", y = "WPA")
powerMod <- lm(WPA~dRating, data = pdiscRatings)
summary(powerMod)
```
```{r comparison of WPA vs. Clutch}
options(scipen = 999)
data("pdiscRatings", package = "ggplot2")
theme_set(theme_bw())
ggplot(data = pdiscRatings, mapping = aes(x = clutch, y= WPA)) + geom_point() + geom_smooth(method = "loess", se = F) + labs(title = "Disc Rating vs. Clutch of Individual Players", y = "WPA", x = "Clutch")
powerMod <- lm(WPA~clutch, data = pdiscRatings)
summary(powerMod)
```
```{r}
teamData <- read.csv(file = "teamData.csv", header = TRUE, sep = ",")
names(teamData)[1]<-"Name"
updTeam <- select(teamData,Name,W)
combdRatings <- read.csv(file = "combDratings.csv", header = TRUE, sep = ",")
updTeam[,3] <- combdRatings$combined
```
```{r combined disc vs. total wins}
options(scipen = 999)
data("updTeam", package = "ggplot2")
theme_set(theme_bw())
theme_update(plot.title = element_text(hjust = 0.5))
p <- ggplot(data = updTeam, mapping = aes(y = W, x= V3)) + geom_point() + geom_smooth(method = "loess", se = F) + labs(title = "Combined Disc Ratings vs. Wins", x = "Disc Ratings", y = "Wins")
p + theme(
panel.background = element_rect(fill = "lightblue",
colour = "lightblue",
size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white")
)
powerMod <- lm(V3~W, data = updTeam)
summary(powerMod)
```