im试图通过使用并行处理(parlapply)进行10倍的交叉验证并估算关节模型的模型性能。我试图找出为什么我会收到错误消息:" 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)
函数本身被接受,但是当运行群集命令时,我会遇到此错误,该错误提到它无法识别函数中给出的对象。.是否在函数本身中定义它们?还是我不正确使用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:)
已经在堆栈溢出上回答了非常相关的问题。基本上,您有三个解决方案:
- 使用
clusterExport()
将您需要的变量导出到群集(最常见的方法) - 将所有变量作为您函数
CrossValJM()
的参数传递,以便将它们自动导出到群集(我喜欢的解决方案,最适合编程性正确的解决方案) - 使用r软件包{future},它应该自动检测到导出的变量(懒惰解决方案,但似乎也很好地工作)
例如,请参见此。