I am using Maxent in R for performing species distribution modeling. My problem is that raster::predict() is very slow because of the resolution and extent of my environmental data. Is there a way to parallel process the prediction step to speed this up? At the current time it is running for days and this creates problems.

2 Answers 2


You could split your raster brick/stack along the y-axis and pass the subsets to different CPUs using foreach:

#load raster brick
filename = "predictors"
predictors = raster::brick(filename)

#register parallel computing backend
cl = parallel::makeCluster(ncores)

#compute indices for data splitting
rows = 1:nrow(predictors)
split = sort(rows%%ncores)+1

outname = "prediction"

#perform the prediction on subsets of the predictor dataset
prediction = foreach(i=unique(split), .combine=c)%dopar%{
  rows_sub = rows[split==i]
  sub = raster::crop(predictors,raster::extent(predictors, min(rows_sub), max(rows_sub), 1, ncol(predictors)))
  raster::predict(sub, model, filename=paste(outname, i, sep="_"))

This will write different files with a suffix "_1" to "_{ncores}" which can then easily mosaicked back together along the y-axis.

  • Thanks. This worked perfectly. However, it does eat up a lot of hard drive space and that is causing me some problems. I am going to ask a new question related to the temporary directory for raster().
    – user44796
    Aug 17, 2016 at 13:23

using clusterR from the raster package:

preds_rf<- clusterR(rast, raster::predict, args = list(model = model))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.