OLS与 pandas :datetime索引作为预测器 [英] OLS with pandas: datetime index as predictor

查看:130
本文介绍了OLS与 pandas :datetime索引作为预测器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用大熊猫OLS功能来适应趋势线到我的数据系列。有没有人知道如何使用熊猫系列中的日期时间索引作为OLS中的预测器?

I would like to use pandas OLS function to fit a trendline to my data Series. Does anyone knows how to use the datetime index from the pandas Series as predictor in the OLS?

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

For example, let say that I have a simple time series:

>>> 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:

I would like to do an OLS on it using the index as predictor:

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

但是作为x是datetime索引列表,该函数返回一个错误。任何人都有一个想法?

But as x is a list of datetime index, the function returns an error. Anyone has an idea?

我可以使用来自 scipy.stats 的linregress,但是我想知道是否可以与熊猫。

I could use linregress from scipy.stats but I wonder if it is possible with Pandas.

谢谢,
Greg

Thanks, Greg

推荐答案

问题是你不能通过索引 ols

将其更改为系列

The problem is that you cannot pass an Index to ols.
Change it to a 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---------------------------------

这篇关于OLS与 pandas :datetime索引作为预测器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