# Auto.arima with xreg in R, restriction on forecast periods

I am using the forecast package and implement auto.arima with xreg. Here I want to forecast only for 1 year ahead but I am unable to use h parameter in the forecast function. Below is the reason for that:

Definition is given in manual(F1 check):

h = "Number of period of forecast but if xreg is used 'h' is ignored and the forecast period will be number of rows"

Please suggest me an alternate way to use h for the specific period forecast.

## 1 Answer

Using xreg suggests that you have external (exogenous) variables. In this, a regression model is fitted to the external variables with ARIMA errors.

When forecasting you need to provide future values of these external variables. In practice, these are often forecasts or could be known. For example, if you're trying to predict Sales and you use Advertising spend as an external variable, you may know the advertising spend for the upcoming year.

auto.arima then produces forecasts for the length of xreg, therefore disregarding h.

Based on your comments below, I've provided an example script demonstrating this based on the Sales example above.

library(forecast)

# Generate sample data
sales <- sample(100:170, 4*10, replace = TRUE)
advertising <- sample(50:70, 4*10, replace = TRUE)

# Create time series objects.
sales_ts <- ts(sales, frequency = 4, end = c(2017, 4))

fit <- auto.arima(sales_ts, xreg = advertising)

# If we pass external_regressor into the forecast, h will be disregarded and we will
# get a forecast for length(external_regressor)

wrong_forecast = forecast(fit, h = 4, xreg = advertising)

length(wrong_forecast) # Will be 40

# To forecast four quarters in advance, we must provide forecasted external regressor data
# for the upcoming four quarters, so that length(new_regressor) == 4.
# In reality, this data is either forecasted from another forecast, or is known. We'll randomly generate it.

upcoming_advertising <- sample(50:70, 4, replace = TRUE)

correct_forecast <- forecast(fit, xreg = upcoming_advertising)

length(correct_forecast\$mean) # Will be 4


The key things to note are:

If we forecast with the same regressors as we did when generating the forecast, h will be disregarded and a forecast will be generated for the length of xreg in your case, 10 years.

As such, we must provide new data for xreg for the length of time we wish to forecast - in your case, 4 quarters.

• Thank you James for your response, 1. I am using 8 years (2010-2017) of past quarterly data for training model with external variables (same length 2010-2017) data. Now I want to forecast for (4 quarters) 2018. This is where I am facing issue, Now I am trying to forecast(using 'h=4') for 4 quarters, but it is forecasting till 2025 (Next 8 years) 2. You mentioned in your answer, "provide future values of these external variables", Which parameter shall I use to input those values. Please clarify, it will very helpful. Jun 18 '19 at 10:14
• Hi Zuber, in this case it sounds as though you've created your time-series object ts incorrectly. You can set quarterly data by setting the frequency parameter in ts to 4. For example: ts(data, frequency=4, start=c(2010,1)) # First quarter of 2010. Jun 19 '19 at 1:07
• Hey James, I have given the frequency parameter to 4 only, attaching below code: library(forecast) TSobject = ts(NumericVec, frequency = 4, end = c(2017,4)) # 10 years data exreg = as.matrix(xregData), fit = auto.arima(TSobject, xreg = exreg), predict = forecast(fit, h = 4, xreg = exreg) In the package manual, it is mention that if 'xreg' is used, h will be avoided and forecast period will be the number of rows but I want to forecast only for 4 quarter. please help me out. Jun 20 '19 at 14:16
• I've edited my answer based on your comments with an example piece of code. Jun 20 '19 at 21:48
• Thanks a lot, James. Your very patient person, now I got complete clarity. Jun 21 '19 at 8:40