Attempting to apply raster::predict to multiple raster stacks with BRT model and parallel processing

100 Views Asked by At

I was originally trying to use raster::predict() to generate a prediction raster from a BRT model and a single raster stack containing all of my predictor variables. However, this process was on track to take months to finish due to the size and resolution of my raster data. In an attempt to fast-track this process, I split my raster stack into eight smaller raster stacks, which are now compiled in a list. I am now trying to using parallel processing to apply the raster::predict() function to all eight raster stacks at once using parallel processing (one raster stack per core, as my computer has eight cores). If I can get this to work, then my plan is to simply mosaic the eight output rasters into a larger prediction raster layer. However, right now, I am getting an error message stating that the 'predict' function cannot handle 'gbm' objects, which is false, based on past experience.

Here is some example code, with fake data:

####Load in required packages####
library(gbm)
library(dplyr)
library(raster)
library(foreach)
library(doParallel)

####Generate fake data and BRT model####
df <- data.frame(w=c(1:50))
df <- df %>% mutate(x=rnorm(50,0,1))
df <- df %>% mutate(y=w*x*runif(50))
seep.BRT1 <- gbm.step(data=df,gbm.x=c(1:2),gbm.y=3, max.trees=20000, family = "gaussian", 
                      tree.complexity = 2, learning.rate = 0.005, bag.fraction = 0.75)

####Create fake rasters####
r1 <- raster(nrows=10,ncols=10)
r1 <- setValues(r1, seq(min(df$w),max(df$w),length.out=100))
r2 <- raster(nrows=10,ncols=10)
r2 <- setValues(r2, seq(min(df$x),max(df$x),length.out=100))
rstack <- stack(r1,r2)

####Create list of eight raster stacks####
splitrasters <- list(rstack,rstack,rstack,rstack,rstack,rstack,rstack,rstack)

####Attempt parallel processing with raster::predict()####
#Number of cores to be used
num_cores <- 8

#Set up parallel backend
cl <- makeCluster(num_cores)
registerDoParallel(cl)

#Function to apply raster::predict() to each raster stack
apply_predict <- function(raster_stack) {
  raster::predict(model=seep.BRT1, object=raster_stack,type="response",progress="text")
}

#Apply predict::raster to each raster stack using parallel processing
results <- foreach(ras = splitrasters, .combine = "c") %dopar% {
  apply_predict(ras)
}

#Stop the parallel backend
stopCluster(cl)

Unfortunately, I get the following error message when using the foreach() function:

Error in { : task 1 failed - "no applicable method for 'predict' applied to an object of class "gbm""

This is very confusing to me, because raster::predict() can usually handle 'gbm' objects with no issue. Also, please note that although I used the "foreach" and "doParallel" packages to assist with parallel processing, I am also very open to solutions that do not use these packages. Thanks!

0

There are 0 best solutions below