This problem only happens on macOS with a normal R startup. Using R in Vanilla mode solves it.
Up to now its unclear what exactly causes this behaviour (http://stackoverflow.com/questions/44058387/r-mclapply-pblapply-vs-lapply-use-case).
It also works fine on Linux. Anyway, to be able to deal with the NULL
output and return an informative error message, it would be important that pbmclapply()
does not run infinite.
library(MASS)
library(sperrorest)
library(pbmcapply)
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 = "+")))
# pbmclapply
runreps_res <- pbmclapply(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
runreps_res <- mclapply(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))