{"id":1468,"date":"2017-12-11T09:18:42","date_gmt":"2017-12-11T09:18:42","guid":{"rendered":"http:\/\/kourentzes.com\/forecasting\/?p=1468"},"modified":"2017-12-11T09:18:42","modified_gmt":"2017-12-11T09:18:42","slug":"update-for-nnfor-reuse-and-retrain-models","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/forecasting\/2017\/12\/11\/update-for-nnfor-reuse-and-retrain-models\/","title":{"rendered":"Update for nnfor: reuse and retrain models"},"content":{"rendered":"<p>A feature that was missing in the nnfor package was the ability to reuse and retrain mlp and elm model fits. This is now possible with the new arguments <code>model<\/code> and <code>retrain<\/code>.<\/p>\n<p>As an example, let us use the AirPassengers time series, with three different sample sizes and re-use and re-train the same model in various combinations.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nlibrary(nnfor)\r\n# Get some data\r\ny &lt;- AirPassengers\r\ny1 &lt;- window(y,end=c(1958,12))\r\ny2 &lt;- window(y,end=c(1959,12))\r\ny3 &lt;- window(y,end=c(1960,12))\r\n\r\n# Fit NN \r\nfit &lt;- list()\r\nfit[[1]] &lt;- mlp(y1)\r\nfit[[2]] &lt;- mlp(y2,model=fit[[1]])\r\nfit[[3]] &lt;- mlp(y2,model=fit[[1]],retrain=TRUE)\r\nfit[[4]] &lt;- mlp(y3,model=fit[[1]])\r\nfit[[5]] &lt;- mlp(y3,model=fit[[3]])\r\nfit[[6]] &lt;- mlp(y3,model=fit[[1]],retrain=TRUE)\r\nfit[[7]] &lt;- mlp(y3)\r\n\r\n# Get MSE and number of lags\r\nmse &lt;- unlist(lapply(fit,function(x){x$MSE}))\r\nlags &lt;- unlist(lapply(fit,function(x){length(x$lags)}))\r\nround(mse,2)\r\nlags\r\n<\/pre>\n<div class=\"table-responsive\"><table  style=\"width:560px; \"  class=\"easy-table easy-table-default \" >\n<caption>Results<\/caption>\n<thead>\r\n<tr><th  style=\"text-align:left\" >Model<\/th>\n<th  style=\"text-align:center\" >Series Sample<\/th>\n<th  style=\"text-align:center\" >Training Sample<\/th>\n<th  style=\"text-align:center\" >Retrain<\/th>\n<th  style=\"text-align:center\" >MSE<\/th>\n<th  style=\"text-align:center\" >Lags<\/th>\n<\/tr>\n<\/thead>\n<tbody>\r\n<tr><td  style=\"text-align:left\" >fit[[1]]<\/td>\n<td  style=\"text-align:center\" >up to 1958<\/td>\n<td  style=\"text-align:center\" >up to 1958<\/td>\n<td  style=\"text-align:center\" >X<\/td>\n<td  style=\"text-align:center\" >6.73<\/td>\n<td  style=\"text-align:center\" >9<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >fit[[2]]<\/td>\n<td  style=\"text-align:center\" >up to 1958<\/td>\n<td  style=\"text-align:center\" >up to 1958<\/td>\n<td  style=\"text-align:center\" ><\/td>\n<td  style=\"text-align:center\" >61.25<\/td>\n<td  style=\"text-align:center\" >9<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >fit[[3]]<\/td>\n<td  style=\"text-align:center\" >up to 1959<\/td>\n<td  style=\"text-align:center\" >up to 1959<\/td>\n<td  style=\"text-align:center\" >X<\/td>\n<td  style=\"text-align:center\" >6.68<\/td>\n<td  style=\"text-align:center\" >9<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >fit[[4]]<\/td>\n<td  style=\"text-align:center\" >up to 1960<\/td>\n<td  style=\"text-align:center\" >up to 1958<\/td>\n<td  style=\"text-align:center\" ><\/td>\n<td  style=\"text-align:center\" >541.13<\/td>\n<td  style=\"text-align:center\" >9<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >fit[[5]]<\/td>\n<td  style=\"text-align:center\" >up to 1960<\/td>\n<td  style=\"text-align:center\" >up to 1959<\/td>\n<td  style=\"text-align:center\" ><\/td>\n<td  style=\"text-align:center\" >260.22<\/td>\n<td  style=\"text-align:center\" >9<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >fit[[6]]<\/td>\n<td  style=\"text-align:center\" >up to 1960<\/td>\n<td  style=\"text-align:center\" >up to 1960<\/td>\n<td  style=\"text-align:center\" >X<\/td>\n<td  style=\"text-align:center\" >12.65<\/td>\n<td  style=\"text-align:center\" >9<\/td>\n<\/tr>\n\r\n<tr><td  style=\"text-align:left\" >fit[[7]]<\/td>\n<td  style=\"text-align:center\" >up to 1960<\/td>\n<td  style=\"text-align:center\" >up to 1960<\/td>\n<td  style=\"text-align:center\" >New fit<\/td>\n<td  style=\"text-align:center\" >7.95<\/td>\n<td  style=\"text-align:center\" >10<\/td>\n<\/tr>\n<\/tbody><\/table><\/div>\n<p>As you can see, with different in-sample data and no retraining the in-sample MSE deteriorates. Using the &#8220;up to 1958&#8221; fit on the &#8220;up to 1959&#8221; and &#8220;up to 1960&#8221; samples returns MSE of 61.25 and 541.13 respectively. If we refit the network (keeping the specification fixed) the error reduces to 6.68 and 12.65 respectively. Building a new model on the &#8220;up to 1960&#8221; data finds a different lag structure (increasing the used lags from 9 to 10), resulting in an MSE of 7.95.<\/p>\n<p>The same arguments can be used with <code>elm<\/code>.<\/p>\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>A feature that was missing in the nnfor package was the ability to reuse and retrain mlp and elm model fits. This is now possible with the new arguments model and retrain. As an example, let us use the AirPassengers time series, with three different sample sizes and re-use and re-train the same model in\u2026 <span class=\"read-more\"><a href=\"https:\/\/kourentzes.com\/forecasting\/2017\/12\/11\/update-for-nnfor-reuse-and-retrain-models\/\">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":[14,45,12,39],"_links":{"self":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1468"}],"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=1468"}],"version-history":[{"count":5,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1468\/revisions"}],"predecessor-version":[{"id":1473,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1468\/revisions\/1473"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/media?parent=1468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/categories?post=1468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/tags?post=1468"}],"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. -->