Hello, I am new to R Studio. Here is my problem.
I have classified a raster image using the random forests classifier. I had a set of 19 tif images as a predictor variables. I have made the sampling in qgis 2.18. After I finish the classification I was trying to export the results in a tif format. But I am getting this error "Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘writeRaster’ for signature ‘"character", "character"’’.
Here is the code:
setwd("C:/Users/nikos/Desktop/test11")
library(randomForest)
library(raster)
library(rgdal)
read samples data
pointssamples=readOGR("C:/Users/nikos/Desktop/test11", "pst11")
factor tis katigorikes metavlites
pointssamples$Classes=as.factor(pointssamples$Classes)
pointssamples=as.data.frame(pointssamples)
#remove na data#
pointssamples=pointssamples[,-c(2,15)]
set.seed(150)
#sep training & ground truthing#
sep=sample(2,nrow(pointssamples), replace = TRUE, prob = c(0.7, 0.3))
train=pointssamples[sep==1,]
gr_truth=pointssamples[sep==2,]
#random forests#
set.seed(160)
rf=randomForest(Classes~.,data=train)
print(rf)
##accuracy assessment##
library(caret)
prediction1=predict(rf,gr_truth)
library(e1071)
confusionMatrix(prediction1,gr_truth$Classes)
plot(rf, main="Rate of error")
##mtry
calibration=tuneRF(train[,-1], train[,1],stepFactor=0.5,ntreeTry=300,improve=0.1,plot=TRUE,trace=TRUE)
rf2=randomForest(Classes~.,data=train, ntree=300, mtry=8, importance=TRUE)
prediction2=predict(rf2,gr_truth)
confusionMatrix(prediction2,gr_truth$Classes)
varImpPlot(rf2, main="Variable importance")
#export classified map##
writeRaster("rf2",filename="rf2.tif",format="raster",overwrite-TRUE)
Thank you.