【问题标题】:How to use the bigstatsr R package using two datasets for estimating the parameters?如何使用 bigstatsr R 包使用两个数据集来估计参数?
【发布时间】:2022-01-01 21:35:13
【问题描述】:

我有独立和依赖的数据集。我想测试因变量和自变量之间所有可能的关系。在我之前的帖子 (How to replicate a function using mapply with multiple arguments to calculate the power of a method?) 中,我想使用仿真数据进行功耗分析。现在,我想使用相同的功能分析真实数据。问题是 test_function 需要更多时间,因为我的数据集很大(每个数据集的维度大于 10000 X 40000)。另外,我想使用并行计算来加快计算速度。我发现 bigstatsr 包 (https://privefl.github.io/bigstatsr/index.html) 可以处理太大而无法放入内存的矩阵。此外,我想避免 expand.grid,因为它对于大数据的计算成本也很高。我没有找到任何可以使用 bigstatsr 包同时使用两个数据集并同时估计参数的帖子。数据集示例和代码如下:


# 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)

编辑: 我想应用我的 proposed method 来估计参数并将结果与​​ lm 方法进行比较。我提出的方法如下:

library(pracma)
Proposed_method<- function(Data, Beta) 
{ 
  n = dim(Data)[1]
  Median <- t(apply(Data,2,median))
  Dist <- sqrt(rowSums((Data - as.matrix(rep(1,dim(Data)[1]))%*%Median)^2))
  Data0 <- as.matrix(Data[which(Dist <= as.numeric(quantile(Dist, p=.45, na.rm = TRUE))),])
  Yo <- as.matrix(Data0[,dim(Data0)[2]])
  Xo <- as.matrix(Data0[,-dim(Data0)[2]])
  Gama0 <- as.numeric(pinv(crossprod(Xo, Xo))%*%crossprod(Xo, Yo))
  Sigma2o <- var(Yo)
  Y <- as.matrix(Data[,dim(Data)[2]])
  X <- as.matrix(Data[,-dim(Data)[2]])
  
  DiffTol = 0.0001;
  DiffNorm = +10000;
  Iter = 0;
  ###########While loop################
  while (DiffNorm > DiffTol)
  {
    Const <- sqrt(2*pi*Sigma2o)
    devmat <- (Y-X%*%Gama0)
    Squaremat <- as.matrix(apply(devmat, c(1,2), function(x) x^2))
    Gauss <- exp(-Squaremat/(2*as.numeric(Sigma2o)))/as.numeric(Const)
    Wbeta <- exp(-(Beta*((Y-X%*%Gama0)*(Y-X%*%Gama0)))/(2*as.numeric(Sigma2o)))
    ONE1 <- rep(1,dim(X)[2]);
    Xb <- (X*(Wbeta%*%ONE1)) 
    Gama <- as.numeric(pinv(crossprod(X, Xb))%*%crossprod(Xb, Y)) 
    hedprod <- (Y-X%*%Gama)*(Y-X%*%Gama) 
    tWbeta <- as.matrix(t(Wbeta)) 
    One_1 <- as.matrix(rep(1,dim(X)[1])) 
    Sigma2 <- (tWbeta%*%hedprod)*pinv(tWbeta%*%One_1)
    
    LHb<-(sum(Gauss^Beta)/n-1)/Beta
    LH<-prod(Gauss)
    ##########
    Norm2 <- ((sum(Gama*Gama))^0.5 + abs(Sigma2))
    DiffNorm <-((sum((Gama-Gama0)*(Gama-Gama0)))^0.5 + abs(Sigma2 - Sigma2o))/Norm2
    ###
    Gama0 = Gama
    Sigma2o=Sigma2
    Iter = Iter + 1 
  }
  return(list(Gama=Gama,Sigma2=Sigma2,Wt=Wbeta,LHb=LHb,LH=LH))
}
# independent variable dataset
test_A <- data.frame(matrix(sample(c(0,1,2), 500, replace = TRUE), nr=10, nc=50))
# dependent variable dataset
test_B <- data.frame(matrix(rnorm(1000), nr=10, nc=100))
# 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 by proposed method and lm 
test_function <- function(x, y){
  c1 <- test_A[[x]]
  c2 <- test_B[[y]]
  Data <- data.frame(1, XX=c1, YY=c2)
  nn <- dim(Data)[1]
  Beta = 0.1
  Omit = 2
  ResL1 <- Proposed_method(Data, Beta)
  ResL0 <- Proposed_method(as.matrix(Data[,-Omit]), Beta)
  LR0 <- (-nn)*log(ResL1$Sigma2/ResL0$Sigma2)
  
  # Proposed estimator
  Proposed_estimator <- (ResL1$Gama)[2]
  Proposed_pvalue <- as.numeric(pchisq(q=LR0, df=1, lower.tail = FALSE))
  
  #lm model
  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, Proposed_estimator,Proposed_pvalue)))
}

