LoginSignup
6
7

More than 5 years have passed since last update.

PythonのStatsmodelsで重回帰分析を行う際に、定数項(y切片)を追加する

Posted at

はじめに

重回帰分析を行っていたところ、エクセルで算出した傾きや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()

さいごに

ありがとうございました。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7