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
Created January 5, 2014 22:34
Shiny application to fit a beta curve given the median and 90th percentile.
library(shiny)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
library(LearnBayes)
quantile1 = list(p=.5, x=input$p50)
quantile2 = list(p=.9, x=input$p90)
ab = beta.select(quantile1, quantile2)
@bayesball
bayesball / jan.6.2014.R
Created January 5, 2014 23:47
R script to compute improved estimates of a set of batting rates.
# R Script for Jan 6, 2014 blog
# function shrink will compute improved estimates at true batting rates
# for all players with at least Lower.Bound opportunities
# be sure that packages Lahman, LearnBayes, and plyr have been installed
# before running this function
shrink <- function(year, n.var, d.var, Lower.Bound=100){
# load required packages (each should be installed first)
@bayesball
bayesball / server.R
Last active January 2, 2016 10:59
Shiny application to graph the average number of offensive events (H, HR, SO, etc) per game per team over the history of Major League Baseball.
library(shiny)
library(Lahman)
library(ggplot2)
# Define server logic required to plot various variables against year
shinyServer(function(input, output) {
# Compute the forumla text in a reactive function since it is
# shared by the output$caption and output$mpgPlot functions
formulaText <- reactive({
@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 / parse.retrosheet2.pbp.R
Created February 9, 2014 01:50
R function for downloading, upzipping, and appending Retrosheet play-by-play data
parse.retrosheet2.pbp = function(season){
# ADJUSTED FOR MAC -- function will work for WINDOWS and MAC
# download, unzip, append retrosheet data
# assume current directory has a folder download.folder
# download.folder has two subfolders unzipped and zipped
# program cwevent.exe is in unzipped folder (for windows)
download.retrosheet <- function(season){
# get zip file from retrosheet website
download.file(
@bayesball
bayesball / compute.runs.expectancy.R
Last active May 22, 2020 23:21
R function for computing runs values for all plays in a play-by-play Retrosheet file
compute.runs.expectancy <- function(season){
# changed -- plyr function replaced with dplyr
# (increases speed from 114 to 30 sec for 2013 data)
# assume that files "allseason.csv" and "fields.csv"
# are in current working folder
# for example, if season = 1961, all1961.csv should be
# available
# returns play-by-play matrix with new variables
@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)