【问题标题】:Is there a fast estimation of simple regression (a regression line with only intercept and slope)?是否有简单回归的快速估计(只有截距和斜率的回归线)?
【发布时间】:2017-03-01 16:54:32
【问题描述】:

这个问题与机器学习特征选择过程有关。

我有一个大的特征矩阵 - 列是主题(行)的特征:

set.seed(1)
features.mat <- matrix(rnorm(10*100),ncol=100)
colnames(features.mat) <- paste("F",1:100,sep="")
rownames(features.mat) <- paste("S",1:10,sep="")

在不同条件 (C) 下测量每个主题 (S) 的响应,因此看起来像这样:

response.df <-
data.frame(S = c(sapply(1:10, function(x) rep(paste("S", x, sep = ""),100))),
           C = rep(paste("C", 1:100, sep = ""), 10),
           response = rnorm(1000), stringsAsFactors = F)

所以我匹配response.df中的主题:

match.idx <- match(response.df$S, rownames(features.mat))

我正在寻找一种快速计算每个特征和响应的单变量回归的方法。

还有什么比这更快的吗?:

fun <- function(f){
  fit <- lm(response.df$response ~ features.mat[match.idx,f])
  beta <- coef(summary(fit))
  data.frame(feature = colnames(features.mat)[f], effect = beta[2,1],
             p.val = beta[2,4], stringsAsFactors = F))
  }

res <- do.call(rbind, lapply(1:ncol(features.mat), fun))

我对边际提升感兴趣,即通过mclapplymclapply2 使用并行计算以外的方法。

【问题讨论】:

  • 试试lm.fit(甚至.lm.fit
  • 正确的只有 Estimate 和 Pr(>|t|)
  • Zheyuan Li 似乎 lm.chol 将 lm 拟合到一个模型中的所有特征,而我正在寻找一种快速方法来将 lm 拟合到每个特征,分别针对目标对。我错了吗?
  • 是的,我的实际数据是数百万个特征
  • speedlm.fit 来自speedglm 包如果您有很多观察结果可能会很有用。当您有数百万个观察值时,它会非常快,并且它会返回一个非常丰富的对象。

标签: r regression linear-regression lm


【解决方案1】:

我将提供一个用于估计简单回归模型的轻量级玩具例程: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.defaultqr.coefqr.fittedqr.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 被定制为逻辑回归,但我们可以轻松地将其推广到其他响应。

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 2020-10-16
    • 2015-09-09
    • 1970-01-01
    • 2020-05-01
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多