I saw this video and I understood that to build a random forest are used different decision tree, with a different structure. My code about that is:



data$Species <- as.factor(data$Species)

ind <- sample(2, nrow(data), replace = TRUE, prob = c(0.7, 0.3))
train <- data[ind==1,]
test <- data[ind==2,]

rf <- randomForest(Species~., data=train, proximity=TRUE) 

p1 <- predict(rf, train)
confusionMatrix(p1, train$ Species)
p2 <- predict(rf, test)
confusionMatrix(p2, test$ Species)

When I run print(rf) I saw: OOB estimate of error rate: 4.95% So my expected accuracy is 1-4.95%? Expected accuracy is significant differently from train accuracy (1) and test accuracy (0,93), what does it means?


1 Answer 1


The OOB error rate is just the sum missclassified classes over the sum of all observation. Please refer to the following article for guidance: https://www.blopig.com/blog/2017/04/a-very-basic-introduction-to-random-forests-using-r/#:~:text=The%20OOB%20estimate%20of%20error%20rate%20is%20a%20useful%20measure,value%20for%20this%20error%20rate.

However your second question is a little bit more delicate. You have to understand the difference between train and test. Normally the error of the test is always higher than the train (difference between insample vs out of sample data). Imagine your algorithm it is trained on train data and it fits train data. However during the test you are trying to predict test with data fitted on the train, it is normal that the error is higher (or accuracy lower) since it has never seen those data.

The error comes from these lines :

p1 <- predict(rf, train)
confusionMatrix(p1, train$ Species)

rf already holds the information. Donc the OOB you find in p1 is predicting on the same data you have fitted. For train part use table that comes out from rf.

  • $\begingroup$ You said " Imagine your algorithm it is trained on train data and it fits train data." so my train accuracy will be always 1? It is necessary to calculate it? $\endgroup$
    – Inuraghe
    Mar 22, 2022 at 13:14
  • 1
    $\begingroup$ Not really, it really depends on the data. It is not always 1 because it is still a fit. You can't always find a curve or an approximation that fits very well the data. But it can be an indicator of how the test will perform (sometimes it can be a higher band limit on your accuracy). To conclude, the rf gives in sample data results and p2 will give you the out of sample results, meaning how it will perform with unseen data $\endgroup$ Mar 22, 2022 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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