title: Predicting values using Arima model for periods that are not ahead the end of the series
body: <p>I'm generating an Arima model with an external regressor. Let's suppose I have <strong>n</strong> observations. The <code>predict.Arima</code> function from <code>forecast</code> package just make predictions for <strong>n + 1</strong> observation on.</p>
  
  <p>I need to make a prediction for the <strong>n</strong> value (last value of the series), changing the value of the external regressor, i.e., I need to predict the value of the <strong>n</strong> observation given an specific value for the external regressor.</p>
  
  <pre><code>library(forecast)
  set.seed(123)
  aux &lt;- 1:24
  covari &lt;- aux + rnorm(24,0,2)
  vari &lt;- ts(aux * runif(24,0,3), start=c(2010,1), freq=12)
  
  mod &lt;- auto.arima(vari, xreg=covari)
  
  predict(mod, newxreg=20)
  </code></pre>
  
  <p>This code generate a model, and shows how to generate a prediction. I can control the number of periods ahead setting the parameter <code>n.ahead</code>.</p>
  
  <pre><code>predict(mod, newxreg=runif(4,15,25), n.ahead=4)
  </code></pre>
  
  <p>This code will generate predictions for the next 4 values of the series.</p>
  
  <p>What I need is an <code>n.ahead=-1</code>, i.e., a prediction for a value inside the series, but with a different external regressor.</p>
  
  <p>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.</p>
  
  <p>Is there any way to predict values that are not ahead the end of the series of an Arima model?</p>