Ordinary least squares (OLS)
Ordinary least squares (OLS) is the workhorse of statistics. It gives a way of taking complicated outcomes and explaining behaviour (such as trends) using linearity. The simplest application of OLS is fitting a line.
Residuals
Residuals are the observable errors from the estimated coefficients. In a sense, the residuals are estimates of the errors.
Let's explain the things using R
code:
First fit a ordinary least square line of diamond datasets in UsingR
library:
library(UsingR)
data("diamond")
y <- diamond$price
x <- diamond$carat
n <- length(y)
olsline <- lm(y ~ x)
plot(x, y,
main ="Odinary Least square line",
xlab = "Mass (carats)",
ylab = "Price (SIN $)",
bg = "lightblue",
col = "black", cex = 2, pch = 21,frame = FALSE)
abline(olsline, lwd = 2)
Now, Let's calculate the residual i.e residual sum of squares:
In R
you can easily calculate the residual as resid(olsline)
, for visualisation let's calculate it manually:
# The residuals from R method
e <- resid(olsline)
## Obtain the residuals manually, get the predicated Ys first
yhat <- predict(olsline)
# The residuals are y -yhat, Let's check by comparing this with R's build in resid function
ce <- y - yhat
max(abs(e-ce))
## Let's do it again hard coding the calculation of Yhat
max(abs(e- (y - coef(olsline)[1] - coef(olsline)[2] * x)))
# Residuals arethe signed length of the red lines
plot(diamond$carat, diamond$price,
main ="Residuals sum of (actual Y - predicted Y)^2",
xlab = "Mass (carats)",
ylab = "Price (SIN $)",
bg = "lightblue",
col = "black", cex = 2, pch = 21,frame = FALSE)
abline(olsline, lwd = 2)
for (i in 1 : n)
lines(c(x[i], x[i]), c(y[i], yhat[i]), col = "red" , lwd = 2)
Hope these visualization will clear your doubts between RSS & OLS