vignettes/tutorial/out_of_bag_predictions.Rmd
Some learners like random forest use bagging. Bagging means that the learner consists of an ensemble of several base learners and each base learner is trained with a different random subsample or bootstrap sample from all observations. A prediction made for an observation in the original data set using only base learners not trained on this particular observation is called out-of-bag (OOB) prediction. These predictions are not prone to overfitting, as each prediction is only made by learners that did not use the observation for training.
To get a list of learners that provide OOB predictions, you can call listLearners(obj = NA, properties = "oobpreds").
listLearners(obj = NA, properties = "oobpreds")[c("class", "package")]
## class package
## 1 classif.randomForest randomForest
## 2 classif.randomForestSRC randomForestSRC
## 3 classif.ranger ranger
## 4 classif.rFerns rFerns
## 5 regr.randomForest randomForest
## 6 regr.randomForestSRC randomForestSRC
## ... (#rows: 8, #cols: 2)In mlr function getOOBPreds() can be used to extract these observations from the trained models. These predictions can be used to evaluate the performance of a given learner like in the following example.
lrn = makeLearner("classif.ranger", predict.type = "prob", predict.threshold = 0.6)
mod = train(lrn, sonar.task)
oob = getOOBPreds(mod, sonar.task)
oob
## Prediction: 208 observations
## predict.type: prob
## threshold: M=0.60,R=0.40
## time: NA
## id truth prob.M prob.R response
## 1 1 R 0.5331047 0.4668953 R
## 2 2 R 0.6056414 0.3943586 M
## 3 3 R 0.5798999 0.4201001 R
## 4 4 R 0.4472660 0.5527340 R
## 5 5 R 0.6056473 0.3943527 M
## 6 6 R 0.4417684 0.5582316 R
## ... (#rows: 208, #cols: 5)
performance(oob, measures = list(auc, mmce))
## auc mmce
## 0.9238414 0.2067308As the predictions that are used are out-of-bag, this evaluation strategy is very similar to common resampling strategies like 10-fold cross-validation, but much faster, as only one training instance of the model is required.