Reputation: 127
I saw multiple simmilar questions about the following error in the sklearn:
'AttributeError: LinearRegression object has no attribute...'
I couldn't find any hint about my problem tough:
AttributeError: LinearRegression object has no attribute 'model'
I tried to do a multilinear regression y ~ x with the following code:
import statsmodels.api as sma
from sklearn import linear_model
#https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html
#perform linear regression
df_x = df.drop('Migration distance',1) #for simplicity I did't use any testing data just this 2
df_y = df['Migration distance']
reg = linear_model.LinearRegression().fit(df_x, df_y)
reg_score=reg.score(df_x, df_y)
print('R2 score:',reg_score)
#plot the residuals
fig = plt.figure(figsize=(12,8))
fig = sma.graphics.plot_regress_exog(reg, 'Migration distance', fig=fig)
but this error occurs every time I try to plot the residuals:
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12904/573591118.py in <module>
10
11 fig = plt.figure(figsize=(12,8))
---> 12 fig = sma.graphics.plot_regress_exog(reg, 'Migration distance', fig=fig)
C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\graphics\regressionplots.py in plot_regress_exog(results, exog_idx, fig)
218 fig = utils.create_mpl_fig(fig)
219
--> 220 exog_name, exog_idx = utils.maybe_name_or_idx(exog_idx, results.model)
221 results = maybe_unwrap_results(results)
222
AttributeError: 'LinearRegression' object has no attribute 'model'
I think my linear regression works because I can compute the R2 score but I have no clue how to overcome this error in order to plot the residuals.
Upvotes: 0
Views: 1563
Reputation: 60321
As the documentation for graphics.plot_regress_exog
implies, the model passed in the results
argument (i.e. your reg
here) must be
A result instance with resid, model.endog and model.exog as attributes.
i.e. a statsmodels model, and not a scikit-learn one, as your LinearRegression
here. In other words, the function cannot work with scikit-learn models.
Since you are actually doing a simple OLS regression, if you really need the functionality, I would suggest using the respective statsmodels model instead of the scikit-learn one.
Upvotes: 1