0

I have an annual time series with the following format:

import pandas as pd
import numpy as np

index = pd.date_range(start='2011-01-01', end='2021-01-01', freq='AS')
values = np.array([.065, .07, .07, .082, .078, .074, .079, .094, .111, .145, .127]) * 10**4
time_series = pd.DataFrame(values, index=index)

                     0
2011-01-01   650.0
2012-01-01   700.0
2013-01-01   700.0
2014-01-01   820.0
2015-01-01   780.0
2016-01-01   740.0
2017-01-01   790.0
2018-01-01   940.0
2019-01-01  1110.0
2020-01-01  1450.0
2021-01-01  1270.0

I would like to apply an ensemble model via moving block bootstrapping and Box-Cox transformation, as suggested by Christoph Bergmeir, Rob Hyndman (the genius), and José Benítez in this article.

The first steps in the procedure are to decompose the time series and bootstrap the residuals. However, neither the STL() nor the seasonal_decompose() from Statsmodels return relevant values for the residual.

In fact, when I try to implement STL() I get this error:

from statsmodels.tsa.seasonal import STL
stl = STL(time_series).fit()
stl.plot()

ValueError: period must be a positive integer >= 2

And the seasonal_decompose() does not lead to any residual, as it assigns all the changes over time to the trend component:

from statsmodels.tsa.seasonal import seasonal_decompose
seasonal = seasonal_decompose(time_series, model='additive')
seasonal.plot()

Plot of seasonal decomposition of yearly time series

I've tried to circumvent the STL() error with the suggestions on this thread, to no avail. The authors of the study above do not apply STL() to non-seasonal series, they apply the loess method instead, which could be explored if someone has any function or module to recommend.

I've also tried to transform the data prior to decomposing it, which again didn't work.

Does anyone have any suggestions on how to tune the parameters for the seasonal decomposers above? Or maybe on how to decompose this time series in a different way?

1 Answer 1

0

If you look at the documentation for STL(), you can see that there is a default of 7 for the seasonal period if you do not have a frequency (which is what I think you have here).

The decomposition requires 1 input, the data series. If the data series does not have a frequency, then you must also specify period. The default value for seasonal is 7, and so should also be changed in most applications.

Hopefully, this will help!

I have also faced this issue when using stats model:

import statsmodels.api as sm
decomposition = sm.tsa.seasonal_decompose(df, model='additive')
fig = decomposition.plot()

I get the following error:

ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None

However, when I set a period of 2, it works fine.

import statsmodels.api as sm
decomposition = sm.tsa.seasonal_decompose(df, period = 2, 
model='additive')
fig = decomposition.plot()

The problem is, I have only yearly data, without monthly information, and no clear period to work with. Therefore, it is arbitrary (perhaps incorrect) to set a random period for my data set. If you have a period (some seasonal time span) in your data, you can use this; otherwise, I am not sure how to help. See further the issue discussed here.

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