对nlminb使用parLapply

时间:2018-08-25 10:48:03

标签: r parallel-processing

尽管有几个问题让人无法接受,但我找不到当前任务的答案...

我尝试在B引导程序样本矩阵列表上使用parLapply。根据选择的转换(在或在一次差分内),我使用nlminb()来最大化对数似然。

问题,如果我运行代码,则内核无法在SFM.firstDiff内找到SFM.。 我的软件包fepsfrontieR中的SFM.within和SFM.firstDiff是复杂函数。

问题:如何提供功能集群?

请在代码下方找到。请原谅缺少的示例数据,因为我相信有经验的parLapply用户可以轻松回答此问题...

no_of_cores = detectCores()
cl = makeCluster(no_of_cores, type="PSOCK")


clusterExport(cl, c("myPar", "lowerInt", "Time", "N", "bootListMat", "mu", "optim", "K", "R", "method", "cumTime"))

if (method == "within"){
    bootEstimates <- parLapply (cl = cl, bootListMat, function(x) nlminb(lower = lowerInt,
                                                      start = myPar,
                                                      Time = Time,
                                                      N = N,
                                                      xv = as.matrix (x[, 2:(2+K-1)]),
                                                      y = as.matrix (x[, 1]),
                                                      z = as.matrix (x[, (2+K):cols]),
                                                      mu = mu,
                                                      optim = optim,
                                                      K = K, R = R,
                                                      objective = SFM.within,
                                                      cumTime = cumTime
                                                      )$par)  # we want only the estimates   } else {
    bootEstimates <- parLapply (cl = cl, bootListMat, function(x) nlminb(lower = lowerInt,
                                                             start = myPar,  # TBD by Rouven
                                                             Time = Time,
                                                             N = N,
                                                             xv = as.matrix (x[, 2:(2+K-1)]),
                                                             y = as.matrix (x[, 1]),
                                                             z = as.matrix (x[, (2+K):cols]),
                                                             mu = mu,
                                                             optim = optim,
                                                             K = K, R = R,
                                                             objective = SFM.firstDiff,
                                                             cumTime = cumTime
                                                             )$par)  # we want only the estimates
}
stopCluster(cl)

2 个答案:

答案 0 :(得分:1)

如果集群中“ workers”(后台R会话)评估的代码取决于您的软件包fepsfrontieR,那么您需要将该软件包附加到每个worker中(就像您在主R会话中所做的那样) 。因此,尝试:

clusterEvalQ(cl, library(fepsfrontieR))

答案 1 :(得分:0)

我认为您也必须将函数导出到集群