简体   繁体   中英

Statsmodels "predict" function - why cant I predict out of sample? :(

I have fitted a multiple linear regression model using statsmodels OLS with 4 inputs and wish to predict one month into the future. I can predict "into my test-set" and evaluate on the performance of my forecast, but when I try predicting into a specific time period outside my dataset nothing works. This is the prediction into my test-set that works:

est = sm.OLS(y_train, x_train)
est2 = est.fit()
ypred = est2.predict(x_valid)

This is my best attempt to predict into a specific timeperiod outside my dataset:

start_index = datetime(2019, 12, 2)
end_index = datetime(2020, 1, 2)
forecast = est2.predict(start_index, end_index)

This is my error code:

ValueError: shapes (1,1) and (4,) not aligned: 1 (dim 1) != 4 (dim 0)

Any help or references on where to find help is very highly valued.

Br and big thanks in advance

Each of the element of x_valid collections should have the same type as elements in the x_train collection. So you should:

  1. Extract from the x_valid needed records based on date.
  2. Call the predict method with extracted collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM