# Day 1 R scripts ############################################ ############################################# # Baseball Examples # ############################################# Data = read.table("RunsPerGame.txt",header=T) attach(Data) summary(League) modelAVG = lm(R.G ~ AVG) modelSLG = lm(R.G ~ SLG) modelOBP = lm(R.G ~ OBP) summary(modelAVG) plot(AVG,R.G,pch=19,cex=1.5,xlab="AVG",ylab = "Runs per Game") abline(modelAVG$coef[1],modelAVG$coef[2],col=2,lwd=2) plot(SLG,R.G,pch=19,cex=1.5,xlab="SLG",ylab = "Runs per Game") abline(modelSLG$coef[1],modelSLG$coef[2],col=2,lwd=2) plot(OBP,R.G,pch=19,cex=1.5,xlab="OBP",ylab = "Runs per Game") abline(modelOBP$coef[1],modelOBP$coef[2],col=2,lwd=2) ############################################# # House Example # ############################################# House = read.table("housedata.txt",header=T) attach(House) plot(Size,Price,pch=19) Housemodel = lm(Price~Size,data=House) summary(Housemodel) names(Housemodel) names(summary(Housemodel)) confint(Housemodel) confint(Housemodel,level=0.99) Xfuture <- data.frame(Size=seq(0,8,by=0.01)) Future1 = predict(Housemodel, Xfuture, interval = "prediction",se.fit=T) Future2 = predict(Housemodel, Xfuture, interval = "prediction",se.fit=T,level=0.99) plot(Size,Price,xlim=c(0,8),ylim=range(Future1$fit),pch=19,cex.lab=1.3) abline(lsfit(Size,Price),lwd=2,col=2) lines(Xfuture$Size,Future1$fit[,2],col=4,lwd=2,lty=2) lines(Xfuture$Size,Future1$fit[,3],col=4,lwd=2,lty=2)