data.source<-"http://www.math.mcgill.ca/dstephens/Regression/Data/2-1-RocketProp.csv" RocketProp<-read.csv(file=data.source) names(RocketProp)<-c('i','Strength','Age') x<-RocketProp$Age y<-RocketProp$Strength plot(x,y) plot(x,y,pch=19) plot(x,y,pch=19,xlab='Age',ylab='Shear Strength') (xmean<-mean(x)) (ymean<-mean(y)) abline(v=xmean,h=ymean,lty=2) dev.copy2pdf(file='RocketPropPlot.pdf',paper='USr',width=11,height=9) ############################################ #Fit the simple linear regression using lm fit.RP<-lm(y~x) summary(fit.RP) coef(fit.RP) abline(coef(fit.RP),col='red') title('Line of best fit for Rocket Propulsion Data') ############################################ #Using the matrix formulae X<-cbind(rep(1,n),x) (XtX<-t(X)%*%X) Xty<-t(X) %*% y (beta.hat<-solve(XtX,Xty)) ############################################ #New material plot(x[1:12],y[1:12],pch=19,xlab='Age',ylab='Shear Strength') fit.RP2<-lm(y[1:12]~x[1:12]) summary(fit.RP2) fit.RP<-lm(Strength~Age,data=RocketProp) fit.RP0<-lm(Strength~Age-1,data=RocketProp) plot(x,y,pch=19,xlab='Age',ylab='Shear Strength') coef(fit.RP0) abline(coef(fit.RP0),0) fit.RP2<-lm(Strength~Age,subset=(i >= 1 & i <= 12),data=RocketProp) fit.RP3<-lm(Strength~I(Age-1),subset=(i >= 1 & i <= 12),data=RocketProp) fit.RP4<-lm(Strength~log(Age),data=RocketProp) plot(log(x),y,pch=19,xlab='log(Age)',ylab='Shear Strength') abline(coef(fit.RP4)) abline(coef(fit.RP4),col='white') fit.RP5<-lm(Strength~Age^2,data=RocketProp) plot(x^2,y,pch=19,xlab='Age squared',ylab='Shear Strength') abline(coef(fit.RP5)) ############################################