I am a newbie in R. I want to classify images using random forest in R. I am trying to learn the codes. But in the mean time, is there a push button (like a gui) in R that can be used for classification. Looks model map package can do it, but i cannot figure out how?
-
There are two related posts: gis.stackexchange.com/q/36801/8104 & gis.stackexchange.com/q/39021/8104 – Aaron♦ Apr 11 '13 at 19:31
It is really is worth the time to learn the code interface. Here is some annotated code for specifying a simple Random Forest image classification using spatial data.
# Add required libraries
require(randomForest)
require(sp)
require(rgdal)
require(raster)
# SET WORKING DIRECTORY
setwd("D:/ANALYSIS/Kenya_Hirola/RandomForest")
# Read point shapefile with training observations ("CLASS" field contains data)
sdata <- readOGR(getwd(), "L5_2010_02_Train")
# Read spectral data (ERDAS img image stack)
r <- stack( paste(getwd(), "L5_2010_02caldrkbdy.img", sep="/") )
# EXTRACT RASTER DATA TO POINTS
sdata@data <- data.frame(sdata@data, extract(r, sdata))
# CREATE RF MODEL
( rf.mdl <- randomForest(x=sdata@data[,3:ncol(sdata@data)], y=sdata@data[,"CLASS"],
ntree=501, proximity=TRUE, importance=TRUE) )
# PREDICT SINGLE CLASSIFIED RASTER
predict(r, rf.mdl, filename="ClassPred.img", type="response", na.rm=TRUE,
overwrite=FALSE, progress="window")