# Why are there many lines in the graph of random forest?

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?

• 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. Mar 5, 2020 at 11:50
• 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. Mar 5, 2020 at 11:55
• 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? Mar 5, 2020 at 12:01
• @StephanKolassa It's best that you write and I accept it because it's your solution to differentiate them ^^ Mar 5, 2020 at 12:03

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.

• Lurked around just to upvote this. :D Mar 5, 2020 at 12:14