简体   繁体   English

在 R 中的 Parlapply 内创建数据框

[英]Data frame creation inside Parlapply in R

I am trying something pretty simple, want to run a bunch of regressions parallelly.我正在尝试一些非常简单的事情,想要并行运行一堆回归。 When I use the following data generator (PART 1), The parallel part does not work and give the error listed below当我使用以下数据生成器(第 1 部分)时,并行部分不起作用并给出下面列出的错误

#PART 1
p <- 20; rho<-0.7;
cdc<- diag(p)
for( i in 1:(p-1) ){ for( j in (i+1):p ){
  cdc[i,j] <- cdc[j,i] <- rho^abs(i-j)
}}
my.data <- mvrnorm(n=100, mu = rep(0, p), Sigma = cdc)

The following Parallel Part does work but if I generate the data as PART 2以下并行部分确实有效,但如果我将数据生成为第 2 部分

# PART 2
my.data<-matrix(rnorm(1000,0,1),nrow=100,ncol=10)

I configured the function that I want to run parallelly... as我将要并行运行的 function 配置为...

parallel_fun<-function(obj,my.data){
  p1 <- nrow(cov(my.data));store.beta<-matrix(0,p1,length(obj))
  count<-1
  for (itration in obj) {
    my_df<-data.frame(my.data)
    colnames(my_df)[itration] <- "y"
    my.model<-bas.lm(y ~ ., data= my_df, alpha=3,
                     prior="ZS-null", force.heredity = FALSE, pivot = TRUE)
    cf<-coef(my.model, estimator="MPM") 
    betas<-cf$postmean[-1]
    store.beta[ -itration, count]<- betas
    count<-count+1
  }
  result<-list('Beta'=store.beta)
}

So I write the following way of running parlapply所以我写了以下运行parlapply的方式


{
  no_cores <- detectCores(logical = TRUE)  
  myclusternumber<-(no_cores-1)
  cl <- makeCluster(myclusternumber)  
  registerDoParallel(cl)
  p1 <- ncol(my.data)
  obj<-splitIndices(p1, myclusternumber) 
  clusterExport(cl,list('parallel_fun','my.data','obj'),envir=environment())
   clusterEvalQ(cl, {
    library(MASS)
    library(Matrix)
    library(BAS)
  })
  newresult<-parallel::parLapply(cl,obj,fun = parallel_fun,my.data)
  stopCluster(cl)
  
}

But whenever am doing PART 1 I get the following error但是每当我在做第 1 部分时,我都会收到以下错误

Error in checkForRemoteErrors(val): 7 nodes produced errors; checkForRemoteErrors(val) 中的错误:7 个节点产生错误; first error: object 'my_df' not found第一个错误:未找到 object 'my_df'

But this should not happen, the data frame should be created, I have no idea why this is happening.但这不应该发生,应该创建数据框,我不知道为什么会这样。 Any help is appreciated.任何帮助表示赞赏。

Posting this as one possible workaround, see if it works:将此作为一种可能的解决方法发布,看看它是否有效:

parallel_fun<-function(obj,my.data){
  p1 <- nrow(cov(my.data));store.beta<-matrix(0,p1,length(obj))
  count<-1
  for (itration in obj) {
    my_df<-data.frame(my.data)
    colnames(my_df)[itration] <- "y"
    my_df <<- my_df
    my.model<-bas.lm(y ~ ., data= my_df, alpha=3,
                     prior="ZS-null", force.heredity = FALSE, pivot = TRUE)
    cf<-BAS:::coef.bas(my.model, estimator="MPM") 
    betas<-cf$postmean[-1]
    store.beta[ -itration, count]<- betas
    count<-count+1
  }
  result<-list('Beta'=store.beta)
}

The issue seems to be with BAS:::coef.bas function, that calls eval in order to get my_df and fails to do that when called in parallel.问题似乎出在BAS:::coef.bas function 上,它调用eval以获取my_df并且在并行调用时无法做到这一点。 The "hack" here is to force my_df out to the parent environment by calling my_df <<- my_df .这里的“hack”是通过调用my_df <<- my_df来强制my_df进入父环境。

There should be a better way to do this, but <<- might be the fastest one.应该有更好的方法来做到这一点,但<<-可能是最快的方法。 In general, <<- may cause unwanted behaviour, especially when used in loops.通常, <<-可能会导致不需要的行为,尤其是在循环中使用时。 Assigning unique variable name before exporting (and don't forgetting to remove after use) is one way to tackle them.在导出之前分配唯一的变量名(并且不要忘记在使用后删除)是解决它们的一种方法。

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

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