1

I can decompose the timeseries data using this way -

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(ts)

trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

My question is how to covert this residual value to original value. So that I can use them for predicting future value.

Enthusiast
  • 181
  • 1
  • 12

2 Answers2

1

I tried to add original trend and seasonality back but doesn't work well. So when I am comparing the predicted result with original result, I just used predicted result with residual.

efrom statsmodels.tsa.seasonal import seasonal_decompose

# trend, seasonality are separated out from data, and we can model the residuals
decomposition = seasonal_decompose(ts_log)
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid

# AR model
model = ARIMA(ts_log, order=(2, 1, 0))  
results_AR = model.fit(disp=-1) 
plt.figure(figsize=(20,10))
plt.plot(ts_log_decompose)
plt.plot(results_AR.fittedvalues, color='red')
result = (results_AR.fittedvalues-ts_log_decompose)**2
result.dropna(inplace=True)
plt.title('Decompose RSS: %.4f'% sum(result))
plt.show()

I tried AR, MA, ARIMA models and found AR model had lowest RSS. So now, I am doing forecasting with AR model.

predictions_AR = pd.Series(results_AR.fittedvalues, copy=True)
print predictions_AR.head()
plt.figure(figsize=(20,10))
plt.plot(series, color='red')
plt.plot(predictions_ARIMA, color='green')
result = (predictions_AR-residual)**2
result.dropna(inplace=True)
plt.title('RMSE: %.4f'% np.sqrt(sum(result)/len(series)))
plt.show()

It works well: enter image description here

If you want to check all my code: https://github.com/hanhanwu/Hanhan_Data_Science_Practice/blob/master/sequencial_analysis/try_LSTM.ipynb

Just Scroll Down to decompose method

Cherry Wu
  • 3,844
  • 9
  • 43
  • 63
0

If you're doing an additive model, you just add up the trend, seasonal, and residual components, to get back to the original values. The end result is your original data, so there's not a lot of value in backing into it. The residual is what is left of your original data after taking into account the trend and seasonal effects. It's like linear regression, where the predicted value plus the residual returns your actual outcome for the dependent variable.

Andrew B
  • 96
  • 1
  • 5
  • The residuals are used to make the series stationary so that future points can be forecast. But then the forecast would be of the residuals and to forecast for the original series, the forecast then has to be inverted. I think that is what was meant to be asked. Correct me if I am wrong! – callmeanythingyouwant Jul 14 '21 at 12:39