# Read in data
homerun<-read.table("homerun.dat",col.names=c("Game","StL.month",
 "StL.date","StL.season.day","StL.home","StL.runs","StL.opp.runs","StL.win",
 "McGwire.HR","McGwire.HR.RBI","McGwire.out","Chi.month","Chi.date",
 "Chi.season.day","Chi.home","Chi.runs","Chi.opp.runs","Chi.win","Sosa.HR",
 "Sosa.HR.RBI","Sosa.out"))
attach(homerun)
# Frequency distributions
table(McGwire.HR)
table(Sosa.HR)
# Figure 1
hist(StL.runs,col=0,prob=T,xlab="Runs scored by St. Louis",ylab="Density")
# Figure 2
hist(StL.runs-StL.opp.runs,col=0,prob=T,xlab="Victory margin for St. Louis",
 ylab="Density")
# Figure 3
plot(StL.season.day,cumsum(McGwire.HR),type="n",ylab="Home runs hit",
 xlab="Calendar date of season",xaxt="n")
axis(1,at=c(16,47,77,108,139,169),labels=c("April","May","June","July",
 "August","September"),ticks=F)
axis(1,at=c(2,32,63,93,124,155,182),labels=F)
abline(h=61,lty=2)
points(StL.season.day,cumsum(McGwire.HR),pch="o",col=6)
points(Chi.season.day,cumsum(Sosa.HR),pch="x",col=3)
# Table 1
StL.result<-category(StL.win,label=c("Loss","Win"),exclude=-1)
McGwire.homerun<-cut(McGwire.HR,c(-1,0,1,3),labels=c("Zero","One","Two or more"))
mactable<-table(StL.result,McGwire.homerun)
loglin(mactable,list(1,2),fit=T)
for (j in 1:3){
 print(mactable[,j]/sum(mactable[,j]))}
# Corresponding table only including games McGwire played
StL.result.played<-category(StL.win[McGwire.out==0],label=c("Loss","Win"))
McGwire.homerun.played<-cut(McGwire.HR[McGwire.out==0],c(-1,0,1,3),labels=
 c("Zero","One","Two or more"))
macplayed<-table(StL.result.played,McGwire.homerun.played)
loglin(macplayed,list(1,2),fit=T)
# Table 2
Chi.result<-category(Chi.win,label=c("Loss","Win"))
Sosa.homerun<-cut(Sosa.HR,c(-1,0,1,3),labels=c("Zero","One","Two or more"))
samtable<-table(Chi.result,Sosa.homerun)
loglin(samtable,list(1,2),fit=T)
for (j in 1:3){
 print(samtable[,j]/sum(samtable[,j]))}
# Corresponding table only including games Sosa played
Chi.result.played<-category(Chi.win[Sosa.out==0],label=c("Loss","Win"))
Sosa.homerun.played<-cut(Sosa.HR[Sosa.out==0],c(-1,0,1,3),labels=
 c("Zero","One","Two or more"))
samplayed<-table(Chi.result.played,Sosa.homerun.played)
loglin(samplayed,list(1,2),fit=T)
# Table 3
homefield<-array(c(table(Chi.home,Chi.win),table(StL.home,StL.win,exclude=-1)),
 dim=c(2,2,2),dimnames=list(c("Away","Home"),c("Loss","Win"),c("Chicago","St. Louis")))
loglin(homefield,list(c(1,2)),fit=T)
# Figure 4
macsmooth<-gam(McGwire.HR[McGwire.out==0]~lo(StL.season.day[McGwire.out==0],
 degree=2,span=.6),family=poisson)
plot(StL.season.day[McGwire.out==0],predict(macsmooth,type="response"),type="n",
 xlab="Calendar date of season",ylab="Home runs per game",ylim=c(0,.75),
 xaxt="n")
axis(1,at=c(16,47,77,108,139,169),labels=c("April","May","June","July",
 "August","September"),ticks=F)
axis(1,at=c(2,32,63,93,124,155,182),labels=F)
lines(StL.season.day[McGwire.out==0],predict(macsmooth,type="response"),col=6)
samsmooth<-gam(Sosa.HR[Sosa.out==0]~lo(Chi.season.day[Sosa.out==0],
 degree=2,span=.6),family=poisson)
lines(Chi.season.day[Sosa.out==0],predict(samsmooth,type="response"),col=3,lty=4)
