library(rpart)
library(rpart.plot)
= airquality[complete.cases(airquality),] data
5 Tree-based Algorithms
5.1 Classification and Regression Trees
Tree-based algorithms use a series of if-then rules to generate predictions from one or more decision trees. In this lecture, we will explore regression and classification trees by the example of the airquality data set. There is one important hyperparameter for regression trees: “minsplit”.
- It controls the depth of tree (see the help of rpart for a description).
- It controls the complexity of the tree and can thus also be seen as a regularization parameter.
Fit and visualize one(!) regression tree:
= rpart(Ozone~., data = data, control = rpart.control(minsplit = 10))
rt rpart.plot(rt)
Visualize the predictions:
= predict(rt, data)
pred plot(data$Temp, data$Ozone)
lines(data$Temp[order(data$Temp)], pred[order(data$Temp)], col = "red")
The angular form of the prediction line is typical for regression trees and is a weakness of it.
5.2 Random Forest
To overcome this weakness, a random forest uses an ensemble of regression/classification trees. In principle, a random forest is nothing more than a normal regression/classification tree, but it uses the idea of “wisdom of the crowd”: by asking many people (regression/classification trees), you can make a more informed decision (prediction/classification). For example, if you wanted to buy a new phone, you wouldn’t go directly to the store, but you would search the Internet and ask your friends and family.
There are two randomization steps with the random forest that are responsible for their success:
- Bootstrap samples for each tree (we will sample observations with replacement from the data set. For the phone this is like not everyone has experience about each phone).
- At each split, we will sample a subset of predictors that is then considered as potential splitting criterion (for the phone this is like that not everyone has the same decision criteria). Annotation: While building a decision tree (random forests consist of many decision trees), one splits the data at some point according to their features. For example if you have females and males, big and small people in a crowd, you con split this crowd by gender and then by size or by size and then by gender to build a decision tree.
Applying the random forest follows the same principle as for the methods before: We visualize the data (we have already done this so often for the airquality data set, thus we skip it here), fit the algorithm and then plot the outcomes.
Fit a random forest and visualize the predictions:
library(randomForest)
set.seed(123)
= airquality[complete.cases(airquality),]
data
= randomForest(Ozone~., data = data)
rf = predict(rf, data)
pred plot(Ozone~Temp, data = data)
lines(data$Temp[order(data$Temp)], pred[order(data$Temp)], col = "red")
One advantage of random forest is that we get an importance of the variables. For each split in each tree, the improvement in the split criterion is the measure of importance attributed to the split variable, and is accumulated over all trees in the forest separately for each variable. Thus, the variable importance tells us how important a variable is averaged across all trees.
$importance rf
IncNodePurity
Solar.R 17969.59
Wind 31978.36
Temp 34176.71
Month 10753.73
Day 15436.47
There are several important hyperparameters in a random forest that we can tune to get better results:
Hyperparameter | Explanation |
---|---|
mtry | Subset of features randomly selected in each node (from which the algorithm can select the feature that will be used to split the data). |
minimum node size | Minimal number of observations allowed in a node (before the branching is canceled) |
max depth | Maximum number of tree depth |
5.3 Boosted Regression Trees
A boosted regression tree (BRT) starts with a simple regression tree (weak learner) and then sequentially fits additional trees to improve the results. There are two different strategies to do this:
- AdaBoost: Wrong classified observations (by the previous tree) will get a higher weight and therefore the next trees will focus on difficult/missclassified observations.
- Gradient boosting (state of the art): Each sequential model will be fit on the residual errors of the previous model (strongly simplified, the actual algorithm is complex).
We can fit a boosted regression tree using xgboost, but before we have to transform the data into a xgb.Dmatrix
(which is a xgboost specific data type, the package sadly doesn’t support R matrices or data.frames).
library(xgboost)
set.seed(123)
= airquality[complete.cases(airquality),] data
= xgb.DMatrix(data = as.matrix(scale(data[,-1])), label = data$Ozone)
data_xg = xgboost(data_xg, nrounds = 16L) brt
[1] train-rmse:39.724624
[2] train-rmse:30.225761
[3] train-rmse:23.134840
[4] train-rmse:17.899179
[5] train-rmse:14.097785
[6] train-rmse:11.375457
[7] train-rmse:9.391276
[8] train-rmse:7.889690
[9] train-rmse:6.646586
[10] train-rmse:5.804859
[11] train-rmse:5.128437
[12] train-rmse:4.456416
[13] train-rmse:4.069464
[14] train-rmse:3.674615
[15] train-rmse:3.424578
[16] train-rmse:3.191301
The parameter “nrounds” controls how many sequential trees we fit, in our example this was 16. When we predict on new data, we can limit the number of trees used to prevent overfitting (remember: each new tree tries to improve the predictions of the previous trees).
Let us visualize the predictions for different numbers of trees:
= par(mfrow = c(2, 2))
oldpar for(i in 1:4){
= predict(brt, newdata = data_xg, ntreelimit = i)
pred plot(data$Temp, data$Ozone, main = i)
lines(data$Temp[order(data$Temp)], pred[order(data$Temp)], col = "red")
}
[10:05:51] WARNING: src/c_api/c_api.cc:935: `ntree_limit` is deprecated, use `iteration_range` instead.
[10:05:51] WARNING: src/c_api/c_api.cc:935: `ntree_limit` is deprecated, use `iteration_range` instead.
[10:05:51] WARNING: src/c_api/c_api.cc:935: `ntree_limit` is deprecated, use `iteration_range` instead.
[10:05:51] WARNING: src/c_api/c_api.cc:935: `ntree_limit` is deprecated, use `iteration_range` instead.
par(oldpar)
There are also other ways to control for complexity of the boosted regression tree algorithm:
- max_depth: Maximum depth of each tree.
- shrinkage (each tree will get a weight and the weight will decrease with the number of trees).
When having specified the final model, we can obtain the importance of the variables like for random forests:
::xgb.importance(model = brt) xgboost
Feature Gain Cover Frequency
<char> <num> <num> <num>
1: Temp 0.570072012 0.2958229 0.24836601
2: Wind 0.348230653 0.3419576 0.24183007
3: Solar.R 0.058795502 0.1571072 0.30718954
4: Day 0.019529985 0.1779925 0.16993464
5: Month 0.003371847 0.0271197 0.03267974
sqrt(mean((data$Ozone - pred)^2)) # RMSE
[1] 17.89918
= xgb.DMatrix(data = as.matrix(scale(data[,-1])), label = data$Ozone) data_xg
One important strength of xgboost is that we can directly do a cross-validation (which is independent of the boosted regression tree itself!) and specify its properties with the parameter “n-fold”:
set.seed(123)
= xgboost(data_xg, nrounds = 5L) brt
[1] train-rmse:39.724624
[2] train-rmse:30.225761
[3] train-rmse:23.134840
[4] train-rmse:17.899179
[5] train-rmse:14.097785
= xgboost::xgb.cv(data = data_xg, nfold = 3L,
brt_cv nrounds = 3L, nthreads = 4L)
[1] train-rmse:39.895106+2.127355 test-rmse:40.685477+5.745327
[2] train-rmse:30.367660+1.728788 test-rmse:32.255812+5.572963
[3] train-rmse:23.446237+1.366757 test-rmse:27.282435+5.746244
print(brt_cv)
##### xgb.cv 3-folds
iter train_rmse_mean train_rmse_std test_rmse_mean test_rmse_std
<num> <num> <num> <num> <num>
1 39.89511 2.127355 40.68548 5.745327
2 30.36766 1.728788 32.25581 5.572963
3 23.44624 1.366757 27.28244 5.746244
Annotation: The original data set is randomly partitioned into \(n\) equal sized subsamples. Each time, the model is trained on \(n - 1\) subsets (training set) and tested on the left out set (test set) to judge the performance.
If we do three-folded cross-validation, we actually fit three different boosted regression tree models (xgboost models) on \(\approx 67\%\) of the data points. Afterwards, we judge the performance on the respective holdout. This now tells us how well the model performed.
Important hyperparameters:
Hyperparameter | Explanation |
---|---|
eta | learning rate (weighting of the sequential trees) |
max depth | maximal depth in the trees (small = low complexity, large = high complexity) |
subsample | subsample ratio of the data (bootstrap ratio) |
lambda | regularization strength of the individual trees |
max tree | maximal number of trees in the ensemble |
5.4 Exercise - Trees
The goal of this exercise is to understand how the hyperparameter mincut (minsplit) affects the complexity of regression trees.
library(tree)
set.seed(123)
= airquality
data = tree(Ozone~., data = data,
rt control = tree.control(mincut = 1L, nobs = nrow(data)))
plot(rt)
text(rt)
= predict(rt, data)
pred plot(data$Temp, data$Ozone)
lines(data$Temp[order(data$Temp)], pred[order(data$Temp)], col = "red")
sqrt(mean((data$Ozone - pred)^2)) # RMSE
Tasks:
- The code snippet above returns NA for the RMSE, what is wrong in the snippet?
- Read the
tree.control
documentation, what does the mincut parameter do? - Try different mincut values and check how the predictions (and the RMSE) change. What was wrong in the snippet above?
library(tree)
set.seed(123)
= airquality[complete.cases(airquality),]
data
= function(mincut){
doTask = tree(Ozone~., data = data,
rt control = tree.control(mincut = mincut, nobs = nrow(data)))
= predict(rt, data)
pred plot(data$Temp, data$Ozone,
main = paste0(
"mincut: ", mincut,
"\nRMSE: ", round(sqrt(mean((data$Ozone - pred)^2)), 2)
)
)lines(data$Temp[order(data$Temp)], pred[order(data$Temp)], col = "red")
}
for(i in c(1, 2, 3, 5, 10, 15, 25, 50, 54, 55, 56, 57, 75, 100)){ doTask(i) }
Approximately at mincut = 15, prediction is the best (mind overfitting). After mincut = 56, the prediction has no information at all and the RMSE stays constant.
Mind the complete cases of the airquality data set, that was the error.
The goal of this exercise is to understand how the hyperparameter nodesize affects the complexity of random forest.
library(randomForest)
set.seed(123)
= airquality[complete.cases(airquality),]
data
= randomForest(Ozone~., data = data)
rf
= predict(rf, data)
pred importance(rf)
IncNodePurity
Solar.R 17969.59
Wind 31978.36
Temp 34176.71
Month 10753.73
Day 15436.47
cat("RMSE: ", sqrt(mean((data$Ozone - pred)^2)), "\n")
RMSE: 9.507848
plot(data$Temp, data$Ozone)
lines(data$Temp[order(data$Temp)], pred[order(data$Temp)], col = "red")
Tasks:
- Check the documentation of the
randomForest
function and read the description of the nodesize parameter - Try different nodesize values and describe how the predictions change
library(randomForest)
set.seed(123)
= airquality[complete.cases(airquality),]
data
for(nodesize in c(1, 15, 50, 100)){
for(mtry in c(1, 3, 5)){
= randomForest(Ozone~., data = data, nodesize = nodesize)
rf
= predict(rf, data)
pred
plot(data$Temp, data$Ozone, main = paste0(
" nodesize: ", nodesize,
"\nRMSE: ", round(sqrt(mean((data$Ozone - pred)^2)), 2)
)
)lines(data$Temp[order(data$Temp)], pred[order(data$Temp)], col = "red")
} }
Nodesize affects the complexity. In other words: The bigger the nodesize, the smaller the trees and the more bias/less variance.
The goal of this exercise is to understand how complexity in BRT affects predictions. For that, we will simulate data with two predictors x1
and x2
and the y
response variable will be an interaction of the two predictors:
\[y = e^{-x_1^2 - x_2^2} \] We can visualize the simulated data as an image (x1 and x2 on the x and y axis, and the y values as colors)
library(xgboost)
library(animation)
set.seed(123)
= seq(-3, 3, length.out = 100)
x1 = seq(-3, 3, length.out = 100)
x2 = expand.grid(x1, x2)
x = apply(x, 1, function(t) exp(-t[1]^2 - t[2]^2))
y
image(matrix(y, 100, 100), main = "Original image", axes = FALSE, las = 2)
axis(1, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1))
axis(2, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1), las = 2)
= xgboost::xgboost(xgb.DMatrix(data = as.matrix(x), label = y),
model nrounds = 500L, verbose = 0L)
= predict(model, newdata = xgb.DMatrix(data = as.matrix(x)),
pred ntreelimit = 10L)
saveGIF(
{for(i in c(1, 2, 4, 8, 12, 20, 40, 80, 200)){
= predict(model, newdata = xgb.DMatrix(data = as.matrix(x)),
pred ntreelimit = i)
image(matrix(pred, 100, 100), main = paste0("Trees: ", i),
axes = FALSE, las = 2)
axis(1, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1))
axis(2, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1), las = 2)
}
},movie.name = "boosting.gif", autobrowse = FALSE
)
Tasks:
- Run the code above and try different max_depth values and describe what you see!
Tip: have a look at the boosting.gif.
library(xgboost)
library(animation)
set.seed(123)
= seq(-3, 3, length.out = 100)
x1 = seq(-3, 3, length.out = 100)
x2 = expand.grid(x1, x2)
x = apply(x, 1, function(t) exp(-t[1]^2 - t[2]^2))
y
image(matrix(y, 100, 100), main = "Original image", axes = FALSE, las = 2)
axis(1, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1))
axis(2, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1), las = 2)
for(max_depth in c(3, 6, 10, 20)){
= xgboost::xgboost(xgb.DMatrix(data = as.matrix(x), label = y),
model max_depth = max_depth,
nrounds = 500, verbose = 0L)
saveGIF(
{for(i in c(1, 2, 4, 8, 12, 20, 40, 80, 200)){
= predict(model, newdata = xgb.DMatrix(data = as.matrix(x)),
pred ntreelimit = i)
image(matrix(pred, 100, 100),
main = paste0("eta: ", eta,
" max_depth: ", max_depth,
" Trees: ", i),
axes = FALSE, las = 2)
axis(1, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1))
axis(2, at = seq(0, 1, length.out = 10),
labels = round(seq(-3, 3, length.out = 10), 1), las = 2)
}
},movie.name = paste0("boosting_", max_depth, "_", eta, ".gif"),
autobrowse = FALSE
) }
We see that for high values of max_depth, the predictions “smooth out” faster. On the other hand, with a low max_depth (low complexity of the individual trees), more trees are required in the ensemble to achieve a smooth prediction surface.
::xgboost ?xgboost
Just some examples:
Important hyperparameters:
Hyperparameter | Explanation |
---|---|
eta | learning rate (weighting of the sequential trees) |
max depth | maximal depth in the trees (small = low complexity, large = high complexity) |
subsample | subsample ratio of the data (bootstrap ratio) |
lambda | regularization strength of the individual trees |
max tree | maximal number of trees in the ensemble |
The goal of this exercise is to tune a BRT on the titanic_ml dataset and beat yesterday’s RF predictions.
Prepare the data:
library(EcoData)
library(dplyr)
Attaching package: 'dplyr'
The following object is masked from 'package:xgboost':
slice
The following object is masked from 'package:randomForest':
combine
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(missRanger)
data(titanic_ml)
= titanic_ml
data =
data %>% select(survived, sex, age, fare, pclass)
data -1] = missRanger(data[,-1], verbose = 0)
data[,
=
data_sub %>%
data mutate(age = scales::rescale(age, c(0, 1)),
fare = scales::rescale(fare, c(0, 1))) %>%
mutate(sex = as.integer(sex) - 1L,
pclass = as.integer(pclass - 1L))
= data_sub[is.na(data_sub$survived),] # for which we want to make predictions at the end
data_new = data_sub[!is.na(data_sub$survived),] # data with known response data_obs
Tasks:
- Tune eta and max depth!
library(xgboost)
set.seed(42)
= data_sub[!is.na(data_sub$survived),]
data_obs = 3
cv
= sample.int(cv, nrow(data_obs), replace = T)
split
# sample minnodesize values (must be integers)
= sample(200, 20)
hyper_depth = runif(20, 0, 1)
hyper_eta
=
tuning_results sapply(1:length(hyper_depth), function(k) {
= NULL
auc_inner for(j in 1:cv) {
= split == j
inner_split = data_obs[!inner_split, ]
train_inner = data_obs[inner_split, ]
test_inner
= xgb.DMatrix(data = as.matrix(train_inner[,-1]), label = train_inner$survived)
data_xg
= xgboost(data_xg, nrounds = 16L, eta = hyper_eta[k], max_depth = hyper_depth[k], objective = "reg:logistic", verbose = 0)
model = predict(model, newdata = as.matrix(test_inner)[,-1])
predictions
= Metrics::auc(test_inner$survived, predictions)
auc_inner[j]
}return(mean(auc_inner))
})
= data.frame(depth = hyper_depth, eta = hyper_eta, AUC = tuning_results)
results
print(results)
depth eta AUC
1 63 0.81336270 0.8201865
2 190 0.87043248 0.8288359
3 19 0.01916128 0.8150229
4 21 0.82474818 0.8181561
5 107 0.40126850 0.8222465
6 191 0.67205829 0.8187190
7 98 0.11867222 0.8221003
8 116 0.47642434 0.8197725
9 135 0.92533996 0.8179209
10 177 0.57483934 0.8203259
11 68 0.91275158 0.8230057
12 173 0.75530192 0.8241578
13 115 0.63099992 0.8209234
14 65 0.79442065 0.8289604
15 92 0.92354646 0.8134317
16 81 0.16446881 0.8252634
17 164 0.19288394 0.8242471
18 162 0.09764267 0.8177379
19 155 0.82910940 0.8159333
20 154 0.80586396 0.8215947
Make predictions:
= xgb.DMatrix(data = as.matrix(data_obs[,-1]), label = data_obs$survived)
data_xg
= xgboost(data_xg, nrounds = 16L, eta = results[which.max(results$AUC), 2], max_depth = results[which.max(results$AUC), 1], objective = "reg:logistic")
model
= predict(model, newdata = as.matrix(data_new)[,-1])
predictions
# Single predictions from the ensemble model:
write.csv(data.frame(y = predictions), file = "Max_titanic_xgboost.csv")
You can easily implement a BRT or boosted linear model using the rpart package or the lm function.
Go through the code line by line and try to understand it. Ask, if you have any questions:
Let’s try it:
= model.matrix(~. , data = airquality)
data
= get_boosting_model(x = data[,-2], y = data[,2], n_trees = 5L )
model = predict(model, newdata = data[,-2])
pred plot(data[,2], pred, xlab = "observed", ylab = "predicted")
Hyperparameter | Explanation |
---|---|
mtry | Subset of features randomly selected in each node (from which the algorithm can select the feature that will be used to split the data). |
minimum node size | Minimal number of observations allowed in a node (before the branching is canceled) |
max depth | Maximum number of tree depth |
Combing back to the titanic dataset from the morning, we want to optimize min node size in our RF using a simple CV.
Prepare the data:
library(EcoData)
library(dplyr)
library(missRanger)
data(titanic_ml)
= titanic_ml
data =
data %>% select(survived, sex, age, fare, pclass)
data -1] = missRanger(data[,-1], verbose = 0)
data[,
=
data_sub %>%
data mutate(age = scales::rescale(age, c(0, 1)),
fare = scales::rescale(fare, c(0, 1))) %>%
mutate(sex = as.integer(sex) - 1L,
pclass = as.integer(pclass - 1L))
= data_sub[is.na(data_sub$survived),] # for which we want to make predictions at the end
data_new = data_sub[!is.na(data_sub$survived),] # data with known response
data_obs $survived = as.factor(data_sub$survived)
data_sub$survived = as.factor(data_obs$survived) data_obs
Hints:
- adjust the ‘
type
’ argument in thepredict(…)
method (the default is to predict classes) - when predicting probabilities, the randomForest will return a matrix, a column for each class, we are interested in the probability of surviving (so the second column)
Bonus:
- tune min node size (and mtry)
- use more features
library(randomForest)
set.seed(42)
= data_sub[!is.na(data_sub$survived),]
data_obs $survived = as.factor(data_obs$survived)
data_obs
= 3
cv = sample(100, 20)
hyper_minnodesize = sample.int(cv, nrow(data_obs), replace = T)
split
=
tuning_results sapply(1:length(hyper_minnodesize), function(k) {
= NULL
auc_inner for(j in 1:cv) {
= split == j
inner_split = data_obs[!inner_split, ]
train_inner = data_obs[inner_split, ]
test_inner = randomForest(survived~.,data = train_inner, nodesize = hyper_minnodesize[k])
model = predict(model, test_inner, type = "prob")[,2]
predictions
= Metrics::auc(test_inner$survived, predictions)
auc_inner[j]
}return(mean(auc_inner))
})
= data.frame(minnodesize = hyper_minnodesize, AUC = tuning_results)
results
print(results)
minnodesize AUC
1 49 0.8157904
2 65 0.8097477
3 25 0.8222759
4 74 0.8135160
5 18 0.8257731
6 100 0.8086002
7 47 0.8153151
8 24 0.8246248
9 71 0.8181533
10 89 0.8101602
11 37 0.8247795
12 20 0.8239917
13 26 0.8263038
14 3 0.8309202
15 41 0.8212540
16 27 0.8239286
17 36 0.8251308
18 5 0.8274042
19 34 0.8214008
20 87 0.8148782
Make predictions:
= randomForest(survived~.,data = data_obs, nodesize = results[which.max(results$AUC),1])
model
write.csv(data.frame(y = predict(model, data_new, type = "prob")[,2]), file = "Max_titanic_rf.csv")