library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
batting <- read.csv("/Users/justincarter/Downloads/Batting.csv")
HOF <-  read.csv("/Users/justincarter/Downloads/HallOfFame.csv")
fielding <- read.csv("/Users/justincarter/Downloads/Fielding.csv")
HOF <-  read.csv("/Users/justincarter/Downloads/HallOfFame.csv")

batting <- batting[1:79248,]
batting <- batting %>% replace(is.na(.), 0)
batting <- batting %>% summarize(G = sum(G), R = sum(R), H = sum(H), XBH = sum(X2B)+sum(X3B),
                                          HR = sum(HR), RBI = sum(RBI), SB = sum(SB), SO = sum(SO),
                                          Ryear = max(yearID), .by = "playerID")

fielding <- filter(fielding, POS != "P")
fielding <- fielding %>% replace(is.na(.), 0)
fielding <- fielding  %>% summarize(PO = sum(PO), A = sum(A), E = sum(E), .by = "playerID" )

position_players <- inner_join(batting, fielding, by = "playerID")
position_players$HOF <- 0

HOF <- filter(HOF, inducted == "Y" & category == "Player")
HOF <- semi_join(HOF, position_players, by = "playerID")
HOF <- HOF  %>% arrange(HOF, playerID)

HOF_players <- semi_join(position_players, HOF, by = "playerID")
HOF_players <- HOF_players  %>% arrange(HOF_players, playerID)
HOF_players$HOF = 1
HOF_players$years_to_induction <- (c(HOF$yearid) - c(HOF_players$Ryear))
non_HOF_Players <- anti_join(position_players, HOF, by = "playerID")
position_players <- full_join(HOF_players, non_HOF_Players)
## Joining with `by = join_by(playerID, G, R, H, XBH, HR, RBI, SB, SO, Ryear, PO,
## A, E, HOF)`
position_players$HOF <- as.factor(position_players$HOF)
HOF <-  read.csv("/Users/justincarter/Downloads/HallOfFame.csv")
pitching <- read.csv("/Users/justincarter/Downloads/Pitching.csv")

pitching <- pitching[1:32898,]
pitching <- pitching  %>% replace(is.na(.), 0)
pitching <- pitching %>% summarize(G = sum(G), W = sum(W), L = sum(L),
                                   WHIP = ((sum(H) + sum(BB)) / (sum(IPouts) * 3)),
                                   ERA = (sum(ER) / (sum(IPouts) / 3)) * 9, 
                                   SO = sum(SO), BB = sum(BB), HR = sum(HR), Ryear = max(yearID),
                                   .by = "playerID")
pitching['ERA'][sapply(pitching['ERA'], is.infinite)] <- 10
pitching['ERA'][sapply(pitching['ERA'], is.nan)] <- 10
pitching['WHIP'][sapply(pitching['WHIP'], is.infinite)] <- 2
pitching['WHIP'][sapply(pitching['WHIP'], is.nan)] <- 2

HOF <- filter(HOF, inducted == "Y" & category == "Player")
HOF <- semi_join(HOF, pitching, by = "playerID")
HOF <- HOF  %>% arrange(HOF, playerID)

HOF_pitchers <- semi_join(pitching, HOF, by = "playerID")
HOF_pitchers <- HOF_pitchers  %>% arrange(HOF_pitchers, playerID)
HOF_pitchers$HOF = 1
HOF_pitchers$years_to_induction <- (c(HOF$yearid) - c(HOF_pitchers$Ryear))
HOF_pitchers <- filter(HOF_pitchers, G>100)
non_HOF_pitchers<- anti_join(pitching, HOF, by = "playerID")
non_HOF_pitchers$HOF = 0
pitchers <- full_join(HOF_pitchers, non_HOF_pitchers)
## Joining with `by = join_by(playerID, G, W, L, WHIP, ERA, SO, BB, HR, Ryear,
## HOF)`
pitchers$HOF <- as.factor(pitchers$HOF)
library(caTools) 
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(caret)
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
## Loading required package: lattice
set.seed(2002)

tuned_1 <- tuneRF(x = position_players[,c(2,3,4,5,6,7,8,9,11,12,13)], y = position_players[,14],
                  ntreeTry = 500, stepFactor = 1.5, improve = 1e-5 )
