# Diagnostics # Reproduces code for Fox Chapter 11 and Faraway Ch 7 ########################## # get data and fit model # ########################## library(faraway) data(savings) # fit the model m <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data=savings) summary(m) ################### # leverage points # ################### # make plot par(mfrow=c(1,1)) plot(hatvalues(m),ylab="Leverages",main="Index plot of leverages") abline(h=2*(4+1)/50) # identify points interactively countries <- row.names(savings) identify(c(1:50), hatvalues(m), countries) # stop process by clicking 'stop' in the menu, or use middle # (and if not available, right) mousebutton ################# # residual plot # ################# par(mfrow=c(2,2)) plot(fitted(m),resid(m), ylab="Residuals", main="Residuals vs. fitted values") abline(h=0) identify(fitted(m), resid(m), countries) plot(fitted(m),rstandard(m), ylab="Standardized residuals", main="Standardized residuals vs fitted values") abline(h=0) abline(h=-2) abline(h=2) identify(fitted(m), rstandard(m), countries) plot(fitted(m),rstudent(m), ylab="Studentized residuals", main="Studentized residuals vs fitted values") abline(h=0) abline(h=-2) abline(h=2) identify(fitted(m), rstudent(m) , countries) ############# # Influence # ############# par(mfrow=c(1,1)) plot(cooks.distance(m), ylab="Cook's distance", main="Index plot of Cook's distance") n <- nrow(savings) abline(h = 4/(n-4-1)) identify(c(1:50), cooks.distance(m) , countries) #################################### # easy built-in plotting functions # #################################### par(mfrow=c(2,3)) plot(m, which=c(1:5))