如何在Pandas中使用Rolling OLS Model接口?

时间:2014-04-16 09:04:24

标签: python pandas

我想校准MovingOLS,但一直收到错误消息

IndexError: index -1 is out of bounds for axis 0 with size 0

我用来训练MovingOLS的数据框如下:

   x1  y  x2
0  1   1   1
1  2   2   2
2  3   3   3

代码

model = pandas.stats.ols.MovingOLS(y = df['y'], x=df[['x1', 'x2']], window_type='rolling', window=2, min_periods=2)

该模型非常简单,因为我只想让自己熟悉MovingOLS API,如果我正确理解Movingrolling部分,我希望得到2个OLS模型。 我想知道是否有任何好材料,因为我无法在线查找文档/教程,提供有关Rolling OLS / MovingOLS API的足够详细信息。

顺便说一下,库版本是0.11.0。

谢谢!

1 个答案:

答案 0 :(得分:3)

我认为这是由K(参数)>引起的。 N(观察)。 pandas中的movingOLS增加了一个拦截。您正在尝试使用2个观察值的滚动窗口估计3个参数。

如果添加一些行并使滚动窗口变大,它应该可以工作。例如,window = 4:

df=pd.DataFrame({'y': [1,2,3,4,5,6],
                 'x1' : [3,4,6,7,10,12],
                 'x2' : [2,7,4,9,3,15]})
print df

   x1  x2  y
0   3   2  1
1   4   7  2
2   6   4  3
3   7   9  4
4  10   3  5
5  12  15  6

print pd.stats.ols.MovingOLS(y = df['y'], x=df[['x1', 'x2']], 
                             window_type='rolling', 
                             window=4, min_periods=4)

-------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <x1> + <x2> + <intercept>

Number of Observations:         4
Number of Degrees of Freedom:   3

R-squared:         0.9777
Adj R-squared:     0.9331

Rmse:              0.3339

F-stat (2, 1):    21.9240, p-value:     0.1493

Degrees of Freedom: model 2, resid 1

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
            x1     0.4319     0.0850       5.08     0.1237     0.2653     0.5984
            x2     0.0262     0.0425       0.62     0.6483    -0.0572     0.1096
     intercept     0.5180     0.6415       0.81     0.5675    -0.7392     1.7753
---------------------------------End of Summary---------------------------------

当我设置window = 2时,我也得到一个索引越界错误。