# # Epage 191 # # 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. # #----- # version$language == "R" for R version$language == NULL for SPlus if(is.null(version$language) == FALSE){ require(alr3) }else{ library(alr3) } data(stopping) attach(stopping) # plot( Speed, Distance ) # fit a constant variance model: # m0 <- lm( Distance ~ Speed ) abline(m0) # generate residual plots by hand # postscript("../../WriteUp/Graphics/Chapter8/prob_4_res_vs_yhat_plot.eps", onefile=FALSE, horizontal=FALSE) plot(predict(m0),residuals(m0,type="pearson"),xlab="(a) Yhat", ylab="Residuals") abline(h=0) dev.off() postscript("../../WriteUp/Graphics/Chapter8/prob_4_res_vs_speed_plot.eps", onefile=FALSE, horizontal=FALSE) plot(Speed,residuals(m0,type="pearson"),xlab="(b) Speed", ylab="Residuals") abline(h=0) dev.off() postscript("../../WriteUp/Graphics/Chapter8/prob_4_res_vs_speed2_plot.eps", onefile=FALSE, horizontal=FALSE) plot(Speed^2,residuals(m0,type="pearson"),xlab="(c) Speed^2", ylab="Residuals") abline(h=0) dev.off() # score tests, using car library(car) s1 <- ncv.test(m0,~Speed,data=stopping) s2 <- ncv.test(m0,~Speed+I(Speed^2),data=stopping) s3 <- ncv.test(m0,~I(Speed^2),data=stopping) s4 <- ncv.test(m0) ans <- data.frame( df=c(s1$Df,s2$Df,s3$Df,s4$Df), S=c(s1$ChiSquare,s2$ChiSquare,s3$ChiSquare,s4$ChiSquare), p=c(s1$p,s2$p,s3$p,s4$p)) # the next line adds row lables to the output, labelled according to the # model. This does not work in SPlus because models are lists, and I can't # figure out how to fix it up. if(is.null(version$language) == FALSE) row.names(ans) <-c(s1$formula, s2$formula, s3$formula, s4$formula) round(ans,3)