R中加权最小二乘均值估计的加速逆计算 [英] Speed-up inverse calculation of weighted least squares mean estimate in R

查看:224
本文介绍了R中加权最小二乘均值估计的加速逆计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加快R中WLS中beta的均值估计的计算-我能够加快协方差计算

I need to speed up the calculation of the mean estimate of beta in a WLS in R - I was able to speed up the covariance calculation thanks to SO, and now I am wondering if there is another trick to also speed up the mean calculation (or if what I am doing is already efficient enough).

n = 10000
y = rnorm(n, 3, 0.4)
X = matrix(c(rnorm(n,1,2), sample(c(1,-1), n, replace = TRUE), rnorm(n,2,0.5)), nrow = n, ncol = 3)
Q = diag(rnorm(n, 1.5, 0.3))
wls.cov.matrix = crossprod(X / sqrt(diag(Q)))
Q.inv = diag(1/diag(Q))
wls.mean = wls.cov.matrix%*%t(X)%*%Q.inv%*%y
system.time(wls.cov.matrix%*%t(X)%*%Q.inv%*%y)

是否还有与wls.cov.matrix crossprod中类似的技巧,以加快整个均值计算的速度,还是不需要?谢谢!

Is there another similar trick as in wls.cov.matrix crossprod to speed up the whole mean calculation, or no need? Thanks!

推荐答案

在此过程中,主要的增益将通过在过程中不使用任何n×n矩阵来实现.我的意思是没有Q矩阵,只用它的对角线.

The main gain in the performance would be achieved by not having any n-by-n matrices along the way. I mean not having the Q matrix, only working with its diagonal.

以@Roland的答案为基础:

Building on the answer by @Roland:

Qdiag = rnorm(n, 1.5, 0.3);
Q = diag(Qdiag);

wls.mean3 <- wls.cov.matrix %*% crossprod(X/Qdiag, y);
all.equal(wls.mean, wls.mean3)
microbenchmark(wls.cov.matrix %*% t(X) %*% Q.inv %*% y,
               wls.cov.matrix %*% crossprod(X, Q.inv) %*% y,
               wls.cov.matrix %*% crossprod(X/Qdiag, y),
           times=5)
#      wls.cov.matrix %*% t(X) %*% Q.inv %*% y 358050.195 363713.250 368820.818 372414.747 374824.56     5
# wls.cov.matrix %*% crossprod(X, Q.inv) %*% y  79449.856  81411.195  84616.706  85351.968  88108.62     5
#     wls.cov.matrix %*% crossprod(X/Qdiag, y)    279.092    284.867    285.252    291.796    295.26     5

这篇关于R中加权最小二乘均值估计的加速逆计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