Created
January 23, 2015 20:03
-
-
Save bayesball/a8e5255c98954d151974 to your computer and use it in GitHub Desktop.
R function to plot win probabilities for a specific baseball game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
graph.game <- function(d, game.id){ | |
require(ggplot2) | |
d1 <- subset(d, substr(HALF.INNING, 1, 12) == game.id) | |
d1$Play <- 1:dim(d1)[1] | |
yr <- substr(d1[1, "HALF.INNING"], 4, 7) | |
mo <- substr(d1[1, "HALF.INNING"], 8, 9) | |
day <- substr(d1[1, "HALF.INNING"], 10, 11) | |
print(ggplot(d1, aes(Play, P.NEW)) + geom_line() + | |
ylim(0, 1) + geom_hline(yintercept=0.5, color="red") + | |
ylab("Probability Home Team Wins") + | |
labs(title = paste(d1[1, "AWAY_TEAM_ID"], "AT", | |
substr(d1[1, "HALF.INNING"], 1, 3), | |
"--", mo, "/", day, "/", yr ))) | |
d1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment