Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Computes platoon advantage percentage for all hitters using Retrosheet data
platoon_dist2 <- function(pbpdata, season, data=FALSE){
require(dplyr)
require(ggplot2)
require(Lahman)
d_PA <- filter(pbpdata, BAT_EVENT_FL==TRUE)
S_left <- summarize(group_by(filter(d_PA,
BAT_HAND_CD=="L"), BAT_ID),
Opp=sum(PIT_HAND_CD=="R"),
Same=sum(PIT_HAND_CD=="L"))
S_right <- summarize(group_by(filter(d_PA,
BAT_HAND_CD=="R"), BAT_ID),
Opp=sum(PIT_HAND_CD=="L"),
Same=sum(PIT_HAND_CD=="R"))
S <- full_join(S_left, S_right, by="BAT_ID")
names(S)[2:5] <- c("LR", "LL", "RL", "RR")
# replace NA's with 0's
for (j in c(2:5)){
N.na <- sum(is.na(S[, j]))
S[is.na(S[, j]), j] <- rep(0, N.na)
}
# identify as right-handed, left-handed, or both sides hitter
S <- mutate(S,
L = LR + LL, R = RL + RR,
Side=ifelse(L > 0 & R > 0, "B",
ifelse(L > 0, "L", "R")))
S <- mutate(S,
N_opp = LR + RL,
N = LR + LL + RL + RR,
P_opp = 100 * N_opp / N)
SM_L <- summarize(S_left, O=sum(Opp), S=sum(Same))
SM_R <- summarize(S_right, O=sum(Opp), S=sum(Same))
SM_L <- mutate(SM_L, N=O + S, Pct = 100 * O / N)
SM_R <- mutate(SM_R, N=O + S, Pct = 100 * O / N)
TH <- theme(
plot.title = element_text(
colour = "blue",
size = 18,
hjust = 0.5,
vjust = 0.8,
angle = 0
)
)
p <- ggplot(filter(S, N >= 100),
aes(N, P_opp, color=Side)) +
geom_point() + geom_smooth() +
ggtitle(paste(season, "Platoon Advantage vs Number of PA")) +
ylab("Pct Platoon Advantage") +
xlab("Plate Appearances") +
geom_hline(yintercept=SM_L$Pct) +
geom_hline(yintercept=SM_R$Pct) +
annotate("text", x = 650, y = SM_L$Pct + 5,
label = "Batting Left") +
annotate("text", x = 650, y = SM_R$Pct + 5,
label = "Batting Right") +TH
if(data==TRUE) S else p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.