setwd('~/mlb') require("lme4") yr <- 2004 facs <- c("cnt", "stands", "umpire", "pitcher", "catcher", "batter", "throws", "home_batting") model.file <- sprintf("csaa.%d.model.output.R", yr) ifile <- sprintf("csaaFiles/csaa.in.%d.csv", yr) din <- read.csv(ifile, stringsAsFactors=TRUE) for ( fac in facs) { din[fac] <- as.factor(din[[fac]]) } summary(din) value.csaa.full <- glmer (cs ~ throws*stands + home_batting + cnt + (1| catcher) + (1|pitcher) + (1|umpire) + (1|batter), din, family=binomial(link = 'probit'), nAGQ = 0) value.csaa.0 <- glmer(cs ~ (1|catcher), din, family=binomial(link = 'probit'), nAGQ = 0) model <- value.csaa.full rr <- ranef(model) rrc <- row.names(rr$catcher) model.predictions <- pnorm(predict(model)) df <- data.frame(cbind(row.names(rr$catcher), rr$catcher, 1:length(rr$catcher))) names(df) <- c("catcher", "value", "csaa") for ( cname in rrc ) { cc <- din$catcher == cname model.predictions.with <- sum(pnorm(model.predictions[cc])) model.predictions.without <- sum(pnorm(model.predictions[cc] - df[df$catcher==cname,]$value)) num.pa <- sum(cc) model.predictions.with model.predictions.without wowy <- (model.predictions.with - model.predictions.without) df[df$catcher==cname,]$csaa <- wowy/num.pa } df.csaa.full <- df model <- value.csaa.0 rr <- ranef(model) rrc <- row.names(rr$catcher) model.predictions <- pnorm(predict(model)) df <- data.frame(cbind(row.names(rr$catcher), rr$catcher, 1:length(rr$catcher))) names(df) <- c("catcher", "value", "csaa") for ( cname in rrc ) { cc <- din$catcher == cname model.predictions.with <- sum(pnorm(model.predictions[cc])) model.predictions.without <- sum(pnorm(model.predictions[cc] - df[df$catcher==cname,]$value)) num.pa <- sum(cc) model.predictions.with model.predictions.without wowy <- (model.predictions.with - model.predictions.without) df[df$catcher==cname,]$csaa <- wowy/num.pa } df.csaa.0 <- df save(value.csaa.full, value.csaa.0, df.csaa.full, df.csaa.0, file=sprintf("csaa.%d.model.output.R", yr)) mean(model.predictions)