business-science / modeltime

Modeltime unlocks time series forecast models and machine learning in one framework
https://business-science.github.io/modeltime/
Other
522 stars 79 forks source link

Refit and Forecast 1 day ahead with exogenous regressors #24

Closed Steviey closed 4 years ago

Steviey commented 4 years ago

Ubuntu: 16.4 LTS, R: 4.0.2, modeltime: 0.0.2

Thank you Matt for the marvelous code.

I can't find any documentation/information how to refit and forecast 1 day ahead, using exogenous regressors.

In the forecast package by Prof. Hyndman I would say:

arima.forecast <- forecast(arima.model,h=myH,xreg=newRegressors,biasadj=T)

When I say...

            unseenPredict<-calibration_table %>%
                modeltime_refit(model_table,data=allData) %>%
                #modeltime_forecast(h="3 months",actual_data=allData)# %>%
                modeltime_forecast(new_data=testing(splits),actual_data=allData)# %>%

... I get data by type actual and forecast until the last day contained in splits. But I want to forecast one unseen day ahead, including exogenous regressors used while training.

In regard to "modeltime_forecast()" I read about a future tibble. But then the documentation PDF ends... "Forecasting Future Data: See future_frame() for creating future tibbles." "future_frame()" seems to be a dead link.

Are there any hints available- or a small code example?

Thank you.

mdancho84 commented 4 years ago

For a 1-step or multi-step ahead forecasts that include XREGS (e.g. lags, events, etc), you need to use new_data providing all columns with the exception of the target column (optionally the target can be encoded as NA values). This means you need to supply the Xregs as part of the future data frame.

Steviey commented 4 years ago

OK, I understand Matt. Is it right to forecast the xregs for the future data frame e.g. with a default ets-method from package forecast?...


allData<-as_tibble(sampleData)
            futureDf<-tail(allData,n=sSize)

            modelXreg<-function(allData,title,h,n){
                what1<-tail(allData,n=n)
                what1$date <- as.Date(what1$date,format="%Y-%m-%d")
                what1<-what1[,c('date',title)]
                what1 <-tibble::as_tibble(what1)
                what1<-tk_ts(what1,start=1,freq=1, silent = TRUE)
                fcMyXreg<- forecast::forecast(what1,h=h)$mean
                fcMyXreg<-as.numeric(fcMyXreg)
                return(fcMyXreg)
            }

myFuture<- futureDf %>% timetk::future_frame(.date_var=date,.length_out=myH,.inspect_weekdays=TRUE,.inspect_months = TRUE)
            myFuture$myXreg<-modelXreg(allData,'myXreg',h=myH,n=sSize)

unseenPredictTib<-calibration_table %>%
                modeltime_refit(model_table,data=allData) %>%
                modeltime_forecast(new_data=myFuture,actual_data=allData)

Is this the right strategy? Thank you, for your advise Matt!

Vidaringa commented 4 years ago

For a 1-step or multi-step ahead forecasts that include XREGS (e.g. lags, events, etc), you need to use new_data providing all columns with the exception of the target column (optionally the target can be encoded as NA values). This means you need to supply the Xregs as part of the future data frame.

How does this work if the lags are of the outcome variable you are trying to forecast? This was helpful in the M5 competition I think. I tried to include step_lag(all_outcomes(), lag = 1:7) but this alone will not work. What other steps to I have to take to make a forecast e.g. 14 days into the future?

mdancho84 commented 4 years ago

Don't create lags as a preprocessing step inside of your preprocessing recipe. Rather create the lags outside of your recipe.

In the upcoming Time Series course, I teach you to:

  1. Extend your data frame through your forecast window
  2. Add lags and any external regressors.
  3. Split into a data_prepared_tbl and a forecast_tbl. <-- Now you have lags in your future data
  4. Split data_prepared_tbl into train/test <-- Now you have training set that you can model with, and test for calibration
  5. Apply recipes to training set <-- Don't do lags here, this is too late.
Vidaringa commented 4 years ago

Ok I see. Looking forward to the course!

mdancho84 commented 4 years ago

Yes - There is a strategy. :)