# Computer code for example 3.20 # HINT: # The program assumes that all files are stored in # C:\monographregression\computercode # and corresponding subfolders. # Change directory if code is located elsewhere. # change working directory setwd("c:/monographregression/computercode") library(foreign) # read data golf <- read.dta(file="data/stata/golffull.dta") attach(golf) # Estimate AIC best model of example 3.19 mod9 <- lm(price~kilometerop1+kilometerop2+ageop1+ageop2, data=golf) # Compute studentized residuals rs<-rstudent(mod9) # Compute predicted values pred <- predict(mod9) # quantiles of the t-distribution t <- qt(0.01/344,df=172-6) # Plot studentized residuals # Note that the original plots in the book have been generated with STATA. Therefore the plots produced with R are slightly different. plot(pred,rs,ylab="studentized residuals",xlab="estimated sales price",main="studentized residuals versus estimated sales price",ylim=c(-5,5)) abline(-t,0) abline(t,0) abline(0,0) plot(kilometer,rs,ylab="studentized residuals",xlab="kilometer reading in 1000 km",main="studentized residuals versus kilometer reading",ylim=c(-5,5)) abline(-t,0) abline(t,0) abline(0,0) plot(age,rs, ylab="studentized residuals",xlab="age in months",main="studentized residuals versus age in months",ylim=c(-5,5)) abline(-t,0) abline(t,0) abline(0,0) plot(TIA,rs, ylab="studentized residuals",xlab="months until next TIA appointment",main="studentized residuals versus months until TIA",ylim=c(-5,5)) abline(-t,0) abline(t,0) abline(0,0)