# How to use Dynamic Regression models in R to forecast future sales

I want to forecast the sales having 2 independent variables, x1 and x2. I want to choose between different combinations and lags, e.g:

sales ~ x1

sales ~ lag(x1,-1)

sales ~ lag(x1,-1) + lag(x2,-1)

etc ...

I use the function auto.arima(sales, xreg=c(x1,x2)) in R.

My questions are:

i) What is the best way to choose the most appropriate model for forecasting purposes?

ii) I want to forecast sales, lets say, for the whole 2018. Do I have to separately forecast x1 and x2 and use these forecasts as inputs to the regression model? Is this the correct approach?

Does this process of forecasting the predictors and then using the forecasts as inputs to the regression model have a specific name?

1. What is the best way to choose the most appropriate model for forecasting purposes?

One approach is to 1.) set up lagged predictors, 2.) fit auto.arima 3.) compare aicc

The below is untested code, but hopefully useful

my_x1 <- cbind(
Lag0 = df[,"x1"],
Lag1 = stats::lag(df[,"x1"],-1),
Lag2 = stats::lag(df[,"x1"],-2),
Lag3 = stats::lag(df[,"x1"],-3)) %>%

my_x2 <- cbind(
Lag0 = df[,"x2"],
Lag1 = stats::lag(df[,"x2"],-1),
Lag2 = stats::lag(df[,"x2"],-2),
Lag3 = stats::lag(df[,"x2"],-3)) %>%

# Restrict data so models use same fitting period
fit1 <- auto.arima(df[4:40,1], xreg=c(my_x1[4:40,1], my_x2[4:40,1]),
stationary=TRUE)
fit2 <- auto.arima(df[4:40,1], xreg=c(my_x1[4:40,1:2], my_x2[4:40,1:2]),
stationary=TRUE)
fit3 <- auto.arima(df[4:40,1], xreg=c(my_x1[4:40,1:3], my_x2[4:40,1:3]),
stationary=TRUE)
fit4 <- auto.arima(df[4:40,1], xreg=c(my_x1[4:40,1:4], my_x2[4:40,1:4]),
stationary=TRUE)

c(fit1[["aicc"]],fit2[["aicc"]],fit3[["aicc"]],fit4[["aicc"]])


2. I want to forecast sales, lets say, for the whole 2018. Do I have to separately forecast x1 and x2 and use these forecasts as inputs to the regression model? Is this the correct approach?

It may be that your best option here is to setup some scenario forecasting. For example, if x1 went up by 5% then the forecast would be sales_y. To forecast your predictors then use those forecasts to forecast sales introduces additional potential for error.

3. Does this process of forecasting the predictors and then using the forecasts as inputs to the regression model have a specific name?

Unless you are doing "scenario forecasting" as described above, I think that some forecasters would call the processes of building forecasts off of forecasts not recommended, but maybe there are others on this forum who can provide more insight into this approach.

• I can understand that this process allows for more error, but if lets say, I don't want to do scenario forecasting but get a normal 6 months ahead forecast, then what do I do? – Vasilis Jul 3 '18 at 6:56
• I can't know your exact situation, but a pragmatic approach would be to back test as many senarios you can. Take a collection of univariate time series method, such as arima, exponential smoothing, and seasonal trend loess. Also, try some methods usually dubbed 'inappropriate' such as multiple regression. Test the monthly forecasts of these methods on the last 6 months and compair the MAE. Keep in mind that your multiple regression output may be overly optomisitc because you know your predictors with certainty. – Alex Jul 3 '18 at 9:44

This is an advanced class response to your question where dynamic regression structure is identified and error diagnostics are incorporated to refine tentative models.

1. Follow http://docplayer.net/12080848-Outliers-level-shifts-and-variance-changes-in-time-series.html to possibly identify anomalies (pulses,level shiifts, seasonal pulses) and possible deterministic error variance change.

2. Review Transfer function in forecasting models - interpretation and http://svds.com/avoiding-common-mistakes-with-time-series/ to find out why ols methods to identify a tf are to be studiously avoided

3. study http://www.autobox.com/cms/index.php/afs-university/autobox-examples/modeling-with-autobox/ paaticularly section 4 and the flow diagram

4. Finally when you predict use monte carlo methods enabling possible future anomalies in order to obtain meaningful prediction intervals.

• If you are happy with mu answer , please accept it to close the question – IrishStat Aug 15 at 8:51