Skip to content

Instantly share code, notes, and snippets.

@bquast
Last active May 24, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bquast/7b4b0def086bc6beab483d9968a19e31 to your computer and use it in GitHub Desktop.
Save bquast/7b4b0def086bc6beab483d9968a19e31 to your computer and use it in GitHub Desktop.
#### OLS by hand
solve(qr.R(qr(freeny.x)))%*%t(qr.Q(qr(freeny.x)))%*%freeny.y
QR1<- qr(freeny.x)
solve(qr.R(QR1))%*%t(qr.Q(QR1))%*%freeny.y
solve(qr.R(QR1))%*%crossprod(qr.Q(QR1),freeny.y)
backsolve(qr.R(QR1),crossprod(qr.Q(QR1),freeny.y))
coef(lm(freeny.y~freeny.x -1))
library(rbenchmark)
benchmark(solve(qr.R(QR1))%*%t(qr.Q(QR1))%*%freeny.y,
solve(qr.R(QR1))%*%crossprod(qr.Q(QR1),freeny.y),
backsolve(qr.R(QR1),crossprod(qr.Q(QR1),freeny.y)),
replications=1000)
method_1 <- function(X,y) solve(crossprod(X))%*%crossprod(X,y)
method_2 <- function(X,y) {
QR1<- qr(X)
backsolve(qr.R(QR1),crossprod(qr.Q(QR1),y))
}
all.equal(method_1(freeny.x, freeny.y),method_2(freeny.x, freeny.y),
check.attributes=FALSE)
benchmark(method_1(freeny.x, freeny.y),
method_2(freeny.x, freeny.y),
replications=1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment