2012-03-30 82 views
3

我一直在使用scikits.statsmodels OLS predict函數來預測擬合的數據,但現在想轉向使用熊貓。使用熊貓OLS預測

文檔refers to OLS以及稱爲y_predict的函數,但我找不到任何有關如何正確使用它的文檔。

舉例來說:

exogenous = { 
    "1998": "4760","1999": "5904","2000": "4504","2001": "9808","2002": "4241","2003": "4086","2004": "4687","2005": "7686","2006": "3740","2007": "3075","2008": "3753","2009": "4679","2010": "5468","2011": "7154","2012": "4292","2013": "4283","2014": "4595","2015": "9194","2016": "4221","2017": "4520"} 
endogenous = { 
    "1998": "691", "1999": "1580", "2000": "80", "2001": "1450", "2002": "555", "2003": "956", "2004": "877", "2005": "614", "2006": "468", "2007": "191"} 

import numpy as np 
from pandas import * 

ols_test = ols(y=Series(endogenous), x=Series(exogenous)) 

然而,當我能夠產生一個合適的:

>>> ols_test.y_fitted 
1998  675.268299 
1999  841.176837 
2000  638.141913 
2001 1407.354228 
2002  600.000352 
2003  577.521485 
2004  664.681478 
2005 1099.611292 
2006  527.342854 
2007  430.901264 

預測產生什麼不同:

>>> ols_test.y_predict 
1998  675.268299 
1999  841.176837 
2000  638.141913 
2001 1407.354228 
2002  600.000352 
2003  577.521485 
2004  664.681478 
2005 1099.611292 
2006  527.342854 
2007  430.901264 

在scikits.statsmodels一會請執行以下操作:

import scikits.statsmodels.api as sm 
... 
ols_model = sm.OLS(endogenous, np.column_stack(exogenous)) 
ols_results = ols_mod.fit() 
ols_pred = ols_mod.predict(np.column_stack(exog_prediction_values)) 

我該如何在熊貓中做到這一點,以預測內源性數據達到外源性極限?

更新:感謝張,大熊貓的新版本(0.7.3)現在有這個功能作爲標準。

+0

嗨,你會介意就如何使用ols.predict的例子嗎?說你有三個獨立變量,因此三個beta [b1,b2,b3]現在你想用[x1,x2,x3]來預測ay – tesla1060 2013-03-24 12:33:40

回答

2

是你的問題如何獲得你的迴歸預測y值?或者,如何使用迴歸係數爲外生變量的不同樣本組獲得預測的y值? pandas y_predict和y_fitted應該給你相同的值,兩者都應該給你與scikits.statsmodels中預測方法相同的值。

如果您正在尋找回歸係數,做ols_test.beta

+0

我想預測2008年到2017年的y值,我可以用scikits得到.statsmodels預測,但我不知道如何與熊貓得到它。 – Turukawa 2012-04-01 18:54:58

+0

Gotcha。如果你想使用pandas ols函數,你現在可以做(ols_result.beta ['x'] * exog_2008_2017).sum()+ ols_result.beta ['intercept']。 – 2012-04-07 20:40:26

+0

我在這裏打開了一個關於它的Github問題:https://github.com/pydata/pandas/issues/1008 提供了一個複製statsmodels功能的函數 – 2012-04-07 21:03:34