## mtry = 3  OOB error = 1.22% 
## Searching left ...
## mtry = 2     OOB error = 1.31% 
## -0.08108108 1e-05 
## Searching right ...
## mtry = 4     OOB error = 1.2% 
## 0.009009009 1e-05 
## mtry = 6     OOB error = 1.24% 
## -0.02727273 1e-05

tuned_2 <- tuneRF(x = pitchers[,c(2,3,4,5,6,7,8,9)], y = pitchers[,11],
                  ntreeTry = 500, stepFactor = 1.5, improve = 1e-5 )
## mtry = 2  OOB error = 0.52% 
## Searching left ...
## Searching right ...
## mtry = 3     OOB error = 0.57% 
## -0.08108108 1e-05

tuned_3 <- tuneRF(x = HOF_players[,c(2,3,4,5,6,7,8,9,11,12,13)], y = HOF_players[,15],
                  ntreeTry = 500, stepFactor = 1.5, improve = 1e-5 )
## mtry = 3  OOB error = 260.8404 
## Searching left ...
## mtry = 2     OOB error = 264.7602 
## -0.0150275 1e-05 
## Searching right ...
## mtry = 4     OOB error = 260.3891 
## 0.001730467 1e-05 
## mtry = 6     OOB error = 265.0283 
## -0.01781646 1e-05

tuned_4 <- tuneRF(x = HOF_pitchers[,c(2,3,4,5,6,7,8,9)], y = HOF_pitchers[,12],
                  ntreeTry = 500, stepFactor = 1.5, improve = 1e-5 )
## mtry = 2  OOB error = 292.4192 
## Searching left ...
## Searching right ...
## mtry = 3     OOB error = 294.3751 
## -0.006688694 1e-05

set.seed(2002)

data_split <- sample.split(position_players$G, SplitRatio = 0.75 )

train_1 <- filter(position_players, data_split == TRUE)
test_1 <- filter(position_players, data_split == FALSE)
train_1 <-  as.data.frame(train_1)
test_1 <- as.data.frame(test_1)

RF_pPlayer <- randomForest(x = train_1[,c(2,3,4,5,6,7,8,9,11,12,13)], y = train_1[,14],
                           mtry = 4, ntree = 500)
plot(RF_pPlayer)

RF_pPlayer
## 
## Call:
##  randomForest(x = train_1[, c(2, 3, 4, 5, 6, 7, 8, 9, 11, 12,      13)], y = train_1[, 14], ntree = 500, mtry = 4) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 1.46%
## Confusion matrix:
##      0  1 class.error
## 0 6946 24 0.003443329
## 1   80 89 0.473372781
predictions_1 <- predict(RF_pPlayer, newdata = test_1[,c(2,3,4,5,6,7,8,9,11,12,13)])
tab_1 <- confusionMatrix(data = predictions_1, reference = test_1[,14])
tab_1
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 1984    8
##          1    1    3
##                                           
##                Accuracy : 0.9955          
##                  95% CI : (0.9915, 0.9979)
##     No Information Rate : 0.9945          
##     P-Value [Acc > NIR] : 0.3399          
##                                           
##                   Kappa : 0.3982          
##                                           
##  Mcnemar's Test P-Value : 0.0455          
##                                           
##             Sensitivity : 0.9995          
##             Specificity : 0.2727          
##          Pos Pred Value : 0.9960          
##          Neg Pred Value : 0.7500          
##              Prevalence : 0.9945          
##          Detection Rate : 0.9940          
##    Detection Prevalence : 0.9980          
##       Balanced Accuracy : 0.6361          
##                                           
##        'Positive' Class : 0               
## 
varImpPlot(RF_pPlayer, main = "Importance")

importance(RF_pPlayer)
##     MeanDecreaseGini
## G           26.61221
## R           69.88409
## H           57.97436
## XBH         44.13089
## HR          15.83333
## RBI         36.81185
## SB          12.92611
## SO          17.29758
## PO          17.13721
## A           18.03437
## E           12.31427
set.seed(2002)
data_split_2 <- sample.split(HOF_players$playerID, SplitRatio = 0.75 )

