To tackle this challenge, we are provided with two datasets: players historical stats and salaries. Let’s take a look at them.
library(readr)
batting <- read.csv('~/Desktop/R Github/Moneyball/Batting.csv')
head(batting)
## playerID yearID stint teamID lgID G G_batting AB R H X2B X3B HR RBI SB
## 1 aardsda01 2004 1 SFN NL 11 11 0 0 0 0 0 0 0 0
## 2 aardsda01 2006 1 CHN NL 45 43 2 0 0 0 0 0 0 0
## 3 aardsda01 2007 1 CHA AL 25 2 0 0 0 0 0 0 0 0
## 4 aardsda01 2008 1 BOS AL 47 5 1 0 0 0 0 0 0 0
## 5 aardsda01 2009 1 SEA AL 73 3 0 0 0 0 0 0 0 0
## 6 aardsda01 2010 1 SEA AL 53 4 0 0 0 0 0 0 0 0
## CS BB SO IBB HBP SH SF GIDP G_old
## 1 0 0 0 0 0 0 0 0 11
## 2 0 0 0 0 0 1 0 0 45
## 3 0 0 0 0 0 0 0 0 2
## 4 0 0 1 0 0 0 0 0 5
## 5 0 0 0 0 0 0 0 0 NA
## 6 0 0 0 0 0 0 0 0 NA
salaries <- read.csv('~/Desktop/R Github/Moneyball/Salaries.csv')
head(salaries)
## yearID teamID lgID playerID salary
## 1 1985 BAL AL murraed02 1472819
## 2 1985 BAL AL lynnfr01 1090000
## 3 1985 BAL AL ripkeca01 800000
## 4 1985 BAL AL lacyle01 725000
## 5 1985 BAL AL flanami01 641667
## 6 1985 BAL AL boddimi01 625000
There are plenty of metrics that help us analysing players’ performance but we will be focusing on a few of them only, whicih we still need to calculate:
# Batting Average
batting$BA <- batting$H / batting$AB
# On Base Percentage
batting$OBP <- (batting$H + batting$BB + batting$HBP)/(batting$AB + batting$BB + batting$HBP + batting$SF)
# Creating X1B (Singles) for Slugging Percentage calculation
batting$X1B <- batting$H - batting$X2B - batting$X3B - batting$HR
# Slugging Average (SLG)
batting$SLG <- ((1 * batting$X1B) + (2 * batting$X2B) + (3 * batting$X3B) + (4 * batting$HR) ) / batting$AB
In 2001, we lost Giambja01, Damonjo01 and Saenzol01, who were at the peak of their performance.
library(ggplot2)
library(ggExtra)
GDS <- subset(batting,playerID %in% c('giambja01','damonjo01','saenzol01'))
ggplot(data=GDS[GDS$yearID<2001,], aes(x=yearID, y=BA, group=1)) +
geom_line(stat="identity", color="green4") + xlab("Year") + facet_grid(~playerID) + theme_bw() + removeGrid() + ggtitle("Batting Average (1994-2001)")
ggplot(data=GDS[GDS$yearID<2001,], aes(x=yearID, y=OBP, group=1)) +
geom_line(stat="identity", color="green4") + xlab("Year") + facet_grid(~playerID) + theme_bw() + removeGrid() + ggtitle("On Base Percentage (1994-2001)")
ggplot(data=GDS[GDS$yearID<2001,], aes(x=yearID, y=SLG, group=1)) +
geom_line(stat="identity", color="green4") + xlab("Year") + facet_grid(~playerID) + theme_bw() + removeGrid() + ggtitle("Slugging Percentage (1994-2001)")
As salary is a key condition in our quest for potential players, is useful to merge both datasets.
data <- merge(batting,salaries,by=c('playerID','yearID'))
Although we have data since 1985, we only care about the year of 2001.
data <- subset(data, data$yearID==2001)
How are these players performing?
data$lost_players <- ifelse(data$playerID == "giambja01" | data$playerID == "damonjo01" | data$playerID == "saenzol01", "Lost_Players", "Available_Players")
mycolors <- c("Lost_Players" = "green4", "Available_Players" = "grey87")
ggplot(data = data, aes(x = OBP, y = salary)) +
geom_point(size = 1, aes(colour = lost_players)) +
scale_color_manual("Status", values = mycolors) + theme_minimal()
In green we can see our lost players and in grey the ones we have available on the market. Let’s see how the players we lost performed that season
lost_players <- subset(data,playerID %in% c('giambja01','damonjo01','saenzol01'))
lost_players <- lost_players[,c('playerID','H','X2B','X3B','HR','OBP','SLG','BA','AB')]
head(lost_players)
## playerID H X2B X3B HR OBP SLG BA AB
## 5141 damonjo01 165 34 4 9 0.3235294 0.3633540 0.2562112 644
## 7878 giambja01 178 47 2 38 0.4769001 0.6596154 0.3423077 520
## 20114 saenzol01 67 21 1 9 0.2911765 0.3836066 0.2196721 305
We have a lot of players who have 0% of OBP. It might mean that they haven’t had the chance to show their skills as they haven’t played yet. The same rational for 100% OBP, meaning they might have played once and were lucky enough to perform too well.
Now we have to find replacement players for the key three players we lost! However, you have three constraints:
So, am going to highlight potential players who we can consider:
avail.players <- subset(data, !(playerID %in% c('giambja01','damonjo01','saenzol01')))
#OBP vs Salary
ggplot(avail.players,aes(x=OBP,y=salary)) + geom_point() + geom_rect(data=avail.players, mapping=aes(xmin=0.367, xmax=0.5, ymin=0, ymax=8000000), color="red", alpha=0) + theme_minimal() + ggtitle("Players OBP vs Salary (2001)")
#Aggregate AB should be above 1469, so let's set individual AB at around 500.
ggplot(avail.players,aes(x=AB,y=salary)) + geom_point() + ggtitle("Players AB vs Salary (2001)") + geom_rect(data=avail.players, mapping=aes(xmin=500, xmax=700, ymin=0, ymax=8000000), color="red", alpha=0) + theme_minimal()
By filtering these players and sorting them, we can make a decision who to sign in next:
library(dplyr)
potential_players <- subset(avail.players, salary<8000000 & OBP>0.367 & AB>500)
potential_players_resume <- potential_players[,c('playerID','OBP','AB','salary','BA','SLG')]
head(arrange(potential_players_resume,desc(OBP),desc(AB),desc(BA),desc(SLG),salary),10)
## playerID OBP AB salary BA SLG
## 1 heltoto01 0.4316547 587 4950000 0.3356048 0.6848382
## 2 berkmla01 0.4302326 577 305000 0.3310225 0.6204506
## 3 gonzalu01 0.4285714 609 4833333 0.3251232 0.6880131
## 4 thomeji01 0.4161491 526 7875000 0.2908745 0.6235741
## 5 alomaro01 0.4146707 575 7750000 0.3356522 0.5408696
## 6 gilesbr02 0.4035608 576 7333333 0.3090278 0.5902778
## 7 pujolal01 0.4029630 590 200000 0.3288136 0.6101695
## 8 olerujo01 0.4011799 572 6700000 0.3024476 0.4720280
## 9 bagweje01 0.3974895 600 6500000 0.2883333 0.5683333
## 10 aloumo01 0.3958692 513 5250000 0.3313840 0.5536062
I would go with Heltoto01, Berkmla01 and Gonzalu01!