8
$\begingroup$

I'm running random forest algorithm on the dataset Sonar with the following code:

library(mlbench)
data(Sonar)
library(randomForest)

n <- 208
ntrain <- 100
ntest <- 108
train.idx <- sample(1:n, ntrain, replace = FALSE)
train.set <- Sonar[train.idx, ]
test.set <- Sonar[-train.idx, ]

rf <- randomForest(Class ~ ., data = train.set, keep.inbag = TRUE, importance = TRUE)
plot(rf)

Could you please explain why we have up to three different lines in the graph? What do the three line represent?

enter image description here

$\endgroup$
4
  • $\begingroup$ Good question, +1. ?plot.randomForest is not helpful at all. However, it does tell us that plot.randomForest invisibly returns the data that is plotted (try (plot(rf)) with the brackets), which is a three column matrix with columns called OOB, M and R. The first is pretty obviously the out-of-bag error. The other two? No idea. $\endgroup$ Mar 5, 2020 at 11:50
  • $\begingroup$ Thank you @StephanKolassa, I think the other two are OOB for attributes M and R respectively. The only problem remains is which color corresponds to which attribute. $\endgroup$
    – Akira
    Mar 5, 2020 at 11:55
  • $\begingroup$ Ah, yes, those are the target classes. You can deduce which line is which by looking at the return values. The last row of the returned matrix is easiest to map to the lines (tail(plot(rf))): the bottom line is M, the middle one is OOB, the top one is R. Do you want to write up a self-answer? $\endgroup$ Mar 5, 2020 at 12:01
  • 1
    $\begingroup$ @StephanKolassa It's best that you write and I accept it because it's your solution to differentiate them ^^ $\endgroup$
    – Akira
    Mar 5, 2020 at 12:03

1 Answer 1

9
$\begingroup$

Unfortunately, ?plot.randomForest is not helpful at all. However, it does tell us that plot.randomForest invisibly returns the data that is plotted (try (plot(rf)) with the brackets), which is a three column matrix with columns called OOB, M and R. The first is pretty obviously the overall out-of-bag error. The other two are equally obviously the out-of-bag errors for the two possible classes, which are M and R.

As to which line corresponds to which column, we inspect the output matrix, specifically the last few lines:

> tail(plot(rf))
        OOB          M         R
[495,] 0.22 0.09259259 0.3695652
[496,] 0.22 0.09259259 0.3695652
[497,] 0.22 0.09259259 0.3695652
[498,] 0.22 0.09259259 0.3695652
[499,] 0.22 0.09259259 0.3695652
[500,] 0.22 0.09259259 0.3695652

Thus, the bottom line is M, the middle one is OOB, the top one is R.

$\endgroup$
1
  • $\begingroup$ Lurked around just to upvote this. :D $\endgroup$
    – usεr11852
    Mar 5, 2020 at 12:14

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.