Home > Enterprise >  QR Factorization for Linear Regression Line in R (using qr.solve)
QR Factorization for Linear Regression Line in R (using qr.solve)

Time:11-27

So I'm trying to figure out how to use the command qr.solve in R to solve for the components of a best fit line (slope and intercept) in accordance to a particular data set. Although I already know how to do this using the lm function, to my understanding, the qr factorization of a matrix could yield the same thing. Would someone be able to explain how such a thing is true, and maybe its benefits over a simple linear model command? Could the inputs of the qr.solve function be implements in R, or would I have to solve it myself first and plug it into R afterwards?

I tried imputing my data set (2 columns each of who's rows represent points on a graph) into a matrix and using that as an x argument in the function qr.solve. However, I'm not too sure what to plug in for b.

CodePudding user response:

QR decomposition can solve Ax = b. Thus when the least squares solution gets to the point: XTy = XTXB we can use A = XTX and b = XTy to solve for x = B.

?qr.solve

mtcars
lm(mpg ~ cyl, data=mtcars)
with(mtcars, {
  X <<- cbind(1, cyl)
  y <- mpg
  A = t(X) %*% X
  b = t(X) %*% y
  qr.solve(A, b)
})
  • Related