【问题标题】:Decompose annual time series with Statsmodels使用 Statsmodels 分解年度时间序列
【发布时间】:2021-08-22 10:26:38
【问题描述】:

我有一个格式如下的年度时间序列:

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

我想通过 moving block bootstrappingBox-Cox transformation 应用一个集成模型,正如 Christoph Bergmeir、Rob Hyndman(天才)和 José Benítez 在 this article 中所建议的那样。

该过程的第一步是分解时间序列并引导残差。但是,来自 Statsmodels 的 STL()seasonal_decompose() 都不会返回残差的相关值。

事实上,当我尝试实现STL() 时,我得到了这个错误:

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

ValueError: period must be a positive integer >= 2

并且seasonal_decompose() 不会导致任何残差,因为它将随时间的所有变化分配给趋势组件:

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

我尝试使用this thread 上的建议来规避STL() 错误,但无济于事。上述研究的作者并没有将STL()应用于非季节性系列,而是使用了黄土方法,如果有人有任何功能或模块可以推荐,可以探索。

我还尝试在分解数据之前对其进行转换,但还是没有用。

有人对如何调整上述季节性分解器的参数有任何建议吗?或者也许关于如何以不同的方式分解这个时间序列?

【问题讨论】:

    标签: python time-series statsmodels forecasting


    【解决方案1】:

    如果您查看STL() 的文档,您会发现如果您没有频率(我认为您在此处拥有的频率),则季节性期间的默认值为 7。

    分解需要 1 个输入,即数据系列。如果数据 系列没有频率,那么您还必须指定周期。 季节性的默认值为 7,因此也应在 大多数应用程序。

    希望这会有所帮助!

    我在使用统计模型时也遇到过这个问题:

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

    我收到以下错误:

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

    但是,当我将周期设置为 2 时,它可以正常工作。

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

    问题是,我只有年度数据,没有月度信息,也没有明确的工作时间段。因此,为我的数据集设置一个随机周期是任意的(可能是不正确的)。如果您的数据中有一个时期(一些季节性时间跨度),您可以使用它;否则,我不知道如何提供帮助。进一步查看here讨论的问题。

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 2017-01-09
      • 2018-11-19
      相关资源
      最近更新 更多