R foreach根本不循环

R foreach根本不循环,r,domc,parallel-foreach,R,Domc,Parallel Foreach,在Linux x64上使用包doMC尝试组合foreach语句的输出时遇到一些问题: # I'm running 33 different models, one for each label library(doMC) registerDoMC(2) labels = paste0('y', 1:33) dataset = get.Data() test.data = get.Test() pred = foreach(yi = labels, .combine = cbind) %dop

在Linux x64上使用包doMC尝试组合foreach语句的输出时遇到一些问题:

# I'm running 33 different models, one for each label
library(doMC)
registerDoMC(2)

labels = paste0('y', 1:33)
dataset = get.Data()
test.data = get.Test()

pred = foreach(yi = labels, .combine = cbind) %dopar% {

    # handle special case, don't need a model here
    if (yi == 'y14')
        y.pred = 0
    else {
        # fit model
        fit  = glm(y ~ ., data = data.frame(y = dataset[, yi], dataset[, -1]))
        # get predicted probabilities
        y.pred = predict(fit, test.data, type = 'response')
    }
    y.pred
}

# This gives me an error, saying pred is not a matrix
colnames(pred) = labels

`Error in `colnames<-`(`*tmp*`, value = c("y1", "y2", "y3", "y4", "y5",  : 
attempt to set 'colnames' on an object with less than two dimensions`
PS:doSNOW给出了同样的错误!
谢谢你的澄清

当yi为y14时,返回的是0,这可能比yi不是y14时返回的长度要短。试试rep0,nrowtest.data我希望这就是问题所在,但事实并非如此。在这种情况下,yi变量永远不会达到y14。在这种情况下,0将被回收。它达到了什么程度?[实际上你是对的,0应该自动重复,我只是自己测试了一下。]。你为什么不尝试不合并结果呢?即返回一个列表,然后查看返回的列表,看看其中是否有任何有趣的元素。你还注意到,在foreach body中似乎根本没有使用yi,只是在if语句中。。是吗?让我根据你的提示做一些测试,谢谢!彝语被使用、编辑。
library(doMC)
registerDoMC(2)
set.seed(1)
M = foreach(i = 1:4, .combine = cbind) %dopar% {runif(3)}
print(M)

      result.1  result.2   result.3   result.4
[1,] 0.1370338 0.1556492 0.12846072 0.24629143
[2,] 0.3940809 0.4537018 0.69545139 0.02654378
[3,] 0.6287515 0.2379316 0.02993252 0.17504858