# How to perform unsupervised Random Forest classification using R?

I am working with the randomForest and predict packages in R for land classification. For each 4-band CIR image, I have created training data in a GIS, run the training data through a model to produce a .csv, which is then input into R's randomForest algorithm. I am finding the Random Forest supervised classification is seriously overclassifying areas. In fact, I am producing better results using the unsupervised ISODATA algorithm.

Is there a way to implement unsupervised classification with the randomForest package in R? I have attached a section of code I've been using to run supervised random forest classification.

myrf = randomForest(factor(category) ~ band1 + band2 + band3 + band4, data = intable,
ntree=2000,
importance = TRUE)

predict(rasters, myrf, filename="RFtest7.img", type="response",
index=1, na.rm=TRUE, progress="window", overwrite=TRUE)


## 2 Answers

Over classification may be caused by prediction bias, which is a problem for the canonical RF method and for which a number of modifications have been researched. Probably the principal approach to mitigating bias is to utilise randomised split thresholds, sometimes referred to as 'extreme' random forest. I'm not sure what flavour of RF is implemented in the R package, but certainly the problem will be more prominent when working with unbalanced classification data sets - by taking a majority vote the forest loses the information regarding the balance of votes, and that can and often will introduce bias to the classifications.

• Good call. Prediction bias is definitely present while I am creating the training sets--thus, the need for an unsupervised approach. – Aaron Nov 10 '12 at 1:55
• Basically classification introduces a problem - if class A has probability 51% then our best prediction is class A. Thus the balance of probabilities is lost in the predictions - they are biased towards class A. If we try and fix the bias by using a probabilistic prediction (predict class A 51% of the time, entirely randomly) then we increase prediction error. I don't see any way around that (but I'm no expert, merely an enthusiastic amateur!) – redcalx Nov 11 '12 at 2:35

In the randomforest function, instead of listing a y ~ x model, simply input your predictor matrix.

randomforest thinks you want to run a supervised classification because you are listing the classifying variable factor(category) as the y in your model.

• Thanks for the reply. Any pointers on how to input the predictor matrix? – Aaron Nov 10 '12 at 14:26
• Construct your matrix so that rows are observations and columns are predictor variables, and input this instead of your y ~ x model. You may want to check help(randomforest) to make sure you wouldn't have to transpose this (columns as observations and rows are variables), as I don't remember the format that randomforest likes to see. – Nick Adams Nov 12 '12 at 19:06