# # Epage # # 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) # 7.2.1: # postscript("../../WriteUp/Graphics/Chapter7/prob_2_orig_scatter_plot.eps", onefile=FALSE, horizontal=FALSE) plot( Speed, Distance ) m <- lm( Distance ~ Speed ) abline(m) dev.off() # 7.2.2: try to find a transformation that linearizes this regression: # postscript("../../WriteUp/Graphics/Chapter7/prob_2_predictor_tran.eps", onefile=FALSE, horizontal=FALSE) a <- inv.tran.plot( Distance, predict(m), key=c(100,20)) dev.off() # fit the transformed model (maybe there is an easier way to do this?) # # Y^{\lambda} = alpha0 + alpha1 Speed # optLambda <- a$lambda[1] distanceToTheLambda <- Distance^(optLambda) tm <- lm( distanceToTheLambda ~ Speed ) alpha0 <- coefficients(tm)[1] alpha1 <- coefficients(tm)[2] # 7.2.3: Fit the model of Hald # theWeights <- 1 / (Speed^2) # fit the various four cases of models to this dateset: # wm <- lm( Distance ~ +1 + Speed + I(Speed^2), weights=theWeights) # this is model 1 the most general # visually compare these two models: # postscript("../../WriteUp/Graphics/Chapter7/prob_2_model_comparison .eps", onefile=FALSE, horizontal=FALSE) newSpeeds <- seq( 0, 40, length=100 ) plot( Speed, Distance ) lines( newSpeeds, ( alpha0 + alpha1 * newSpeeds )^(1/optLambda), lty=1 ) lines( newSpeeds, predict( wm, data.frame(Speed=newSpeeds) ), lty=2 ) dev.off()