Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
    
There are two related posts: gis.stackexchange.com/q/36801/8104 & gis.stackexchange.com/q/39021/8104 –  Aaron Apr 11 '13 at 19:31
add comment

marked as duplicate by Aaron, Fezter, Get Spatial, Aragon, R.K. Apr 12 '13 at 8:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

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")
share|improve this answer
add comment

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