Skip to content
Prev 174934 / 398503 Next

very fast OLS regression?

check the following options:

ols1 <- function (y, x) {
     coef(lm(y ~ x - 1))
}

ols2 <- function (y, x) {
     xy <- t(x)%*%y
     xxi <- solve(t(x)%*%x)
     b <- as.vector(xxi%*%xy)
     b
}

ols3 <- function (y, x) {
     XtX <- crossprod(x)
     Xty <- crossprod(x, y)
     solve(XtX, Xty)
}

ols4 <- function (y, x) {
     lm.fit(x, y)$coefficients
}

# check timings
MC <- 500
N <- 10000

set.seed(0)
x <- matrix(rnorm(N*MC), N, MC)
y <- matrix(rnorm(N*MC), N, MC)

invisible({gc(); gc(); gc()})
system.time(for (mc in 1:MC) ols1(y[, mc], x[, mc]))

invisible({gc(); gc(); gc()})
system.time(for (mc in 1:MC) ols2(y[, mc], x[, mc]))

invisible({gc(); gc(); gc()})
system.time(for (mc in 1:MC) ols3(y[, mc], x[, mc]))

invisible({gc(); gc(); gc()})
system.time(for (mc in 1:MC) ols4(y[, mc], x[, mc, drop = FALSE]))


I hope it helps.

Best,
Dimitris
ivo welch wrote: