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
datamatrix <- matrix(c( | |
1,1,1,0,1,1,1,1,1,0,1,1,0,1,0,1,0,1,1,1,0,1,1,1,1,0,1,1,0,1,1,1,1,0,1, | |
1,0,1,1,1,1,0,1,0,1,1,0,1,1,0,0,0,1,1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,0,0, | |
1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0,0,0, | |
0,1,1,0,1,1,1,1,0,0,1,1,0,1,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0,1,1,0,0,0,0, | |
1,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,1,1,1,1,0,0,0,1,1,0,1,0,0,0,0,0, | |
1,0,1,0,1,1,0,0,1,1,0,1,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,1,1,0,0,0,0, | |
0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,0,1,0,0,0,1, | |
1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,0,0,0, | |
1,0,1,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0, |
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
plot.prob.home <- function(year, plot=TRUE){ | |
require(arm) | |
require(ggplot2) | |
load.gamelog <- function(season, headers){ | |
download.file( | |
url=paste("http://www.retrosheet.org/gamelogs/gl", season, ".zip" | |
, sep="") | |
, destfile=paste("gl", season, ".zip", sep="") | |
) | |
unzip(paste("gl", season, ".zip", sep="")) |
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
plot.batting.average <- function(name, season){ | |
require(Lahman) | |
require(dplyr) | |
Name <- unlist(strsplit(name, split=" ")) | |
id <- subset(Master, nameFirst==Name[1] & | |
nameLast==Name[2])$playerID[1] | |
data <- subset(Batting, playerID==id & yearID==season) | |
data <- summarize(data, H=sum(H), AB=sum(AB), | |
SO=sum(SO), HR=sum(HR)) | |
so.rate <- with(data, SO / AB) |
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") + |
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
--- | |
title: "WPA work" | |
author: "Jim Albert" | |
date: "January 23, 2015" | |
output: html_document | |
--- | |
First source three functions: | |
* prob.win.game2 computes the win probabilities after each inning |
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
# loading in Retrosheet data for the seasons 1960 through 2013 from my website | |
load(url("http://bayes.bgsu.edu/baseball/pbp.1960.1979.Rdata")) | |
load(url("http://bayes.bgsu.edu/baseball/pbp.1980.1999.Rdata")) | |
load(url("http://bayes.bgsu.edu/baseball/pbp.2000.2013.Rdata")) | |
batter.matchup.ggplot <- function(Name, graph=TRUE, retroid=FALSE){ | |
# this function assumes the data frames pbp.60.79, pbp.80.99, and pbp.00.13 are | |
# in the workspace | |
fit.model <- function(y, n){ | |
require(LearnBayes) |
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
plot.trajectory <- function(player){ | |
d <- subset(sluggerdata, Player==player) | |
p <- ggplot(d, aes(Age, HR/AB)) + | |
geom_point(size=4, color="red") + | |
geom_smooth(size=2, color="blue") + | |
labs(title = player) | |
print(p) | |
} |
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
# Assuming that the retrosheet play-by-play files are available for a particulr | |
# season | |
# Function righthand computes the proportion of right-handed | |
# pitchers and right-handed batters for each inning for a | |
# particular season | |
# inputs are the season and the retrosheet data for that season | |
righthand <- function(season, d){ | |
require(dplyr) |
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
# code for estimating platoon effects for 2014 season | |
# one needs Retrosheet play-by-play for this season | |
# also uses the Master file in the Lahman database to get the player batting sides | |
# last, one needs a file from fangraphs to compute the weights in the wOBA formula | |
library(dplyr) | |
# retrieved wOBA weights from http://www.fangraphs.com/guts.aspx?type=cn | |
# stored in file fangraphs.csv | |
fangraphs <- read.csv("~/Dropbox/2014 WORK/situational/fangraphs.csv") |
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
# load relevant packages | |
library(dplyr) | |
library(ggplot2) | |
# function will download retrosheet game log data for a particular | |
# season | |
load.gamelog <- function(season, headers){ | |
download.file( |