我有正常工作的Pandas OLS代码行,但无法将params用于另一个相关函数:
ES_15M_LR = pd.ols(y = ES_15M_Last_300_Periods['Close'], x = ES_15M_Last_300_Periods['Date'])
上面的代码效果很好,但是当我尝试从中拉出参数时,我得到了错误:
AttributeError: 'OLS' object has no attribute 'params'
例如,我尝试过:
ES_15M_LR.params
以及:
ES_15M_LR.params.x
...拉x系数(斜率)。这会产生与上述相同的错误。然而,我可以看到统计数据按预期工作:
我似乎无法自动提取参数,我需要将其作为其他函数的变量。有人可以帮忙吗?
答案 0 :(得分:3)
首先,强烈建议您使用statsmodels,因为......
pandas.stats.ols
,pandas.stats.plm
和pandas.stats.var
例程 已弃用,将在以后的版本中删除(GH6077: MIGRATE:将统计代码移至statsmodels /弃用于pandas #6077 )
关于param
访问,
import numpy as np
import pandas as pd
import statsmodels.api as sm
df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('AB'))
model = sm.OLS(df['A'], df['B'])
fit = model.fit()
print fit.params
B 0.724865
print fit.summary()
OLS Regression Results
==============================================================================
Dep. Variable: A R-squared: 0.533
Model: OLS Adj. R-squared: 0.528
Method: Least Squares F-statistic: 113.0
Date: Thu, 16 Feb 2017 Prob (F-statistic): 4.66e-18
Time: 10:27:13 Log-Likelihood: -509.62
No. Observations: 100 AIC: 1021.
Df Residuals: 99 BIC: 1024.
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
B 0.7249 0.068 10.629 0.000 0.590 0.860
==============================================================================
Omnibus: 3.447 Durbin-Watson: 1.724
Prob(Omnibus): 0.178 Jarque-Bera (JB): 2.856
Skew: 0.301 Prob(JB): 0.240
Kurtosis: 2.432 Cond. No. 1.00
==============================================================================
答案 1 :(得分:1)
我从未将OLS与熊猫一起使用,但它似乎曾经存在于pandas中并且已移至statsmodel包中。似乎文档也已过时或不正确,但ES_15M_LR.beta
应该可以解决问题。