# # # data source('chap01/ex4/chap01_ex4_kobe_bryant_data.r') ex1_4.kobe.total ex1_4.kobe.fg post.results<-ex1_4.kobe.fg #post.results <- rbind( post.results, c('Total', 'LAL', apply(ex1_4.kobe.fg[,3:5],2,mean))) a<-0.01 b<-0.01 apost <- post.results[,4]+a bpost <- post.results[,5]-post.results[,4]+b post.mean <- 100*apost/(apost+bpost) post.sd <- 100*sqrt( apost*bpost/((apost+bpost)^2*(apost+bpost+1))) post.ci1 <- 100*qbeta( 0.025, apost, bpost) post.ci2 <- 100*qbeta( 0.975, apost, bpost) post.results <- cbind( post.results, apost ) post.results <- cbind( post.results, bpost ) post.results <- cbind( post.results, post.mean ) post.results <- cbind( post.results, post.sd ) post.results <- cbind( post.results, post.ci1 ) post.results <- cbind( post.results, post.ci2 ) round(post.results[,6:11], 1) step<-0.1 x<-seq(40, 50, step) y1 <-dbeta( x/100, apost[1], bpost[1] ) y2 <-dbeta( x/100, apost[2], bpost[2] ) y3 <-dbeta( x/100, apost[3], bpost[3] ) y4 <-dbeta( x/100, apost[4], bpost[4] ) y5 <-dbeta( x/100, apost[5], bpost[5] ) y6 <-dbeta( x/100, apost[6], bpost[6] ) y7 <-dbeta( x/100, apost[7], bpost[7] ) y8 <-dbeta( x/100, apost[8], bpost[8] ) yall<-c(y1,y2,y3,y4,y5,y6,y7,y8) postscript('chap1_ex03_plot1.ps', width = 10.0, height = 6.0, horizontal=FALSE) par( xaxs='i', yaxs='i', bty='l' , cex=1.3) plot(x,y1,type='l', ylab='Posterior density', xlab='Percentage of Field Goals', ylim=c( min(yall), max(yall)+0.1 ) ) lines(x,y2, lty=2) lines(x,y3, lty=3) lines(x,y4, lty=4) lines(x,y5, lty=5) lines(x,y6, lty=6) lines(x,y7, lty=7) lines(x,y8, lty=8) graphics.off() x<-matrix( c(46.8,44.0,49.7, 46.4,43.9,48.9, 46.9,44.5,49.4, 45.1,42.9,47.3, 43.8,41.0,46.6, 43.3,40.6,46.0, 45.0,42.9,47.1, 47.2,43.9,50.6, 45.5,44.6,46.4), ncol=3, byrow=TRUE) xlabel<- as.character(1999:2006) xlabel<- c(xlabel, 'Total') xlabel[1]<-paste(xlabel[1],'00',sep='/') xlabel[2:8]<-paste(xlabel[2:8],1:7,sep='/0') postscript('chap1_ex03_plot1.ps', width = 10.0, height = 6.0) par( xaxs='i', yaxs='i', bty='l' , cex=1.3 ) # R #errbar( x=as.character(xlabel), y=x[,1], yminus=x[,2], yplus=x[,3] ) # SPLUS error.bar( y=1:9, x = x[,1], lower=x[,2], upper=x[,3], incr = T, add = F, horizontal=T, ylab = 'Season', xlab='Field Goals Percentage', ylim=c(0,10), incr=FALSE, gap=0, bar.ends=F, xlim=c(40,50)) #, xlim, ylim) graphics.off() postscript('chap1_ex03_plot1.ps', width = 10.0, height = 6.0) par( xaxs='i', yaxs='i', bty='l' , cex=1.3 ) # R #errbar( x=as.character(xlabel), y=x[,1], yminus=x[,2], yplus=x[,3] ) # SPLUS error.bar( x=1:9, y = x[,1], lower=x[,2], upper=x[,3], incr = T, add = F, horizontal=F, xlab = 'Season', ylab='Field Goals Percentage', xlim=c(0,10), incr=F, gap=0, bar.ends=F, ylim=c(40,50) , xaxt='n', cex=1.0) text( 1:9, rep(39,9), labels=xlabel, cex=0.8 ) segments( 1:9, rep(39.5,9), 1:9, rep(40.0,9),xpd=T ) graphics.off() #- ------------------------------------ # - post.results2<-post.results post.results2[,1]<-paste( 1999, 2000:2007, sep='-' ) for (i in 3:7) post.results2[,i] <- cumsum(post.results2[,i]) post.results2[,6] <- post.results2[,4]+0.01 post.results2[,7] <- post.results2[,5]-post.results2[,4]+0.01 apost <- post.results2[,6] bpost <- post.results2[,7] post.mean <- 100*apost/(apost+bpost) post.sd <- 100*sqrt( apost*bpost/((apost+bpost)^2*(apost+bpost+1))) post.ci1 <- 100*qbeta( 0.025, apost, bpost) post.ci2 <- 100*qbeta( 0.975, apost, bpost) post.results2[,8] <- post.mean post.results2[,9] <- post.sd post.results2[,10] <- post.ci1 post.results2[,11] <- post.ci2 round(post.results2[,6:11], 1) step<-0.1 x<-seq(42, 50, step) y1 <-dbeta( x/100, apost[1], bpost[1] ) y2 <-dbeta( x/100, apost[2], bpost[2] ) y3 <-dbeta( x/100, apost[3], bpost[3] ) y4 <-dbeta( x/100, apost[4], bpost[4] ) y5 <-dbeta( x/100, apost[5], bpost[5] ) y6 <-dbeta( x/100, apost[6], bpost[6] ) y7 <-dbeta( x/100, apost[7], bpost[7] ) y8 <-dbeta( x/100, apost[8], bpost[8] ) yall<-c(y1,y2,y3,y4,y5,y6,y7,y8) postscript('chap1_ex03_plot2.ps', width = 10.0, height = 6.0, horizontal=FALSE) par( xaxs='i', yaxs='i', bty='l' , cex=1.3) plot(x,y1,type='l', ylab='Posterior density', xlab='Percentage of Field Goals', ylim=c( min(yall), max(yall)+0.1 ) ) lines(x,y2, lty=2) lines(x,y3, lty=3) lines(x,y4, lty=4) lines(x,y5, lty=5) lines(x,y6, lty=6) lines(x,y7, lty=7) lines(x,y8, lty=8) posarrow<-c( 48, 47.5, 47, 46.5, 46, 45.50, 45.45, 45.52 ) arrows( posarrow+1, dbeta( posarrow/100, apost, bpost )+15, posarrow, dbeta( posarrow/100, apost, bpost ) ,xpd=T , length=0.05) text( posarrow+1.55, dbeta( posarrow/100, apost, bpost )+15, labels=post.results2[,1],xpd=T,cex=0.8) #x0, y0, x1, y1, length = 0.25, angle = 30, code = 2,col = par("fg"), lty = NULL, lwd = par("lwd"), xpd = NULL) graphics.off()