<h2>Out-of-sample extrapolation</h2>
<p>This post is an offshoot of some simple experiments I made to help clarify my thinking about some machine learning methods. In this experiment I fit four kinds of model to a super-simple artificial dataset with two columns, x and y; and then try to predict new values of y based on values of x that are outside the original range of y. Here’s the end result:</p>
<p></p>
<p>An obvious limitation of the extreme gradient boosting and random forest methods leaps out of this graph - when predicting y based on values of x that are outside the range of the original training set, they presume y will just be around the highest value of y in the original set. These tree-based methods (more detail below) basically can’t extrapolate the way we’d find most intuitive, whereas linear regression and the neural net do ok in this regard.</p>
<h2>Data and set up</h2>
<p>The data was generated by this:</p>
<pre><code><span># set up functionality for modelling down the track
</span><span>library</span><span>(</span><span>xgboost</span><span>)</span><span> </span><span># extreme gradient boosting
</span><span>library</span><span>(</span><span>nnet</span><span>)</span><span> </span><span># neural network
</span><span>library</span><span>(</span><span>ranger</span><span>)</span><span> </span><span># for random forests
</span><span>library</span><span>(</span><span>rpart</span><span>)</span><span> </span><span># for demo single tree
</span><span>library</span><span>(</span><span>rpart.plot</span><span>)</span><span>
</span><span>library</span><span>(</span><span>viridis</span><span>)</span><span> </span><span># for palette of colours
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span> </span><span># for annotations
</span><span>
</span><span># sample data - training set
</span><span>set.seed</span><span>(</span><span>134</span><span>)</span><span> </span><span># for reproducibility
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>:</span><span>100</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>100</span><span>)</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>3</span><span> </span><span>+</span><span> </span><span>0.3</span><span> </span><span>*</span><span> </span><span>x</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>100</span><span>)</span><span>
</span><span># extrapolation / test set, has historical data plus some more extreme values
</span><span>extrap</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>x</span><span>,</span><span> </span><span>1</span><span>:</span><span>5</span><span> </span><span>*</span><span> </span><span>10</span><span> </span><span>+</span><span> </span><span>100</span><span>))</span></code></pre><h2>The four different modelling methods</h2>
<p>The four methods I’ve used are:</p>
<ul><li>linear regression estimated with ordinary least squares</li>
<li>single layer artificial neural network with the <code>nnet</code> R package</li>
<li>extreme gradient boosting with the <code>xgboost</code> R package</li>
<li>random forests with the <code>ranger</code> R package (faster and more efficient than the older <code>randomForest</code> package, not that it matters with this toy dataset)</li>
</ul><p>All these four methods are now a very standard part of the toolkit for predictive modelling. Linear regression, is the oldest and arguably the most fundamental statistical model of this sort around. The other three can be characterised as black box methods in that they don’t return a parameterised model that can be expressed as a simple equation.</p>
<p>Fitting the linear model in R is as simple as:</p>
<pre><code><span>mod_lm</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>x</span><span>)</span></code></pre><p>Neural networks create one or more hidden layers of machines (one in this case) that transform inputs to outputs. Each machine could in principle be a miniature parameterised model but the net effect is a very flexible and non-linear transformation of the inputs to the outputs. This is conceptually advanced, but simple to fit in R again with a single line of code. Note the meta-parameter <code>size</code> of the hidden layer, which I’ve set to 8 after some experimentation (with real life data I’d used cross-validation to test out the effectiveness of different values).</p>
<pre><code><span>mod_nn</span><span> </span><span>&lt;-</span><span> </span><span>nnet</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>x</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>8</span><span>,</span><span> </span><span>linout</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre><p><code>xgboost</code> fits a shallow regression tree to the data, and then additional trees to the residuals, repeating this process until some pre-set number of rounds set by the analyst. To avoid over-fitting we use cross-validation to determine the best number of rounds. This is a little more involved, but not much:</p>
<pre><code><span># XG boost. This is a bit more complicated as we need to know how many rounds
# of trees to use. Best to use cross-validation to estimate this. Note - # I use a maximum depth of 2 for the trees which I identified by trial and error
# with different values of max.depth and cross-validation, not shown
</span><span>xg_params</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>objective</span><span> </span><span>=</span><span> </span><span>&quot;reg:linear&quot;</span><span>,</span><span> </span><span>max.depth</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span>
</span><span>mod_cv</span><span> </span><span>&lt;-</span><span> </span><span>xgb.cv</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>params</span><span> </span><span>=</span><span> </span><span>xg_params</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>as.matrix</span><span>(</span><span>x</span><span>),</span><span> </span><span>nrounds</span><span> </span><span>=</span><span> </span><span>40</span><span>,</span><span> </span><span>nfold</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span> </span><span># choose nrounds that gives best value of root mean square error on the training set
</span><span>best_nrounds</span><span> </span><span>&lt;-</span><span> </span><span>which</span><span>(</span><span>mod_cv</span><span>$</span><span>test.rmse.mean</span><span> </span><span>==</span><span> </span><span>min</span><span>(</span><span>mod_cv</span><span>$</span><span>test.rmse.mean</span><span>))</span><span>
</span><span>mod_xg</span><span> </span><span>&lt;-</span><span> </span><span>xgboost</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>y</span><span>,</span><span> </span><span>params</span><span> </span><span>=</span><span> </span><span>xg_params</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>as.matrix</span><span>(</span><span>x</span><span>),</span><span> </span><span>nrounds</span><span> </span><span>=</span><span> </span><span>best_nrounds</span><span>)</span></code></pre><p>Then there’s the random forest. This is another tree-based method. It fits multiple regression trees to different row and column subsets of the data (of course, with only one column of explanatory features in our toy dataset, it doesn’t need to create different column subsets!), and takes their average. Doing this with the defaults in <code>ranger</code> is simple again (noting that <code>lm</code>, <code>nnet</code> and <code>ranger</code> all use the standard R formula interface, whereas <code>xgboost</code> needs the input as a matrix of explanatory features and a vector of ‘labels’ ie the response variable).</p>
<pre><code><span>mod_rf</span><span> </span><span>&lt;-</span><span> </span><span>ranger</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>x</span><span>)</span></code></pre><p>Finally, to create the graphic from the beginning of the post with the predictions of each of these models using the extrapolation dataset, I create a function to draw the basic graph of the real data (as I’ll be doing this four times which makes it worth while encapsulating in a function, to avoid repetitive code). I call this function once for each graphic, and superimpose the predicted points over the top.</p>
<pre><code><span>p</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>title</span><span>){</span><span>
</span><span>plot</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>150</span><span>),</span><span> </span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>50</span><span>),</span><span> </span><span>pch</span><span> </span><span>=</span><span> </span><span>19</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.6</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>title</span><span>,</span><span> </span><span>xlab</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>grid</span><span>()</span><span>
</span><span>}</span><span>
</span><span>predshape</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>
</span><span>par</span><span>(</span><span>mfrow</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>2</span><span>),</span><span> </span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>,</span><span> </span><span>mar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>7</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>4</span><span>,</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span> </span><span>0.1</span><span>)</span><span>
</span><span>p</span><span>(</span><span>&quot;Linear regression&quot;</span><span>)</span><span>
</span><span>points</span><span>(</span><span>extrap</span><span>$</span><span>x</span><span>,</span><span> </span><span>predict</span><span>(</span><span>mod_lm</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>extrap</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>pch</span><span> </span><span>=</span><span> </span><span>predshape</span><span>)</span><span>
</span><span>p</span><span>(</span><span>&quot;Neural network&quot;</span><span>)</span><span>
</span><span>points</span><span>(</span><span>extrap</span><span>$</span><span>x</span><span>,</span><span> </span><span>predict</span><span>(</span><span>mod_nn</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>extrap</span><span>),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>pch</span><span> </span><span>=</span><span> </span><span>predshape</span><span>)</span><span>
</span><span>p</span><span>(</span><span>&quot;Extreme gradient boosting&quot;</span><span>)</span><span>
</span><span>points</span><span>(</span><span>extrap</span><span>$</span><span>x</span><span>,</span><span> </span><span>predict</span><span>(</span><span>mod_xg</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>as.matrix</span><span>(</span><span>extrap</span><span>)),</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;darkgreen&quot;</span><span>,</span><span> </span><span>pch</span><span> </span><span>=</span><span> </span><span>predshape</span><span>)</span><span>
</span><span>p</span><span>(</span><span>&quot;Random forest&quot;</span><span>)</span><span>
</span><span>fc_rf</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>mod_rf</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>extrap</span><span>)</span><span>
</span><span>points</span><span>(</span><span>extrap</span><span>$</span><span>x</span><span>,</span><span> </span><span>fc_rf</span><span>$</span><span>predictions</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;plum3&quot;</span><span>,</span><span> </span><span>pch</span><span> </span><span>=</span><span> </span><span>predshape</span><span>)</span><span> </span><span>grid.text</span><span>(</span><span>0.5</span><span>,</span><span> </span><span>0.54</span><span>,</span><span> </span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>),</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Tree-based learning methods (like xgboost and random forests)\nhave a particular challenge with out-of-sample extrapolation.&quot;</span><span>)</span><span>
</span><span>grid.text</span><span>(</span><span>0.5</span><span>,</span><span> </span><span>0.04</span><span>,</span><span> </span><span>gp</span><span> </span><span>=</span><span> </span><span>gpar</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>),</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;In all the above plots, the black points are the original training data,\nand coloured circles are predictions.&quot;</span><span>)</span></code></pre><h2>Tree-based limitations with extrapolation</h2>
<p>The limitation of the tree-based methods in extrapolating to an out-of-sample range are obvious when we look at a single tree. Here’a single regression tree fit to this data with the standard <code>rpart</code> R package. This isn’t exactly the sort of tree used by either <code>xgboost</code> or <code>ranger</code> but illustrates the basic approach. The tree algorithm uses the values of x to partition the data and allocate an appropriate value of y (this isn’t usually done with only one explanatory variable of course, but it makes it simple to see what is going on). So if x is less than 11, y is predicted to be 4; if x is between 11 and 28 y is 9; etc. If x is greater than 84, then y is 31.</p>
<p></p>
<p>What happens in the single tree is basically repeated by the more sophisticated random forest and the extreme gradient boosting models. Hence no matter how high a value of x we give them, they predict y to be around 31.</p>
<p>The implication? Just to bear in mind this limitation of tree-based machine learning methods - they won’t handle well new data that is out of the range of the original training data.</p>
<p>Here’s the code for fitting and drawing the individual regression tree.</p>
<pre><code><span>#==============draw an example tree===================
# this is to illustrate the fundamental limitation of tree-based methods
# for out-of-sample extrapolation
</span><span>tree</span><span> </span><span>&lt;-</span><span> </span><span>rpart</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>x</span><span>)</span><span>
</span><span>par</span><span>(</span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col.main</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>)</span><span>
</span><span>rpart.plot</span><span>(</span><span>tree</span><span>,</span><span> </span><span>digits</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>box.palette</span><span> </span><span>=</span><span> </span><span>viridis</span><span>(</span><span>10</span><span>,</span><span> </span><span>option</span><span> </span><span>=</span><span> </span><span>&quot;D&quot;</span><span>,</span><span> </span><span>begin</span><span> </span><span>=</span><span> </span><span>0.85</span><span>,</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>0</span><span>),</span><span> </span><span>shadow.col</span><span> </span><span>=</span><span> </span><span>&quot;grey65&quot;</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>,</span><span> </span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Tree-based methods will give upper and lower bounds\nfor predicted values; in this example, the highest possible\npredicted value of y is 31, whenever x>84.&quot;</span><span>)</span></code></pre>