# Difference between the Out-of-Bag error and the predicted error

library(randomForest)
library(caret)
library(e1071)

> rand.forest = randomForest(Y~., data = trainset)
> print(rand.forest)
Call:
randomForest(formula = Y ~ ., data = trainset)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 23

OOB estimate of  error rate: 4.24%
Confusion matrix:
0  1 class.error
0 19234 53 0.0001245
1  2432 10 0.9221

> p = predict(rand.forest, trainset)
> confusionMatrix(p, trainset\$Y)
Confusion Matrix and Statistics

Reference
Prediction   0     1
0     12564   742
1        11   15
Accuracy : 0.931


Can someone explain, why I get different confusion matrices, even I've used the same training set? What is the theoretical reason for this?

• Thank you for your explanation! So does the second approach not make much sense? Should I rather use predict(rand.forest, test_set) – Textime Jun 13 '19 at 11:58