繁体   English   中英

如何使用 bigstatsr R package 使用两个数据集来估计参数?

[英]How to use the bigstatsr R package using two datasets for estimating the parameters?

我有独立和依赖的数据集。 我想测试因变量和自变量之间所有可能的关系。 在我之前的帖子( 如何使用带有多个 arguments 的 mapply 复制 function 来计算方法的功效? )中,我想使用仿真数据进行功效分析。 现在,我想使用相同的 function 分析真实数据。 问题是test_function需要更多时间,因为我的数据集很大(每个数据集的维度大于 10000 X 40000)。 另外,我想使用并行计算来加快计算速度。 I have found that the bigstatsr package ( https://privefl.github.io/bigstatsr/index.html ) can handle matrices that are too large to fit in memory. 此外,我想避免expand.grid ,因为它对于大数据的计算成本也很高。 我没有找到任何可以使用 bigstatsr package 同时使用两个数据集并同时估计参数的帖子。 数据集示例和代码如下:


# dependent dataset
test_A <- data.frame(matrix(rnorm(100), nr=10, nc=10))
# independent dataset
test_B <- data.frame(matrix(sample(c(0,1,2), 500, replace = TRUE), nr=50, nc=10))
# Find all combination using dependent and independe datasets's variables
A_B_pair <- subset(expand.grid(c1=names(test_A), c2=names(test_B), 
                               stringsAsFactors = FALSE))
# Main function to estimate the parameter and p-values 
test_function <- function(test_A, test_B, x,y){
  c1 <- test_A [[x]]
  c2 <- test_B[[y]]
  Data <- data.frame(1, XX=c1, YY=c2)
  
  model_lm <- lm(YY ~ XX, Data)
  est_lm <- as.numeric(model_lm$coefficients)[2]
  pvalue_lm <- as.numeric(summary(model_lm)$coeffi[,4][2])
  
  return(unlist(data.frame(lm.estimator = est_lm, lm.pvalue =pvalue_lm)))
}
# Final output
output <- mapply(test_function, MoreArgs = list(test_A, test_B),
                 x = A_B_pair$c1, y = A_B_pair$c2)

如何应用 bigstatsr 并并行计算此 function 以获得输出? 非常感谢您的努力和帮助。

我不认为这里真的存在大小问题(内存方面),而只是计算时间问题。

我想你只是想做一些单变量测试。 为此,您可以使用 function big_univLinReg

library(bigstatsr)
X <- as_FBM(test_B)
NCORES <- nb_cores()

k <- 1  ## replace by loop here
stats <- big_univLinReg(X, test_A[[k]], ncores = NCORES)
pval <- predict(stats, log10 = FALSE)

这个 function 应该很快,并为您提供test_B中所有变量的所有系数。 然后你只需要遍历test_A中的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM