<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/tuning-xgboost-in-r-part-i/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://insightr.wordpress.com/2018/05/17/tuning-xgboost-in-r-part-i/"> R – insightR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div><strong>By Gabriel Vasconcelos</strong></div>
<div></div>
<div>Before we begin, I would like to thank Anuj for kindly including our blog in his list of the top40 R blogs! Check out the full list at his page, <a rel="nofollow" href="https://blog.feedspot.com/r_programming_blogs/" target="_blank">FeedSpot</a>!</div>
<h1>Introduction</h1>
<p>Tuning a Boosting algorithm for the first time may be a very confusing task. There are so many parameters to choose and they all have different behaviour on the results. Also, the best choice may depends on the data. Every time I get a new dataset I learn something new. A good understanding of classification and regression trees (CART) is also helpful because we will be boosting trees, you can start <a rel="nofollow" href="https://insightr.wordpress.com/2017/09/23/how-random-forests-improve-simple-regression-trees/" target="_blank">here</a> if you have no idea of what a CART is.</p>
<p><span></span></p>
<p>My favourite Boosting package is the xgboost, which will be used in all examples below. Before going to the data let’s talk about some of the parameters I believe to be the most important. These parameters mostly are used to control how much the model may fit to the data. We would like to have a fit that captures the structure of the data but only the real structure. In other words, we do not want the model to fit noise because this will be translated in a poor out-of-sample performance.</p>
<ul>
<li><strong>eta:</strong> Learning (or shrinkage) parameter. It controls how much information from a new tree will be used in the Boosting. This parameter must be bigger than 0 and limited to 1. If it is close to zero we will use only a small piece of information from each new tree. If we set eta to 1 we will use all information from the new tree. Big values of eta result in a faster convergence and more over-fitting problems. Small values may need to many trees to converge.</li>
<li><strong>colsample_bylevel:</strong> Just like Random Forests, some times it is good to look only at a few variables to grow each new node in a tree. If we look at all variables the algorithm needs less trees to converge, but looking at, for example, <img alt="2/3" title="2/3" src="https://s0.wp.com/latex.php?latex=2%2F3&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> of the variables may result in models more robust to over-fitting. There is a similar parameter called colsample_bytree that re-sample the variables in each new tree instead of each new node.</li>
<li><strong>max_depth:</strong> Controls the maximum depth of the trees. Deeper trees have more terminal nodes and fit more data. Convergence also requires less trees if we grow them deep. However, if the trees are to deep we will be using a lot of information from the first trees and the final trees of the algorithm will have less importance on the loss function. The Boosting benefits from using information from many trees. Therefore it is intuitive that huge trees are not desirable. Smaller trees also grow faster and because the Boosting grow new trees in a pseudo-residual and we do not require any amazing adjustment for an individual tree.</li>
<li><strong>sub_sample:</strong> This parameter determines if we are estimating a Boosting or a Stochastic Boosting. If we use 1 we obtain the regular Boosting. Values between 0 and 1 are for the stochastic case. The stochastic Boosting uses only a fraction of the data to grow each tree. For example, if we use 0.5 each tree will sample 50% of the data to grow. Stochastic Boosting is very useful if we have outliers because it limits their influence on the final model because they are dropped on several sub-samples. Moreover, it is possible to have a significant improvement on smaller instances because they are more susceptible to over-fitting.</li>
<li><strong>gamma</strong>: Controls the minimum reduction in the loss function required to grow a new node in a tree. This parameter is sensitive to the scale of the loss function, which will be linked to the scale of your response variable. The main consequence of using a gamma different from 0 is to stop the algorithm from growing useless trees that barely reduce the in-sample error and are likely to result in over-fitting. I will leave this parameter to a future post to save some space here.</li>
<li><strong>min_child_weigth:</strong> Controls the minimum number of observations (instances) in a terminal node. The minimum value for this parameter is 1, which allows the tree to have terminal nodes with only one observation. If we use bigger values we limit a possible perfect fit on some observations. This parameter will also be left to part II.</li>
</ul>
<h2>Example</h2>
<p>The data will be generate from the following equation:</p>
<p><img alt="displaystyle y_i = (x_i'beta)^2 + epsilon_i " title="displaystyle y_i = (x_i'beta)^2 + epsilon_i " src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+y_i+%3D+%28x_i%27%5Cbeta%29%5E2+%2B+%5Cepsilon_i+&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>where <img alt="x_i sim N(0,1)" title="x_i sim N(0,1)" src="https://s0.wp.com/latex.php?latex=x_i+%5Csim+N%280%2C1%29&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">, <img alt="beta_k = (-0.5)^k,~~ k=1,dots,K" title="beta_k = (-0.5)^k,~~ k=1,dots,K" src="https://s0.wp.com/latex.php?latex=%5Cbeta_k+%3D+%28-0.5%29%5Ek%2C~~+k%3D1%2C%5Cdots%2CK&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">, <img alt="e_i sim N(0,1)" title="e_i sim N(0,1)" src="https://s0.wp.com/latex.php?latex=e_i+%5Csim+N%280%2C1%29&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">. The number of variables, <img alt="K" title="K" src="https://s0.wp.com/latex.php?latex=K&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">, will be set to 10 and the number of instances to 1000.</p>
<p>The experiment will be to change each Boosting parameter keeping all the others constant to try to isolate their effects. The standard model will have the following parameters:</p>
<ul>
<li>eta: 0.1</li>
<li>colsample_bylevel: <img alt="2/3" title="2/3" src="https://s0.wp.com/latex.php?latex=2%2F3&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></li>
<li>max_depth: 6</li>
<li>sub_sample: 0.5</li>
</ul>
<p>I will change each of these parameters to the values in the code. We will analyse the convergence and the root mean squared error (RMSE) in the test sample. The code below will prepare everything to run the models.</p>
<pre>library(xgboost)
library(ggplot2)
library(reshape2)
library(Ecdat)

