{"id":138,"date":"2014-04-19T16:48:22","date_gmt":"2014-04-19T16:48:22","guid":{"rendered":"http:\/\/kourentzes.com\/forecasting\/?p=138"},"modified":"2014-12-18T10:44:32","modified_gmt":"2014-12-18T10:44:32","slug":"tstools-for-r","status":"publish","type":"post","link":"https:\/\/kourentzes.com\/forecasting\/2014\/04\/19\/tstools-for-r\/","title":{"rendered":"TStools for R"},"content":{"rendered":"<p>This is a collection of functions for time series analysis\/modelling for R. Follow link to <a href=\"https:\/\/github.com\/trnnick\/TStools\">GitHub<\/a>. If you need help installing this package in R have a look at this <a title=\"How to use R packages from GitHub\" href=\"http:\/\/kourentzes.com\/forecasting\/2014\/04\/21\/how-to-use-r-packages-from-github\/\">post<\/a>. Alternatively just type in R the following commands:<\/p>\n<pre>&gt; if (!require(\"devtools\"))\r\n    install.packages(\"devtools\")\r\n&gt; devtools::install_github(\"trnnick\/TStools\")\r\n<\/pre>\n<p>At the time of posting the following functions are included:<\/p>\n<ul>\n<li>cmav: Centred moving average;<\/li>\n<li>coxstuart: Cox-Stuart test;<\/li>\n<li>decomp: Time series decomposition;<\/li>\n<li>kdemode: Mode estimation via KDE;<\/li>\n<li>nemenyi: Friedman and Nemenyi tests;<\/li>\n<li>regopt: Identify regression beta using various cost functions;<\/li>\n<li>seasplot: Seasonal plots.<\/li>\n<\/ul>\n<p>Here are some examples of what these functions do.<\/p>\n<p>The <em>cmav<\/em> function will calculate the centred moving average of a time series. It differs from the <em>ma<\/em> function in the forecast package in the sense that it can provide values for the first and last segment of the time series, where the centred moving average cannot be calculated, by fitting an ETS model.<\/p>\n<pre>&gt; cmav(referrals,outplot=TRUE)<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/tstoolR.fig9_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-311 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/tstoolR.fig9_.png\" alt=\"tstoolR.fig9\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/tstoolR.fig9_-150x116.png 150w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/tstoolR.fig9_-300x232.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><\/p>\n<p>The <em>decomp<\/em> function performs classical decomposition on a time series.<\/p>\n<pre>&gt; decomp(referrals,outplot=TRUE)\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig1_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-260 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig1_.png\" alt=\"tstoolR.fig1\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig1_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig1_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><br \/>\nYou can estimate the season using either the mean or the median of the historical indices or fit a pure seasonal model. Here, I also choose the type of decomposition and ask the seasonal indices for the next 12 months.<\/p>\n<pre>&gt; decomp(referrals,outplot=TRUE,type=\"pure.seasonal\",decomposition=\"additive\",h=12)\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig2_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-261 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig2_.png\" alt=\"tstoolR.fig2\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig2_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig2_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><\/p>\n<p>The <em>kdemode<\/em> can be used to find the mode of a distribution by kernel density estimation. The bandwidth is automatically identified.<\/p>\n<pre>&gt; data &lt;- rlnorm(200,mean=0,sd=1)\r\nkdemode(data,outplot=TRUE)<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig3_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-262 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig3_.png\" alt=\"tstoolR.fig3\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig3_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig3_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><br \/>\nThe bandwidth is estimated automatically using the excellent estimation proposed by <a href=\"http:\/\/projecteuclid.org\/euclid.aos\/1281964340\">Botev et al<\/a>. Alternatively the bandwidth can be calculated using either the Sheater and Jones method or the Silverman heuristic. This was quite useful for producing the mode ensemble neural networks that are proposed <a title=\"Neural network ensemble operators for time series forecasting\" href=\"http:\/\/kourentzes.com\/forecasting\/2014\/04\/19\/neural-network-ensemble-operators-for-time-series-forecasting\/\">here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>The <em>nemenyi<\/em> function can be used to run the Nemenyi test to compare models. Here is an example:<\/p>\n<pre>&gt; N &lt;- 50\r\n&gt; M &lt;- 4\r\n&gt; data &lt;- matrix( rnorm(N*M,mean=0,sd=1), N, M) \r\n&gt; data[,2] &lt;- data[,2]+1\r\n&gt; data[,3] &lt;- data[,3]+0.7\r\n&gt; data[,4] &lt;- data[,4]+0.5\r\n&gt; colnames(data) &lt;- c(\"Method A\",\"Method B\",\"Method C\",\"Method D\");\r\n&gt; nemenyi(data,conf.int=0.95,plottype=\"vline\")\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig4_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-263 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig4_.png\" alt=\"tstoolR.fig4\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig4_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig4_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><br \/>\nI can also get the MCB test by using:<\/p>\n<pre>&gt; nemenyi(data,conf.int=0.95,plottype=\"mcb\")\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig5_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-264 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig5_.png\" alt=\"tstoolR.fig5\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig5_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig5_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><br \/>\nThese tests use the same statistic. A discussion on this and various visualisations can be found <a title=\"Statistical Significance of Forecasting Methods \u2013 an empirical evaluation of the robustness and interpretability of the MCB, ANOM and Friedman-Nemenyi Test\" href=\"http:\/\/kourentzes.com\/forecasting\/2012\/04\/19\/statistical-significance-of-forecasting-methods-an-empirical-evaluation-of-the-robustness-and-interpretability-of-the-mcb-anom-and-friedman-nemenyi-test\/\">here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>Finally, the <em>seasplot<\/em> function produces seasonal plots. It differs from the similar <em>seasonplot<\/em> in the forecast package as it can automatically test for presence of trend and remove it, if present.<\/p>\n<pre>&gt; seasplot(referrals)\r\n<\/pre>\n<p><a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig6_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-266 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig6_.png\" alt=\"tstoolR.fig6\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig6_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig6_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><br \/>\nFurthermore, there are a few alternative visualisations you can do with <em>seasplot<\/em>. Here are some examples:<br \/>\n<a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig7_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-267 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig7_.png\" alt=\"tstoolR.fig7\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig7_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig7_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><br \/>\n<a href=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig8_.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-265 aligncenter\" src=\"http:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig8_.png\" alt=\"tstoolR.fig8\" width=\"350\" height=\"270\" srcset=\"https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig8_.png 501w, https:\/\/kourentzes.com\/forecasting\/wp-content\/uploads\/2014\/04\/tstoolR.fig8_-300x231.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><br \/>\nYou may wonder how is that p-value calculated. Based on the definition of deterministic seasonality, I just compare the distributions of the seasonal elements and test whether at least one is different. I do this by using the nonparametric Friedman test.<\/p>\n<p style=\"text-align: justify;\">The plan is to keep on updating TStools on <a href=\"https:\/\/github.com\/trnnick\/TStools\">GitHub<\/a>. When there are substantial updates, I will post them here.<\/p>\n<p style=\"text-align: justify;\">For examples of some of the new features of TStools have a look <a title=\"Update for TStools\" href=\"http:\/\/kourentzes.com\/forecasting\/2014\/05\/04\/update-for-tstools\/\">here<\/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>This is a collection of functions for time series analysis\/modelling for R. Follow link to GitHub. If you need help installing this package in R have a look at this post. Alternatively just type in R the following commands: &gt; if (!require(&#8220;devtools&#8221;)) install.packages(&#8220;devtools&#8221;) &gt; devtools::install_github(&#8220;trnnick\/TStools&#8221;) At the time of posting the following functions are included:\u2026 <span class=\"read-more\"><a href=\"https:\/\/kourentzes.com\/forecasting\/2014\/04\/19\/tstools-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":[10],"tags":[45,39,25,42],"_links":{"self":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/138"}],"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=138"}],"version-history":[{"count":0,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"wp:attachment":[{"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kourentzes.com\/forecasting\/wp-json\/wp\/v2\/tags?post=138"}],"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. -->