train_2 <- filter(HOF_players, data_split_2 == TRUE)
test_2 <- filter(HOF_players, data_split_2 == FALSE)
train_2 <-  as.data.frame(train_2)
test_2 <- as.data.frame(test_2)

RF_HOF_Pplayers <- randomForest(formula = years_to_induction ~ G + R + H + XBH + HR + RBI + SB
                   + SO + PO + E + A, data = train_2, mtry = 2,  ntree = 500)
plot(RF_HOF_Pplayers)

RF_HOF_Pplayers
## 
## Call:
##  randomForest(formula = years_to_induction ~ G + R + H + XBH +      HR + RBI + SB + SO + PO + E + A, data = train_2, mtry = 2,      ntree = 500) 
##                Type of random forest: regression
##                      Number of trees: 500
## No. of variables tried at each split: 2
## 
##           Mean of squared residuals: 293.1835
##                     % Var explained: 40.17
varImpPlot(RF_HOF_Pplayers, main = "Importance")

importance(RF_HOF_Pplayers)
##     IncNodePurity
## G        6298.900
## R        3352.168
## H        4192.191
## XBH      3659.027
## HR       7640.961
## RBI      4908.941
## SB       3400.154
## SO       7018.880
## PO       5624.812
## E       10487.636
## A        3962.662
set.seed(2002)
data_split_3 <- sample.split(pitchers$playerID, SplitRatio = 0.75 )

train_3 <- filter(pitchers, data_split_3 == TRUE)
test_3<- filter(pitchers, data_split_3 == FALSE)
train_3 <-  as.data.frame(train_3)
test_3 <- as.data.frame(test_3)

RF_pitchers <- randomForest(x = train_3[,c(2,3,4,5,6,7,8,9)], y = train_3[,11],
                            mtry = 4, ntree = 500)
plot(RF_pitchers)

RF_pitchers
## 
## Call:
##  randomForest(x = train_3[, c(2, 3, 4, 5, 6, 7, 8, 9)], y = train_3[,      11], ntree = 500, mtry = 4) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 4
## 
##         OOB estimate of  error rate: 0.55%
## Confusion matrix:
##      0  1 class.error
## 0 5243  9 0.001713633
## 1   20 31 0.392156863
predictions_2 <- predict(RF_pitchers, newdata = test_3[,c(2,3,4,5,6,7,8,9)])
tab_2 <- confusionMatrix(data = predictions_2, reference = test_3[,11])
tab_2
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 1743    5
##          1    5   15
##                                           
##                Accuracy : 0.9943          
##                  95% CI : (0.9896, 0.9973)
##     No Information Rate : 0.9887          
##     P-Value [Acc > NIR] : 0.01048         
##                                           
##                   Kappa : 0.7471          
##                                           
##  Mcnemar's Test P-Value : 1.00000         
##                                           
##             Sensitivity : 0.9971          
##             Specificity : 0.7500          
##          Pos Pred Value : 0.9971          
##          Neg Pred Value : 0.7500          
##              Prevalence : 0.9887          
##          Detection Rate : 0.9859          
##    Detection Prevalence : 0.9887          
##       Balanced Accuracy : 0.8736          
##                                           
##        'Positive' Class : 0               
## 
varImpPlot(RF_pitchers, main = "Importance")

importance(RF_pitchers)
##      MeanDecreaseGini
## G            7.325837
## W           33.527092
## L            7.696084
## WHIP         9.568001
## ERA          8.420895
## SO          20.828467
## BB           7.082479
## HR           5.053687
set.seed(2002)
data_split_4 <- sample.split(HOF_pitchers$playerID, SplitRatio = 0.75 )

train_4 <- filter(HOF_pitchers, data_split_4 == TRUE)
test_4 <- filter(HOF_pitchers, data_split_4 == FALSE)
train_4 <-  as.data.frame(train_4)
test_4 <- as.data.frame(test_4)

RF_HOF_pitchers <- randomForest(formula = years_to_induction ~ G + W + L + WHIP + 
                                  ERA + SO + BB + HR, data = HOF_pitchers, mtry = 2,  ntree = 500)
plot(RF_HOF_pitchers)

