# using the same Autos1 data frame from the previous R code library(leaps) Auto.SmallFit <- regsubsets(LMPG ~ VOL +HP +SP +WT +lowvol +hihp, data=Autos1, nbest=5) library(car) subsets(Auto.SmallFit,statistic="bic",cex.subsets=.8,min.size=2,max.size=6) # Here we have picked the best model according to the SBC criterion # for further investigation Auto.SmallModel1 <- lm(LMPG ~ SP +WT +lowvol , data=Autos1) summary(Auto.SmallModel1) # this code creates a residual-by-predicted plot and a normal plot of the residuals par(mfrow=c(2,2)) # makes all future plots appear four per page plot(fitted(Auto.SmallModel1),resid(Auto.SmallModel1),xlab="Predicted Values", ylab="Residuals",main="Residual by Predicted plot") qqnorm(resid(Auto.SmallModel1)) qqline(resid(Auto.SmallModel1)) plot(Auto.SmallModel1$model$SP,resid(Auto.SmallModel1),xlab="SP",ylab="Residuals",main="Residuals vs. Fat") plot(Auto.SmallModel1$model$WT,resid(Auto.SmallModel1),xlab="WT",ylab="Residuals",main="Residuals vs. Cholesterol") par(mfrow=c(1,1)) # restores one plot per page windows() # this plots Studentized residuals vs. Leverage values, with reference lines # to help identify points that have high leverage and are outliers plot(hatvalues(Auto.SmallModel1),rstudent(Auto.SmallModel1),xlim=c(0,1),main=c("Studentized Residuals vs. Hat values")) abline(h=-2) abline(h=2) abline(v=2*length(Auto.SmallModel1$coefficients)/length(Auto.SmallModel1$residuals)) # reference line at 2p/n text(hatvalues(Auto.SmallModel1),rstudent(Auto.SmallModel1),labels=rownames(Auto.SmallModel1$model),pos=4,cex=.8)