# Output:
output <- mapply(test_function, x = A_B_pair$c1, y = A_B_pair$c2)
# transpose the output
output_t <- data.frame(t(output))

# Final output
output_final <- cbind(A_B_pair, output_t)
output_final <- structure(list(c1 = c("X1", "X2", "X3", "X4", "X5"), c2 = c("X1", 
"X1", "X1", "X1", "X1"), lm.estimator = c(-0.855708052636761, 
0.227250280548332, -0.128955946232531, 0.171650221327542, -0.701027831473379
), lm.pvalue = c(0.0361141129937136, 0.646905371365762, 0.816730073250761, 
0.780290676037238, 0.261013977519426), Proposed_estimator = c(-0.879232513006948, 
0.242368232504351, -0.110999951753211, 0.174574390311335, -0.76456493319124
), Proposed_pvalue = c(0.0131801103443272, 0.583155149115837, 
0.870570103632653, 0.783460676404866, 0.154142429946211)), row.names = c(NA, 
5L), class = "data.frame"))

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

【问题讨论】:

  • test_Atest_B 应该有相同的行数,对吧?
  • @F. Privé,是的,test_A 和 test_B 数据集的行号应该相同
  • 我仍然认为您这里没有内存问题。您始终可以将test_Atest_B 转换为一些FBM,这样您在使用并行化时就不需要复制数据。但除此之外,您应该尝试优化您的代码,使其运行得更快。
  • 我尝试使用 big_apply 而不是 mapply,但它不起作用。你能帮我优化我的代码并为我的函数应用 bigstatsr 以获得所需的输出吗?谢谢你的努力。@F.Privé
  • 这里不需要big_apply,直接遍历两个数据集的变量即可。抱歉,我没有时间进行基准测试并让您的代码更快。

标签: r parallel-processing bigdata mapply bigstatsr


【解决方案1】:

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

我认为您只是想做一些单变量测试。为此,您可以使用函数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)

这个函数应该很快,并为您提供test_B 中所有变量的所有系数。那么你只需要循环遍历test_A中的变量即可。

【讨论】:

  • 感谢您的回复@F。私人的。我想使用我自己的函数而不是 lm 函数来估计参数和 p 值。另外,我想向输出数据集添加两列。一个是test_A的名称,另一个是test_B数据集的名称。
  • 输出将是这样的:output_t &lt;- data.frame(t(output)); output_final &lt;- cbind(A_B_pair, output_t); final_output &lt;- structure(list(c1 = c("X1", "X2", "X3", "X4", "X5"), c2 = c("X1", "X1", "X1", "X1", "X1"), lm.estimator = c(-0.0422342260166708, -0.0187980183564189, 0.192428884676606, -0.0257373964876148, 0.0673635213446617), lm.pvalue = c(0.701851660233888, 0.876046574990813, 0.0962188742808562, 0.817364911991616, 0.54800706638316)), row.names = c(NA, 5L), class = "data.frame")
  • 请编辑您的问题,使其更接近您当时真正想做的事情。
  • 亲爱的@F。 Privé,我在我的问题中添加了一个名为“编辑”的部分。请立即查看此内容并提出您的建议,以通过应用 bigstatsr 来调整代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
相关资源
最近更新 更多