Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / server.R
Last active August 29, 2015 13:56
Shiny application to graph batting trajectories of baseball sluggers
library(shiny)
library(Lahman)
library(ggplot2)
# preliminary work
Master$birthyear <- with(Master,
ifelse(birthMonth >= 7, birthYear + 1, birthYear))
Batting <- merge(Batting,
Master[, c("playerID", "nameFirst", "nameLast", "birthyear")],
by="playerID")
@bayesball
bayesball / trajectory.2014.R
Last active August 29, 2015 13:56
R script and function to plot the career trajectory for a MLB baseball hitter
#######################################################
# trajectory.2014.R
# script and function to plot a career trajectory for a batter
# in major league baseball
# requires installation of packages
# Lahman, dplyr, and ggplot2
#
# once this script is sourced, then graph trajectory of
# Alex Rodriquez's home run rates by typing
# plot.trajectory("Alex Rodriguez", "HR")
@bayesball
bayesball / server.R
Last active August 29, 2015 13:56
Shiny application to plot a baseball hitter's career trajectory
# setup work
library(Lahman)
library(dplyr)
# create new data frame Batting.new by
# collapsing over stint variable
Batting.new <- summarise(group_by(Batting, playerID, yearID),
AB = sum(AB),
H = sum(H),
X2B = sum(X2B),
@bayesball
bayesball / ryan_howard.R
Last active August 29, 2015 13:56
Trajectories of Ryan Howard and 10 similar hitters
# Comparing Ryan Howard's with 10 similar players
# Richie Sexson, Cecil Fiedler, Mo Vaughn, Mark McGwire, Norm Cash
# Jay Buhner, Willie Stargel, Jason Giambi, Frank Howard, David Justice
# uses packages Lahman, dplyr, ggplot2
# setup work and function to plot trajectory
# add argument plot=TRUE or FALSE
# plot=FALSE outputs the data frame with the rate data
@bayesball
bayesball / ryan.platoon.R
Last active August 29, 2015 13:57
Ryan Howard Runs Value Platoon Splits Work
#########################################################
# Ryan Howard platoon advantage R work
#########################################################
# download Ryan.csv from https://docs.google.com/spreadsheets/d/1KRwk03LT4EaRA14oeEJlTE0rqoL88j3B11r1TB36O2U/edit?usp=sharing
# save as a csv file "Ryan.csv" in current working directory
Ryan <- read.csv("Ryan.csv")
library(dplyr)
@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
#----------------------------------------------