I want perform randomforest classification on Landsat data. Here is some code I found from a paper. But when I run into the "classification" part, there is an error said "Error in eval(expr, envir,enclos): object not found t.1". How to solve this error?
###Invoking the R packages
library(sp)
library(maptools)
library(randomForest)
library(raster)
library(rgdal)
###Reading the .txt files of each band and the training data
train = scan('E:/toronto/2009/txt/roi.txt')
b1 = scan('E:/toronto/2009/txt/2009b1.txt')
b2 = scan('E:/toronto/2009/txt/2009b2.txt')
b3 = scan('E:/toronto/2009/txt/2009b3.txt')
b4 = scan('E:/toronto/2009/txt/2009b4.txt')
b5 = scan('E:/toronto/2009/txt/2009b5.txt')
b7 = scan('E:/toronto/2009/txt/2009b7.txt')
###Store all layers into one dataframe
data_all = data.frame(class = train, t.1 = b1, t.2 = b2, t.3 = b3, t.4 = b4, t.5 = b5, t.6 = b7)
###Eliminate all data with value of 0
data <- data_all[data_all$class != 0, ]
data$class <- factor(data$class)
###Training using random forest
rf <- randomForest(class~., data = data, ntree = 500, mtry = 3, importance = T, proximity = T)
importance(rf)
print(rf)
varImpPlot(rf)
###Read the image and store the classification data as .tif file
satImage <- stack('E:/toronto/2009/2009.tif')
outImage <- 'E:/toronto/2009/2009_RF.tif'
###Classification
predict(satImage, rf, filename=outImage, progress='text', format='GTiff', datatype='INT1U', type='response', overwrite=TRUE)