#====================================================================== # Figure 12.4.R. # R script to draw scatterplot of Old Faithful data with three # candidate prediction lines overlayed. # # Data file should have two columns: response variable to be # predicted labeled "y" and predictor variable labeled "x". # Change the R working directory to the location of the data file. #====================================================================== Geyser=read.table("old_faithful.txt",header=TRUE) # Change file name if # necessary. attach(Geyser) plot(x,y,type="p",xlab="duration of eruption (minutes)", ylab="time until next eruption (minutes)") #Scatterplot of data. b=numeric(2) # Set up vector to hold intercept and slope. # Calculate and plot first line. b[1]=25 # Intercept. b[2]=18 # Slope. yhat1=b[1]+b[2]*min(x) yhat2=b[1]+b[2]*max(x) yhat=rbind(yhat1,yhat2) xvals=rbind(min(x),max(x)) points(xvals,yhat,type="l",lty=2) # Calculate and plot second line. b[1]=50 b[2]=8 yhat1=b[1]+b[2]*min(x) yhat2=b[1]+b[2]*max(x) yhat=rbind(yhat1,yhat2) xvals=rbind(min(x),max(x)) points(xvals,yhat,type="l",lty=2) # Calculate and plot third line. b[1]=33 b[2]=10 yhat1=b[1]+b[2]*min(x) yhat2=b[1]+b[2]*max(x) yhat=rbind(yhat1,yhat2) xvals=rbind(min(x),max(x)) points(xvals,yhat,type="l",lty=2) detach(Geyser)