Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is a follow-up to a previous post: Machine Learning Algorithms for Land Classification.

It seems that the Random Forest (RF) classification method is gaining much momentum in the remote sensing world. I am particularly interested in RF due to many of its strengths:

  • A nonparametric approach suited to remote sensing data
  • High reported classification accuracy
  • Variable importance is reported

Given these strengths, I would like to perform Random Forest land classification using high resolution 4 band imagery. There is a lot of material and research touting the advantages of Random Forest, yet very little information exists on how to actually perform the classification analysis. I am familiar with RF regression using R and would prefer to use this environment to run the RF classification algorithm.

How do I collect, process and input training data (i.e. based on high resolution CIR aerial imagery) into the Random Forest algorithm using R? Any step-wise advice on how to produce a classified land cover raster would be greatly appreciated.

share|improve this question
I'm trying to understand this raster-classification, since I'm working on a RF-class (for species-distribution) with 10cm-resolution images. So I think I cannot input point shapes, but only raster. How am I able to proceed with *png or *tif -files? – steveomb Aug 17 '14 at 17:38
up vote 16 down vote accepted

I am not sure that I understand what you mean by "collect" data. If you are referring to heads-up digitizing and assignment of classes, this is best done in a GIS. There are many free options that would be suitable (i..e, QGIS, GRASS). Ideally you would have field data to train your classification.

The procedure for classification using Random Forests is fairly straight forward. You can read in your training data (i.e., a point shapefile) using "rgdal" or "maptools", read in your spectral data using raster::stack, assign the raster values to your training points using raster:extract and then pass this to randomForest. You will need to coerce your "class" column into a factor to have RF recognize the model as a classification instance. Once you have a fit model you can use the predict function, passing it you raster stack. You will need to pass the standard arguments to predict in addition to ones specific to the raster predict function. The raster package has the ability to handle rasters "out of memory" and as such is memory safe, even with very large rasters. One of the arguments in the raster predict function is "filename" allowing for a raster to written to disk. For a multiclass problem you will need to set type="response" and index=1 which will output an interger raster of you classes.

There are a few caveats that should be noted: 1) You cannot have more than 32 level in your response variable or any factor on the right side of the equation 2) your classes must be balanced. A 30% rule is a good one to follow, that is if you have more than 30% more observations on one class than any other you problem becomes imbalanced and the results can be based 3) it is a misnomer that RF cannot overfit. If you over correlate your ensemble you can overfit the model. A good way to avoid this is to run a preliminary model and plot the error stabilization. As a rule of thumb, I choose 2X the number of bootstraps required to stabilize the error for the ntree parameter. This is because variable interaction stabilizes at a slower rate than error. If you are not including many variables in the model you can be much more conservative with this parameter. 4) Do not use node purity as a measure of variable importance. It is not permuted like the mean decrease in accuracy.

I have code available for model selection and class imbalance in binary models on my website under the tools section.

Here is some simple code to get you started.


rlist=list.files(getwd(), pattern="img$", full.names=TRUE) 

xvars <- stack(rlist)      

sdata <- readOGR(dsn=getwd() layer=inshape)

v <-, sdata))
  sdata@data = data.frame(sdata@data, v[match(rownames(sdata@data), rownames(v)),])

rf.mdl <- randomForest(x=sdata@data[,3:ncol(sdata@data)], y=as.factor(sdata@data[,"train"]),
                       ntree=501, importance=TRUE)


# PLOT mean decrease in accuracy VARIABLE IMPORTANCE
varImpPlot(rf.mdl, type=1)

predict(xvars, rf.mdl, filename="RfClassPred.img", type="response", 
        index=1, na.rm=TRUE, progress="window", overwrite=TRUE)
share|improve this answer
I have been seeing fairly good results using RF and predict() to identify canopy cover. However, I cannot seem to produce better results than with the ISODATA algorithm. I suspect my training samples are biased, or there is too much spectral overlap. Is there an unsupervised implementation of RF that may produce better results? Is it possible to assign the number of classes to the output, as you would the ISODATA algorithm? – Aaron Nov 10 '12 at 22:09
@Aaron, it is possible to run an unlabeled (unsupervised) RF but the results are difficult to deal with. I would suggest looking at the RF imputation method available in the YaImpute package. This may deal with some of the bias/imbalance issues that you are encountering. – Jeffrey Evans Nov 12 '12 at 18:55

Here is a tutorial on supervised classification / regression with R, which includes a RandomForest example. The context is modeling ranges of single species, but the difference with using multiple classes is small.

share|improve this answer

I know that this thread is a little old, but for anyone wanting to try classification of remote sensing data in R, a very promising new package has been released.


It comes with functions for both unsupervised and supervised classification (using random forests). More information can be found here -

share|improve this answer
This looks very promising, thanks for your work on this package. One clarification, are you specifying random forests through caret? If so, you should make this VERY clear in the documentation. The caret package uses a different node splitting statistic than the original Breiman (2001) definition. The statistic follows Strobl et al., (2007) and where the proposed statistic may be valid I have never bought her simulation conditions. Additionally, the observed bias is based on variation in factorial levels. I prefer the original entropy statistic and as such do not use caret. – Jeffrey Evans Dec 4 '15 at 17:05
Hi Jeffrey, afraid that I'm not the author of the package. His details can be found at the link I provided. – JPD Dec 8 '15 at 10:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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