Add a constant term (y-intercept) when performing multiple regression analysis with Python's Statsmodels

Introduction

When I was doing multiple regression analysis, I was worried that the slope and p-value calculated by Excel and the value calculated by Statsmodel of Python were different, but I noticed that I did not add a constant term to Statsmodel. ・ ・.

It seems that you can use ʻadd_const` of Stats model to add it. statsmodels.tools.tools.add_constant

code

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()

at the end

Thank you very much.

Recommended Posts

Add a constant term (y-intercept) when performing multiple regression analysis with Python's Statsmodels
Points to note when performing logistic regression with Statsmodels
Multiple regression analysis with Keras
I tried multiple regression analysis with polynomial regression
Linear regression with statsmodels
Regression analysis with NumPy