{"id":355,"date":"2014-05-04T14:05:49","date_gmt":"2014-05-04T14:05:49","guid":{"rendered":"http:\/\/kourentzes.com\/forecasting\/?p=355"},"modified":"2014-12-18T10:43:33","modified_gmt":"2014-12-18T10:43:33","slug":"update-for-tstools","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/forecasting\/2014\/05\/04\/update-for-tstools\/","title":{"rendered":"Update for TStools"},"content":{"rendered":"<p style=\"text-align: justify;\">Amongst various minor improvements a few interesting new functions have been added to <a title=\"TStools for R\" href=\"http:\/\/kourentzes.com\/forecasting\/2014\/04\/19\/tstools-for-r\/\">TStools<\/a> package for R:<\/p>\n<ul style=\"text-align: justify;\">\n<li>Theta method<\/li>\n<li>ABC-XYZ analysis<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Theta method was found to be the most accurate in the M3 forecasting competition, but since then there has been limited use of the method. As it was later shown, the M3 competition Theta method setup is equivalent to using a single exponential smoothing with drift and an additive constant, with classical decomposition.<\/p>\n<p style=\"text-align: justify;\">This is a beta function incorporating a few tweaks over the original method:<\/p>\n<ul style=\"text-align: justify;\">\n<li>Decomposition is performed only if seasonality is detected. The original method tested this using an ACF based test. This implementation uses the approach used in the seasplot function;<\/li>\n<li>Seasonality can be additive or multiplicative, estimated using classical decomposition or a pure seasonal exponential smoothing model;<\/li>\n<li style=\"text-align: justify;\">If decomposition is performed then the Theta0 line is based on the trend component, rather than the deseasonalised time series;<\/li>\n<li>Theta0 is a linear trend only if changes in the level are detected, otherwise it is just a mean level estimation;<\/li>\n<li>Theta0 and Theta2 lines, as well as the pure seasonal model can be optimised with various cost functions. The idea is that it may be beneficial to have robust Theta0 line (optimised on MAE) and more reactive Theta2 line and so on.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Limited benchmarking has shown that this implementation is marginally more accurate than the original Theta method, or the reformulated one. The best setup and options are still under investigation!<\/p>\n<p style=\"text-align: justify;\">To use the method simply run:<\/p>\n<pre>&gt; theta(referrals,h=18)<\/pre>\n<p>The output includes the forecasted values, the result of trend and seasonality identification, the predicted values of the various Theta lines and the seasonal component, model parameters and cost functions used. There are two different visualisations available, just the forecast:<br \/>\n<a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig10.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-356 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig10-300x230.png\" alt=\"tstoolsR.fig10\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig10-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig10-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig10.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nor the forecast with the Theta lines. I quite prefer the second one, as it explains how the forecast is calculated:<br \/>\n<a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig11.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-357 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig11-300x230.png\" alt=\"tstoolsR.fig11\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig11-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig11-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig11.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">There are three functions to support the ABC-XYZ analysis. Let us first get some quarterly time series to analyse:<\/p>\n<pre>&gt; library(Mcomp)\r\n&gt; M3.subset &lt;- subset(M3, \"QUARTERLY\")\r\n&gt; x &lt;- matrix(NA,46,100)\r\n&gt; for (i in 1:100){\r\n  x.temp &lt;- c(M3.subset[[i]]$x,M3.subset[[i]]$xx)\r\n  x[1:length(x.temp),i] &lt;-x.temp\r\n}<\/pre>\n<p style=\"text-align: justify;\">To perform ABC analysis use the <em>abc<\/em> function. Unless the importance of a series is calculated externally, then it is estimated as the mean volume of sales of a product.<\/p>\n<pre>&gt; imp &lt;- abc(x,outplot=TRUE)<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.12.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-358 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.12-300x230.png\" alt=\"tstoolsR.fig.12\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.12-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.12-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.12.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nThe plot shows how concentrated is the value in the A, B or C classes. The bigger the difference between the straight diagonal line and the concentration curve, the more value is included in the A and B classes. For example, here we have set class A to contain 20% of the products, but it accounts for 28.7% of the value. Another example with very high concentration is the following:<\/p>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.17.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-366 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.17-300x230.png\" alt=\"tstoolsR.fig.17\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.17-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.17-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.17.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">Here A products account for 47.9% of the total value. On the other hand, the C products account for only 11.8% of the total value, even though this class contains 50% of the items.<\/p>\n<p style=\"text-align: justify;\">Although ABC analysis usually classifies items in three groups, it is fairly easy to introduce more groups, depending on the percentages used:<\/p>\n<pre>&gt; abc(x,c(0.1,0.2,0.3,0.4))<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.13.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-359 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.13-300x230.png\" alt=\"tstoolsR.fig.13\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.13-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.13-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.13.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nSimilarly XYZ analysis can be done using the <em>xyz<\/em> function. Forecastability can be estimated in three different ways. The default is to calculate a scaled in-sample RMSE of the best of two methods: Naive and Seasonal Naive. The scaling is done by dividing the RMSE with mean of each time series. The second approach is to calculate the coefficient of variation for each time series. The last approach is to fit an ETS model (using the <em>forecast<\/em> package) and calculate again the scaled in-sample RMSE. The advantage of the first approach is that it can tackle with seasonal time series and it is relatively fast. The second approach is very simple, but it does not handle well trended or seasonal time series. The last approach selects that appropriate ETS model for each time series, thus being able to handle most types of time series. On the downside, it requires substantially more time.<\/p>\n<pre>&gt; frc &lt;- xyz(x,m=4,outplot=TRUE)<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.14.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-360 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.14-300x230.png\" alt=\"tstoolsR.fig.14\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.14-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.14-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.14.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nAs with the <em>abc<\/em> function, external measures of forecastability can be used and any number of classes.<\/p>\n<p style=\"text-align: justify;\">Finally, the function the <em>abcxyz<\/em> visualises the combined classification:<\/p>\n<pre>&gt; abcxyz(imp,frc)\r\n   Z  Y  X\r\nA  4  7  9\r\nB  6  7 17\r\nC 10 16 24\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.15.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-361 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.15-300x230.png\" alt=\"tstoolsR.fig.15\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.15-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.15-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.15.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nThis function also supports custom number of classes.<br \/>\n<a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.16.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-362 size-medium aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.16-300x230.png\" alt=\"tstoolsR.fig.16\" width=\"300\" height=\"230\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.16-300x230.png 300w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.16-150x115.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/05\/tstoolsR.fig_.16.png 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<div class=\"SPOSTARBUST-Related-Posts\"><H3>Related Posts<\/H3><ul class=\"entry-meta\"><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=\"Tutorial for the nnfor R package\" href=\"https:\/\/kourentzes.com\/forecasting\/2019\/01\/16\/tutorial-for-the-nnfor-r-package\/\" rel=\"bookmark\">Tutorial for the nnfor R package<\/a><\/li>\n<li class=\"SPOSTARBUST-Related-Post\"><a title=\"R package: tsutils\" href=\"https:\/\/kourentzes.com\/forecasting\/2019\/01\/14\/r-package-tsutils\/\" rel=\"bookmark\">R package: tsutils<\/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>Amongst various minor improvements a few interesting new functions have been added to TStools package for R: Theta method ABC-XYZ analysis Theta method was found to be the most accurate in the M3 forecasting competition, but since then there has been limited use of the method. As it was later shown, the M3 competition Theta\u2026 <span class=\"read-more\"><a href=\"https:\/\/kourentzes.com\/forecasting\/2014\/05\/04\/update-for-tstools\/\">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,39,51,42],"_links":{"self":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/355"}],"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=355"}],"version-history":[{"count":0,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/tags?post=355"}],"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. -->