2013-01-16 2 views
7

pandas OLS 함수를 사용하여 데이터 계열에 추세선을 맞추려합니다. 누구든지 OLS에서 예측기로 팬더 시리즈의 datetime 인덱스를 사용하는 방법을 알고 있습니까? x가로pandas가있는 OLS : 예측 자로 datetime 인덱스

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

그러나 :

>>> 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을하고 싶습니다 : 예를 들어

, 나는 단순한 시계열을 가정 해 봅시다 datetime 인덱스의 목록, 함수는 오류를 반환합니다. 누구나 아이디어가 있습니까?

저는 에서 linofress를 사용할 수 있습니다. scipy.stats하지만 팬더와 함께 할 수 있는지 궁금합니다.

덕분에, 그렉

답변

5

문제는 당신이 Indexols에 통과 할 수 없다는 것입니다. 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--------------------------------- 
+0

많은 감사합니다! – leroygr

+2

이 솔루션은 더 이상 작동하지 않을 수 있습니다 (2 년 후). 여기를 참조하십시오 : http://stackoverflow.com/questions/30425490/linear-regression-from-time-series-pandas/30431930#30431930 팬더가 datetime 인덱스를 변경했을 가능성이 있습니까? – JohnE

관련 문제