我一直在使用scikits.statsmodels OLS predict函数来预测拟合数据,但现在想改用 Pandas。
该文档指的是 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))
如何在 Pandas 中执行此操作以将内生数据预测到外生数据的极限?
更新:感谢 Chang,新版本的 Pandas (0.7.3) 现在具有此功能作为标准。