3

Trying to figure out how to inverse-difference correctly if using 2 differences.

I can difference twice with the option differences = 2 and everything works:

diff(c(3,6,23,64,77)) = 3 17 41 13
diff(c(3, 17, 41, 13)) = 14  24 -28
diff(c(3,6,23,64,77), lag = 1, differences = 2) = 14  24 -28

When I use diffinv twice, using 3 as the init conditions (since the first element of the first difference is 3, the answer is the correct, it is the starting vector:

diffinv(
    diffinv(
        diff(c(3,6,23,64,77), lag = 1, differences = 2), 
    xi = 3), 
xi=3) = 3 6 23 64 77

But if I try to do two inverse differences, I get the incorrect answer:

diffinv(
    diff(c(3, 6, 23, 64, 77), lag = 1, differences = 2), 
    lag = 1, differences = 2, xi = c(3, 3)
) = 3  3 17 55 65

What am I doing wrong when trying to use difinv with differences = 2 ?

Frank
  • 952
  • 1
  • 9
  • 23

1 Answers1

1

If you perform the inverse difference individually, you need the first term in each case.

But, if you do two inverse differences at once, you need the first 2 values of the original data

diffinv(
    diff(c(3, 6, 23, 64, 77), lag = 1, differences = 2), 
    lag = 1, differences = 2, xi = c(3, 6)
) = 3  6 23 64 77

Notice in this case I had to use c(3, 6), which are the first two elements of the original data.

In the previous case, I had used diffinv twice, and so c(3, 3) was the first element of original data, and the first element of the first difference of the data.

Frank
  • 952
  • 1
  • 9
  • 23