Search code examples
rintegrationdifferencecontinuous

Continuous Integration of a 1st Differenced Logged Series in R


I have a 1st differenced logged series that I need to convert back to the original level units. How do I do this in R?

Below is my data series and attempted code:

Original Series:

1.1, 2.6, 3.6, 4.8, 5.1, 6.0, 7.3, 8.8, 9.4, 10.5

Attempted Code:

o <- c(1.1, 2.6, 3.6, 4.8, 5.1, 6.0, 7.3, 8.8, 9.4, 10.5)
l <- log(o)
dl <- diff(l)

exp(diffinv(dl, differences = 1)) # Attempt to recover o

Current Output:

[1] 1.000000 2.363636 3.272727 4.363636 4.636364 5.454545 6.636364 8.000000 
[9] 8.545455 9.545455

Desired Output:

[1] 1.1 2.6 3.6 4.8 5.1 6.0 7.3 8.8
[9] 9.4 10.5

Solution

  • It's not possible to fully recover the original series without knowing its first element:

    exp(diffinv(dl) + log(1.1))
    # [1]  1.1  2.6  3.6  4.8  5.1  6.0  7.3  8.8  9.4 10.5
    

    or

    exp(cumsum(c(log(1.1), dl)))
    # [1]  1.1  2.6  3.6  4.8  5.1  6.0  7.3  8.8  9.4 10.5
    

    Let X1, X2, X3, X4 be the original series. Then ultimately you have

    Z1 = ln(X2) - ln(X1),

    Z2 = ln(X3) - ln(X2)

    Z3 = ln(X4) - ln(X3)

    Given Z1, Z2, Z3, you wish to recover X1, X2, X3, X4. Then notice that

    Z1 = ln(X2) - ln(X1),

    Z2 + Z1 = ln(X3) - ln(X1),

    Z3 + Z2 + Z1 = ln(X4) - ln(X1),

    so that

    Z1 + ln(X1) = ln(X2),

    Z2 + Z1 + ln(X1) = ln(X3),

    Z3 + Z2 + Z1 + ln(X1) = ln(X4),

    and

    exp(Z1 + ln(X1)) = X2,

    exp(Z2 + Z1 + ln(X1)) = X3,

    exp(Z3 + Z2 + Z1 + ln(X1)) = X4,

    which is exactly what (more transparently than the first solution)

    exp(cumsum(c(log(1.1), dl)))
    

    does. As a consequence,

     exp(diffinv(dl))
    

    alone works only when the original series starts from 1 = exp(0).

    Thus, you must now the initial levels of the series as to recover it from any kind of differences, which is perfectly natural. Imagine that we only know how much you are going to earn and spend every day. At no point it's possible to say how much in total you have money without knowing the initial amount.