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
Just bag an
mlr learner already
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
bw.itersOn how many subsets (samples) do we want to train our Learner?
bw.replaceSample with replacement (also known as bootstrapping)?
bw.sizePercentage size of the samples. If
bw.replace = TRUE,
bw.size = 1is the default. This does not mean that one sample will contain all the observations as observations will occur multiple times in each sample.
bw.featsPercentage size of randomly selected features for each iteration.
Of course we also need a Learner which we have to pass to
Now we can compare the performance with and without bagging. First let’s try it without bagging:
And now with bagging:
Training more learners takes more time, but can outperform pure learners on noisy data with many features.
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.
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
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.
getLearnerModel(), you can access the models fitted in the individual iterations.
head(getLearnerModel(mod), 2) ## [] ## 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:
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.