{"id":1574,"date":"2019-01-16T13:26:21","date_gmt":"2019-01-16T13:26:21","guid":{"rendered":"http:\/\/kourentzes.com\/forecasting\/?p=1574"},"modified":"2019-01-16T13:26:21","modified_gmt":"2019-01-16T13:26:21","slug":"tutorial-for-the-nnfor-r-package","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/forecasting\/2019\/01\/16\/tutorial-for-the-nnfor-r-package\/","title":{"rendered":"Tutorial for the nnfor R package"},"content":{"rendered":"<div id=\"the-nnfor-package\" class=\"section level2\">The <a href=\"https:\/\/cran.r-project.org\/package=nnfor\"><strong>nnfor<\/strong><\/a> (development version <a href=\"https:\/\/github.com\/trnnick\/nnfor\">here<\/a>) package for R facilitates time series forecasting with Multilayer Perceptrons (MLP) and Extreme Learning Machines (ELM). Currently (version 0.9.6) it does not support deep learning, though the plan is to extend this to this direction in the near future. Currently, it relies on the <strong>neuralnet<\/strong> package for R, which provides all the machinery to train MLPs. The training of ELMs is written within the <strong>nnfor<\/strong> package. Note that since <strong>neuralnet<\/strong> cannot tap on GPU processing, large networks tend to be very slow to train. <strong>nnfor<\/strong> differs from existing neural network implementations for R in that it provides code to automatically design networks with reasonable forecasting performance, but also provide in-depth control to the experienced user. The automatic specification is designed with parsimony in mind. This increases the robustness of the resulting networks, but also helps reduce the training time.<\/div>\n<div id=\"forecasting-with-mlps\" class=\"section level2\">\n<h2>Forecasting with MLPs<\/h2>\n<p>With the <strong>nnfor<\/strong> package you can either produce extrapolative (univariate) forecast, or include explanatory variables as well.<\/p>\n<div id=\"univariate-forecasting\" class=\"section level3\">\n<h3>Univariate forecasting<\/h3>\n<p>The main function is <code>mlp()<\/code>, and at its simplest form you only need to input a time series to be modelled.<\/p>\n<pre class=\"r\"><code>library(nnfor)\r\nfit1 &lt;- mlp(AirPassengers)\r\nprint(fit1)<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,10,12)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 7.4939.<\/code><\/pre>\n<p>The output indicates that the resulting network has 5 hidden nodes, it was trained 20 times and the different forecasts were combined using the median operator. The <code>mlp()<\/code> function automatically generates ensembles of networks, the training of which starts with different random initial weights. Furthermore, it provides the inputs that were included in the network. This <a href=\"http:\/\/kourentzes.com\/forecasting\/2014\/04\/19\/neural-network-ensemble-operators-for-time-series-forecasting\/\">paper<\/a> discusses the performance of different combination operators and finds that the median performs very well, the mode can achieve the best performance but needs somewhat larger ensembles and the arithmetic mean is probably best avoided! Another interesting finding in that paper is that bagging (i.e.\u00a0training the network on bootstrapped series) or using multiple random training initialisations results in similar performance, and therefore it appears that for time series forecasting we can avoid the bootstrapping step, greatly simplifying the process. These findings are embedded in the default settings of <strong>nnfor<\/strong>.<\/p>\n<p>You can get a visual summary by using the <code>plot()<\/code> function.<\/p>\n<pre class=\"r\"><code>plot(fit1)<\/code><\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1575\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1.png\" alt=\"\" width=\"500\" height=\"357\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1.png 1344w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1-150x107.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1-300x214.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1-768x549.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1-1024x731.png 1024w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-1-660x471.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>The grey input nodes are autoregressions, while the magenta ones are deterministic inputs (seasonality in this case). If any other regressors were included, they would be shown in light blue.<\/p>\n<p>The <code>mlp()<\/code> function accepts several arguments to fine-tune the resulting network. The <code>hd<\/code> argument defines a fixed number of hidden nodes. If it is a single number, then the neurons are arranged in a single hidden node. If it is a vector, then these are arranged in multiple layers.<\/p>\n<pre class=\"r\"><code>fit2 &lt;- mlp(AirPassengers, hd = c(10,5))\r\nplot(fit2)<\/code><\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1576\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2.png\" alt=\"\" width=\"500\" height=\"357\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2.png 1344w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2-150x107.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2-300x214.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2-768x549.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2-1024x731.png 1024w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-2-660x471.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>We will see later on how to automatically select the number of nodes. In my experience (and evidence from the literature), conventional neural networks, forecasting single time series, do not benefit from multiple hidden layers. The forecasting problem is typically just not that complex!<\/p>\n<p>The argument <code>reps<\/code> defines how many training repetitions are used. If you want to train a single network you can use <code>reps=1<\/code>, although there is overwhelming evidence that there is no benefit in doing so. The default <code>reps=20<\/code> is a compromise between training speed and performance, but the more repetitions you can afford the better. They help not only in the performance of the model, but also in the stability of the results, when the network is retrained. How the different training repetitions are combined is controlled by the argument <code>comb<\/code> that accepts the options <code>median<\/code>, <code>mean<\/code>, and <code>mode<\/code>. The mean and median are apparent. The mode is calculated using the maximum of a kernel density estimate of the forecasts for each time period. This is detailed in the aforementioned <a href=\"http:\/\/kourentzes.com\/forecasting\/2014\/04\/19\/neural-network-ensemble-operators-for-time-series-forecasting\/\">paper<\/a> and exemplified <a href=\"http:\/\/kourentzes.com\/forecasting\/2014\/08\/18\/ensembles-size-and-combination-operators\/\">here<\/a>.<\/p>\n<p>The argument <code>lags<\/code> allows you to select the autoregressive lags considered by the network. If this is not provided then the network uses lag 1 to lag <code>m<\/code>, the seasonal period of the series. These are suggested lags and they may not stay in the final networks. You can force that using the argument <code>keep<\/code>, or turn off the automatic input selection altogether using the argument <code>sel.lag=FALSE<\/code>. Observe the differences in the following calls of <code>mlp()<\/code>.<\/p>\n<pre class=\"r\"><code>mlp(AirPassengers, lags=1:24)<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,4,7,8,9,10,11,12,13,18,21,23,24)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 5.6388.<\/code><\/pre>\n<pre class=\"r\"><code>mlp(AirPassengers, lags=1:24, keep=c(rep(TRUE,12), rep(FALSE,12)))<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,9,10,11,12,13,18,21,23,24)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 3.6993.<\/code><\/pre>\n<pre class=\"r\"><code>mlp(AirPassengers, lags=1:24, sel.lag=FALSE)<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 1.7347.<\/code><\/pre>\n<p>In the first case lags (1,2,4,7,8,9,10,11,12,13,18,21,23,24) are retained. In the second case all 1-12 are kept and the rest 13-24 are tested for inclusion. Note that the argument <code>keep<\/code> must be a logical with equal length to the input used in <code>lags<\/code>. In the last case, all lags are retained. The selection of the lags heavily relies on <a href=\"http:\/\/kourentzes.com\/forecasting\/2010\/04\/19\/feature-selection-for-time-series-prediction-a-combined-filter-and-wrapper-approach-for-neural-networks\/\">this<\/a> and <a href=\"http:\/\/kourentzes.com\/forecasting\/2010\/04\/19\/frequency-independent-automatic-input-variable-selection-for-neural-networks-for-forecasting\/\">this<\/a> papers, with evidence of its performance on high-frequency time series outlined <a href=\"http:\/\/kourentzes.com\/forecasting\/2016\/07\/09\/the-impact-of-special-days-in-call-arrivals-forecasting-a-neural-network-approach-to-modelling-special-days\/\">here<\/a>. An overview is provided in: Ord K., Fildes R., Kourentzes N. (2017) Principles of Business Forecasting 2e. Wessex Press Publishing Co., Chapter 10.<\/p>\n<p>Note that if the selection algorithm decides that nothing should stay in the network, it will include lag 1 always and you will get a warning message: <code>No inputs left in the network after pre-selection, forcing AR(1)<\/code>.<\/p>\n<p>Neural networks are not great in modelling trends. You can find the arguments for this <a href=\"http:\/\/kourentzes.com\/forecasting\/2016\/12\/28\/can-neural-networks-predict-trended-time-series\/\">here<\/a>. Therefore it is useful to remove the trend from a time series prior to modelling it. This is handled by the argument <code>difforder<\/code>. If <code>difforder=0<\/code> no differencing is performed. For <code>diff=1<\/code>, level differences are performed. Similarly, if <code>difforder=12<\/code> then 12th order differences are performed. If the time series is seasonal with seasonal period 12, this would then be seasonal differences. You can do both with <code>difforder=c(1,12)<\/code> or any other set of difference orders. If <code>difforder=NULL<\/code> then the code decides automatically. If there is a trend, first differences are used. The series is also tested for seasonality. If there is, then the Canova-Hansen test is used to identify whether this is deterministic or stochastic. If it is the latter, then seasonal differences are added as well.<\/p>\n<p>Deterministic seasonality is better modelled using seasonal dummy variables. by default the inclusion of dummies is tested. This can be controlled by using the logical argument <code>allow.det.season<\/code>. The deterministic seasonality can be either a set of binary dummies, or a pair of sine-cosine (argument <code>det.type<\/code>), as outlined <a href=\"http:\/\/kourentzes.com\/forecasting\/2016\/07\/09\/the-impact-of-special-days-in-call-arrivals-forecasting-a-neural-network-approach-to-modelling-special-days\/\">here<\/a>. If the seasonal period is more than 12, then the trigonometric representation is recommended for parsimony.<\/p>\n<p>The logical argument <code>outplot<\/code> provides a plot of the fit of network.<\/p>\n<p>The number of hidden nodes can be either preset, using the argument <code>hd<\/code> or automatically specified, as defined with the argument <code>hd.auto.type<\/code>. By default this is <code>hd.auto.type=\"set\"<\/code> and uses the input provided in <code>hd<\/code> (default is 5). You can set this to <code>hd.auto.type=\"valid\"<\/code> to test using a validation sample (20% of the time series), or <code>hd.auto.type=\"cv\"<\/code> to use 5-fold cross-validation. The number of hidden nodes to evaluate is set by the argument <code>hd.max<\/code>.<\/p>\n<pre class=\"r\"><code>fit3 &lt;- mlp(AirPassengers, hd.auto.type=\"valid\",hd.max=8)\r\nprint(fit3)<\/code><\/pre>\n<pre><code>## MLP fit with 4 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,10,12)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 14.2508.<\/code><\/pre>\n<p>Given that training networks can be a time consuming business, you can reuse an already specified\/trained network. In the following example, we reuse <code>fit1<\/code> to a new time series.<\/p>\n<pre class=\"r\"><code>x &lt;- ts(sin(1:120*2*pi\/12),frequency=12)\r\nmlp(x, model=fit1)<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,10,12)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 0.0688.<\/code><\/pre>\n<p>This retains both the specification and the training from <code>fit1<\/code>. If you want to use only the specification, but retrain the network, then use the argument <code>retrain=TRUE<\/code>.<\/p>\n<pre class=\"r\"><code>mlp(x, model=fit1, retrain=TRUE)<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,10,12)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 0.<\/code><\/pre>\n<p>Observe the difference in the in-sample MSE between the two settings.<\/p>\n<p>Finally, you can pass arguments directly to the <code>neuralnet()<\/code> function that is used to train the networks by using the ellipsis <code>...<\/code>.<\/p>\n<p>To produce forecasts, we use the function <code>forecast()<\/code>, which requires a trained network object and the forecast horizon <code>h<\/code>.<\/p>\n<pre class=\"r\"><code>frc &lt;- forecast(fit1,h=12)\r\nprint(frc)<\/code><\/pre>\n<pre><code>##              Jan         Feb         Mar         Apr         May\r\n## 1961 447.3392668 421.2532515 497.5052166 521.1640683 537.4031708\r\n##              Jun         Jul         Aug         Sep         Oct\r\n## 1961 619.5015908 707.8790407 681.0523280 602.8467629 529.0477736\r\n##              Nov         Dec\r\n## 1961 470.8292734 517.6762262<\/code><\/pre>\n<pre class=\"r\"><code>plot(frc)<\/code><\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1577\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3.png\" alt=\"\" width=\"500\" height=\"357\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3.png 1344w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3-150x107.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3-300x214.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3-768x549.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3-1024x731.png 1024w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-3-660x471.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>The plot of the forecasts provides in grey the forecasts of all the ensemble members. The output of <code>forecast()<\/code> is of class <code>forecast<\/code> and those familiar with the <strong>forecast<\/strong> package will find familiar elements there. To access the point forecasts use <code>frc$mean<\/code>. The <code>frc$all.mean<\/code> contains the forecasts of the individual ensemble members.<\/p>\n<\/div>\n<div id=\"using-regressors\" class=\"section level3\">\n<h3>Using regressors<\/h3>\n<p>There are three arguments in the <code>mlp()<\/code> function that enable to use explanatory variables: <code>xreg<\/code>, <code>xreg.lags<\/code> and <code>xreg.keep<\/code>. The first is used to input additional regressors. These must be organised as an array and be at least as long as the in-sample time series, although it can be longer. I find it helpful to always provide <code>length(y)+h<\/code>. Let us suppose that we want to use a deterministic trend to forecast the time series. First, we construct the input and then model the series.<\/p>\n<pre class=\"r\"><code>z &lt;- 1:(length(AirPassengers)+24) # I add 24 extra observations for the forecasts\r\nz &lt;- cbind(z) # Convert it into a column-array\r\nfit4 &lt;- mlp(AirPassengers,xreg=z,xreg.lags=list(0),xreg.keep=list(TRUE),\r\n            # Add a lag0 regressor and force it to stay in the model\r\n            difforder=0) # Do not let mlp() to remove the stochastic trend\r\nprint(fit4)<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Univariate lags: (1,4,5,8,9,10,11,12)\r\n## 1 regressor included.\r\n## - Regressor 1 lags: (0)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 32.4993.<\/code><\/pre>\n<p>The output reflects the inclusion of the regressor. This is reflected in the plot of the network with a light blue input.<\/p>\n<pre class=\"r\"><code>plot(fit4)<\/code><\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1578\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4.png\" alt=\"\" width=\"500\" height=\"357\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4.png 1344w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4-150x107.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4-300x214.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4-768x549.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4-1024x731.png 1024w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-4-660x471.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Observe that <code>z<\/code> is organised as an array. If this is a vector you will get an error. To include more lags, we expand the <code>xreg.lags<\/code>:<\/p>\n<pre class=\"r\"><code>mlp(AirPassengers,difforder=0,xreg=z,xreg.lags=list(1:12))<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Univariate lags: (1,4,5,8,9,10,11,12)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 48.8853.<\/code><\/pre>\n<p>Observe that nothing was included in the network. We use the <code>xreg.keep<\/code> to force these in.<\/p>\n<pre class=\"r\"><code>mlp(AirPassengers,difforder=0,xreg=z,xreg.lags=list(1:12),xreg.keep=list(c(rep(TRUE,3),rep(FALSE,9))))<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Univariate lags: (1,4,5,8,9,10,11,12)\r\n## 1 regressor included.\r\n## - Regressor 1 lags: (1,2,3)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 32.8439.<\/code><\/pre>\n<p>Clearly, the network does not like the deterministic trend! It only retains it, if we force it. Observe that both <code>xreg.lags<\/code> and <code>xreg.keep<\/code> are lists. Where each list element corresponds to a column in <code>xreg<\/code>. As an example, we will encode extreme residuals of <code>fit1<\/code> as a single input (see this <a href=\"http:\/\/kourentzes.com\/forecasting\/2016\/07\/09\/the-impact-of-special-days-in-call-arrivals-forecasting-a-neural-network-approach-to-modelling-special-days\/\">paper<\/a> for a discussion on how networks can code multiple binary dummies in a single one). For this I will use the function <code>residout()<\/code> from the <strong>tsutils<\/strong> package.<\/p>\n<pre class=\"r\"><code>if (!require(\"tsutils\")){install.packages(\"tsutils\")}\r\nlibrary(tsutils)\r\nloc &lt;- residout(AirPassengers - fit1$fitted, outplot=FALSE)$location\r\nzz &lt;- cbind(z, 0)\r\nzz[loc,2] &lt;- 1\r\nfit5 &lt;- mlp(AirPassengers,xreg=zz, xreg.lags=list(c(0:6),0),xreg.keep=list(rep(FALSE,7),TRUE))\r\nprint(fit5)<\/code><\/pre>\n<pre><code>## MLP fit with 5 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,10,12)\r\n## 1 regressor included.\r\n## - Regressor 1 lags: (0)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## MSE: 7.2178.<\/code><\/pre>\n<p>Obviously, you can include as many regressors as you want.<\/p>\n<p>To produce forecasts, we use the <code>forecast()<\/code> function, but now use the <code>xreg<\/code> input. The way to make this work is to input the regressors starting from the same observation that was used during the training of the network, expanded as need to cover the forecast horizon. You do not need to eliminate unused regressors. The network will take care of this.<\/p>\n<pre class=\"r\"><code>frc.reg &lt;- forecast(fit5,xreg=zz)<\/code><\/pre>\n<\/div>\n<\/div>\n<div id=\"forecasting-with-elms\" class=\"section level2\">\n<h2>Forecasting with ELMs<\/h2>\n<p>To use Extreme Learning Machines (EMLs) you can use the function <code>elm()<\/code>. Many of the inputs are identical to <code>mlp()<\/code>. By default ELMs start with a very large hidden layer (100 nodes) that is pruned as needed.<\/p>\n<pre class=\"r\"><code>fit6 &lt;- elm(AirPassengers)\r\nprint(fit6)<\/code><\/pre>\n<pre><code>## ELM fit with 100 hidden nodes and 20 repetitions.\r\n## Series modelled in differences: D1.\r\n## Univariate lags: (1,2,3,4,5,6,7,8,10,12)\r\n## Deterministic seasonal dummies included.\r\n## Forecast combined using the median operator.\r\n## Output weight estimation using: lasso.\r\n## MSE: 91.288.<\/code><\/pre>\n<pre class=\"r\"><code>plot(fit6)<\/code><\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1579\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5.png\" alt=\"\" width=\"500\" height=\"357\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5.png 1344w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5-150x107.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5-300x214.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5-768x549.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5-1024x731.png 1024w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-5-660x471.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Observe that the plot of the network has some black and some grey lines. The latter are pruned. There are 20 networks fitted (controlled by the argument <code>reps<\/code>). Each network may have different final connections. You can inspect these by using <code>plot(fit6,1)<\/code>, where the second argument defines which network to plot.<\/p>\n<pre class=\"r\"><code>par(mfrow=c(2,2))\r\nfor (i in 1:4){plot(fit6,i)}\r\npar(mfrow=c(1,1))<\/code><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1580\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6.png\" alt=\"\" width=\"500\" height=\"357\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6.png 1344w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6-150x107.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6-300x214.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6-768x549.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6-1024x731.png 1024w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-6-660x471.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/pre>\n<p>How the pruning is done is controlled by the argument.<code>type<\/code> The default option is to use LASSO regression (type=\u201classo\u201d). Alternatively, use can use \u201cridge\u201d for ridge regression, \u201cstep\u201d for stepwise OLS and \u201clm\u201d to get the OLS solution with no pruning.<\/p>\n<p>The other difference from the <code>mlp()<\/code> function is the <code>barebone<\/code> argument. When this is <code>FALSE<\/code>, then the ELMs are built based on the <strong>neuralnet<\/strong> package. If this is set to <code>TRUE<\/code> then a different internal implementation is used, which is helpful to speed up calculations when the number of inputs is substantial.<\/p>\n<p>To forecast, use the <code>forecast()<\/code> function in the same way as before.<\/p>\n<pre class=\"r\"><code>forecast(fit6,h=12)<\/code><\/pre>\n<pre><code>##              Jan         Feb         Mar         Apr         May\r\n## 1961 449.0982276 423.0329121 455.7643540 488.5096713 499.3616506\r\n##              Jun         Jul         Aug         Sep         Oct\r\n## 1961 567.7486203 645.6309202 622.8311280 543.9549163 495.8305278\r\n##              Nov         Dec\r\n## 1961 429.9258197 468.7921570<\/code><\/pre>\n<\/div>\n<div id=\"temporal-hierarchies-and-nnfor\" class=\"section level2\">\n<h2>Temporal hierarchies and <strong>nnfor<\/strong><\/h2>\n<p>People who have followed my research will be familiar with <a href=\"http:\/\/kourentzes.com\/forecasting\/2017\/07\/22\/multiple-temporal-aggregation-the-story-so-far-part-iv-temporal-hierarchies\/\">Temporal Hierarchies<\/a> that are implemented in the package <strong>thief<\/strong>. You can use both <code>mlp()<\/code> and <code>elm()<\/code> with <code>thief()<\/code> by using the functions <code>mlp.thief()<\/code> and <code>elm.thief()<\/code>.<\/p>\n<pre class=\"r\"><code>if (!require(\"thief\")){install.packages(\"thief\")}\r\nlibrary(thief)\r\nthiefMLP &lt;- thief(AirPassengers,forecastfunction=mlp.thief)\r\n# Similarly elm.thief:\r\nthiefELM &lt;- thief(AirPassengers,forecastfunction=elm.thief)\r\npar(mfrow=c(1,2))\r\nplot(thiefMLP)\r\nplot(thiefELM)\r\npar(mfrow=c(1,1))<\/code><\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1581\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7.png\" alt=\"\" width=\"500\" height=\"357\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7.png 1344w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7-150x107.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7-300x214.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7-768x549.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7-1024x731.png 1024w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2019\/01\/nnfor-7-660x471.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>This should get you going with time series forecasting with neural networks.<\/p>\n<p><em>Happy forecasting!<\/em><\/p>\n<\/div>\n<div class=\"SPOSTARBUST-Related-Posts\"><H3>Related Posts<\/H3><ul class=\"entry-meta\"><li class=\"SPOSTARBUST-Related-Post\"><a title=\"Discussion panel on &#8216;AI in research&#8217; at Sk\u00f6vde\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/11\/06\/discussion-panel-on-ai-in-research-at-skovde\/\" rel=\"bookmark\">Discussion panel on &#8216;AI in research&#8217; at Sk\u00f6vde<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"OR62 -The quest for greater forecasting accuracy: Perspectives from Statistics &#038; Machine Learning\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/10\/20\/or62-forecasting-stream\/\" rel=\"bookmark\">OR62 -The quest for greater forecasting accuracy: Perspectives from Statistics &#038; Machine Learning<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"Forecasting keynote at AMLC 2019\" href=\"https:\/\/kourentzes.com\/forecasting\/2019\/08\/01\/forecasting-keynote-at-amlc-2019\/\" rel=\"bookmark\">Forecasting keynote at AMLC 2019<\/a><\/li>\n<\/ul><\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>The nnfor (development version here) package for R facilitates time series forecasting with Multilayer Perceptrons (MLP) and Extreme Learning Machines (ELM). Currently (version 0.9.6) it does not support deep learning, though the plan is to extend this to this direction in the near future. Currently, it relies on the neuralnet package for R, which provides\u2026 <span class=\"read-more\"><a href=\"https:\/\/kourentzes.com\/forecasting\/2019\/01\/16\/tutorial-for-the-nnfor-r-package\/\">Read More &raquo;<\/a><\/span><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41],"tags":[45,12,39],"_links":{"self":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1574"}],"collection":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/comments?post=1574"}],"version-history":[{"count":3,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1574\/revisions"}],"predecessor-version":[{"id":1584,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1574\/revisions\/1584"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/media?parent=1574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/categories?post=1574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/tags?post=1574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- WP Super Cache is installed but broken. The constant WPCACHEHOME must be set in the file wp-config.php and point at the WP Super Cache plugin directory. -->