Skip to content

Instantly share code, notes, and snippets.

@bayesball
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayesball/9389168 to your computer and use it in GitHub Desktop.
Save bayesball/9389168 to your computer and use it in GitHub Desktop.
Ryan Howard Runs Value Platoon Splits Work
#########################################################
# Ryan Howard platoon advantage R work
#########################################################
# download Ryan.csv from https://docs.google.com/spreadsheets/d/1KRwk03LT4EaRA14oeEJlTE0rqoL88j3B11r1TB36O2U/edit?usp=sharing
# save as a csv file "Ryan.csv" in current working directory
Ryan <- read.csv("Ryan.csv")
library(dplyr)
# finds mean run value and PA for each season and pitching hand
Runs.Season.Pitcher <- summarize(group_by(Ryan, Season, PIT_HAND_CD),
Mean=mean(RUNS.VALUE),
PA=length(RUNS.VALUE))
# finds mean run value and PA for each season
Runs.Season <- summarize(group_by(Ryan, Season),
Mean=mean(RUNS.VALUE),
PA=length(RUNS.VALUE))
Runs.Season$PIT_HAND_CD <- "Overall"
# graphs the left, right, and overall mean runs value against
# season
library(ggplot2)
print(ggplot(rbind(Runs.Season.Pitcher, Runs.Season),
aes(Season, Mean, color=PIT_HAND_CD)) +
geom_point(size=2) + geom_line(size=2) +
labs(title="Ryan Howard's Mean Run Values") +
theme(plot.title = element_text(size = rel(2))) +
theme(axis.text = element_text(size = rel(2))) +
theme(axis.title = element_text(size = rel(2))) +
theme(legend.text = element_text(size = rel(1.5))) +
theme(legend.title = element_text(size = rel(1.5)))
)
# merges the left and right split data horizontally
Merged.Runs <- merge(subset(Runs.Season.Pitcher, PIT_HAND_CD=="R"),
subset(Runs.Season.Pitcher, PIT_HAND_CD=="L"),
by="Season")
plot(ggplot(Merged.Runs, aes(Season, Mean.x - Mean.y)) +
geom_point(size=4) + geom_line(size=2) +
ylab("Platoon Advantage") +
labs(title="Ryan Howard's Platoon Advanatage") +
theme(plot.title = element_text(size = rel(2))) +
theme(axis.text = element_text(size = rel(2))) +
theme(axis.title = element_text(size = rel(2))) )
# computes the fraction of PA against right-handed pitchers for
# each season
Merged.Runs$Fraction.Right <- with(Merged.Runs,
round(PA.x / (PA.x + PA.y), 2))
print(Merged.Runs[, c("Season", "PA.x", "PA.y", "Fraction.Right")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment