Stan, solutions to the exercises for some chapters of ABDR are available at https://github.com/beanumber/baseball_R

For my teaching stats using baseball book, just send me an email at albert@bgsu.edu, explaining that you are not a student in a class and I can send you solutions.

Thomas: Here is similar more concise code that seems to work — hope this is helpful.

# load several packages

library(tidyverse)

library(mgcv)

library(lme4)

# read in the 2017 Statcast data

sc_2017 <- read_csv("../data/statcast2017.csv")

# only consider the called pitches

taken <- filter(sc_2017, type != "X")

# model the probability of a strike

strike_mod <- gam(type == "S" ~ s(plate_x, plate_z),

family = binomial,

data = sample_n(taken, size = 100000))

# work with a sample of 10,000 pitches

sample_taken <- sample_n(taken, size = 10000)

# define new variable that predicts the probability

# of a strike

sample_taken <- mutate(sample_taken,

strike_prob = predict(strike_mod,

newdata = sample_taken,

type = "response"))

# fits random effects model using strike_prob as

# covariate and catcher random effects

mod_a <- glmer(type == "S" ~ strike_prob +

(1|pos2_person_id),

data = sample_taken, family = binomial)

mod_a <- glmer(type == "S" ~ strike_prob + (1|fielder_2),

data = sc_taken, family = binomial)

mod_a is being created as . I’m typing the code exactly as it is written in the text so I’m not sure why this would be occurring. Any help on obtaining the proper code would be much appreciated.

]]>Daniel, sorry but I think that’s all we have currently available in terms of solutions. Jim

]]>Casey, if the csv files are in the working directory, then you read them into R by statements like

data <- read.csv("filename.csv") It appears that you were trying to use a source() function that is used to read in R functions. I would try some of the sample scripts from the chapters to get started. Jim

/Users/caseystevens/Desktop/baseball_R-master/baseballdatabank-master/core/People.csv:1:9: unexpected ‘,’

1: playerID,

^

I can open it in Numbers just fine, but when I select a csv file and ‘Open With’ the data looks messy. What can I do to get this fixed?

]]>Patrick. The value 30 was chosen so that the regression coefficients were easier to interpret. Any value could be chosen and it won’t affect the regression coefficients.

]]>