{"id":1449,"date":"2017-11-09T16:33:58","date_gmt":"2017-11-09T16:33:58","guid":{"rendered":"http:\/\/kourentzes.com\/forecasting\/?p=1449"},"modified":"2017-12-10T12:07:31","modified_gmt":"2017-12-10T12:07:31","slug":"mapax-example-for-r","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/forecasting\/2017\/11\/09\/mapax-example-for-r\/","title":{"rendered":"MAPAx example for R"},"content":{"rendered":"<p>I have been asked a few time to provide an example how to use MAPAx, from the <a href=\"https:\/\/cran.r-project.org\/package=MAPA\" target=\"_blank\" rel=\"noopener\">MAPA<\/a> package for R, so I prepared this blog post. I admit the documentation could be better, so I put together this example from a retailing case &#8211; the original setting MAPAx was developed for (see <a href=\"http:\/\/kourentzes.com\/forecasting\/2015\/09\/08\/forecasting-with-multivariate-temporal-aggregation-the-case-of-promotional-modelling\/\">paper here<\/a>). This provides a step by step use of MAPAx.<\/p>\n<p>First we load the package and some data.<\/p>\n<pre class=\"brush: r; gutter: false; title: ; notranslate\" title=\"\">\r\n\r\ny &lt;- cbind(c(1030.829,893.551,1084.09,1278.436,936.708,915.322,885.713,2364.399,774.88,977.506,831.616,813.656,1569.956,967.925,806.146,1063.117,1123.787,906.686,996.498,1088.464,977.414,1128.328,896.594,1007.172,1046.379,1514.648,1626.115,2959.558,838.506,949.377,1433.307,805.048,1218.907,872.43,1730.103,865.734,1845.713,919.291,1003.363,1102.969,847.38,1965.26,809.673,953.193,1066.089,991.352,1115.694,1003.333,1090.48,930.749,1006.184,1239.068,873.707,728.583,881.316,1302.468,997.442,3481.118,841.042,997.601,1830.194,909.693,2358.406,2573.673,777.08,773.781,945.424,968.646,1074.589,1046.22,1155.559,990.627,931.943,786.285,2297.025,628.166,889.238,937.631,1113.925,870.384,1018.375,799.458,1542.328,1879.587,750.307,1087.948,1247.803,1052.352,883.899,793.126,913.736,1082.142,968.823,2099.176,841.224,964.227,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA))\r\nx &lt;- cbind(c(0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),\r\n c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0),\r\n c(0,0,0,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,1,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,1,0,0))\r\ny &lt;- y[!is.na(y)]\r\ny &lt;- ts(y,frequency=12)\r\n\r\n<\/pre>\n<p>This toy set includes sales of a heavily promoted item, with three different promotion. Let&#8217;s visualise what we have:<\/p>\n<pre class=\"brush: r; gutter: false; title: ; notranslate\" title=\"\">\r\n\r\nplot(y)\r\ncmp &lt;- brewer.pal(3,&quot;Set1&quot;)\r\nfor (i in 1:3){\r\n  points(time(y)[which(x[,i]==1)],y[which(x[,i]==1)],col=cmp[i],pch=i,cex=1.5)\r\n}\r\nlegend(&quot;topright&quot;,c(&quot;Promo1&quot;,&quot;Promo2&quot;,&quot;Promo3&quot;),col=cmp,pch=1:3)\r\n\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1451\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example1.png\" alt=\"\" width=\"500\" height=\"246\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example1.png 830w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example1-150x74.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example1-300x147.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example1-768x378.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example1-660x324.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Now let&#8217;s fit a MAPA and MAPAx and produce forecasts. I am modelling the sales in logs to capture the multiplicative promotional effects. For other types of applications this is not needed.<\/p>\n<pre class=\"brush: r; gutter: false; title: ; notranslate\" title=\"\">\r\nlibrary(MAPA)\r\nmapafit.x &lt;- mapaest(log(y),type=&quot;es&quot;,display=1,outplot=1,xreg=x)\r\nmapafit &lt;- mapaest(log(y),type=&quot;es&quot;,outplot=1)\r\n\r\nfrc.x &lt;- mapafor(log(y),mapafit.x,ifh=13,fh=13,xreg=x,conf.lvl=c(0.8,0.9,0.95),comb=&quot;w.mean&quot;)\r\nfrc &lt;- mapafor(log(y),mapafit,ifh=13,fh=13,conf.lvl=c(0.8,0.9,0.95),comb=&quot;w.mean&quot;)\r\n\r\n<\/pre>\n<p>Let&#8217;s plot the results<\/p>\n<pre class=\"brush: r; gutter: false; title: ; notranslate\" title=\"\">\r\n\r\npar(mfrow=c(1,2))\r\nplot(1:96, y, type=&quot;l&quot;,xlim=c(1,109),xaxs=&quot;i&quot;,main=&quot;MAPA&quot;,xlab=&quot;Period&quot;,ylab=&quot;Sales&quot;)\r\nfor (i in 1:96){\r\n  lines(i:(12+i),exp(frc$infor[,i]),col=&quot;red&quot;)\r\n}\r\ncmp &lt;- brewer.pal(9,&quot;Reds&quot;)[4:2]\r\nfor (i in 1:3){\r\n  polygon(c(97:109,109:97),exp(c(frc$PI[i,],rev(frc$PI[7-i,]))),col=cmp[i],border=NA)\r\n}\r\nlines(97:109,exp(frc$outfor),col=&quot;red&quot;)\r\n\r\nplot(1:96, y, type=&quot;l&quot;,xlim=c(1,109),xaxs=&quot;i&quot;,main=&quot;MAPAx&quot;,xlab=&quot;Period&quot;,ylab=&quot;Sales&quot;)\r\nfor (i in 1:96){\r\n  lines(i:(12+i),exp(frc.x$infor[,i]),col=&quot;red&quot;)\r\n}\r\ncmp &lt;- brewer.pal(9,&quot;Reds&quot;)[4:2]\r\nfor (i in 1:3){\r\n  polygon(c(97:109,109:97),exp(c(frc.x$PI[i,],rev(frc.x$PI[7-i,]))),col=cmp[i],border=NA)\r\n}\r\nlines(97:109,exp(frc.x$outfor),col=&quot;red&quot;)\r\n\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1452\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example2.png\" alt=\"\" width=\"500\" height=\"246\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example2.png 830w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example2-150x74.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example2-300x147.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example2-768x378.png 768w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2017\/11\/mapax_example2-660x324.png 660w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>As you can see, MAPA provides a flat forecast. This is &#8220;correct&#8221; in the sense that apart from promotions this time series contains like information that could be captured by exponential smoothing. On the other hand, MAPAx is provided with the promotional information and makes use of it.<\/p>\n<div class=\"SPOSTARBUST-Related-Posts\"><H3>Related Posts<\/H3><ul class=\"entry-meta\"><li class=\"SPOSTARBUST-Related-Post\"><a title=\"Special issue on innovations in hierarchical forecasting\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/10\/25\/special-issue-on-innovations-in-hierarchical-forecasting\/\" rel=\"bookmark\">Special issue on innovations in hierarchical forecasting<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"Elucidate structure in intermittent demand time series\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/05\/25\/elucidate-structure-in-intermittent-demand-time-series\/\" rel=\"bookmark\">Elucidate structure in intermittent demand time series<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"Cross-temporal aggregation: Improving the forecast accuracy of hierarchical electricity consumption\" href=\"https:\/\/kourentzes.com\/forecasting\/2020\/05\/25\/cross-temporal-aggregation-improving-the-forecast-accuracy-of-hierarchical-electricity-consumption\/\" rel=\"bookmark\">Cross-temporal aggregation: Improving the forecast accuracy of hierarchical electricity consumption<\/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>I have been asked a few time to provide an example how to use MAPAx, from the MAPA package for R, so I prepared this blog post. I admit the documentation could be better, so I put together this example from a retailing case &#8211; the original setting MAPAx was developed for (see paper here).\u2026 <span class=\"read-more\"><a href=\"https:\/\/kourentzes.com\/forecasting\/2017\/11\/09\/mapax-example-for-r\/\">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,38,33,39,36],"_links":{"self":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1449"}],"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=1449"}],"version-history":[{"count":5,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1449\/revisions"}],"predecessor-version":[{"id":1460,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/1449\/revisions\/1460"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/media?parent=1449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/categories?post=1449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/tags?post=1449"}],"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. -->