【问题标题】:parallel computing in R , implementing bootstrapR中的并行计算,实现引导
【发布时间】:2015-11-14 09:58:06
【问题描述】:

我目前正在尝试使用 BLB bootstrap 计算模型估计器,并且希望并行进行。当我不并行执行时,我的代码可以正常工作。当我并行计算时的问题是我从每个核心得到的结果都包含 NA 值。我不明白如何获得 NA 值,而 Iris 数据集的值根本不包含 NA。 这是我正在使用的代码:

library(doParallel)
library(itertools)

 num_of_cores <- detectCores()
 cl <- makePSOCKcluster(num_of_cores)
 registerDoParallel(cl)

 attach(iris)
 data <- iris
 coeftmp <- data.frame()
 system.time(
 r <- foreach(dat = isplitRows(data, chunks=num_of_cores),
             .combine = cbind) %dopar% {

                 BLBsize = round(nrow(dat)^0.6)
                 for (i in 1:400){
                         set.seed(i)

                         # sampling B(n) data points from the original data set without replacement
                         sample_BOFN <- dat[sample(nrow(dat), size = BLBsize, replace = FALSE), ]

                          # sampling from the subsample with replacment
                         sample_bootstrap <- sample_BOFN[sample(nrow(sample_BOFN), size = nrow(sample_BOFN), replace = TRUE), ]

                         bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)
                         coeftmp <- rbind(coeftmp, bootstrapModel$coefficients)

                 }
                 #calculating the estimators of the model with mean
                  colMeans(coeftmp)

         })

【问题讨论】:

  • 由于我不知道你有多少个核心,我不确定这个问题是否能解决你的问题。但它可能:stackoverflow.com/questions/33221779
  • 另外,我不清楚如果你是引导程序,你为什么对sample_BOFN 进行采样而不用替换。但是您似乎也没有使用sample_BOFN,因此您可能希望从(示例)代码中删除它。
  • 我正在尝试实现 BLB 引导程序,该引导程序需要从子样本中采样而无需替换。所以这就是为什么。
  • 实际上对该链接的引用并没有帮助,因为我有 4 个内核,并且我正在使用迭代器将我的数据集分成 4 个块。我想使用 BLB 引导程序在每个核心上训练模型。我不明白我怎么可能得到 NA 值? (顺便说一句,我在 MAC 上运行代码)
  • 如果你只使用 1 个核心,你会得到NAs 吗?

标签: r parallel-processing cluster-computing parallel-foreach


【解决方案1】:

我认为您将不得不通过调试器的几次迭代来解决它。但是你从这条线得到NAs

bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)

我猜你会从你的sample_bootstraps 中得到一个奇点,因为奇点会给你一个 NA 系数。但也有可能是其他原因导致了这个错误,虽然它肯定来自这行代码......你需要逐步调试调试器来隔离它。

...也就是说,这不是一个完整的答案。但这应该可以让您解决自己的问题:

您可以通过调查看到这一点:

r2 <- foreach(dat = isplitRows(data, chunks=1)) %dopar% {

     BLBsize = round(nrow(dat)^0.6)
     for (i in 1:400){
       set.seed(i)

       # sampling B(n) data points from the original data set without replacement
       sample_BOFN <- dat[sample(nrow(dat), size = BLBsize, replace = FALSE), ]

       # sampling from the subsample with replacment
       sample_bootstrap <- sample_BOFN[sample(nrow(sample_BOFN), size = nrow(sample_BOFN), replace = TRUE), ]

       bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)
       coeftmp <- rbind(coeftmp, bootstrapModel$coefficients)

     }
     #calculating the estimators of the model with mean
     # return a list, not just the colMeans -- for debugging purposes
     return(list(coeftmp= coeftmp, result= colMeans(coeftmp)))

   }

   sum(is.na(r2[[1]][[1]])) # no missing coefficients with 1 core

r <- foreach(dat = isplitRows(data, chunks=num_of_cores)) %dopar% {

     BLBsize = round(nrow(dat)^0.6)
     for (i in 1:400){
       set.seed(i)

       # sampling B(n) data points from the original data set without replacement
       sample_BOFN <- dat[sample(nrow(dat), size = BLBsize, replace = FALSE), ]

       # sampling from the subsample with replacment
       sample_bootstrap <- sample_BOFN[sample(nrow(sample_BOFN), size = nrow(sample_BOFN), replace = TRUE), ]

       bootstrapModel <- glm(sample_bootstrap$Petal.Width ~ Petal.Length + Sepal.Length + Sepal.Width, data = sample_bootstrap)
       coeftmp <- rbind(coeftmp, bootstrapModel$coefficients)

     }
     #calculating the estimators of the model with mean
     # return a list, not just the colMeans -- for debugging purposes
     return(list(coeftmp= coeftmp, result= colMeans(coeftmp)))

   }

 # lots of missing values in your coeftmp results.
 lapply(r, function(l) {sum(is.na(l[[1]]))}) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    相关资源
    最近更新 更多