Time Series – How to Inverse-Difference Time Series Data in R

data transformationdifferencinglstmrtime series

I'm preparing a time series model with LSTM, I noticed that the time series data is not stationary so I used diff(period=1) in pandas. I fitted the LSTM model and it looks good. I invert the difference operation using original data. When we are forecasting in real time, how to invert the predictions because the original data is not available?

How I inverse-transformed is

pred = model.predict(x_test_diff)
y_test_inv_diff = y_test.shift(1) + y_test_diff
pred = y_test.shift(1) + pred

I have the original dataset y_test, so I was able to inverse-difference predictions. What to do when I'm making forecasts in real time?

Best Answer

The inverse difference is the cumulative sum of the first value of the original series and the first differences:

y=rnorm(10)    # original series
dy=diff(y)     # first differences
invdy=cumsum(c(y[1],dy)) # inverse first differences
print(y-invdy) # discrepancy between the original series and its inverse first differences

There is a tiny discrepancy between the original series and its inverse first differences due to rounding.

Whenever you need to invert differencing (in real time or otherwise), you have to have some value of the original series available (e.g. the first value as in my example); you cannot do without it.