如何将自定义函数加载到 R 中的 foreach 循环中?

How can I load custom functions into foreach loop in R?

我正在尝试 运行 具有特定空间相关结构的 gls 模型,该结构来自于修改 nlme 包/在全球环境中构建新功能 post(答案来自 post 创建新函数,允许实现相关结构)。不幸的是,当我通过 foreach 循环 运行 时,我无法让这个空间相关结构工作:

#setup example data
data("mtcars")
mtcars$lon = runif(nrow(mtcars)) #include lon and lat for the new correlation structure
mtcars$lat = runif(nrow(mtcars))
mtcars$marker = c(rep(1, nrow(mtcars)/2), rep(2, nrow(mtcars)/2)) #values for iterations

#set up cluster
detectCores()
cl <- parallel::makeCluster(6, setup_strategy = "sequential")
doParallel::registerDoParallel(cl)

#run model
list_models<-foreach(i=1:2, .packages=c('nlme'), .combine = cbind,
                     .export=ls(.GlobalEnv)) %dopar% {
                    
                       .GlobalEnv$i <- i
                       
                       model_trial<-gls(disp ~ wt, 
                                             correlation = corHaversine(form=~lon+lat, 
                                                                        mimic="corSpher"),
                                             data = mtcars)
                     }


stopCluster(cl)

当我 运行 收到错误消息时:

Error in { : 
  task 1 failed - "do not know how to calculate correlation matrix of “corHaversine” object"
In addition: Warning message:
In e$fun(obj, substitute(ex), parent.frame(), e$data) :
  already exporting variable(s): corHaversine, mtcars, path_df1

该模型在添加的相关结构下运行良好:

correlation = corHaversine(form=~lon+lat,mimic="corSpher")

在正常循环中。如有任何帮助,我们将不胜感激!

我不确定为什么您的 foreach 方法不起作用,而且我也不确定您实际计算的是什么。无论如何,您可以使用 parallel::parLapply() 尝试这种似乎有效的替代方法:

首先,我使用 rm(list=ls()) 清除了工作区,然后我 运行 this answer 的整个第一个代码块,他们在其中创建 "corStruct" class 和 corHaversine 方法将其放入工作区以及下面的 Data,为 clusterExport().

做好准备
library(parallel)
cl <- makeCluster(detectCores() - 1)
clusterEvalQ(cl, library(nlme))
clusterExport(cl, ls())
r <- parLapply(cl=cl, X=1:2, fun=function(i) {
  gls(disp ~ wt, 
      correlation=corHaversine(form= ~ lon + lat, mimic="corSpher"),
      data=mtcars)
})
stopCluster(cl)  ## stop cluster
r  ## result
# [[1]]
# Generalized least squares fit by REML
# Model: disp ~ wt 
# Data: mtcars 
# Log-restricted-likelihood: -166.6083
# 
# Coefficients:
#   (Intercept)          wt 
# -122.4464    110.9652 
# 
# Correlation Structure: corHaversine
# Formula: ~lon + lat 
# Parameter estimate(s):
#   range 
# 10.24478 
# Degrees of freedom: 32 total; 30 residual
# Residual standard error: 58.19052 
# 
# [[2]]
# Generalized least squares fit by REML
# Model: disp ~ wt 
# Data: mtcars 
# Log-restricted-likelihood: -166.6083
# 
# Coefficients:
#   (Intercept)          wt 
# -122.4464    110.9652 
# 
# Correlation Structure: corHaversine
# Formula: ~lon + lat 
# Parameter estimate(s):
#   range 
# 10.24478 
# Degrees of freedom: 32 total; 30 residual
# Residual standard error: 58.19052 

数据:

set.seed(42)  ## for sake of reproducibility
mtcars <- within(mtcars, {
  lon <- runif(nrow(mtcars))
  lat <- runif(nrow(mtcars))
  marker <- c(rep(1, nrow(mtcars)/2), rep(2, nrow(mtcars)/2))
})