OLS of statsmodels does not work with inversely proportional data?

青春壹個敷衍的年華 提交于 2019-12-12 02:04:46

问题


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


回答1:


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 an m 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)

import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt

y = np.arange(100, 0, -1)
x = np.arange(0, 100)
x = sm.add_constant(x)

result = sm.OLS(y, x).fit()
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 8), sharey=True)
ax.plot(x[:,1], result.fittedvalues, 'r-')
ax.plot(x[:,1], y, 'x')

plt.savefig("out.png")

produces

with coefficients of [ 100. -1.].



来源:https://stackoverflow.com/questions/24723339/ols-of-statsmodels-does-not-work-with-inversely-proportional-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!