I have a really small time series dataset (21 yearly observations) and I want to check if my data is stationary.

ndiffs(TS, test="adf")

 2

TSdiff2=diff(TS, differences=2)

Augmented Dickey-Fuller Test

data: TSdiff2
Dickey-Fuller = -2.4232, Lag order = 2, p-value = 0.4112
alternative hypothesis: stationary


According to the explanation in this link [http://www.r-bloggers.com/time-series-analysis-using-r-forecast-package/] : "The null-hypothesis for an ADF test is that the data are non-stationary. So large p-values are indicative of non-stationarity, and small p-values suggest stationarity. Using the usual 5% threshold, differencing is required if the p-value is greater than 0.05.

So it seems that my time series is not stationary despite the fact that I used the ndiffs function to estimate the number of differences.

The function ndiffs allows for at most second order differencing by default (argument max.d=2), see the help file. Perhaps that is why it returns 2 (which indicates order of at least 2).

Consider the following example: generate a series that is integrated of order 3 and find its order of integration using the function ndiffs using the default maximum order:

set.seed(1)
x=cumsum(cumsum(cumsum(rnorm(1000))))
ndiffs(x)
ndiffs(diff(x,differences=1))
ndiffs(diff(x,differences=2))
ndiffs(diff(x,differences=3))


It will suggest 2 for the raw series (incorrectly, and similarly to your case), 2 for the first difference of the series (correctly), 1 for the second difference (correctly) and 0 for the third difference (correctly).

Now allow for a greater maximum order of integration:

ndiffs(x,max.d=5)
ndiffs(diff(x,differences=1),max.d=5)
ndiffs(diff(x,differences=2),max.d=5)
ndiffs(diff(x,differences=3),max.d=5)


Now you are getting order 3 for the raw series, which is correct.

However, beware that economic and financial time series can rarely be argued to have an order of integration greater than 2 (this could be the motivation for the default value max.d=2).

• Thank you very much for your response. I allowed for a greater maximum in the max.d argument and now the ndiffs gives the right answer. – Nikos Tavoularis Jun 17 '16 at 13:27