Link Search Menu Expand Document (external link)

How to compute the residuals of a linear model (in R)

See all solutions.

Task

If a model has been fit to a dataset, the residuals are the differences between the actual data points and the results the model would predict. Given a linear model and a dataset, how can we compute those residuals?

Solution

Let’s assume that you’ve already built a linear model similar to the one below. This one uses a small amount of fake data, but it’s just an example. See also how to fit a linear model to two columns of data.

1
2
3
xs <- c( 393, 453, 553, 679, 729, 748, 817 )
ys <- c(  24,  25,  27,  36,  55,  68,  84 )
model <- lm(ys ~ xs)

We can extract the residuals of the model in either of two ways.

R has a built-in residuals() function for this purpose.

1
residuals(model)
1
2
         1          2          3          4          5          6          7 
  9.162630   2.199457  -9.072500 -16.795165  -4.431143   6.047185  12.889535 

The model itself has a $residuals attribute.

1
model$residuals
1
2
         1          2          3          4          5          6          7 
  9.162630   2.199457  -9.072500 -16.795165  -4.431143   6.047185  12.889535 

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)