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
#---------------------------------------------- | |
# 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 | |
#---------------------------------------------- |
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
#---------------------------------------------- | |
# 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 |
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
#---------------------------------------------- | |
# 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 | |
################################################## |
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
# 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" , |
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
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))) |
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.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
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) |