I have implemented Random Forest classifier to classify remote sensing data in R. The original code comes from here:
How to perform Random Forest land classification?
Everything works fine, but what I need is to obtain confusion matrix and out of the bag error for fast classification accuracy assessment. I have created junk dataset for validating functionality of the whole script itself (only 2 classes). I added parallel computing to speed up computations.
I am wondering if my implementation is correct and If I will be able to obtain confusion matrix and out of the bag error when I will add much more features describing classes later. Here is part of my code:
sdata <- readOGR(dsn = vyber_shp, layer = "trenink")
# extraction from raster to be classified
rdata <- data.frame(extract(r, sdata))
# create random forest model in parallel
cl <- makeCluster(detectCores())
registerDoParallel(cl)
rf.mdl <- foreach(ntree=rep(125, 4), .combine = combine, .packages = 'randomForest')
%dopar% {randomForest(x=rdata, y=sdata$Class, ntree=500,
proximity=TRUE, importance=TRUE, confusion=TRUE,
do.trace=TRUE, err.rate=TRUE)
}
stopCluster(cl)
# classify raster in parallel
beginCluster()
predikce <- clusterR(r, raster::predict, args=list(model=rf.mdl))
endCluster()
# write and save result of classification as raster image
klasifikace <- writeRaster(predikce, filename='Klasifikace_RandomForest',
format='HFA', options='INTERLEAVE=BSQ', datatype='INT2S', overwrite=TRUE)
varImpPlot(rf.mdl)
rf.mdl$confusion