# R detect increasing/decreasing trend of time series

I have lots of time series with periods: day, week or month. With stl() function or with loess(x ~ y) I can see how trends of particular time series look. I need to detect if trend of time series is increasing or decreasing. How can I manage that?

I tried to compute linear regression coefficients with lm(x ~ y) and play with slope coefficient. (If |slope|>2 and slope>0 then increasing trend, else if |slope|>2 and slope<0 – decreasing). Maybe there is another and more effective method for trend detection? Thank you!

For example: I have timeserie1, timeserie2. I need a simple algorithm that would tell me that timeserie2 is an increasing algorithm, and in timeserie1, the trend isn't increasing or decreasing. What criteria should I use?

timeserie1:

1774 1706 1288 1276 2350 1821 1712 1654 1680 1451 1275 2140 1747 1749 1770 1797 1485 1299 2330 1822
1627 1847 1797 1452 1328 2363 1998 1864 2088 2084  594  884 1968 1858 1640 1823 1938 1490 1312 2312
1937 1617 1643 1468 1381 1276 2228 1756 1465 1716 1601 1340 1192 2231 1768 1623 1444 1575 1375 1267
2475 1630 1505 1810 1601 1123 1324 2245 1844 1613 1710 1546 1290 1366 2427 1783 1588 1505 1398 1226
1321 2299 1047 1735 1633 1508 1323 1317 2323 1826 1615 1750 1572 1273 1365 2373 2074 1809 1889 1521
1314 1512 2462 1836 1750 1808 1585 1387 1428 2176 1732 1752 1665 1425 1028 1194 2159 1840 1684 1711
1653 1360 1422 2328 1798 1723 1827 1499 1289 1476 2219 1824 1606 1627 1459 1324 1354 2150 1728 1743
1697 1511 1285 1426 2076 1792 1519 1478 1191 1122 1241 2105 1818 1599 1663 1319 1219 1452 2091 1771
1710 2000 1518 1479 1586 1848 2113 1648 1542 1220 1299 1452 2290 1944 1701 1709 1462 1312 1365 2326
1971 1709 1700 1687 1493 1523 2382 1938 1658 1713 1525 1413 1363 2349 1923 1726 1862 1686 1534 1280
2233 1733 1520 1537 1569 1367 1129 2024 1645 1510 1469 1533 1281 1212 2099 1769 1684 1842 1654 1369
1353 2415 1948 1841 1928 1790 1547 1465 2260 1895 1700 1838 1614 1528 1268 2192 1705 1494 1697 1588
1324 1193 2049 1672 1801 1487 1319 1289 1302 2316 1945 1771 2027 2053 1639 1372 2198 1692 1546 1809
1787 1360 1182 2157 1690 1494 1731 1633 1299 1291 2164 1667 1535 1822 1813 1510 1396 2308 2110 2128
2316 2249 1789 1886 2463 2257 2212 2608 2284 2034 1996 2686 2459 2340 2383 2507 2304 2740 1869  654
1068 1720 1904 1666 1877 2100  504 1482 1686 1707 1306 1417 2135 1787 1675 1934 1931 1456 1363 2027
1740 1544 1727 1620 1232 1199


timeserie2:

 122  155  124   97  155  134  115  122  162  115  102  163  135  120  139  160  126  122  169  154
121  134  143  100  121  182  139  145  135  147   60   58  153  145  130  126  143  129   98  171
145  107  133  115  113   96  175  128  106  117  124  107  114  172  143  111  104  132  110   80
159  131  113  123  123  104  101  179  127  105  133  127  101   97  164  134  124   90  110  102
90  186   79  145  130  115   79  104  191  137  114  131  109   95  119  173  158  137  128  119
109  120  182  140  133  113  121  110  122  159  129  124  119  109  108   95  167  138  125  105
139  118  115  166  140  112  116  139  121  109  164  135  118  121  112  111  102  169  136  151
132  135  130  112  156  134  121  116  114   91   86  141  160  116  118  112   84  114  165  141
109  123  122  110  100  162  145  121  118  115  107  103  162  142  130  139  134  121  118  164
147  125  120  134  107  130  158  141  144  148  124  135  118  212  178  154  167  155  176  143
201  170  144  138  152  136  123  223  189  160  153  190  136  144  276  213  199  211  196  170
179  460  480  499  550  518  493  557  768  685  637  593  507  611  569  741  635  563  577  498
456  446  677  552  515  441  438  462  530  699  629  555  641  625  544  585  705  584  553  622
506  500  533  777  598  541  532  513  434  510  714  631 1087 1249 1102  913  888 1147 1056 1073
1075 1136  927  922 1066 1074  996 1189 1062  999  974 1174 1097 1055 1053 1097 1065 1171  843  441
552  779  883  773  759  890  404  729  703  810  743  743  946  883  813  876  841  742  715  960
862  743  806  732  669  621

• Your second example doesn't have a trend so you should not be detecting one. At period 230, the data has a level shift(ie 0,0,0,0,0,1,1,1,1,1,1,etc) which is different than a trend. Also, there is a change in the variance at about 200 which can be identified using the Tsay test. See more here www.unc.edu/~jbhill/tsay.pdf‎ Feb 26, 2014 at 16:13
• @Tom Yes, what you say is evident in a plot of the data. (Indeed, a plot shows three separate sudden level shifts, not just one). But characterizing this as differing from a "trend" does not do justice to your analysis, which I know will reveal subtle details in the behavior of this time series. I would like to suggest the O.P. would be better served by clear characterizations of the data behavior rather than by discussions of possible definitions of "trend." She asks for an alternative to testing a least squares slope--and that is a de facto indication of what she means by "trend."
– whuber
Feb 26, 2014 at 16:46