set.seed(1)
N = 1000
k = 10
x = matrix(rnorm(N*k),N,k)
b = (-1)^(1:k)
yaux=(x%*%b)^2
e = rnorm(N)
y=yaux+e

# = select train and test indexes = #
train=sample(1:N,800)
test=setdiff(1:N,train)

# = parameters = #
# = eta candidates = #
eta=c(0.05,0.1,0.2,0.5,1)
# = colsample_bylevel candidates = #
cs=c(1/3,2/3,1)
# = max_depth candidates = #
md=c(2,4,6,10)
# = sub_sample candidates = #
ss=c(0.25,0.5,0.75,1)

# = standard model is the second value  of each vector above = #
standard=c(2,2,3,2)

# = train and test data = #
xtrain = x[train,]
ytrain = y[train]
xtest = x[test,]
ytest = y[test]
</pre>
<h3>eta</h3>
<p>We will start analysing the eta parameter. The code below estimates Boosting models for each candidate eta. First we have the convergence plot, which shows that bigger values of eta converge faster. However, the train RMSE just below the plot shows that faster convergence does not translate into good out-of-sample performance. Smaller values of eta like 0.05 and 0.1 are the ones that produce smaller errors. My opinion is that in this case 0.1 gives us a good out-of-sample performance with acceptable convergence speed. Note that the result for eta=0.5 and 1 are really bad compared to the others.</p>
<pre>set.seed(1)
conv_eta = matrix(NA,500,length(eta))
pred_eta = matrix(NA,length(test), length(eta))
colnames(conv_eta) = colnames(pred_eta) = eta
for(i in 1:length(eta)){
  params=list(eta = eta[i], colsample_bylevel=cs[standard[2]],
              subsample = ss[standard[4]], max_depth = md[standard[3]],
              min_child_weigth = 1)
  xgb=xgboost(xtrain, label = ytrain, nrounds = 500, params = params)
  conv_eta[,i] = xgb$evaluation_log$train_rmse
  pred_eta[,i] = predict(xgb, xtest)
}
</pre>
<pre>conv_eta = data.frame(iter=1:500, conv_eta)
conv_eta = melt(conv_eta, id.vars = &quot;iter&quot;)
ggplot(data = conv_eta) + geom_line(aes(x = iter, y = value, color = variable))
</pre>
<p><img alt="plot of chunk unnamed-chunk-62" src="https://i1.wp.com/i.imgur.com/eKojuGt.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>(RMSE_eta = sqrt(colMeans((ytest-pred_eta)^2)))
</pre>
<pre>##      0.05       0.1       0.2       0.5         1
##  9.964462 10.052367 10.223738 13.691344 20.929690
</pre>
<h3>colsample_bylevel</h3>
<p>The next parameter controls the fraction of variables (characteristics) tested in each new node. Recall that if we use 1 all variables are tested. Values smaller than 1 test only the correspondent fraction of variables. The convergence shows that the model is much less sensitive to the colsample_bylevel. The curves with smaller values are just slightly above the curves with big values. The most accurate model seems to be the one that uses 25% of the sample in each tree. This result may change with different types of data. For example, the data we generated used the same distribution to create all response variables and the betas gave the variables a similar weight, which makes the sampling less relevant.</p>
<pre>set.seed(1)
conv_cs = matrix(NA,500,length(cs))
pred_cs = matrix(NA,length(test), length(cs))
colnames(conv_cs) = colnames(pred_cs) = cs
for(i in 1:length(cs)){
  params = list(eta = eta[standard[1]], colsample_bylevel = cs[i],
              subsample = ss[standard[4]], max_depth = md[standard[3]],
              min_child_weigth = 1)
  xgb=xgboost(xtrain, label = ytrain,nrounds = 500, params = params)
  conv_cs[,i] = xgb$evaluation_log$train_rmse
  pred_cs[,i] = predict(xgb, xtest)
}
</pre>
<pre>conv_cs = data.frame(iter=1:500, conv_cs)
conv_cs = melt(conv_cs, id.vars = &quot;iter&quot;)
ggplot(data = conv_cs) + geom_line(aes(x = iter, y = value, color = variable))
</pre>
<p><img alt="plot of chunk unnamed-chunk-64" src="https://i2.wp.com/i.imgur.com/6tbj1hv.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>(RMSE_cs = sqrt(colMeans((ytest-pred_cs)^2)))
</pre>
<pre>## 0.333333333333333 0.666666666666667                 1
##          10.29836          10.05237          10.20938
</pre>
<h3>max_depth</h3>
<p>The third parameter is the maximum deepness allowed in each tree. Naturally, the model converges faster if we grow bigger trees (see the figure below). However, the best out-of-sample performance is for max_depth=4. Remember that bigger trees are more likely to result in over-fitting. In my experience there is no need to use values bigger than 4 to 6, but there may be exceptions.</p>
<pre>set.seed(1)
conv_md=matrix(NA,500,length(md))
pred_md=matrix(NA,length(test),length(md))
colnames(conv_md)=colnames(pred_md)=md
for(i in 1:length(md)){
  params=list(eta=eta[standard[1]],colsample_bylevel=cs[standard[2]],
              subsample=ss[standard[4]],max_depth=md[i],
              min_child_weigth=1)
  xgb=xgboost(xtrain, label = ytrain,nrounds = 500,params=params)
  conv_md[,i] = xgb$evaluation_log$train_rmse
  pred_md[,i] = predict(xgb, xtest)
}
</pre>
<pre>conv_md=data.frame(iter=1:500,conv_md)
conv_md=melt(conv_md,id.vars = &quot;iter&quot;)
ggplot(data=conv_md)+geom_line(aes(x=iter,y=value,color=variable))
</pre>
<p><img alt="plot of chunk unnamed-chunk-66" src="https://i0.wp.com/i.imgur.com/g6RjMJF.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>(RMSE_md=sqrt(colMeans((ytest-pred_md)^2)))
</pre>
<pre>##         2         4         6        10
## 12.502733  9.374257 10.134965 10.100691
</pre>
<h3>sub_sample</h3>
<p>The next parameter determines if we are estimating a Boosting or a Stochastic Boosting. Smaller values result in bigger errors in-sample but it may generate more robust out-of-sample estimates. However, as mentioned before, you will obtain bigger improvement on samples that have to many characteristics compares to the number of observations and when the data is very noisy. This is not the case here. Nevertheless, there is some improvement compared to the deterministic case, which I believed is mostly caused by outliers in the response variable (boxplot the variable y to see).</p>
<pre>set.seed(1)
conv_ss=matrix(NA,500,length(ss))
pred_ss=matrix(NA,length(test),length(ss))
colnames(conv_ss)=colnames(pred_ss)=ss
for(i in 1:length(ss)){
  params=list(eta=eta[standard[1]],colsample_bylevel=cs[standard[2]],
              subsample=ss[i],max_depth=md[standard[3]],
              min_child_weigth=1)
  xgb=xgboost(xtrain, label = ytrain,nrounds = 500,params=params)
  conv_ss[,i] = xgb$evaluation_log$train_rmse
  pred_ss[,i] = predict(xgb, xtest)
}
</pre>
<pre>conv_ss=data.frame(iter=1:500,conv_ss)
conv_ss=melt(conv_ss,id.vars = &quot;iter&quot;)
ggplot(data=conv_ss)+geom_line(aes(x=iter,y=value,color=variable))
</pre>
<p><img alt="plot of chunk unnamed-chunk-68" src="https://i1.wp.com/i.imgur.com/6NM3BYH.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>(RMSE_ss=sqrt(colMeans((ytest-pred_ss)^2)))
</pre>
<pre>##      0.25       0.5      0.75         1
##  9.731273 10.052367 11.119535 11.233855
</pre>
<p>The main conclusion here is that there is no unique rule to tune Boosting models. The best way is to test several configurations.</p>
<p>Stay tuned if you liked this article, we will be talking more about Boosting soon.</p>
<h2>References</h2>
<p>(Boosting) Friedman, Jerome H. “Greedy function approximation: a gradient boosting machine.” Annals of statistics (2001): 1189-1232.</p>
<p>(Stochastig Boosting) Friedman, Jerome H. “Stochastic gradient boosting.” Computational Statistics &amp; Data Analysis 38.4 (2002): 367-378.</p>
<p>(CART) Breiman, Leo. Classification and regression trees. Routledge, 2017.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://insightr.wordpress.com/2018/05/17/tuning-xgboost-in-r-part-i/"> R – insightR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/yV68OJBHpcc" height="1"></a></div>