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
# load relevant packages | |
library(dplyr) | |
library(ggplot2) | |
# function will download retrosheet game log data for a particular | |
# season | |
load.gamelog <- function(season, headers){ | |
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
# loads in the Retrosheet data | |
load("~/OneDriveBusiness/Retrosheet/pbp.2014.Rdata") | |
# removes all non-pitches from PITCH_SEQ_TX | |
pbp.14$pseq <- gsub("[.>123N+*]", "", pbp.14$PITCH_SEQ_TX) | |
# create a b and s sequence | |
pbp.14$pseq <-gsub("[BIPV]", "b", pbp.14$pseq) | |
pbp.14$pseq <-gsub("[CFKLMOQRST]", "s", pbp.14$pseq) |
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
groundball.plot <- function(pbp, season){ | |
require(dplyr) | |
require(ggplot2) | |
require(car) | |
inplay <- filter(pbp, BATTEDBALL_CD == "F" | | |
BATTEDBALL_CD == "G" | | |
BATTEDBALL_CD == "L" | | |
BATTEDBALL_CD == "P") | |
inplay <- mutate(inplay, |
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
# Script to Learn About Ryan Howard's Batting Ability from a "0 for 35" Slump | |
# Uses a function from the BayesTestStreak package | |
# install_github("bayesball/BayesTestStreak") | |
library(MASS) | |
library(BayesTestStreak) | |
# Simulate 500 at-bats with a constant hitting probability p = 0.250 |
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
# Daniel Murphy Exercise | |
# Part I -- learning about Murphy's home run ability | |
# and updating this knowledge after the NLDS and NLCS | |
library(ggplot2) | |
library(LearnBayes) | |
# career home run data for Murphy |
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
# requires packages | |
# dplyr, Lahman, ggplot2 | |
# some preliminary work | |
library(dplyr) | |
library(Lahman) | |
get.birthyear <- function(player.id){ |
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(pitchRx) | |
library(dplyr) | |
library(ggplot2) | |
dat <- scrape(start = "2015-09-05", end = "2015-09-11") | |
pitches <- inner_join(select(dat$atbat, | |
batter_name, pitcher_name, inning, | |
gameday_link, num, url), | |
select(dat$pitch, | |
start_speed, pitch_type, sv_id, num, url), |