One reason why random forests perform so well is that they are using bagging as a technique to gain more stability. But why do you want to limit yourself to the classifiers already implemented in well known random forests when it is really easy to build your own with mlr?

Just bag an mlr learner already makeBaggingWrapper().

As in a random forest, we need a Learner which is trained on a subset of the data during each iteration of the bagging process. The subsets are chosen according to the parameters given to makeBaggingWrapper():

• bw.iters On how many subsets (samples) do we want to train our Learner?
• bw.replace Sample with replacement (also known as bootstrapping)?
• bw.size Percentage size of the samples. If bw.replace = TRUE, bw.size = 1 is the default. This does not mean that one sample will contain all the observations as observations will occur multiple times in each sample.
• bw.feats Percentage size of randomly selected features for each iteration.

Of course we also need a Learner which we have to pass to makeBaggingWrapper().

lrn = makeLearner("classif.rpart")
bag.lrn = makeBaggingWrapper(lrn, bw.iters = 50, bw.replace = TRUE,
bw.size = 0.8, bw.feats = 3/4)

Now we can compare the performance with and without bagging. First let’s try it without bagging:

rdesc = makeResampleDesc("CV", iters = 10)
r = resample(learner = lrn, task = sonar.task, resampling = rdesc, show.info = FALSE)
r$aggr ## mmce.test.mean ## 0.302619 And now with bagging: rdesc = makeResampleDesc("CV", iters = 10) result = resample(learner = bag.lrn, task = sonar.task, resampling = rdesc, show.info = FALSE) result$aggr
## mmce.test.mean
##      0.2161905

Training more learners takes more time, but can outperform pure learners on noisy data with many features.

Changing the type of prediction

In case of a classification problem the predicted class labels are determined by majority voting over the predictions of the individual models. Additionally, posterior probabilities can be estimated as the relative proportions of the predicted class labels. For this purpose you have to change the predict type of the bagging learner as follows.

bag.lrn = setPredictType(bag.lrn, predict.type = "prob")

Note that it is not relevant if the base learner itself can predict probabilities and that for this reason the predict type of the base learner always has to be "response".

For regression the mean value across predictions is computed. Moreover, the standard deviation across predictions is estimated if the predict type of the bagging learner is changed to "se". Below, we give a small example for regression.

train.inds = seq(1, n, 3)
test.inds  = setdiff(1:n, train.inds)
lrn = makeLearner("regr.rpart")
bag.lrn = makeBaggingWrapper(lrn)
bag.lrn = setPredictType(bag.lrn, predict.type = "se")

With function getLearnerModel(), you can access the models fitted in the individual iterations.

## []
## Model for learner.id=regr.rpart; learner.class=regr.rpart
## Trained on: task.id = BostonHousing-example; obs = 169; features = 13
## Hyperparameters: xval=0
##
## []
## Model for learner.id=regr.rpart; learner.class=regr.rpart
## Trained on: task.id = BostonHousing-example; obs = 169; features = 13
## Hyperparameters: xval=0

Predict the response and calculate the standard deviation:

##   id truth response       se
## 2  2  21.6 21.53997 2.727530
## 3  3  34.7 34.54394 3.524780
## 5  5  36.2 33.47336 1.704140
## 6  6  28.7 23.79443 1.282300
## 8  8  27.1 14.60342 2.269138
## 9  9  16.5 13.67014 3.147195

In the column labelled se the standard deviation for each prediction is given.

Let’s visualise this a bit using ggplot2::ggplot2(). Here we plot the percentage of lower status of the population (lstat) against the prediction.

library("ggplot2")
library("reshape2")
{"ft_lang_label":"__label__en","ft_lang_prob":0.7835868,"math_prob":0.97857493,"size":4196,"snap":"2022-05-2022-21","text_gpt3_token_len":1151,"char_repetition_ratio":0.11474237,"word_repetition_ratio":0.09247649,"special_character_ratio":0.27979028,"punctuation_ratio":0.1854067,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98230445,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-18T21:36:27Z\",\"WARC-Record-ID\":\"<urn:uuid:5ef3ff8c-37d0-40c7-aa3f-b3883618d796>\",\"Content-Length\":\"27090\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2405c4d7-6cd5-44c5-9476-b6b004bb26b9>\",\"WARC-Concurrent-To\":\"<urn:uuid:99aaefde-419a-4480-b100-47a1398ec626>\",\"WARC-IP-Address\":\"185.199.109.153\",\"WARC-Target-URI\":\"https://mlr.mlr-org.com/articles/tutorial/bagging.html\",\"WARC-Payload-Digest\":\"sha1:L2RKCAEMN47M4CEHU5XQYH57ODQ7YK2Z\",\"WARC-Block-Digest\":\"sha1:RDKMQ4Z6UZDWJDJD6XPQEOGV3TWWEEIU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320301063.81_warc_CC-MAIN-20220118213028-20220119003028-00018.warc.gz\"}"}