This is a simple and reproducible example with meuse
dataset of gstat
package based on @Spacedman answer and using the parallel
package:
More info and help here: Parallelizing and clustering in R
# Load libraries
library('sp')
library('gstat')
library('parallel')
# Load example data: meuse dataset
data("meuse") # data
data("meuse.grid") # 40m x 40m prediction grid for meuse dataset
# Create SpatialPointsDataFrame from meuse
coordinates(meuse) = ~x+y
# Create SpatialPixelsDataFrame from meuse.grid
gridded(meuse.grid) = ~x+y
# Create Spheric Variogram model with predefined parameters (psill, range and nugget)
m <- vgm(psill = 0.59, model = "Sph", range = 874, nugget = 0.04)
# Ordinary kriging (one core processor)
system.time(x <- krige(formula = log(zinc)~1, locations = meuse, newdata = meuse.grid, model = m))
# Plot x
spplot(x["var1.pred"], main = "ordinary kriging predictions")
# Ordinary kriging (parallel processing)
# Calculate the number of cores
no_cores <- detectCores() - 1
# Initiate cluster (after loading all the necessary object to R environment: meuse, meuse.grid, m)
cl <- makeCluster(no_cores)
parts <- split(x = 1:length(meuse.grid), f = 1:no_cores)
clusterExport(cl = cl, varlist = c("meuse", "meuse.grid", "m", "parts"), envir = .GlobalEnv)
clusterEvalQ(cl = cl, expr = c(library('sp'), library('gstat')))
system.time(parallelX <- parLapply(cl = cl, X = 1:no_cores, fun = function(x) krige(formula = log(zinc)~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)
# Plot mergeParallelX
spplot(mergeParallelX["var1.pred"], main = "ordinary kriging predictions")
Note:
- I tested the code above with my own data and in a total of 6364 data locations, 3429 newdata prediction positions and 7 core processors the amount of time of the parallel process of Kriging was half the non-parallel process!