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 
###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) 
###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' 
predict(satImage, rf, filename=outImage, progress='text', format='GTiff', datatype='INT1U', type='response', overwrite=TRUE) 
  • 1
    I would highly recommend not using a symbolic model definition and be mindful of your object names (there is a base function called "data"). Where are you getting the error? Is it in the randomForest model or predict? It would be helpful if you could provide some more details. We cannot speculate on the source of the error if we do not know what the data looks like. Use str() on your data object and edit your question to show the results. Apr 24 '15 at 21:15
  • The error appeared at the "predict" part
    – Christine
    Apr 25 '15 at 20:38

I would imagine that the source of the error is that the names of your raster object "satImage" do not match the variables used in the random forests model.

You can check this by using match() or %in% on the names of the two objects.

x = c("t.1","t.2","t.3","t.4")
y = c("t1","t.2","t.3","t.4")
x %in% y 

If you would like to get very specific you could compare the names that the randomForest model object is referencing to the names of the raster stack. Here is a nonspatial example using the iris dataset.

rf.mdl <- randomForest(iris[,1:4], as.factor(iris[,"Species"]), importance=TRUE)

names(iris[,1:4]) %in% rownames(rf.mdl$importance)  

Do you have the training data in a spatial format? If you have points or polygons representing the training data you could use the extract function to create the model data.frame. Since you are pulling data directly from the "satImage" stack, the names would sort out.

I would highly recommend not using a symbolic model definition and be mindful of your object names (there is a base function called "data"). Take a look at this post for example code for a random forests model. I would also point you towards my rfUtilities R package for random forests model significance and cross validation tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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