I've been trying to run boosted regression tree modelling on spatial data using the caret package in R. My predictor variables were all extracted from raster files on the environment, fx. soil type and landcover. Since these two latter variables are actually factors (but the codes are numeric), I have been creating dummy variables for them before I run the train function.

This all works well, except when I want to predict to larger areas. When predicting I am using a rasterstack with all my predictor rasters, but of course I do not have rasters for all the dummy variables. Do I really need to create dummy rasters as well, or is there a way around this?

I have tried running without dummy variables, but specifying these predictors as facors - the train function then seems to automatically convert these to dummy variables, but I again run into problems when predicting with my raster stack.

I have tried running regular boosted regression tree models using gbm alone (gbm.step), and I can easily get the predictions to work using rasterstack - but I would like to use the caret train function, so I can run some K-fold cross validation.

I hope someone can help me. Thanks, Lene Jung Kjær


1 Answer 1


I have experience with GBM using caret. I found that I can feed the factor variables without encoding to the caret't train with GBM, but when I analyzed the structure of the produced trees I found that inside the function my factor variables were one-hot encoded automatically.

Thus using the trained model for prediction, without making the encoding, always failed for this reason.

  • $\begingroup$ Yeah, that is what I figured, thanks for your answer. So you think, there is no way around making dummy rasters? $\endgroup$ Commented Oct 3, 2017 at 10:43
  • $\begingroup$ If your factors can be treated as Ordinal-Scale variables (or better Interval-Scale, like 1: 1-5; 2: 6-10, etc.) you can cast them into integers before training on the whole dataset. Otherwise I do not see a workaround rather than creating the dummies before training. $\endgroup$ Commented Oct 3, 2017 at 10:46
  • $\begingroup$ They are already integers, as they are integer codes for particular cover types and soil types. $\endgroup$ Commented Oct 3, 2017 at 10:50
  • $\begingroup$ Soil types are not on Ordinal Scale (you can't say Type 1 is less than Type 2, can you?), so the data type Integer cannot substitute the Scale you need to do a regression type task. In other words, you cannot make Factor logic variables to be Integer logic and feed it into a machine. $\endgroup$ Commented Oct 3, 2017 at 11:00
  • 1
    $\begingroup$ HI again, sorry for the delay in answering. Thank you very much for your suggestion, I did manage to get it to run, having caret train and gbm create the dummy variables. Then I checked my dataset for the values of Soil type and landcover and created rasters with just those values - then I was able to predict for a larger area :-) $\endgroup$ Commented Oct 6, 2017 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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