data.source.root<-"http://www.math.mcgill.ca/dstephens/Regression/Data/" file1<-paste(data.source.root,'bloodtype.csv',sep='') Blood<-read.csv(file=file1) names(Blood) x<-Blood$AB.Conc y<-Blood$WickingLength plot(x,y,pch=19,xlab='Conc',ylab='Wicking Length (mm)',ylim=range(0,25)) #Fit the simple linear regression using lm fit.Blood<-lm(y~x) summary(fit.Blood) abline(coef(fit.Blood),col='red',lwd=2) title('Line of best fit') #dev.copy2eps(file='Ex1-Blood.eps',width=11,height=9) plot(x,log(y),pch=19,xlab='Conc',ylab='log(Wicking Length (mm))',ylim=range(2,4)) fit.Blood2<-lm(log(y)~x) summary(fit.Blood2) abline(coef(fit.Blood2),col='red') title('Line of best fit (logged response)') ####################################################################### file2<-paste(data.source.root,'liquid.csv',sep='') Liquid<-read.csv(file=file2) names(Liquid) x<-Liquid$TIME y<-Liquid$MASS plot(x,y,pch=19,xlab='Time (min)',ylab='Mass (lbs)') #dev.copy2eps(file='Ex1-Liquid-Data.eps',width=11,height=9) #Fit the simple linear regression using lm fit.Liquid<-lm(y~x) summary(fit.Liquid) #Fit a quadratic regression using lm fit.Liquid2<-lm(y~x+I(x^2)) summary(fit.Liquid2) plot(x,y,pch=19,xlab='Time (min)',ylab='Mass (lbs)') dev.copy2eps(file='Ex1-Liquid-Fits.eps',width=11,height=9) abline(coef(fit.Liquid),col='red',lwd=2) title('Lines of best fit') yhat<-predict(fit.Liquid2) lines(x,yhat,col='blue',lwd=2) legend(40,5,c('Straight Line', 'Quadratic'),col=c('red','blue'),lty=1,lwd=2) #dev.copy2eps(file='Ex1-Liquid-Fit.eps',width=11,height=9) newx<-data.frame(x=25) (y25.1<-predict(fit.Liquid,newdata=newx)) (y25.2<-predict(fit.Liquid2,newdata=newx)) points(25,y25.1,pch=3,col='green',lwd=2) points(25,y25.2,pch=3,col='cyan',lwd=2) abline(v=25,lty=2) e1<-residuals(fit.Liquid) e2<-residuals(fit.Liquid2) sum(e1) sum(e2) par(mfrow=c(1,2)) plot(x,residuals(fit.Liquid),pch=19,ylim=range(-2,2),xlab='Time',ylab='Residuals');abline(h=0,lty=2) title('Residuals from straight line fit') plot(x,residuals(fit.Liquid2),pch=19,ylim=range(-2,2),xlab='Time',ylab='Residuals');abline(h=0,lty=2) title('Residuals from quadratic fit') #dev.copy2eps(file='Ex1-Liquid-Resids.eps',width=11,height=9) dev.off() ########################################################################## file3<-paste(data.source.root,'brainpain.csv',sep='') Brain<-read.csv(file=file3) names(Brain) x<-Brain$EMPATHY y<-Brain$ACTIVITY plot(x,y,pch=19,xlab='Empathy Score',ylab='Brain Activity') #Fit the simple linear regression using lm fit.Brain<-lm(y~x) summary(fit.Brain) abline(coef(fit.Brain),col='red') title('Brain data: Line of best fit') #dev.copy2eps(file='Ex1-Brain-Fit.eps',width=11,height=9) ei<-residuals(fit.Brain) yhat<-fitted(fit.Brain) par(mfrow=c(1,2)) plot(x,ei,pch=19,ylim=range(-0.5,0.5),xlab='Empathy level',ylab='Residuals');abline(h=0,lty=2) title('Residuals from straight line fit') plot(yhat,ei,pch=19,ylim=range(-0.5,0.5),xlab='Fitted value',ylab='Residuals');abline(h=0,lty=2) #dev.copy2eps(file='Ex1-Brain-Resids.eps',width=11,height=9) dev.off() ########################################################################## file4<-paste(data.source.root,'casino.csv',sep='') Casino<-read.csv(file=file4) names(Casino) x<-Casino$EMPLOYEES y<-Casino$CRIMERAT plot(x,y,pch=19,xlab='Number of Employees',ylab='Crime rate (per 1000)') #Fit the simple linear regression using lm fit.Casino<-lm(y~x) summary(fit.Casino) abline(coef(fit.Casino),col='red',lwd=2) title('Casino data: Line of best fit') #dev.copy2eps(file='Ex1-Casino-Fit.eps',width=11,height=9) #Compute the sample correlation cor(x,y)