[英]Speed-up linear regression using coeftest in R
I developed an R code that requires to compute more than 10 million times the same multiple regression model (15 variables). 我开发了一个R代码,该代码需要计算超过1000万次相同的多元回归模型(15个变量)。 I need to extract for each model the obtained t -statistic for only one independent variable.
我需要为每个模型提取仅针对一个自变量的t统计量。 I need to apply for each model a heteroskedasticity-consistent covariance matrix estimator and I am doing that using the White's estimator via
coeftest
and vcovHC
, but I noticed that this operation increases a lot the required computational time of the simulation. 我需要为每个模型应用异方差一致的协方差矩阵估计器,并且正在通过
coeftest
和vcovHC
使用怀特估计器进行此操作,但是我注意到此操作增加了仿真所需的计算时间。 Therefore I was wondering if there could be a way to speed up the code as I only need the t -statistic of the second variable. 因此,我想知道是否有一种方法可以加快代码速度,因为我只需要第二个变量的t统计量即可。
Below is a toy example of what I am doing at each iteration: 以下是我在每次迭代中所做的一个玩具示例:
model <- lm(y ~ a + b + c, data = data)
model <- coeftest(model, vcov. = vcovHC(model, type = "HC"))
t[i] <- summary(MUR)$coef[2, 3]
The variables involved are always the same, but I am permuting their values randomly. 所涉及的变量总是相同的,但是我正在随机排列它们的值。 In other words I am permuting the model matrix X .
换句话说,我置换模型矩阵X。
It is possible to reduce computation by ~30 % handling heteroscedasticity-corrected covariance matrices using hccm
function of car
package then extracting t -values directly. 使用
car
包装的hccm
函数然后直接提取t值,可以减少约30%的处理异方差校正的协方差矩阵的计算。 Please see the simulation below: 请参见下面的模拟:
library(lmtest)
library(sandwich)
library(microbenchmark)
library(ggplot2)
library(car)
microbenchmark(
hccm = {
m <- lm(cty ~ displ + cyl, data = mpg)
V <- hccm(m, "hc0")
cfs <- m$coefficients
ses <- sqrt(diag(V))
cfs / ses
},
coeftest = {
m <- lm(cty ~ displ + cyl, data = mpg)
coeftest(m, vcov. = vcovHC(m, type = "HC0"))
}
)
Output: 输出:
Unit: milliseconds
expr min lq mean median uq max neval cld
hccm 1.695146 1.777919 1.939631 1.822293 1.891840 10.65045 100 a
coeftest 2.557013 2.650025 2.735701 2.684586 2.764373 3.37536 100 b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.