Repository navigation
Expand file tree
/
Copy pathPitchClassificationModel.R
More file actions
145 lines (97 loc) · 5.57 KB
/
Copy pathPitchClassificationModel.R
File metadata and controls
145 lines (97 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
library(caret)
library(randomForest)
require(caTools)
library(dplyr)
library(tidyr)
library(e1071)
NCAAData <- read.csv("C:/Users/peter/Downloads/Baseball/NCAA_TM_CLEAN.csv")
NCAAData$TaggedPitchType <- as.factor(NCAAData$TaggedPitchType)
NCAAData$TaggedPitchType[NCAAData$TaggedPitchType== "Changeup"] <- "ChangeUp"
NCAAData <- NCAAData %>%
drop_na(12, c(29:40), 44,45,46) %>%
filter(TaggedPitchType != "Undefined" | TaggedPitchType != "Other")%>%
filter(!grepl('Pitcher', Pitcher))
PitchTypeListTM <- c("Fastball", "Sinker", "Cutter", "Curveball", "Slider", "ChangeUp")
PitchTypeListTMColors <- c("Fastball"="red", "Cutter"="blue", "Sinker"="greenyellow", "Curveball"="orange", "Slider"="deepskyblue", "ChangeUp"="palegreen4")
NCAAData$TaggedPitchType <- factor(NCAAData$TaggedPitchType, levels = PitchTypeListTM )
LeftHandedPitches <- NCAAData %>%
filter(PitcherThrows=="Left")
RightHandedPitches <- NCAAData %>%
filter(PitcherThrows == "Right")
sampleLeft = sample.split(LeftHandedPitches$TaggedPitchType, SplitRatio = .70)
trainLeft = subset(LeftHandedPitches, sampleLeft == TRUE)
testLeft = subset(LeftHandedPitches, sampleLeft == FALSE)
dim(trainLeft)
dim(testLeft)
sampleRight = sample.split(RightHandedPitches$TaggedPitchType, SplitRatio = .70)
trainRight = subset(RightHandedPitches, sampleRight == TRUE)
testRight = subset(RightHandedPitches, sampleRight == FALSE)
dim(trainRight)
dim(testRight)
trainLeft <- droplevels(trainLeft)
rfLeft <- randomForest(data=trainLeft, TaggedPitchType ~ VertRelAngle +
HorzRelAngle + HorzApprAngle + VertApprAngle +
SpinAxis + HorzBreak +
InducedVertBreak + RelSpeed + VertBreak +
Extension + RelSide + RelHeight + SpinRate + BatterSide, ntree= 600, mtry = 4, importance = TRUE)
predLeft = predict(rfLeft, newdata=testLeft[-20])
cmLeft = table(testLeft[,20], predLeft)
trainRight <- droplevels(trainRight)
rfRight <- randomForest(data=trainRight, TaggedPitchType ~ VertRelAngle +
HorzRelAngle + HorzApprAngle + VertApprAngle +
SpinAxis + HorzBreak +
InducedVertBreak + RelSpeed + VertBreak +
Extension + RelSide + RelHeight + SpinRate +BatterSide, importance= TRUE, ntree= 600, mtry = 4)
predRight = predict(rfRight, newdata=testRight[-20])
cmRight = table(testRight[,20], predRight)
testLeft$PredictValues <- predict(rfLeft, newdata=testLeft[-20])
testRight$PredictValues <- predict(rfRight, newdata = testRight[-20])
ggplot(testLeft, aes(x=HorzBreak, y = InducedVertBreak, color = PredictValues)) +
geom_point() +
scale_color_manual(values = PitchTypeListTMColors) +
ggtitle("Prediction Movement Plot")
ggplot(testRight, aes(x=HorzBreak, y = InducedVertBreak, color = PredictValues)) +
geom_point() +
scale_color_manual(values = PitchTypeListTMColors) +
ggtitle("Prediction Movement Plot")
ggplot(trainLeft, aes(x=HorzBreak, y = InducedVertBreak, color = TaggedPitchType)) +
geom_point() +
scale_color_manual(values = PitchTypeListTMColors) +
ggtitle("Prediction Movement Plot")
ggplot(trainRight, aes(x=HorzBreak, y = InducedVertBreak, color = TaggedPitchType)) +
geom_point() +
scale_color_manual(values = PitchTypeListTMColors) +
ggtitle("Prediction Movement Plot")
letMeSe <- testLeft[testLeft$PredictValues != testLeft$TaggedPitchType, ]
letMeSe <- letMeSe[, c(6, 20,29:40, 44,45,46,112)]
IowaLefties <- LeftHandedPitches %>%
filter(HomeTeam == "IOW_HAW")
IowaRighties <- RightHandedPitches %>%
filter(HomeTeam == "IOW_HAW")
sampleLeftIowa = sample.split(IowaLefties$TaggedPitchType, SplitRatio = .70)
trainLeftIowa = subset(IowaLefties, sampleLeftIowa == TRUE)
testLeftIowa = subset(IowaLefties, sampleLeftIowa == FALSE)
sampleRightIowa = sample.split(IowaRighties$TaggedPitchType, SplitRatio = .70)
trainRightIowa = subset(IowaRighties, sampleRightIowa == TRUE)
testRightIowa = subset(IowaRighties, sampleRightIowa == FALSE)
trainLeftIowa <- droplevels(trainLeftIowa)
rfLeftIowa <- randomForest(data=trainLeftIowa, TaggedPitchType ~ VertRelAngle +
HorzRelAngle + HorzApprAngle + VertApprAngle +
SpinAxis + HorzBreak +
InducedVertBreak + RelSpeed + VertBreak +
Extension + RelSide + RelHeight + SpinRate + BatterSide, ntree= 600, mtry = 4, importance = TRUE)
predLeftIowa = predict(rfLeftIowa, newdata=testLeftIowa[-20])
cmLeftIowa = table(testLeftIowa[,20], predLeftIowa)
trainRightIowa <- droplevels(trainRightIowa)
rfRightIowa <- randomForest(data=trainRightIowa, TaggedPitchType ~ VertRelAngle +
HorzRelAngle + HorzApprAngle + VertApprAngle +
SpinAxis + HorzBreak +
InducedVertBreak + RelSpeed + VertBreak +
Extension + RelSide + RelHeight + SpinRate +BatterSide, importance= TRUE, ntree= 600, mtry = 4)
predRightIowa = predict(rfRightIowa, newdata=testRightIowa[-20])
cmRightIowa = table(testRightIowa[,20], predRightIowa)
testLeftIowa$PredictValues <- predict(rfLeftIowa, newdata=testLeftIowa[-20])
testRightIowa$PredictValues <- predict(rfRightIowa, newdata = testRightIowa[-20])
letMeSeIowa <- testLeftIowa[testLeftIowa$PredictValues != testLeftIowa$TaggedPitchType, ]
letMeSeIowa <- letMeSeIowa[, c(6, 20,29:40, 44,45,46,112)]
##Look at whether or not you can tag paired data together