Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / webinar.june.2014.part2.R
Last active August 29, 2015 14:01
Webinar Part 2: Posterior Predictive Model Checking
#----------------------------------------------
# R code for Bayesian Computation Webinar
# Jim Albert - June 12, 2014
# albert@bgsu.edu
#----------------------------------------------
#----------------------------------------------
# PART II: Posterior-Predictive Model Checking
# Require packages pscl, arm, MASS
#----------------------------------------------
@bayesball
bayesball / webinar.june.2014.part3.R
Last active August 29, 2015 14:01
Webinar Part 3: Introduction to JAGS
#----------------------------------------------
# R code for Bayesian Computation Webinar
# Jim Albert - June 12, 2014
# albert@bgsu.edu
#----------------------------------------------
#---------------------------------------------------------------------
# PART III: INTRODUCTION TO JAGS
# Negative binomial regression
# Example from Jackman, 2009
@bayesball
bayesball / webinar.june.2014.part4.R
Last active August 29, 2015 14:01
Webinar Part 4: Multilevel Modeling
#----------------------------------------------
# R code for Bayesian Computation Webinar
# Jim Albert - June 12, 2014
# albert@bgsu.edu
#----------------------------------------------
##################################################
# PART IV: JAGS for Multilevel Modeling
# Require R packages Lahman, rjags, ggplot2
##################################################
@bayesball
bayesball / TeamStreaks.R
Created June 28, 2014 16:49
R code to find all team streaks in MLB baseball
# read in variable names for game log dataset
Headers <- c("Date", "DoubleHeader",
"DayOfWeek", "VisitingTeam",
"VisitingTeamLeague", "VisitingTeamGameNumber",
"HomeTeam", "HomeTeamLeague" ,
"HomeTeamGameNumber", "VisitorRunsScored" ,
"HomeRunsScore", "LengthInOuts",
"DayNight", "CompletionInfo",
"ForfeitInfo", "ProtestInfo" ,
@bayesball
bayesball / adjustedrange.R
Created August 31, 2014 02:02
Adjusted Range Factors for MLB 2nd Basemen
library(Lahman)
library(dplyr)
fielding.2b <- filter(Fielding, POS=="2B", yearID >= 1960)
rf.season.2b <- summarize(group_by(fielding.2b, yearID),
RF.9 = 9 * (sum(PO, na.rm=TRUE) +
sum(A, na.rm=TRUE)) /
(sum(InnOuts / 3, na.rm=TRUE)))
@bayesball
bayesball / mathplacement.R
Last active August 29, 2015 14:08
Math Placement data from a 35-question exam adminstered to 500 students
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,
@bayesball
bayesball / plot.batting.average.R
Created January 3, 2015 14:10
R function to plot the components of a batting average
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)
@bayesball
bayesball / graph.game.R
Created January 23, 2015 20:03
R function to plot win probabilities for a specific baseball game
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") +
@bayesball
bayesball / helpers.R
Last active August 29, 2015 14:21
Shiny application to plot a trajectory of a home run hitter
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)
}
@bayesball
bayesball / Platoon.R
Created June 4, 2015 18:47
R code to construct graphs of handedness and platoon advantage
# 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)