【问题标题】:Out of sample prediction in statsmodel with NaN values具有 NaN 值的 statsmodel 中的样本外预测
【发布时间】:2015-10-01 07:06:48
【问题描述】:

我有一个数据集,其中包含有关美国 auto_sales 的各种值。

我正在尝试使用简单的 OLS 回归来预测 2010 年 10 月的 auto_sales。

df2 = pd.read_csv('Paul_data/question12_prediction_data.csv')
window_size = 7                                              #-1 due to zero-indexing of array
window = df2.ix[0:window_size,:]
print window

result = sm.ols(formula="log_sales ~ log_sales_l2 + vehicleshopping_l2 + vehiclebrand_l2 + actual_sales_edmunds_l1 + isSummer + isWinter", data=df2).fit()
print result.predict()[df2[(df2.month == 10) & (df2.year == 2015)].index[0]]

窗口是以下数据:

year  month  auto_sales  log_sales  log_sales_l1  log_sales_l2  \
0  2015      3       83352  11.330828     11.294807     11.317823   
1  2015      4       83871  11.337035     11.330828     11.294807   
2  2015      5       85489  11.356143     11.337035     11.330828   
3  2015      6       84123  11.340035     11.356143     11.337035   
4  2015      7       85320  11.354164     11.340035     11.356143   
5  2015      8         NaN        NaN     11.354164     11.340035   
6  2015      9         NaN        NaN           NaN     11.354164   
7  2015     10         NaN        NaN           NaN           NaN   

   log_sales_l3  GT_vehicleshopping  GT_vehiclemaintenance  GT_suvs  \
0     11.313523              0.1320                  0.694   0.0680   
1     11.317823              0.1150                  0.745   0.0525   
2     11.294807              0.1060                  0.754   0.0560   
3     11.330828              0.0950                  0.785   0.0550   
4     11.337035              0.1025                  0.870   0.1075   
5     11.356143              0.1140                  0.794   0.1240   
6     11.340035                 NaN                    NaN      NaN   
7           NaN                 NaN                    NaN      NaN   

          ...          vansminivans_l2  isWinter  isSummer  vehiclebrands  \
0         ...                   0.0900         1         0           0.08   
1         ...                   0.1250         0         0           0.09   
2         ...                   0.1580         0         0           0.09   
3         ...                   0.1750         0         1           0.12   
4         ...                   0.1920         0         1           0.17   
5         ...                   0.2100         0         1            NaN   
6         ...                   0.2175         0         0            NaN   
7         ...                      NaN       NaN       NaN            NaN   

   vehiclebrand_l1  vehiclebrand_l2  actual_sales_edmunds  edmund_forecast  \
0             0.05             0.03               1542841          1522881   
1             0.08             0.05               1451790          1464176   
2             0.09             0.08               1631234          1591221   
3             0.09             0.09               1473142          1484487   
4             0.12             0.09               1507643          1478025   
5             0.17             0.12               1573573          1538958   
6              NaN             0.17                   NaN              NaN   
7              NaN              NaN                   NaN              NaN   

   actual_sales_edmunds_l1  edmund_forecast_l1  
0                  1255458             1285019  
1                  1542841             1522881  
2                  1451790             1464176  
3                  1631234             1591221  
4                  1473142             1484487  
5                  1507643             1478025  
6                  1573573             1538958  
7                      NaN                 NaN  

[8 rows x 32 columns]

但是我收到以下错误:


IndexError                                Traceback (most recent call last)
<ipython-input-83-16bf72335e7f> in <module>()
      5 
      6 result = sm.ols(formula="log_sales ~ log_sales_l2 + vehicleshopping_l2 + vehiclebrand_l2 + actual_sales_edmunds_l1 + isSummer + isWinter", data=df2).fit()
----> 7 print result.predict()[df2[(df2.month == 10) & (df2.year == 2015)].index[0]]
      8 #np.exp(result.predict(df2.ix[x+(window_size)]))

IndexError: index 7 is out of bounds for axis 0 with size 5

我现在不确定如何进行,我知道我正在尝试进行样本外预测,但到目前为止我尝试的所有方法都未能解决问题。

【问题讨论】:

  • OLS 可以预测给定的解释变量。在我看来,错误在于解释变量的预测数据不在预测调用中,即检查括号result.predict([df2[(df2.month == 10) &amp; (df2.year == 2015)])。不带参数的预测返回用于估计的样本的fittedvalues

标签: python regression statsmodels


【解决方案1】:

我相信,您的问题是您要回归的数据只有 5 个条目,其中并非所有输入都是 NaN。因此:

result.predict()

返回一个包含 5 个元素的数组,但是:

df2[(df2.month == 10) & (df2.year == 2015)].index[0]

返回“7”,因为您正在执行的切片返回一行,对应于原始数据帧中的第 8 行。所以你问“给我这个长度为 5 的数组的第 8 个元素”,因此它会中断。

【讨论】:

  • 我是否正确,理论上应该可以使用通过使用填充的行建立的回归从样本中进行预测?
  • 当然,这确实是重点。当您在不带参数的情况下调用 predict() 时,您使用的是估计参数而不是用于拟合的数据。在您原来的情况下,它返回了一个包含 5 个预测的列表。在您的编辑中,您现在明确告诉 statsmodels 它应该对哪些数据进行预测
【解决方案2】:

user333700 是正确的,这解决了我的问题:

df2 = pd.read_csv('Paul_data/question12_prediction_data.csv')
window_size = 4                                              #-1 due to zero-indexing of array
window = df2.ix[0:window_size,:]

result = sm.ols(formula="log_sales ~ log_sales_l2 + vehicleshopping_l2 + vehiclebrand_l2 + actual_sales_edmunds_l1 + isSummer + isWinter", data=window).fit()
index = df2[(df2.month == 10) & (df2.year == 2015)].index[0] -1
print result.predict(df2)[index]

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2021-04-27
    • 2020-09-10
    • 1970-01-01
    • 2017-10-24
    • 2019-05-15
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    相关资源
    最近更新 更多