【问题标题】:OLS with pandas: datetime index as predictor带有熊猫的 OLS:日期时间索引作为预测器
【发布时间】:2012-12-30 23:05:41
【问题描述】:

我想使用 pandas OLS 函数将趋势线拟合到我的数据系列。有谁知道如何使用 pandas 系列中的日期时间索引作为 OLS 中的预测器?

例如,假设我有一个简单的时间序列:

>>> ts
2001-12-31    19.828763
2002-12-31    20.112191
2003-12-31    19.509116
2004-12-31    19.913656
2005-12-31    19.701649
2006-12-31    20.022819
2007-12-31    20.103024
2008-12-31    20.132712
2009-12-31    19.850609
2010-12-31    19.290640
2011-12-31    19.936210
2012-12-31    19.664813
Freq: A-DEC

我想使用索引作为预测器对其进行 OLS:

model = pd.ols(y=ts,x=ts.index,intercept=True)

但由于 x 是日期时间索引的列表,该函数返回错误。有人有想法吗?

我可以使用 scipy.stats 中的 linregress,但我想知道 Pandas 是否可以。

谢谢, 格雷格

【问题讨论】:

  • 我的 pandas 没有 ols 功能。你能添加如何导入它吗?可能,更新问题。

标签: python datetime pandas linear-regression series


【解决方案1】:

问题是您不能将Index 传递给ols
将其更改为Series

In [153]: ts
Out[153]: 
2011-01-01 00:00:00    19.828763
2011-01-01 01:00:00    20.112191
2011-01-01 02:00:00    19.509116
Freq: H, Name: 1

In [158]: type(ts.index)
Out[158]: pandas.tseries.index.DatetimeIndex


In [154]: df = ts.reset_index()

In [155]: df
Out[155]: 
                index          1
0 2011-01-01 00:00:00  19.828763
1 2011-01-01 01:00:00  20.112191
2 2011-01-01 02:00:00  19.509116

In [160]: type(df['index'])
Out[160]: pandas.core.series.Series


In [156]: model = pd.ols(y=df[1], x=df['index'], intercept=True)

In [163]: model
Out[163]: 

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

Formula: Y ~ <x> + <intercept>

Number of Observations:         3
Number of Degrees of Freedom:   1

R-squared:        -0.0002
Adj R-squared:    -0.0002

Rmse:              0.3017

F-stat (1, 2):       -inf, p-value:     1.0000

Degrees of Freedom: model 0, resid 2

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
             x     0.0000     0.0000       0.00     0.9998    -0.0000     0.0000
     intercept     0.0000 76683.4934       0.00     1.0000 -150299.6471 150299.6471
---------------------------------End of Summary---------------------------------

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 2019-07-12
  • 2023-01-12
  • 2021-11-30
相关资源
最近更新 更多