我将提供一个用于估计简单回归模型的轻量级玩具例程:y ~ x,即只有截距和斜率的回归线。 可以看出,这比 lm + summary.lm 快 36 倍。
## toy data
set.seed(0)
x <- runif(50)
y <- 0.3 * x + 0.1 + rnorm(50, sd = 0.05)
## fast estimation of simple linear regression: y ~ x
simplelm <- function (x, y) {
## number of data
n <- length(x)
## centring
y0 <- sum(y) / length(y); yc <- y - y0
x0 <- sum(x) / length(x); xc <- x - x0
## fitting an intercept-free model: yc ~ xc + 0
xty <- c(crossprod(xc, yc))
xtx <- c(crossprod(xc))
slope <- xty / xtx
rc <- yc - xc * slope
## Pearson estimate of residual standard error
sigma2 <- c(crossprod(rc)) / (n - 2)
## standard error for slope
slope_se <- sqrt(sigma2 / xtx)
## t-score and p-value for slope
tscore <- slope / slope_se
pvalue <- 2 * pt(abs(tscore), n - 2, lower.tail = FALSE)
## return estimation summary for slope
c("Estimate" = slope, "Std. Error" = slope_se, "t value" = tscore, "Pr(>|t|)" = pvalue)
}
我们来做个测试:
simplelm(x, y)
# Estimate Std. Error t value Pr(>|t|)
#2.656737e-01 2.279663e-02 1.165408e+01 1.337380e-15
另一方面,lm + summary.lm 给出:
coef(summary(lm(y ~ x)))
# Estimate Std. Error t value Pr(>|t|)
#(Intercept) 0.1154549 0.01373051 8.408633 5.350248e-11
#x 0.2656737 0.02279663 11.654079 1.337380e-15
所以结果匹配。如果您需要 R 平方和调整后的 R 平方,也可以轻松计算。
让我们有一个基准:
set.seed(0)
x <- runif(10000)
y <- 0.3 * x + 0.1 + rnorm(10000, sd = 0.05)
library(microbenchmark)
microbenchmark(coef(summary(lm(y ~ x))), simplelm(x, y))
#Unit: microseconds
# expr min lq mean median uq
# coef(summary(lm(y ~ x))) 14158.28 14305.28 17545.1544 14444.34 17089.00
# simplelm(x, y) 235.08 265.72 485.4076 288.20 319.46
# max neval cld
# 114662.2 100 b
# 3409.6 100 a
神圣!!!我们有 36 倍的提升!
Remark-1(求解正规方程)
simplelm 基于通过 Cholesky 分解求解正规方程。但由于它很简单,因此不涉及实际的矩阵计算。如果我们需要使用多个协变量进行回归,我们可以使用lm.chol defined in my this answer。
正规方程也可以通过使用 LU 分解来求解。这个我就不多说了,如果你感兴趣的话,这里是:Solving normal equation gives different coefficients from using lm?。
Remark-2(通过cor.test替代)
simplelm 是我的回答 Monte Carlo simulation of correlation between two Brownian motion (continuous random walk) 中 fastsim 的扩展。另一种方法是基于cor.test。它也比lm + summary.lm 快得多,但如该答案所示,它仍然比我上面的建议慢。
Remark-3(通过 QR 方法替代)
基于 QR 的方法也是可能的,在这种情况下,我们希望在 C 级别使用 .lm.fit,这是 qr.default、qr.coef、qr.fitted 和 qr.resid 的轻量级包装器。以下是我们如何将此选项添加到我们的simplelm:
## fast estimation of simple linear regression: y ~ x
simplelm <- function (x, y, QR = FALSE) {
## number of data
n <- length(x)
## centring
y0 <- sum(y) / length(y); yc <- y - y0
x0 <- sum(x) / length(x); xc <- x - x0
## fitting intercept free model: yc ~ xc + 0
if (QR) {
fit <- .lm.fit(matrix(xc), yc)
slope <- fit$coefficients
rc <- fit$residuals
} else {
xty <- c(crossprod(xc, yc))
xtx <- c(crossprod(xc))
slope <- xty / xtx
rc <- yc - xc * slope
}
## Pearson estimate of residual standard error
sigma2 <- c(crossprod(rc)) / (n - 2)
## standard error for slope
if (QR) {
slope_se <- sqrt(sigma2) / abs(fit$qr[1])
} else {
slope_se <- sqrt(sigma2 / xtx)
}
## t-score and p-value for slope
tscore <- slope / slope_se
pvalue <- 2 * pt(abs(tscore), n - 2, lower.tail = FALSE)
## return estimation summary for slope
c("Estimate" = slope, "Std. Error" = slope_se, "t value" = tscore, "Pr(>|t|)" = pvalue)
}
对于我们的玩具数据,QR 方法和 Cholesky 方法给出相同的结果:
set.seed(0)
x <- runif(50)
y <- 0.3 * x + 0.1 + rnorm(50, sd = 0.05)
simplelm(x, y, TRUE)
# Estimate Std. Error t value Pr(>|t|)
#2.656737e-01 2.279663e-02 1.165408e+01 1.337380e-15
simplelm(x, y, FALSE)
# Estimate Std. Error t value Pr(>|t|)
#2.656737e-01 2.279663e-02 1.165408e+01 1.337380e-15
已知 QR 方法比 Cholesky 方法慢 2 ~ 3 倍(详细解释请阅读我的回答 Why the built-in lm function is so slow in R?)。这是一个快速检查:
set.seed(0)
x <- runif(10000)
y <- 0.3 * x + 0.1 + rnorm(10000, sd = 0.05)
library(microbenchmark)
microbenchmark(simplelm(x, y, TRUE), simplelm(x, y))
#Unit: microseconds
# expr min lq mean median uq max neval cld
# simplelm(x, y, TRUE) 776.88 873.26 1073.1944 908.72 933.82 3420.92 100 b
# simplelm(x, y) 238.32 292.02 441.9292 310.44 319.32 3515.08 100 a
确实如此,908 / 310 = 2.93。
Remark-4(GLM 的简单回归)
如果我们继续使用 GLM,还有一个基于 glm.fit 的快速、轻量级版本。您可以阅读我的答案R loop help: leave out one observation and run glm one variable at a time 并使用那里定义的函数f。目前f 被定制为逻辑回归,但我们可以轻松地将其推广到其他响应。