Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / cliff.lee.R
Created March 25, 2014 13:59
Exploring Cliff Lee's pitches in 2013 season using pitchRx package
##################################################
# collect all pitches of Cliff Lee for 2013 season
# using pitchRx package
###################################################
# create database of 2013 pitches
# library(dplyr)
# my_db <- src_sqlite("pitchRx.2013", create = TRUE)
# library(pitchRx)
@bayesball
bayesball / cliff.lee.3.30.2014.R
Created April 1, 2014 14:49
Graphs locations of all pitches that resulted in balls in play for Cliff Lee on Marh 31, 2014
# Cliff Lee's pitches in first game of 2014 season
# March 31, 2014
# scrape all Gameday data for that day
library(pitchRx)
d <- scrape("2014-03-31", "2014-03-31")
# pick out the Cliff Lee pitch data
library(dplyr)
@bayesball
bayesball / first.pitch.effects.R
Created April 6, 2014 20:35
The First Pitch Effect
##########################################
# the first pitch effect
# preview of Chapter 7 material in ABDWR
##########################################
# load in Retrosheet 2013 play-by-play data
# (have to download Retrosheet data and add run values)
load("pbp2013.Rdata")
@bayesball
bayesball / cubs.attendance.R
Last active August 29, 2015 14:01
Graphs of Cubs and White Sox Median Attendance for Recent Seasons
# Cubs / White Sox attendance study
# assume that Retrosheet game log files
# gl1900.txt, gl1901.txt, ... are
# contained in folder gamelogs
# the files gl1954.txt through gl2013.txt are used in this code
get.data <- function(team, Seasons){
data <- NULL
for (year in Seasons){
filename <- paste("gamelogs/gl",year,".txt", sep="")
@bayesball
bayesball / webinar.june.2014.part1.R
Last active August 29, 2015 14:01
Webinar Part 1: Fitting a Bayesian Robust Regression Model
#----------------------------------------------
# R code for Bayesian Computation Webinar
# Jim Albert - June 12, 2014
# albert@bgsu.edu
#----------------------------------------------
#----------------------------------------------
# PART I: Bayesian Robust Regression Modeling
# Require packages foreign, LearnBayes, coda, ggplot2
#----------------------------------------------
@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)))