1
$\begingroup$

I'm using the ADF test to check for stationarity of two variables, using the ndiffs function in R.

> ndiffs(x[, "Apples"], alpha = 0.05, test = c("adf"))
[1] 2
> ndiffs(x[, "Romulans"], alpha = 0.05, test = c("adf"))
[1] 0

Next step is as far as I understand to difference the time series with the appropriate lag to achieve stationarity, but this is when I encounter difficulty.

Using:

d.x1 = diff(x[, "Apples"], lag = 2)
d.x2 = diff(x[, "Romulans"], lag = 0)

doesn't work as the lag and difference must be an integer >= 1.

and excluding the lag option for the Romulans variable gives a warning message as soon as I try dx = cbind(d.x1, d.x2) saying that the number of rows result is not a mulitple of vector length (arg 1).

Should I use lag 2 for both variables? What is the best way to handle a situation like this?

$\endgroup$

1 Answer 1

2
$\begingroup$

Next step is as far as I understand to difference the time series with the appropriate lag

Actually, not with the appropriate lag but an appropriate number of times. ndiffs indicates the argument variable's order of integration. Say, it is $n$. To make the variable stationary, you are supposed to difference $n$ times. This is not the same as to difference using $\text{lag}=n$. Here are some examples:

Difference once with $\text{lag}=1$: diff(x,lag=1,differences=1) yields $\Delta x_t:=x_t-x_{t-1}$.
Difference twice with $\text{lag}=1$: diff(x,lag=1,differences=2) yields $\Delta^2 x_t:=\Delta x_t-\Delta x_{t-1}=(x_t-x_{t-1})-(x_{t-1}-x_{t-2})=x_t-2x_{t-1}+x_{t-2}$.
Difference once with $\text{lag}=2$: diff(x,lag=2,differences=1) yields $\Delta x_t:=x_t-x_{t-1}$.

The last example is not what ndiffs is about, while the first and the middle one are fine.

Also, if $n=0$, you do not have to difference and you just leave the variable as is.

excluding the lag option...

When you do not specify the lag argument explicitly, is assumes its default value which is 1; it does not just disappear.

number of rows result is not a mulitple of vector length (arg 1)

Differencing a variable $n$ times results in it being shortened by $n$ observations. To match a differenced variable that was originally of length $n$ with a non-differenced variable that is of length $n$ you will have to cut the initial $n$ values of the non-differenced variable: x[-c(1:n)]. Then the two variables will match correctly.

$\endgroup$
3
  • $\begingroup$ Thank you very much for such a clear and understandable answer! $\endgroup$ Jan 11, 2016 at 20:22
  • $\begingroup$ However, it doesn't solve the cbind(d.x1, d.x2) problem I am experiencing as it says variable lengths differ (they do not before the diff). $\endgroup$ Jan 11, 2016 at 20:31
  • 1
    $\begingroup$ @youjustreadthis, I added an additional paragraph to my answer that addresses the problem with cbind. $\endgroup$ Jan 11, 2016 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.