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(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) |
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 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) |
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(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({ |
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(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") |
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
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( |
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
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 |
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
####################################################### | |
# 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") |
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
# 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), |
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
# 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 |
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
######################################################### | |
# 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) |
OlderNewer