Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View bayesball's full-sized avatar

Jim Albert bayesball

View GitHub Profile
@bayesball
bayesball / predicting_hr_glm_gam.R
Created April 6, 2024 15:29
Illustration of two algorithms for predicting home runs from distance and spray angle
# load some packages
library(dplyr)
library(ggplot2)
library(mgcv)
library(janitor)
library(metR)
# have Statcast data through games of April 5, 2024
@bayesball
bayesball / count_two_seasons.qmd
Created March 26, 2024 19:43
Quarto file that collects retrosheet data and compares count rates for two seasons
---
title: "Retrosheet Package - Comparing Count Rates for Two Seasons"
format: html
editor: visual
---
Load packages for this particular run.
```{r}
#| message: FALSE
library(abdwr3edata)
@bayesball
bayesball / app.R
Created November 15, 2023 12:47
Shiny app that uses the mlbplotR package to produce scatterplots with team logo points
# live version of Shiny app
# https://bayesball.shinyapps.io/fg2023_discipline/
library(shiny)
library(mlbplotR)
library(dplyr)
library(ggplot2)
library(readr)
# data work
@bayesball
bayesball / Wheeler_Work.qmd
Created October 23, 2023 14:58
Quarto document showing work for Zack Wheeler 2023 NLCS work
---
title: "Zach Wheeler 2023 NCS"
format: html
editor: visual
---
Load in necessary packages:
```{r}
library(baseballr)
@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.
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
# 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
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
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
---
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
# 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