# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # #----- library(DAAG) library(MASS) lm1 = lm( distance ~ stretch, data=elastic1 ) summary(lm1) lm2 = lm( distance ~ stretch, data=elastic2 ) summary(lm2) print( summary(lm1)$r.squared ) print( summary(lm2)$r.squared ) # Use robust regression: # rlm1 = rlm( distance ~ stretch, data=elastic1 ) rlm2 = rlm( distance ~ stretch, data=elastic2 ) # We follow the books suggestion of plotting the residuals as a function of the fitted values for both the lm and the rlm results: # par(mfrow=c(1,2)) plot(lm1,which=1,add.smooth=FALSE) points( resid(rlm1) ~ fitted(rlm1), col=2, pch=2 ) # use functions : resid and fitted to get the residuals and fitted values for the robust linear models plot( lm2, which=1, add.smooth=FALSE ) points( resid(rlm2) ~ fitted(rlm2), col=2, pch=2 ) par(mfrow=c(1,1))