检查远程错误(val)时出错:5个节点产生错误:找不到对象

时间:2017-11-21 23:21:43

标签: r debugging parallel-processing lapply snow

我尝试进行10倍交叉验证,并使用并行处理(parLapply)估算联合模型的模型性能。我试图找出我收到错误消息的原因: “checkForRemoteErrors(val)出错:五个节点产生错误:对象'周'未找到”

代码如下:

# Validation using 10-fold CV
    library("parallel")
    set.seed(123)
    V <- 10
    n <- nrow(dfC)
    splits <- split(seq_len(n), sample(rep(seq_len(V), length.out = n)))
    CrossValJM <- function (i) {
        library("JM")
        library("nlme")
        trainingData <- dfL[!dfL$ID %in% i, ]
        trainingData_ID <- trainingData[!duplicated(trainingData$ID), ]
        testingData <- dfL[dfL$ID %in% i, ]

        lmeFit <- lme(DA ~ ns(Week, 2), data = trainingData,
                           random = ~ ns(Week, 2) | ID)
        coxFit <- coxph(Surv(TT_event, Event) ~ Gender * Age, data = 
                           trainingData_ID, 
                             x = TRUE)

        jointFit <- jointModel(lmeFit, coxFit, timeVar = "Week")

        pe <- prederrJM(jointFit, newdata = testingData, Tstart = 10, 
                                                  Thoriz = 20)
        auc <- aucJM(jointFit, newdata = testingData, Tstart = 10, 
                                                  Thoriz = 20)
        list(pe = pe, auc = auc)
    }

    cl <- makeCluster(5)
    res <- parLapply(cl, splits, CrossValJM)
    stopCluster(cl)

函数本身被接受但是在运行Cluster命令时遇到这个错误,它提到它无法识别函数内给出的对象..它们是否应该在函数本身中定义?或者我没有正确使用parLapply函数?

P.S。:数据如下(dfL是长度为~1000且dfC~200的数据帧):

dfL <- data.frame(ID = c(1, 1, 1, 2, 2, 3), DA = c(0.4, 1.8, 1.2, 3.2, 3.6, 2.8), Week = c(0, 4, 16, 4, 20, 8), Event = c(1, 1, 1, 0, 0, 1), TT_Event = c(16, 20, 8), Gender = c(0, 0, 0, 1, 1, 0), Age = c(24, 24, 24, 56, 56, 76))

dfC <- data.frame(ID = c(1, 2, 3, 4, 5, 6), DA = c(1.2, 3.6, 2.8, 2.4, 1.9, 3.4), Week = c(16, 20, 8, 36, 24, 32), Event = c(1, 0, 1, 1, 1, 0), TT_Event = c(16, 20, 8, 36, 24, 32), Gender = c(0, 1, 0, 0, 1, 1), Age = c(24, 56, 76, 38, 44, 50))

Thnx:)

1 个答案:

答案 0 :(得分:2)

Stack Overflow已经回答了非常相关的问题。 基本上,您有三种解决方案:

  • 使用clusterExport将所需的变量导出到群集(最常用的方法)
  • 将所有变量作为函数CrossValJM的参数传递,以便它们自动导出到集群(我更喜欢的解决方案,编程最正确的解决方案)
  • 使用包future,它应该自动检测要导出的变量(懒惰的解决方案,但似乎也运行良好)

请参阅示例this