超出具有NaN值的statsmodel中的样本预测

时间:2015-09-30 21:49:40

标签: python regression statsmodels

我有一个数据集,其中包含有关美国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

我不知道如何继续这一点,我知道我正在尝试做出样本预测,但到目前为止我尝试的所有内容都未能解决问题。

2 个答案:

答案 0 :(得分:0)

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

result.predict()

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

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

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

答案 1 :(得分:0)

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]