RF_HOF_pitchers
## 
## Call:
##  randomForest(formula = years_to_induction ~ G + W + L + WHIP +      ERA + SO + BB + HR, data = HOF_pitchers, mtry = 2, ntree = 500) 
##                Type of random forest: regression
##                      Number of trees: 500
## No. of variables tried at each split: 2
## 
##           Mean of squared residuals: 287.3263
##                     % Var explained: 38
varImpPlot(RF_HOF_pitchers, main = "Importance")

importance(RF_HOF_pitchers)
##      IncNodePurity
## G         2485.978
## W         3449.482
## L         3590.861
## WHIP      2913.879
## ERA       3753.931
## SO        4146.651
## BB        3225.618
## HR        6441.786
log_Pplayers <- glm(HOF ~ G + R + H + XBH + HR + RBI + SB
                   + SO + PO + E + A, data = train_1, family = "binomial")
summary(log_Pplayers)
## 
## Call:
## glm(formula = HOF ~ G + R + H + XBH + HR + RBI + SB + SO + PO + 
##     E + A, family = "binomial", data = train_1)
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -6.285e+00  2.678e-01 -23.471  < 2e-16 ***
## G           -1.526e-03  7.579e-04  -2.014 0.044032 *  
## R            4.425e-03  1.124e-03   3.938 8.20e-05 ***
## H            8.658e-04  9.757e-04   0.887 0.374906    
## XBH         -1.421e-03  2.554e-03  -0.557 0.577800    
## HR           5.559e-04  3.302e-03   0.168 0.866302    
## RBI          3.933e-03  1.277e-03   3.080 0.002069 ** 
## SB          -3.297e-04  1.061e-03  -0.311 0.756023    
## SO          -1.631e-03  5.902e-04  -2.764 0.005712 ** 
## PO          -5.039e-05  2.938e-05  -1.715 0.086382 .  
## E           -3.693e-03  1.105e-03  -3.344 0.000827 ***
## A            3.070e-04  7.842e-05   3.915 9.05e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1599.25  on 7138  degrees of freedom
## Residual deviance:  707.88  on 7127  degrees of freedom
## AIC: 731.88
## 
## Number of Fisher Scoring iterations: 8
predictions_3 <- rep(0, length(test_1[,1]))
probs_1 <- predict(log_Pplayers, newdata = test_1[,c(2,3,4,5,6,7,8,9,11,12,13)], type = "response")
predictions_3[probs_1 > 0.5] <- 1
predictions_3 <- as.factor(predictions_3)
tab_3 <- confusionMatrix(data = predictions_3, reference = test_1[,14])
tab_3
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 1983   10
##          1    2    1
##                                           
##                Accuracy : 0.994           
##                  95% CI : (0.9895, 0.9969)
##     No Information Rate : 0.9945          
##     P-Value [Acc > NIR] : 0.68900         
##                                           
##                   Kappa : 0.1408          
##                                           
##  Mcnemar's Test P-Value : 0.04331         
##                                           
##             Sensitivity : 0.99899         
##             Specificity : 0.09091         
##          Pos Pred Value : 0.99498         
##          Neg Pred Value : 0.33333         
##              Prevalence : 0.99449         
##          Detection Rate : 0.99349         
##    Detection Prevalence : 0.99850         
##       Balanced Accuracy : 0.54495         
##                                           
##        'Positive' Class : 0               
## 
lm_HOF_Pplayers <- lm(years_to_induction ~ G + R + H + XBH + HR + RBI + SB
                   + SO + PO + E + A, data = train_2)
