Predicting values using Arima model for periods that are not ahead the end of the series

João Daniel picture João Daniel · Apr 24, 2012 · Viewed 9.6k times · Source

I'm generating an Arima model with an external regressor. Let's suppose I have n observations. The predict.Arima function from forecast package just make predictions for n + 1 observation on.

I need to make a prediction for the n value (last value of the series), changing the value of the external regressor, i.e., I need to predict the value of the n observation given an specific value for the external regressor.

library(forecast)
set.seed(123)
aux <- 1:24
covari <- aux + rnorm(24,0,2)
vari <- ts(aux * runif(24,0,3), start=c(2010,1), freq=12)

mod <- auto.arima(vari, xreg=covari)

predict(mod, newxreg=20)

This code generate a model, and shows how to generate a prediction. I can control the number of periods ahead setting the parameter n.ahead.

predict(mod, newxreg=runif(4,15,25), n.ahead=4)

This code will generate predictions for the next 4 values of the series.

What I need is an n.ahead=-1, i.e., a prediction for a value inside the series, but with a different external regressor.

If I'm using just one external regressor the task is not complicated, because since is an additive model, I can just add the difference of the observed xreg value by the value I want multiplied by the coefficient of the xreg. However it gets more complicated if the number of external regressors increase.

Is there any way to predict values that are not ahead the end of the series of an Arima model?

Answer

Rob Hyndman picture Rob Hyndman · Apr 25, 2012

What do you mean by "predict"? With time series, that is an estimate of a future value conditional on the observed past values. So a "prediction" of an observed value is simply the observed value.

But perhaps you mean fitted value. That is, the one-step forecast of an observation conditional on all previous observations. In that case, you can get what you want using fitted(mod).

By the way, predict.Arima() is not part of the forecast package. The forecast package provides the forecast.Arima() function as a replacement. For example:

forecast(mod, xreg=20)
forecast(mod, xreg=runif(4,15,25), h=4)

Update: As explained in the comment, the OP wants a "prediction" of a past observation assuming a different value of the regressor had been observed. There are several ways of interpreting that.

First, where the coefficients are updated to reflect the new information, and only past data are used. In that case, just re-fit the model and get the fitted values.

Second, where the coefficients are not updated, and only past data are used. There is no function for that, and I'm not sure why anyone would need to do it. But it can be done as follows:

fitted(mod) + mod$coef["covari"] * (newx - oldx)

Third, where the coefficients are not updated, and all data are used. Then we get

observed  + mod$coef["covari"] * (newx - oldx)