<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/better-model-selection-for-evolving-models/"></div></div><div><a data-url="https://www.r-bloggers.com/better-model-selection-for-evolving-models/" data-counturl="https://www.r-bloggers.com/better-model-selection-for-evolving-models/" data-count="horizontal" data-text="Better Model Selection for Evolving Models" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.quintuitive.com/2016/09/25/better-model-selection-evolving-models/"> R – Quintuitive</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>For quite some time now I have been using <a target="_blank" href="https://cran.r-project.org/web/packages/caret/">R’s caret package</a> to choose the model for forecasting time series data. The approach is satisfactory as long as the model is not an evolving model (i.e. is not re-trained), or if it evolves rarely. If the model is re-trained often – the approach has significant computational overhead. Interestingly enough, an alternative, more efficient approach allows also for more flexibility in the area of model selection.<br>
<span></span><br>
Let’s first outline how caret chooses a single model. The high level algorithm is outlined <a target="_blank" href="http://topepo.github.io/caret/model-training-and-tuning.html">here</a>:</p>
<div align="center">
<a target="_blank" href="https://i2.wp.com/www.quintuitive.com/wp-content/uploads/2016/09/TrainAlgo.png"><img sizes="(max-width: 629px) 100vw, 629px" alt="Model Selection Algorithm" class="wp-image-3002" src="https://i2.wp.com/www.quintuitive.com/wp-content/uploads/2016/09/TrainAlgo.png?resize=456%2C170" data-recalc-dims="1"></a>
</div>
<p>So let’s say we are training a random forest. For this model, a single parameter, <em>mtry</em> is optimized:</p>
<pre>
require(caret)
getModelInfo('rf')$wsrf$parameters
#   parameter   class                         label
#   1    mtry numeric #Randomly Selected Predictors
</pre>
<p>Let’s assume we are using some form of cross validation. According to the algorithm outline, caret will create a few subsets. On each subset, it will train all models (as many models as different values for <em>mtry</em> there are) and finally it will choose the model behaving best over all cross validation folds. So far so good.</p>
<p>When dealing with time series, using regular cross validation has a future-snooping problem and from my experience general cross validation doesn’t work well in practice for time series data. The results are good on the training set, but the performance on the test set, the hold out, is bad. To address this issue, caret provides the <em>timeslice</em> cross validation method:</p>
<pre>
require(caret)
history = 1000
initial.window = 800
train.control = trainControl(
                    method=&quot;timeslice&quot;,
                    initialWindow=initial.window,
                    horizon=history-initial.window,
                    fixedWindow=T)
</pre>
<p>When the above <em>train.control</em> is used in training (via the <em>train</em> call), we will end up using 200 models for each set of parameters (each value of <em>mtry</em> in the random forest case). In other words, for a single value of <em>mtry</em>, we will compute:</p>
<div align="center">
<table>
<thead>
<tr>
<th>Window</th>
<th>Training Points</th>
<th>Test Point</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1..800</td>
<td>801</td>
</tr>
<tr>
<td>2</td>
<td>2..801</td>
<td>802</td>
</tr>
<tr>
<td>3</td>
<td>3..803</td>
<td>803</td>
</tr>
<tr>
<td>…</td>
<td>…</td>
<td>…</td>
</tr>
<tr>
<td>200</td>
<td>200..999</td>
<td>1000</td>
</tr>
</tbody>
</table>
</div>
<p>The training set for each model is the previous 800 points. The test set for a single model is the single point forecast. Now, for each value of <em>mtry</em> we end up with 200 forecasted points, using the accuracy (or any other metric) we select the best performing model over these 200 points. No future-snooping here, because all history points are prior the points being forecasted.</p>
<p>Granted, this approach (of doing things on daily basis) may sound extreme, but it’s useful to illustrate the overhead which is imposed when the model evolves over time, so bear with me.</p>
<p>So far we have dealt with a single model selection. Once the best model is selected, we can forecast the next data point. Then what? What I usually do is to walk the time series forward and repeat these steps at certain intervals. This is equivalent to saying something like: “Let’s choose the best model each Friday, use the selected model to predict each day for the next week. Then re-fit it on Friday.”. This forward-walking approach has been found useful in trading, but surprisingly, hasn’t been discussed pretty much elsewhere. Abundant time series data is generated everywhere, hence, I feel this evolving model approach deserves at least as much attention as the “fit once, live happily thereafter” approach.</p>
<p>Back to our discussion. To illustrate the inefficiency, consider an even more extreme case – we are selecting the best model every day, using the the above parameters, i.e. the best model for each day is selected tuning the parameters over the previous 200 days. On day <em>n</em> for a given value of the parameter (mtry), we will train this model over a sequence of 200 sliding windows each of which is of size 800. Next we will move to day <em>n+1</em> and we will compute, yet again, this model over a sequence of 200 sliding windows each of which is of size 800. Most of these operations are repeated (the last 800 window on day <em>n</em> is the second last 800 window on day <em>n+1</em>). So just for a single parameter value, we are repeating most of the computation on each step.</p>
<p>At this point, I hope you get the idea. So what is my solution? Simple. For each set of model parameters (each value of <em>mtry</em>), walk the series separately, do the training (no cross validation – we have a single parameter value), do the forecasting and store everything important into, let’s say, SQLite database. Next, pull out all predictions and walk the combined series. On each step, look at the history, and based on it, decide which model prediction to use for the next step. Assuming we are selecting the model over 5 different values for <em>mtry</em>, here is how the combined data may look like for a three-class (0, -1 and 1) classification:</p>
<div align="center">
<a target="_blank" href="https://i0.wp.com/www.quintuitive.com/wp-content/uploads/2016/09/models-1.png"><img sizes="(max-width: 364px) 100vw, 364px" alt="models" class="wp-image-3013" src="https://i0.wp.com/www.quintuitive.com/wp-content/uploads/2016/09/models-1.png?resize=364%2C339" data-recalc-dims="1"></a>
</div>
<p>Obviously the described approach is going to be orders of magnitude faster, but will deliver very similar (there are differences based on the window sizes) results. It also has an added bonus – once the forecasts are generated, one can experiment with different metrics for model selection on each step and all that without re-running the machine learning portion. For instance, instead of model accuracy (the default <em>caret</em> metric for classification), one can compare accumulative returns over the last n days.</p>
<p>Still cryptic or curious about the details? My plan is keep posting details and code as I progress with my Python implementation. Thus, look for the next installments of these series.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.quintuitive.com/2016/09/25/better-model-selection-evolving-models/">Better Model Selection for Evolving Models</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.quintuitive.com/">Quintuitive</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/better-model-selection-for-evolving-models/"></div></div><div><a data-url="https://www.r-bloggers.com/better-model-selection-for-evolving-models/" data-counturl="https://www.r-bloggers.com/better-model-selection-for-evolving-models/" data-count="vertical" data-text="Better Model Selection for Evolving Models" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.quintuitive.com/2016/09/25/better-model-selection-evolving-models/"> R – Quintuitive</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/zerAyoGLIlY"></a></div>