Search code examples
pythonregressionlinear-regression

X=sm.add_constant(X, prepend=True) is not working


I am trying to get the beta and the error term from a linear regression(OLS) in python. I am stuck at the statement X=sm.add_constant(X, prepend=True), which is returning an

error:"AttributeError: 'module' object has no attribute 'add_constant'"

I already installed the statsmodels module.


Solution

  • If sm is a defined object in statsmodels, you need to invoke it by statsmodels.sm, or using from statsmodel import sm, then you can invoke sm directly.