如何正确设置 statsmodels.tsa.ar_model.AR.predict 函数的 start/end 参数

How to properly set start/end params of statsmodels.tsa.ar_model.AR.predict function

我有一个来自不规则间隔时间序列的项目成本数据框,我想尝试应用 statsmodel AR model against

这是其数据框中的数据示例:

               cost
date               
2015-07-16    35.98
2015-08-11    25.00
2015-08-11    43.94
2015-08-13    26.25
2015-08-18    15.38
2015-08-24    77.72
2015-09-09    40.00
2015-09-09    20.00
2015-09-09    65.00
2015-09-23    70.50
2015-09-29    59.00
2015-11-03    19.25
2015-11-04    19.97
2015-11-10    26.25
2015-11-12    19.97
2015-11-12    23.97
2015-11-12    21.88
2015-11-23    23.50
2015-11-23    33.75
2015-11-23    22.70
2015-11-23    33.75
2015-11-24    27.95
2015-11-24    27.95
2015-11-24    27.95
...
2017-03-31    21.93
2017-04-06    22.45
2017-04-06    26.85
2017-04-12    60.40
2017-04-12    37.00
2017-04-12    20.00
2017-04-12    66.00
2017-04-12    60.00
2017-04-13    41.95
2017-04-13    25.97
2017-04-13    29.48
2017-04-19    41.00
2017-04-19    58.00
2017-04-19    78.00
2017-04-19    12.00
2017-04-24    51.05
2017-04-26    21.88
2017-04-26    50.05
2017-04-28    21.00
2017-04-28    30.00

我很难理解如何在 predict 函数中使用 startend

根据the docs

start : int, str, or datetime Zero-indexed observation number at which to start forecasting, ie., the first > forecast is start. Can also be a date string to parse or a datetime type.

end : int, str, or datetime Zero-indexed observation number at which to end forecasting, ie., the first forecast is start. Can also be a date string to parse or a datetime type.

我创建了一个包含空每日时间序列的数据框,将我的不规则间隔时间序列数据添加到其中,然后尝试应用该模型。

data = pd.read_csv('data.csv', index_col=1, parse_dates=True)
df = pd.DataFrame(index=pd.date_range(start=datetime(2015, 1, 1), end=datetime(2017, 12, 31), freq='d'))
df = df.join(data)
df.cost.interpolate(inplace=True)
ar_model = sm.tsa.AR(df, missing='drop', freq='D')
ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
pred = ar_res.predict(start='2016', end='2016')

predict函数导致错误pandas.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 605-12-31 00:00:00

如果我尝试使用更具体的日期,我会收到相同类型的错误:

pred = ar_res.predict(start='2016-01-01', end='2016-06-01')    

如果我尝试使用整数,我会得到一个不同的错误:

pred = ar_res.predict(start=0, end=len(data))
Wrong number of items passed 202, placement implies 197

如果我实际使用 datetime,我会收到一条错误消息 no rule for interpreting end

我在这里撞墙太难了,我想一定是我遗漏了什么。

最终,我想使用该模型进行样本外预测(例如下一季度的预测)。

如果您传递 datetime(而不是 date),此方法有效:

from datetime import datetime

...
pred = ar_res.predict(start=datetime(2015, 1, 1), end=datetime(2017,12,31))

In [21]: pred.head(2)  # my dummy numbers from data
Out[21]:
2015-01-01   35
2015-01-02   23
Freq: D, dtype: float64

In [22]: pred.tail(2)
Out[22]:
2017-12-30   44
2017-12-31   44
Freq: D, dtype: float64

所以我创建了一个每日索引来满足等间隔时间序列的要求,但它仍然不是唯一的(@user333700 的评论)。

我添加了一个 groupby 函数来将重复的日期加在一起,然后可以 运行 使用 datetime 对象的 predict 函数(@andy-hayden 的回答) .

df = df.groupby(pd.TimeGrouper(freq='D')).sum()
...
ar_res.predict(start=min(df.index), end=datetime(2018,12,31))

通过 predict 函数提供的结果,我现在能够分析结果并调整参数以获得有用的东西。