我正在寻求有关此示例的帮助。它可能与pbapply
包无关,而是与mclapply()
. 但是,也许有人知道这里发生了什么。
问题
唯一的区别是使用pblapply()
/mclapply()
而不是lapply()
(我在 macOS 上)。在此示例中,该pblapply()
方法返回NULL
,而lapply()
尝试工作正常。
有趣的一点是,它的行为只与我使用lda()
. glm()
其他使用或其他建模方法的示例工作正常。
似乎并行应用调用甚至没有从这里开始(基于它完成的速度)。
sperrorest()
是一个通用框架(因此这里不应该是问题),我很困惑为什么该示例使用顺序方法(lapply()
)而不是并行方法(pblapply()
)。
此外,foreach()
使用 apply* 函数而不是使用相同的方法可以正常工作。因此,我假设它与lda()
并行 apply* 调用无关且相关。
笔记:
该示例在两个内核上运行,因此它应该在任何机器上运行。
首先,请做devtools::install_github("pat-s/sperrorest@mclapply-vs-lapply")
。
代码(可重现)
library(MASS)
library(sperrorest)
library(parallel)
library(pbapply)
currentSample <- partition.cv(maipo, nfold = 4)
currentSample[[2]] <- partition.cv(maipo, nfold = 4)[[1]]
currentRes <- currentSample
lda.predfun <- function(object, newdata, fac = NULL) {
library(nnet)
majority <- function(x) {
levels(x)[which.is.max(table(x))]
}
majority.filter <- function(x, fac) {
for (lev in levels(fac)) {
x[ fac == lev ] <- majority(x[ fac == lev ])
}
x
}
pred <- predict(object, newdata = newdata)$class
if (!is.null(fac)) pred <- majority.filter(pred, newdata[,fac])
return(pred)
}
data("maipo", package = "sperrorest")
predictors <- colnames(maipo)[5:ncol(maipo)]
fo <- as.formula(paste("croptype ~", paste(predictors, collapse = "+")))
# pblapply attempt (not working)
runreps_res <- pblapply(cl = 2, currentSample, function(X)
runreps(currentSample = X, data = maipo,
formula = fo, par.mode = 1, pred.fun = lda.predfun,
do.try = FALSE, model.fun = lda,
error.fold = TRUE, error.rep = TRUE, do.gc = 1,
err.train = TRUE, importance = FALSE, currentRes = currentRes,
pred.args = list(fac = "field"), response = "croptype", par.cl = 2,
coords = c("x", "y"), progress = 1, pooled.obs.train = c(),
pooled.obs.test = c(), err.fun = err.default))
# mclapply attempt (not working)
runreps_res <- mclapply(mc.cores = 2, currentSample, function(X)
runreps(currentSample = X, data = maipo,
formula = fo, par.mode = 1, pred.fun = lda.predfun,
do.try = FALSE, model.fun = lda,
error.fold = TRUE, error.rep = TRUE, do.gc = 1,
err.train = TRUE, importance = FALSE, currentRes = currentRes,
pred.args = list(fac = "field"), response = "croptype", par.cl = 2,
coords = c("x", "y"), progress = 1, pooled.obs.train = c(),
pooled.obs.test = c(), err.fun = err.default))
# lapply attempt (working)
runreps_res <- lapply(currentSample, function(X)
runreps(currentSample = X, data = maipo,
formula = fo, par.mode = 1, pred.fun = lda.predfun,
do.try = FALSE, model.fun = lda,
error.fold = TRUE, error.rep = TRUE, do.gc = 1,
err.train = TRUE, importance = FALSE, currentRes = currentRes,
pred.args = list(fac = "field"), response = "croptype", par.cl = 2,
coords = c("x", "y"), progress = 1, pooled.obs.train = c(),
pooled.obs.test = c(), err.fun = err.default))