I'm trying to perform a Ordinary Least Squares Regression with some inversely proportional data, but seems like the fitting result is wrong?
import statsmodels.formula.api as sm
import numpy as np
import matplotlib.pyplot as plt
y = np.arange(100, 0, -1)
x = np.arange(0, 100)
result = sm.OLS(y, x).fit()
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(20, 4), sharey=True)
ax.plot(x, result.fittedvalues, 'r-')
ax.plot(x, y, 'x')
fig.show()
You're not adding a constant as the documentation suggests, so it's trying to fit the whole thing as
y = m x
. You wind up with anm
which is roughly 0.5 because it's doing the best it can, given that you have to be 0 at 0, etc.The following code (note the change in import as well)
produces
with coefficients of
[ 100. -1.]
.