## 2 Answers

You can apply a zero phase shifter filter and cut out all frequencies higher than some threshold; this would give you a "trend" of sorts.

For example, look at this question, "How do I run a high pass or low pass filter on data points in R?" They show how to use Butterworth low pass filter. The problem with that filter is that it's not zero phase shift, i.e. as you see the low frequency component's phase is shifted relative to the original signal. You may want to find the filter which does not shift the phase. If this were economic data, I would suggest Christiano's filter as per "The Band Pass Filter" by Lawrence J. Christiano and Terry J. Fitzgerald (1999). For physical data, there must be a ton of zero phase shift filters available.

UPDATE:

Here's an example of applying the low band pass filter to the LOG of the second time series. The LOG is required to even out the variance.

UPDATE2:

Here's a sample decomposition in frequency domain with frequency bands: irregular [2-19], cyclical [20-99], and trend [100-$\infty$] (in period lengths). The frequency bands have to be chosen carefully based on the understanding of the underlying phenomenon.

• I was puzzling over this answer for awhile and finally realized you must be using the word "trend" in much the same way many statisticians would use "smooth." Then it all makes sense. We should hope the O.P. will soon clarify what she means by "trend."
– whuber
Feb 26, 2014 at 16:59
• yes, i'm using "trend" as in time series decomposition sense: y = trend + cyclical + seasonal + irregular, e.g. en.wikipedia.org/wiki/Decomposition_of_time_series the trend would be a "very low" frequency component in this context. how low depends on the problem Feb 26, 2014 at 17:06
• It doesn't quite seem that way. When you filter out high-frequency components you are removing some of the "irregular" (read: noise) and perhaps a tiny part of the cyclical portion, but the trend, the medium- and long-term cyclical components, and the seasonal components will all remain.
– whuber
Feb 26, 2014 at 17:08
• for a trend you would use low band pass filter. let's say you have several years of monthly data series. in this case the low band could be 10 years cycle, so you cut out all frequencies higher than 1/10 (when time is in years). the threshold really depends on the phenomenon, in economic data the business cycles could be 10-15 years long, so you may need to set the low band to 1/16 Feb 26, 2014 at 17:27
• As your new illustration shows, when you are too aggressive in the filtering you lose obvious trends, too: your smooth is so strong it misses almost all of the last third of the data. Regardless, this definitely is not a decomposition into the qualitative notional components described by the Wikipedia article you reference. Another concern with the filtering approach (however it is conducted) is the statistical one: how do you test whether there is a "trend" and whether it is up or down?
– whuber
Feb 26, 2014 at 19:06

Trend detection in a time series can be done simply wrong or more aggressively. A series can have values that exhibit a trend, e.g. 1,2,3,4,5, and then a change in trend at one or more points in time, e.g. 7,9,11,13,15,... Another example is 1,1,1,1,1,2,3,4,5,6 where no trend is present for the first 5 readings, and then a trend ensues. A series like 1,1,1,1,1,2,2,2,2,2 is said to have a level shift (change in intercept). AUTOBOX is a piece of software that uniquely (to my knowledge) incorporates analytics to detect, test and incorporate time trends. I was one of the developers and extended the analytics in this particular area. The procedure that you are trying to use (incorrectly in my opinion) is model presumptive, i.e. no pulses, no level shifts, no seasonal pulses, no ARIMA structure, constant error variance, etc. Another possible bad example of an assumed model is to use $[1-B]y(t)=\theta_0 +[MA/AR]a(t)$, as again, one is assuming a particular structure.

The idea here is that the ARIMA model might be incorrect or that $\theta_0$ is changing (thus different trends) at different points in time.

The whole idea is to let the data "speak" and the analysis "listen" and detect the "correct model" or at least a "useful model".

Orson Bean – or maybe it was somebody else – once said, “A trend is a trend…until it bends, and when the trend bends, the trend is at an end." Local time trends in time series require identification of the break points and then estimation of the local trend. Some useful material/references can be found at http://www.autobox.com/OLDWEB/udontsay.html

If you wish to post some data, please do so, and I will post some results back to you.

• A trend is a trend is a trend\ But the question is, will it bend?\ Will it alter its course\Through some unforeseen force\And come to a premature end? Alexander Cairncross Feb 26, 2014 at 13:33
• Thank you for the answer. I will study your information. I also have posted some data. Feb 26, 2014 at 14:20
• In that case, Marta, it sounds like you are testing for a global "trend" (in the sense of differences in typical values between the two ends of the time series) and not local trends as described in this answer. Is that correct?
– whuber
Feb 26, 2014 at 15:43
• @Nick Thank you! Because it took some effort to hunt down the source, here it is: Economic Forecasting, Presidential Address to the Royal Economic Society, July 3, 1969. Cairncross opened his speech with a Lincoln quotation followed by this original limerick (facetiously attributed to "Stein Age Forecaster"). The subsequent remarks make it clear he was referring to forecasting--extrapolation of trends.
– whuber
Feb 26, 2014 at 16:00
• @whuber, you are right, I'm looking for global trend. Sorry if my question was misleading. Feb 27, 2014 at 7:08