1
$\begingroup$

I run random forest (using package randomForest) on the classic titanic data. Here are the code and results.

dt_rf <- randomForest(Survived ~ ., data = train, ntree = 200)
print(dt_rf)
Call:
 randomForest(formula = Survived ~ ., data = train, ntree = 200) 
               Type of random forest: classification
                     Number of trees: 200
No. of variables tried at each split: 2

        OOB estimate of  error rate: 15.8%
Confusion matrix:
     No Yes class.error
No  401  38  0.08656036
Yes  74 196  0.27407407

Wondering is there a way to access the OOB error from the dt_rf object? I only see error rate table by dt_rf$err.rate. What does this table mean? How is this table different from the error rate in the printable results?

dt_rf$err.rate
            OOB         No       Yes
  [1,] 0.1785714 0.07407407 0.3666667
  [2,] 0.2070588 0.12781955 0.3396226
  [3,] 0.2169811 0.13719512 0.3465347
  [4,] 0.2233503 0.17403315 0.3013100
  [5,] 0.2154088 0.16153846 0.3008130
  [6,] 0.2080838 0.14146341 0.3139535
  [7,] 0.1973490 0.13397129 0.2988506
  [8,] 0.2023290 0.13207547 0.3155894
  [9,] 0.1873199 0.11943794 0.2958801
 [10,] 0.1919771 0.12064965 0.3071161

I tried using mean to calculate the OOB column, but it does not match 15.8% in the printable results.

$\endgroup$

1 Answer 1

0
$\begingroup$

You can calculate the OOB error from the random forest confusion matrix. What you need to do is just to divide the number of correctly classified observations in the confusion matrix by the number of total observations and substract it from 1. In your case: (401 + 196)/(401 + 38 + 74 + 196) = 0.842; 1 - 0.842 = 0.158, which is the same as 15.8%. You can get this number by using diag() and sum() functions:

    conf <- dt_rf$confusion[,-ncol(dt_rf$confusion)]
    oob <- 1 - (sum(diag(conf))/sum(conf))
    oob
    oob * 100
$\endgroup$

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.