Skip to main content
2 of 2
added 121 characters in body
hachiko
  • 89
  • 10

I'm coming late to this conversation but I just want to add something that I think might aid understanding.

The computation for finding the OLS estimator relies on math from linear algebra involving matrices. It's my understanding there are a few different ways to do this. A = QR shows how to create an orthonormal matrix and an upper triangular matrix.

However, in the case of simple linear regression for an ordinary least squares estimate, meaning regression with only one independent and one dependent variable, you can use a shortcut equation in order to figure out the slope and the intercept of the line instead of leaning on the more complicated linear algebra math.

In the case of simple linear regression, the slope and intercept follow these neat closed-form equations: the slope can be calculated by multiplying the correlation r by the quotient of standard deviation of y over standard deviation of x. In this below equation, a refers to the slope and sy and sx refer to the standard deviation of y and the standard deviation of x, respectively.

a = r * ( sy / sx )

The intercept of the line of best fit for ordinary least squares simple linear regression can be calculated easily after you calculate the slope of the line of best fit. You do this by subtracting the slope of the line of best fit from mean of y, then multiplying the result by the mean of x. In the equation below, i refers to y-intercept and the straight line over the x and y values is a way of referring to the mean of x and y respectively; we refer to these terms as x-bar and y-bar.

i = y-bar - r * (sy / sx) * x-bar

Our R code looks like this:

r_slope <- ( sd(mtcars$mpg) / sd(mtcars$wt) ) * cor(mtcars$mpg, mtcars$wt)
r_intercept <- mean(mtcars$mpg) - r_slope * mean(mtcars$wt) 

I know this only covers the case of simple linear regression with one x and one y but I hope it's helpful in visualizing this case. In simple linear regression, you can definitely find the slope and intercept of the line without knowing linear algebra.

hachiko
  • 89
  • 10