Trend Analysis in Time Series – How to Analyze Trends in Non-Periodic Time Series Using R

rtime series

Suppose I have following non-periodic time series. Obviously the trend is decreasing and I would like to prove it by some test (with p-value). I am unable to use classic linear regression due to strong temporal (serial) auto-correlation among values.

library(forecast)
my.ts <- ts(c(10,11,11.5,10,10.1,9,11,10,8,9,9,
               6,5,5,4,3,3,2,1,2,4,4,2,1,1,0.5,1),
            start = 1, end = 27,frequency = 1)
plot(my.ts, col = "black", type = "p",
     pch = 20, cex = 1.2, ylim = c(0,13))
# line of moving averages 
lines(ma(my.ts,3),col="red", lty = 2, lwd = 2)

enter image description here

What are my options?

Best Answer

As you said, the trend in your example data is obvious. If you want just to justify this fact by hypothesis test, than besides using linear regression (the obvious parametric choice), you can use non-parametric Mann-Kendall test for monotonic trend. The test is used to

assess if there is a monotonic upward or downward trend of the variable of interest over time. A monotonic upward (downward) trend means that the variable consistently increases (decreases) through time, but the trend may or may not be linear. (http://vsp.pnnl.gov/help/Vsample/Design_Trend_Mann_Kendall.htm)

moreover, as noted by Gilbert (1987), the test

is particularly useful since missing values are allowed and the data need not conform to any particular distribution

The test statistic is the difference between negative and positive $x_j-x_i$ differences among all the $n(n-1)/2$ possible pairs, i.e.

$$ S = \displaystyle\sum_{i=1}^{n-1}\displaystyle\sum_{j=i+1}^{n}\mathrm{sgn}(x_j-x_i) $$

where $\mathrm{sgn}(\cdot)$ is a sign function. $S$ can be used to calculate $\tau$ statistics that is similar to correlation as it ranges from $-1$ to $+1$, where the sign suggests negative, or positive trend and value of $\tau$ is proportional to slope of the trend.

$$ \tau = \frac{S}{n(n-1)/2} $$

Finally, you can compute $p$-values. For samples of size $n \le 10$ you can use tables of precomputed $p$-values for different values of $S$ and different sample sizes (see Gilbert, 1987). With larger samples, first you need to compute variance of $S$

$$ \mathrm{var}(S) = \frac{1}{18}\Big[n(n-1)(2n+5) - \displaystyle\sum_{p=1}^{g}t_p(t_p-1)(2t_p+5)\Big] $$

and then compute $Z_{MK}$ test statistic

$$ Z_{MK} = \begin{cases} \frac{S-1}{\mathrm{var}(S)} & \text{if} ~ S > 0 \\ 0 & \text{if} ~ S = 0 \\ \frac{S+1}{\mathrm{var}(S)} & \text{if} ~ S < 0 \end{cases} $$

the value of $Z_{MK}$ is compared to standard normal values

  • $Z_{MK} \ge Z_{1-\alpha}$ for upward trend,
  • $Z_{MK} \le -Z_{1-\alpha}$ for downward trend,
  • $|Z_{MK}| \ge Z_{1-\alpha/2}$ for upward or downward trend.

In this thread you can find R code implementing this test.

Since the $S$ statistic is compared to all possible pairs of observations then, instead of using normal approximation for $p$-value you can use permutation test that is obvious for this case. First, you compute $S$ statistic from your data and then you randomly shuffle your data multiple times and compute it for each of the samples. $p$ is simply the proportion of cases when $S_\text{data} \ge S_\text{permutation}$ for upward trend or $S_\text{data} \le S_\text{permutation}$ for downward trend.


Gilbert, R.O. (1987). Statistical Methods for Environmental Pollution Monitoring. Wiley, NY.

Önöz, B., & Bayazit, M. (2003). The power of statistical tests for trend detection. Turkish Journal of Engineering and Environmental Sciences, 27(4), 247-251.

Related Question