yr <- 2004
setwd('~/mlb')
require("dplyr")
require("lme4")
require('ggplot2')
schmidt <- "schmj001"
pedro <- "martp001"
bigunit <- "johnr005"
load(sprintf('draFiles/dra.%d.model.output.R', yr))
din <- read.csv(sprintf('draFiles/dra.in.%d.csv', yr), stringsAsFactors=T)
objects()
summary(din)
inning catcher outs_ct temp Min. : 1.00 kendj001: 5487 Min. :0.000 Min. : 37.00 1st Qu.: 3.00 martv001: 4865 1st Qu.:0.000 1st Qu.: 68.00 Median : 5.00 lopej001: 4848 Median :1.000 Median : 72.00 Mean : 5.02 liebm001: 4789 Mean :0.978 Mean : 72.73 3rd Qu.: 7.00 schnb001: 4782 3rd Qu.:2.000 3rd Qu.: 79.00 Max. :18.00 posaj001: 4757 Max. :2.000 Max. :100.00 (Other) :158991 fraa bat_home_id umpire pitcher Min. :-88.561 Min. :0.0000 westj901: 2923 hernl003: 1053 1st Qu.:-15.904 1st Qu.:0.0000 meric901: 2908 buehm001: 1016 Median : -4.425 Median :0.0000 younl901: 2852 oswar001: 983 Mean : -3.842 Mean :0.4902 buckc901: 2789 johnr005: 964 3rd Qu.: 8.402 3rd Qu.:1.0000 mealj901: 2787 ponss001: 954 Max. :114.087 Max. :1.0000 welkt901: 2771 muldm001: 952 (Other) :171489 (Other) :182597 year_id score_diff woba_mean batter Min. :2004 Min. :-22.00000 Min. :0.33 suzui001: 762 1st Qu.:2004 1st Qu.: -2.00000 1st Qu.:0.33 pierj002: 748 Median :2004 Median : 0.00000 Median :0.33 younm003: 739 Mean :2004 Mean : -0.03723 Mean :0.33 robeb003: 734 3rd Qu.:2004 3rd Qu.: 2.00000 3rd Qu.:0.33 iztuc001: 728 Max. :2004 Max. : 22.00000 Max. :0.33 rollj001: 725 (Other) :184083 game_id stadium woba_pts bats TEX200406240: 153 DEN02 : 6630 Min. :0.0000 L: 80791 PHI200407020: 143 BAL12 : 6464 1st Qu.:0.0000 R:107728 MIN200408080: 142 CHI12 : 6426 Median :0.0000 MIL200404220: 134 SFO03 : 6418 Mean :0.3383 MIN200404060: 129 CLE08 : 6408 3rd Qu.:0.7370 ANA200406130: 127 PHI13 : 6404 Max. :1.9830 (Other) :187691 (Other):149769 role wobascale start_bases_cd Mode :logical Min. :1.18 Min. : 0.00 FALSE:65103 1st Qu.:1.18 1st Qu.: 0.00 TRUE :123416 Median :1.18 Median : 0.00 NA's :0 Mean :1.18 Mean : 32.26 3rd Qu.:1.18 3rd Qu.:100.00 Max. :1.18 Max. :111.00
value.dra.full
Linear mixed model fit by REML ['lmerMod'] Formula: lwts ~ inning * score_diff + start_bases_cd * outs_ct + csaa + temp_log + bats * stadium + role + fraa * bat_home_id + inning * bat_home_id + (1 | batter) + (1 | pitcher) + (1 | catcher) + (1 | umpire) Data: din.dra.csaa REML criterion at convergence: 220536.2 Random effects: Groups Name Std.Dev. batter (Intercept) 0.044280 pitcher (Intercept) 0.020741 catcher (Intercept) 0.003275 umpire (Intercept) 0.006467 Residual 0.431941 Number of obs: 188519, groups: batter, 963; pitcher, 632; catcher, 108; umpire, 84 Fixed Effects: (Intercept) inning2 -1.730e+00 1.307e-02 inning3 inning4 1.044e-02 2.599e-02 inning5 inning6 2.065e-02 3.074e-02 inning7 inning8 2.165e-02 2.216e-02 inning9 inning10 2.123e-02 1.431e-02 inning11 inning12 4.695e-02 1.990e-02 inning13 inning14 -3.362e-03 4.660e-04 inning15 inning16 3.723e-02 -7.134e-02 inning17 inning18 -1.462e-02 1.433e-01 score_diff start_bases_cd1 5.628e-04 3.702e-02 start_bases_cd10 start_bases_cd11 -2.890e-02 -1.969e-03 start_bases_cd100 start_bases_cd101 -6.309e-03 1.102e-02 start_bases_cd110 start_bases_cd111 -3.025e-02 -1.374e-02 outs_ct1 outs_ct2 -6.250e-03 -1.087e-02 csaa temp_log -4.004e-01 2.971e-01 batsR stadiumARL02 -1.331e-02 5.734e-03 stadiumATL02 stadiumBAL12 -1.062e-03 2.828e-03 stadiumBOS07 stadiumCHI11 1.777e-02 7.515e-03 stadiumCHI12 stadiumCIN09 2.463e-02 -6.000e-03 stadiumCLE08 stadiumDEN02 1.076e-02 5.116e-02 stadiumDET05 stadiumHOU03 4.141e-03 1.956e-03 stadiumKAN06 stadiumLOS03 -3.893e-03 3.757e-03 stadiumMIA01 stadiumMIL06 -6.542e-03 3.601e-04 stadiumMIN03 stadiumMON02 5.039e-04 1.871e-02 stadiumNYC16 stadiumNYC17 -4.081e-03 -2.449e-04 stadiumOAK01 stadiumPHI13 1.268e-02 1.580e-02 stadiumPHO01 stadiumPIT08 2.287e-02 2.123e-03 stadiumSAN02 stadiumSEA03 1.499e-03 -4.888e-03 stadiumSFO03 stadiumSJU01 2.430e-02 -2.074e-02 stadiumSTL09 stadiumSTP01 -7.629e-03 6.769e-03 stadiumTOK01 stadiumTOR02 4.867e-02 2.633e-02 roleTRUE fraa 1.823e-02 -1.900e-04 bat_home_id1 inning2:score_diff 2.245e-02 -2.379e-03 inning3:score_diff inning4:score_diff 1.963e-03 -6.591e-04 inning5:score_diff inning6:score_diff 1.266e-04 2.641e-04 inning7:score_diff inning8:score_diff -7.080e-04 5.642e-04 inning9:score_diff inning10:score_diff 1.653e-03 -3.582e-03 inning11:score_diff inning12:score_diff 1.119e-02 1.172e-02 inning13:score_diff inning14:score_diff 3.300e-03 6.974e-02 inning15:score_diff inning16:score_diff 1.039e-01 -1.644e-02 inning17:score_diff inning18:score_diff -7.769e-02 -5.207e-02 start_bases_cd1:outs_ct1 start_bases_cd10:outs_ct1 -5.025e-03 3.801e-02 start_bases_cd11:outs_ct1 start_bases_cd100:outs_ct1 5.074e-02 1.269e-02 start_bases_cd101:outs_ct1 start_bases_cd110:outs_ct1 -1.915e-02 3.180e-02 start_bases_cd111:outs_ct1 start_bases_cd1:outs_ct2 7.218e-03 -1.767e-02 start_bases_cd10:outs_ct2 start_bases_cd11:outs_ct2 4.773e-02 4.553e-02 start_bases_cd100:outs_ct2 start_bases_cd101:outs_ct2 1.052e-02 -3.947e-03 start_bases_cd110:outs_ct2 start_bases_cd111:outs_ct2 2.383e-02 1.627e-02 batsR:stadiumARL02 batsR:stadiumATL02 1.743e-02 -9.816e-04 batsR:stadiumBAL12 batsR:stadiumBOS07 6.415e-03 8.939e-03 batsR:stadiumCHI11 batsR:stadiumCHI12 7.196e-04 8.811e-03 batsR:stadiumCIN09 batsR:stadiumCLE08 -5.371e-03 -7.759e-05 batsR:stadiumDEN02 batsR:stadiumDET05 -9.137e-03 7.367e-03 batsR:stadiumHOU03 batsR:stadiumKAN06 3.366e-03 4.911e-03 batsR:stadiumLOS03 batsR:stadiumMIA01 -1.673e-02 -9.282e-03 batsR:stadiumMIL06 batsR:stadiumMIN03 -3.195e-03 7.717e-03 batsR:stadiumMON02 batsR:stadiumNYC16 -2.231e-02 1.029e-02 batsR:stadiumNYC17 batsR:stadiumOAK01 -1.620e-03 -2.591e-03 batsR:stadiumPHI13 batsR:stadiumPHO01 -1.781e-02 -1.299e-02 batsR:stadiumPIT08 batsR:stadiumSAN02 -1.401e-02 -1.988e-02 batsR:stadiumSEA03 batsR:stadiumSFO03 9.953e-03 -6.813e-03 batsR:stadiumSJU01 batsR:stadiumSTL09 -2.549e-02 6.011e-04 batsR:stadiumSTP01 batsR:stadiumTOK01 -1.690e-02 -2.545e-02 batsR:stadiumTOR02 fraa:bat_home_id1 -3.296e-03 1.149e-04 inning2:bat_home_id1 inning3:bat_home_id1 -2.349e-02 -5.858e-03 inning4:bat_home_id1 inning5:bat_home_id1 -1.910e-02 -1.396e-02 inning6:bat_home_id1 inning7:bat_home_id1 -1.813e-02 -1.126e-02 inning8:bat_home_id1 inning9:bat_home_id1 -2.472e-02 -1.555e-02 inning10:bat_home_id1 inning11:bat_home_id1 -1.495e-02 -2.159e-02 inning12:bat_home_id1 inning13:bat_home_id1 4.885e-02 -4.407e-04 inning14:bat_home_id1 inning15:bat_home_id1 1.968e-02 1.090e-02 inning16:bat_home_id1 inning17:bat_home_id1 2.190e-02 -2.884e-01 inning18:bat_home_id1 1.981e-01
df.small <- data.frame(sapply(1:nrow(din), function(x) {return(1)}))
names(df.small) <- "npa"
df.small$npa <- as.integer(df.small$npa)
df.small$pitcher <- din$pitcher
head(df.small)
npa | pitcher | |
---|---|---|
1 | 1 | escok001 |
2 | 1 | escok001 |
3 | 1 | escok001 |
4 | 1 | escok001 |
5 | 1 | escok001 |
6 | 1 | pinej001 |
df.agg <- aggregate(npa ~ pitcher, df.small, sum)
df.agg$year <- yr
head(df.agg)
pitcher | npa | year | |
---|---|---|---|
1 | aardd001 | 61 | 2004 |
2 | abbop001 | 451 | 2004 |
3 | acevj002 | 704 | 2004 |
4 | adamm001 | 226 | 2004 |
5 | adamt001 | 317 | 2004 |
6 | adkij001 | 271 | 2004 |
objects()
rr.dra.0 <- ranef(value.dra.0)$pitcher
rr.dra.bases.outs <- ranef(value.dra.bases.outs)$pitcher
rr.dra.bats.stadium <- ranef(value.dra.bats.stadium)$pitcher
rr.dra.batter <- ranef(value.dra.batter)$pitcher
rr.dra.catcher <- ranef(value.dra.catcher)$pitcher
rr.dra.csaa <- ranef(value.dra.csaa)$pitcher
rr.dra.fraa.bat_home_id <- ranef(value.dra.fraa.bat_home_id)$pitcher
rr.dra.full <- ranef(value.dra.full)$pitcher
rr.dra.inn.bat_home_id <- ranef(value.dra.inn.bat_home_id)$pitcher
rr.dra.inn.scorediff <- ranef(value.dra.inn.scorediff)$pitcher
rr.dra.role <- ranef(value.dra.role)$pitcher
rr.dra.temp_log <- ranef(value.dra.temp_log)$pitcher
rr.dra.umpire <- ranef(value.dra.umpire)$pitcher
names(rr.dra.0) <- "icpt.dra.0"
names(rr.dra.bases.outs) <- "icpt.dra.bases.outs"
names(rr.dra.bats.stadium) <- "icpt.dra.bats.stadium"
names(rr.dra.batter) <- "icpt.dra.batter"
names(rr.dra.catcher) <- "icpt.dra.catcher"
names(rr.dra.csaa) <- "icpt.dra.csaa"
names(rr.dra.fraa.bat_home_id) <- "icpt.dra.fraa.bat_home_id"
names(rr.dra.full) <- "icpt.dra.full"
names(rr.dra.inn.bat_home_id) <- "icpt.dra.inn.bat_home_id"
names(rr.dra.inn.scorediff) <- "icpt.dra.inn.scorediff"
names(rr.dra.role) <- "icpt.dra.role"
names(rr.dra.temp_log) <- "icpt.dra.temp_log"
names(rr.dra.umpire) <- "icpt.dra.umpire"
ndf <- data.frame(cbind(df.agg,
rr.dra.0,
rr.dra.bases.outs,
rr.dra.bats.stadium,
rr.dra.batter,
rr.dra.catcher,
rr.dra.csaa,
rr.dra.fraa.bat_home_id,
rr.dra.full,
rr.dra.inn.bat_home_id,
rr.dra.inn.scorediff,
rr.dra.role,
rr.dra.temp_log,
rr.dra.umpire))
head(ndf)
pitcher | npa | year | icpt.dra.0 | icpt.dra.bases.outs | icpt.dra.bats.stadium | icpt.dra.batter | icpt.dra.catcher | icpt.dra.csaa | icpt.dra.fraa.bat_home_id | icpt.dra.full | icpt.dra.inn.bat_home_id | icpt.dra.inn.scorediff | icpt.dra.role | icpt.dra.temp_log | icpt.dra.umpire | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
aardd001 | aardd001 | 61 | 2004 | 0.01559938 | 0.01540755 | 0.01599329 | 0.01606483 | 0.01491985 | 0.01394799 | 0.01500188 | 0.013707 | 0.01419301 | 0.01365129 | 0.01642143 | 0.01606338 | 0.01524788 |
abbop001 | abbop001 | 451 | 2004 | 0.0194185 | 0.01956249 | 0.01889716 | 0.01700707 | 0.01883424 | 0.01862725 | 0.0201233 | 0.01332473 | 0.01685562 | 0.01710439 | 0.01660426 | 0.01905941 | 0.01928726 |
acevj002 | acevj002 | 704 | 2004 | 0.01399582 | 0.01365493 | 0.01790974 | 0.01397205 | 0.01232359 | 0.01147238 | 0.01396309 | 0.0121419 | 0.01198174 | 0.01194497 | 0.01112554 | 0.01405902 | 0.01415836 |
adamm001 | adamm001 | 226 | 2004 | -0.01038314 | -0.01046671 | -0.006712409 | -0.01031251 | -0.00981921 | -0.01067685 | -0.0101387 | -0.005677054 | -0.00845206 | -0.007749562 | -0.007989365 | -0.01050188 | -0.01015948 |
adamt001 | adamt001 | 317 | 2004 | 0.006736985 | 0.006622352 | 0.002956735 | 0.006753996 | 0.006488428 | 0.006952328 | 0.007572582 | 0.007625544 | 0.008070246 | 0.008202471 | 0.009621877 | 0.007353421 | 0.006615959 |
adkij001 | adkij001 | 271 | 2004 | 0.01086511 | 0.01058621 | 0.007204668 | 0.01192183 | 0.01015946 | 0.01035898 | 0.0117672 | 0.009778888 | 0.01127752 | 0.01101845 | 0.01349086 | 0.0112144 | 0.01090697 |
write.table(ndf, file = sprintf("dra.value.%d.out.csv", yr), row.names=FALSE, na="",col.names=TRUE, sep=",")