-
Notifications
You must be signed in to change notification settings - Fork 0
/
RScript.R
57 lines (44 loc) · 1.71 KB
/
RScript.R
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
setwd("~/Desktop/Assignment upgrad/Baseball-Playoffs-Prediction/")
library(ggplot2)
library(caret)
data <- read.csv("baseball_train.csv")
class(data)
train <- data[,c("Team","RS","RA","OBP","SLG","BA")]
cls <- data[,c("Team","Playoffs")]
head(train)
head(cls)
unique(data$Playoffs)
ggplot(data, aes(RS,RA, color = Playoffs)) + geom_point()
ggplot(data, aes(RS,OBP, color = Playoffs)) + geom_point()
ggplot(data, aes(RS,SLG, color = Playoffs)) + geom_point()
ggplot(data, aes(RS,BA, color = Playoffs)) + geom_point()
ggplot(data, aes(RA,RS, color = Playoffs)) + geom_point()
ggplot(data, aes(RA,OBP, color = Playoffs)) + geom_point()
ggplot(data, aes(RA,SLG, color = Playoffs)) + geom_point()
ggplot(data, aes(RA,BA, color = Playoffs)) + geom_point()
ggplot(data, aes(RS, RA, BA, color = Playoffs)) + geom_point()
# splitting to training and testing data set for the known samples given
train_size <- floor(0.75 * nrow(data))
set.seed(234)
train_index <- sample(seq_len(nrow(data)), size = train_size)
train <- data[train_index, ]
test <- data[-train_index, ]
#predictM <- lm(Playoffs ~ OBP + SLG + BA, data = train)
predictM <- lm(Playoffs ~ RS + RA + OBP + SLG + BA, data = train)
summary(predictM)
prediction <- predict(predictM, newdata = test)
head(prediction)
head(test$Playoffs)
mean(prediction)
prediction[prediction < mean(prediction)] <- round(0)
prediction[prediction > mean(prediction)] <- round(1)
prediction[prediction < mean(prediction)] <- round(0)
prediction[prediction > mean(prediction)] <- round(1)
head(prediction)
SSE <- sum((test$Playoffs - prediction)^2)
SST <- sum((test$Playoffs - mean(test$Playoffs))^2)
1 - SSE/SST
result <- confusionMatrix(prediction, test$Playoffs)
result
unique(prediction)
length(prediction)