Python 泰尔森回归:平移x轴时的不同结果

Python 泰尔森回归:平移x轴时的不同结果,python,scikit-learn,regression,linear-regression,Python,Scikit Learn,Regression,Linear Regression,我想在时间序列上拟合泰尔森回归(使用scikit learn)。我尝试了两件事: 直接在年份上拟合回归器(X={2002:2019} 直接在年份上拟合回归器-最小年份(X={0:18} 我本以为结果是一样的,但它们是不同的。如果我使用OLS回归,它们确实是相似的。 我错过了什么 from sklearn.linear_model import TheilSenRegressor, LinearRegression y = np.array( [688., 895., 1673.,

我想在时间序列上拟合泰尔森回归(使用scikit learn)。我尝试了两件事:

  • 直接在年份上拟合回归器(X={2002:2019}
  • 直接在年份上拟合回归器-最小年份(X={0:18} 我本以为结果是一样的,但它们是不同的。如果我使用OLS回归,它们确实是相似的。 我错过了什么

from sklearn.linear_model import TheilSenRegressor, LinearRegression

y = np.array(
    [688., 895., 1673., 1077., 855., 1064., 1226., 3900., 699., 699., 2726., 1383., 1542., 2132., 1275., 969., 2789.,
     2576.])

X = np.arange(len(y)).reshape(-1, 1)

X2 = X + 2002

y_pred2 = TheilSenRegressor(random_state=0).fit(X2, y).predict(X2)
print(y_pred2)

y_pred = TheilSenRegressor(random_state=0).fit(X, y).predict(X)


import matplotlib.pyplot as plt

fig, axarr = plt.subplots(2, 2)


axarr[0, 0].scatter(X, y)
axarr[0, 0].plot(X, y_pred, color='orange')
axarr[0, 0].title.set_text('Theil-Sen: X')

axarr[0, 1].scatter(X2, y)
axarr[0, 1].plot(X2, y_pred2, color='orange')
axarr[0, 1].title.set_text('Theil-Sen: Shifted X')

axarr[1, 0].scatter(X, y)
axarr[1, 0].plot(X, LinearRegression().fit(X, y).predict(X), color='orange')
axarr[1, 0].title.set_text('OLS: X')

axarr[1, 1].scatter(X2, y)
axarr[1, 1].plot(X2, LinearRegression().fit(X2, y).predict(X2), color='orange')
axarr[1, 1].title.set_text('OLS: Shifted X')