(pandas)pd.ols と (statsmodels)sm.ols の両方を使用して、回帰直線を使用して回帰散布図を取得しようとしましたが、散布図を取得できますが、取得するためのパラメーターを取得できないようですプロットする回帰直線。ここでカットアンドペーストコーディングを行っていることはおそらく明らかです:-((これをガイドとして使用: http://nbviewer.ipython.org/github/weecology/progbio/blob/master/ipynbs/statistics.ipynb
私のデータは pandas DataFrame にあり、x 列は merged2[:-1].lastqu で、y データ列は merged2[:-1].Units です。コードは次のようになりました: 回帰を取得するには:
def fit_line2(x, y):
X = sm.add_constant(x, prepend=True) #Add a column of ones to allow the calculation of the intercept
model = sm.OLS(y, X,missing='drop').fit()
"""Return slope, intercept of best fit line."""
X = sm.add_constant(x)
return model
model=fit_line2(merged2[:-1].lastqu,merged2[:-1].Units)
print fit.summary()
^^^^大丈夫そうです
intercept, slope = model.params << I don't think this is quite right
plt.plot(merged2[:-1].lastqu,merged2[:-1].Units, 'bo')
plt.hold(True)
^^^^^これで散布図が完成します****そして以下では回帰直線が得られません
x = np.array([min(merged2[:-1].lastqu), max(merged2[:-1].lastqu)])
y = intercept + slope * x
plt.plot(x, y, 'r-')
plt.show()
データフレームのスニペット: [:-1] は、現在の期間をデータから削除します。これは、後で投影されます
Units lastqu Uperchg lqperchg fcast errpercent nfcast
date
2000-12-31 7177 NaN NaN NaN NaN NaN NaN
2001-12-31 10694 2195.000000 0.490038 NaN 10658.719019 1.003310 NaN
2002-12-31 11725 2469.000000
編集:
私ができることがわかりました:
fig = plt.figure(figsize=(12,8))
fig = sm.graphics.plot_regress_exog(model, "lastqu", fig=fig)
ここで説明されているようにStatsmodels doc は、私が望んでいた主なもの (およびそれ以上) を取得しているようですが、以前のコードでどこが間違っていたのかを知りたいです!