はじめに
重回帰分析を行っていたところ、エクセルで算出した傾きやp値と、PythonのStatsmodelで算出した数値が異なり悩んでましたが、Statsmodelに定数項を追加してなかったことに気付きました・・・。
追加するにはStatsmodelのadd_const
と用いればよいようです。
statsmodels.tools.tools.add_constant
コード
hoge.py
from statsmodels import api as sm
X = df[["height","width"]]
X = sm.add_constant(X)
y = df["weight"]
model = sm.OLS(y,X)
result = model.fit()
result.summary()
さいごに
ありがとうございました。