statsmodelsの回帰分析で切片interceptを追加する方法

statsmodelで回帰分析を行った時のこと

import statsmodels.api as sm
model = sm.OLS(Y, X)
results = model.fit()
print(results.summary())

こんな感じのコードを書いていた

が、回帰分析で重要なInterceptが出てこない

statsmodels.regression.linear_model.OLS - statsmodels 0.15.0 (+251)

An intercept is not included by default and should be added by the user. See statsmodels.tools.add_constant.

デフォルトでは切片は出ないようだ

切片まで計算したい場合はadd_constantを使えと書いてある

statsmodels.tools.tools.add_constant - statsmodels 0.14.1
import statsmodels.api as sm
X = sm.add_constant(X) # この行を追加
model = sm.OLS(Y, X)
results = model.fit()
print(results.summary())

これでOK

結果のconstantがInterceptを表していると思われる

 

コメント

タイトルとURLをコピーしました