Including a regression line and residuals
In the Creating scatterplots and line plots section in Chapter 1, Base Graphics in R – One Step at a Time, we saw how to use the abline()
command and the lm()
command to include a regression line in your graph. Now, we will take this idea a little further. The following regression uses a datafile in which a sample of 10 people rated a film by awarding scores out of 100. These people then viewed the film a second time 1 month later and again awarded scores. We wish to use a regression model to see how well the first rating scores predicted the second rating.
In an OLS regression with one predictor, we fit a model of the following form:
Yi = β0 + β1 Xi + ei
In this form, β0
is the intercept, β1
is the slope, and e
i
are the errors (or residuals).
Let's perform the regression on the data and plot the results. Along the way, we will learn some useful R syntax. Go to the code file of this chapter, and copy and paste the following syntax into R. It contains...