Skip to content

Instantly share code, notes, and snippets.

Avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / player_function_lb.R
Last active May 20, 2023 18:28
R function to fit a multilevel quadratic smoothing model to season-to-season AVG data for any player of interest.
View player_function_lb.R
player_function_lb <- function(player_id){
# uses LearnBayes package to simulate from
# multilevel model
library(dplyr)
library(ggplot2)
library(Lahman)
library(LearnBayes)
@bayesball
bayesball / blog_code_example.R
Created April 23, 2023 22:18
R work for Cluster Luck Scoring in 2022 Season
View blog_code_example.R
# read in two functions
source("generate_runs.R")
source("retro_game_work.R")
library(dplyr)
# Phillies game plays on April 16, 2023
plays <- c("HR", "1B", "BB", "BB", "1B", "OUT", "1B",
@bayesball
bayesball / blog_code.R
Created April 12, 2023 12:51
R code to fit several models to study temperature effect on home run hitting
View blog_code.R
library(readr)
library(dplyr)
library(brms)
# read in data from Github site
all_data <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/ten_seasons_retro_hr.csv")
# remove all parks that are either dome or retractable
@bayesball
bayesball / plot_bivar_norm.R
Created April 6, 2023 11:24
R function to plot contours of a bivariate normal distribution
View plot_bivar_norm.R
plot_bivar_norm <- function(mx, my, sx, sy, r){
require(ggplot2)
logbinorm <- function (x, y, par) {
m <- par$m
v <- par$v
zx <- (x - m[1]) / sqrt(v[1, 1])
zy <- (y - m[2]) / sqrt(v[2, 2])
r <- v[1, 2] / sqrt(v[1, 1] * v[2, 2])
@bayesball
bayesball / ChatGPT_examples.qmd
Created April 3, 2023 13:59
Some R functions produced by the ChatGPT program
View ChatGPT_examples.qmd
---
title: "ChatGPT Examples"
format: html
editor: visual
---
## Collecting Cards
_Write R function to illustrate baseball card collection problem._
@bayesball
bayesball / baseballr_examples.R
Created March 7, 2023 16:51
Examples of data acquisition functions using the baseballr package
View baseballr_examples.R
# reference
# https://billpetti.github.io/baseballr/articles/baseballr.html#follow-the-sportsdataverse-on-twitter-and-star-this-repo
library(baseballr)
# -----------------------------------------------------
# Retrosheet data
# -----------------------------------------------------
# acquire retro play-by-play data for seasons 2020:2022
@bayesball
bayesball / two_astros.R
Created February 4, 2023 21:41
R script for comparing two Astros pitchers post
View two_astros.R
# load in required packages
library(dplyr)
library(ggplot2)
library(CalledStrike)
library(janitor)
library(ShinyBaseball)
library(readr)
# collect mlb ids from two pitchers
@bayesball
bayesball / RunMe.R
Created November 13, 2022 22:58
R code to fit nonnested multilevel model to compare the roles of offense and defense in baseball run scoring
View RunMe.R
# load in required packages
library(purrr)
library(ggplot2)
library(tidyr)
library(dplyr)
# read in modeling functions
source("fit_model.R")
@bayesball
bayesball / app.R
Last active November 8, 2022 21:08
Shiny app to compare career trajectories of HOF candidates with contemporary players already in the HOF.
View app.R
library(dplyr)
library(ggplot2)
library(geomtextpath)
library(readr)
# datasets are read from a Github respository
fg_batting <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/fgbatting_complete.csv")
hof <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/hofdata.csv")
hof_candidates <- read_csv("https://raw.githubusercontent.com/bayesball/HomeRuns2021/main/hofdata_candidates.csv")
hof_cand_batting <- filter(hof_candidates,
@bayesball
bayesball / estimate_batting.R
Last active November 5, 2022 19:43
R work for multinomial post -- main file is multinomial_setup.R
View estimate_batting.R
estimate_batting <- function(retro_final_PA_1990_2020d,
season,
s_woba = 0.5){
require(dplyr)
require(LearnBayes)
retro_final_PA_1990_2020d %>%
filter(YEAR == season) -> retroseason
retroseason %>%
group_by(BAT_ID) %>%
summarize(PA = n(),