I am trying to run gls models with a specific spatial correlation structure that comes from modifying the nlme package/ building new functions in the global environment from this post (the answer from this post that creates new functions which allows for the implementation of the correlation structure). Unfortunately I cannot get this spatial correlation structure to work when I run this through a foreach loop:
#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)
When I run this I get the error message:
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
The model works fine with the added correlation structure :
correlation = corHaversine(form=~lon+lat,mimic="corSpher")
in a normal loop. Any help would be appreciated!