summary(lm_HOF_Pplayers)
## 
## Call:
## lm(formula = years_to_induction ~ G + R + H + XBH + HR + RBI + 
##     SB + SO + PO + E + A, data = train_2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -31.135  -9.486  -1.926   7.496  46.162 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 42.6903821  4.5397511   9.404 3.73e-16 ***
## G           -0.0061555  0.0093548  -0.658  0.51177    
## R            0.0180863  0.0135624   1.334  0.18481    
## H           -0.0113898  0.0114601  -0.994  0.32224    
## XBH         -0.0197346  0.0270371  -0.730  0.46683    
## HR          -0.0651313  0.0337321  -1.931  0.05580 .  
## RBI          0.0126175  0.0134200   0.940  0.34896    
## SB          -0.0096986  0.0113123  -0.857  0.39292    
## SO           0.0023027  0.0066239   0.348  0.72871    
## PO          -0.0005059  0.0004017  -1.260  0.21023    
## E            0.0560952  0.0130605   4.295 3.51e-05 ***
## A           -0.0023236  0.0008421  -2.760  0.00667 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 16.55 on 123 degrees of freedom
## Multiple R-squared:  0.491,  Adjusted R-squared:  0.4455 
## F-statistic: 10.79 on 11 and 123 DF,  p-value: 1.055e-13
log_pitchers <- glm(HOF ~ G + W + L + WHIP + ERA + SO + BB + HR, data = train_3,
                    family = "binomial")
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(log_pitchers)
## 
## Call:
## glm(formula = HOF ~ G + W + L + WHIP + ERA + SO + BB + HR, family = "binomial", 
##     data = train_3)
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -4.724e+00  1.683e+00  -2.806 0.005018 ** 
## G            5.651e-03  1.405e-03   4.023 5.76e-05 ***
## W            4.461e-02  8.168e-03   5.462 4.70e-08 ***
## L           -3.677e-02  1.007e-02  -3.651 0.000261 ***
## WHIP        -1.225e+01  1.945e+01  -0.630 0.528739    
## ERA         -1.050e+00  6.471e-01  -1.623 0.104500    
## SO           1.382e-03  6.255e-04   2.209 0.027186 *  
## BB           8.468e-04  1.031e-03   0.821 0.411388    
## HR          -4.399e-03  3.422e-03  -1.286 0.198553    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 575.22  on 5302  degrees of freedom
## Residual deviance: 156.57  on 5294  degrees of freedom
## AIC: 174.57
## 
## Number of Fisher Scoring iterations: 15
predictions_4 <- rep(0, length(test_3[,1]))
probs_2 <- predict(log_pitchers, newdata = test_3[,c(2,3,4,5,6,7,8,9)])
predictions_4[probs_2 > 0.5] <- 1
predictions_4 <- as.factor(predictions_4)
tab_4 <- confusionMatrix(data = predictions_4, reference = test_3[,11])
tab_4
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 1746    8
##          1    2   12
##                                           
##                Accuracy : 0.9943          
##                  95% CI : (0.9896, 0.9973)
##     No Information Rate : 0.9887          
##     P-Value [Acc > NIR] : 0.01048         
##                                           
##                   Kappa : 0.7031          
##                                           
##  Mcnemar's Test P-Value : 0.11385         
##                                           
##             Sensitivity : 0.9989          
##             Specificity : 0.6000          
##          Pos Pred Value : 0.9954          
##          Neg Pred Value : 0.8571          
##              Prevalence : 0.9887          
##          Detection Rate : 0.9876          
##    Detection Prevalence : 0.9921          
##       Balanced Accuracy : 0.7994          
##                                           
##        'Positive' Class : 0               
## 
lm_HOF_pitchers <- lm(years_to_induction~ G + W + L + WHIP + ERA + SO + BB + HR,
                      data = train_4,)
summary(lm_HOF_pitchers)
## 
## Call:
## lm(formula = years_to_induction ~ G + W + L + WHIP + ERA + SO + 
##     BB + HR, data = train_4)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.450  -7.331  -0.654   6.936  38.181 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  7.269e+01  6.632e+01   1.096 0.279014    
## G           -2.980e-02  1.530e-02  -1.948 0.057850 .  
## W           -9.125e-02  5.867e-02  -1.555 0.127042    
## L            3.142e-01  8.327e-02   3.773 0.000478 ***
## WHIP        -1.064e+02  7.904e+02  -0.135 0.893575    
## ERA         -9.852e+00  1.626e+01  -0.606 0.547792    
## SO          -7.779e-03  6.477e-03  -1.201 0.236172    
## BB           1.012e-02  1.560e-02   0.649 0.519770    
## HR          -8.063e-02  3.547e-02  -2.273 0.027963 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 15.34 on 44 degrees of freedom
## Multiple R-squared:  0.5499, Adjusted R-squared:  0.4681 
## F-statistic:  6.72 on 8 and 44 DF,  p-value: 1.004e-05