I am trying to achieve parallel Kriging in R on several variables using a loop. Here is a reproducible example using data meuse and a code for parallel kriging that I found here. In the code below, each kriging is overwritten, but that's not the issue, since I can't even run the first kriging.
library(parallel)
library(sp)
library(gstat)
data(meuse)
names <- colnames(meuse)[3:6]
coordinates(meuse) = ~x+y
data(meuse.grid)
gridded(meuse.grid) = ~x+y
m <- vgm(.59, "Sph", 874, .04)
no_cores <- 7
# ordinary kriging:
for (i in 1:length(names)) {
parts <- split(x = 1:length(meuse.grid), f = 1:no_cores)
cl <- makeCluster(no_cores)
clusterExport(cl = cl, varlist = c("meuse", "meuse.grid", "m", "parts"), envir = .GlobalEnv)
clusterEvalQ(cl = cl, expr = c(library('sp'), library('gstat')))
parallelX <- parLapply(cl = cl, X = 1:no_cores, fun = function(x) krige(formula = log(get(names[i]))~1, locations = meuse, newdata = meuse.grid[parts[[x]],], model = m))
stopCluster(cl)
# Merge all the predictions
mergeParallelX <- maptools::spRbind(parallelX[[1]], parallelX[[2]])
mergeParallelX <- maptools::spRbind(mergeParallelX, parallelX[[3]])
mergeParallelX <- maptools::spRbind(mergeParallelX, parallelX[[4]])
mergeParallelX <- maptools::spRbind(mergeParallelX, parallelX[[5]])
mergeParallelX <- maptools::spRbind(mergeParallelX, parallelX[[6]])
mergeParallelX <- maptools::spRbind(mergeParallelX, parallelX[[7]])
# Create SpatialPixelsDataFrame from mergeParallelX
mergeParallelX <- SpatialPixelsDataFrame(points = mergeParallelX, data = mergeParallelX@data)
}
I keep having issues which are, I think, related to the dynamic formula. I tried a lot of other functions, such as paste
, paste0
, as.formula
, objects
, etc. Nothing works, I can't paste the variable names into looped parallel kriging, no matter how I code the dynamic formula. Maybe that's because of how the parallel function is coded?
Any idea about a dynamic formula that works with the above code?
n_cores
replications of kriging of each of the columns? Because your loop over the columns is outside the cluster creation and execution. This seems a bit off. Normally you'd throw all the replicated work inside the cluster and let the cluster'sn_cores
specification optimise the number of cores working. That then putsi
in side the cluster loop. Also as written you're only going to return the value from the last column's kriging.