我目前正在尝试使用 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)
})