I was doing a linear regression using statsmodels
in Python, and when I plotted the result it appeared erroneous. I checked a different data set, using the code from this question.
But even when I use the following code (taken from the above linked question), the line of best fit is still not displayed correctly. I am unsure what the problem is.
Code:
import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
X = np.random.rand(100)
Y = X + np.random.rand(100)*0.1
results = sm.OLS(Y,sm.add_constant(X)).fit()
print results.summary()
plt.scatter(X,Y)
X_plot = np.linspace(0,1,100)
plt.plot(X_plot, X_plot*results.params[0] + results.params[1])
plt.show()
My output:
Why isn't the line of best fit correct?