# How to build a function with the result of auto.arima in R?

I use: fit = auto.arima(Y, xreg=X) in R to get ARIMA(1,0,0), result as follows:

ar1: 0.3793;    intercept: 9132.46;    X: 22.0469


Then:

1. I build the function: (Y(t) - 9132.46) = 0.3793*(Y(t-1) - 9132.46) + 22.0469*X(t) in Excel to calculate fitted value and predicted value manually.
2. In R, I use fitted(fit) to get fitted value, forecast() to get predicted value.

But I found the results of 1) and 2) are different, is there anything wrong with the function I built?

You should use the following formula:

Y(t) = 0.3793*(Y(t-1) - 9132.46 - 22.0469*X(t-1)) + 9132.46 + 22.0469*X(t).

Example to replicate out-of-sample forecasts:

require(forecast)
set.seed(123)
n <- 100
xreg <- rnorm(n)
x <- arima.sim(n=n, model=list(ar=0.4)) + 2 + 0.8 * xreg
fit <- arima(x, order=c(1,0,0), include.mean=TRUE, xreg = xreg)
newxreg <- rnorm(5)
# forecasts using predict
pred <- predict(fit, n.ahead=5, newxreg = newxreg)\$pred
# forecasts by hand
pred2 <- rep(NA, 5)
pred2[1] <- coef(fit)[1] * (x[n] - coef(fit)[2] - coef(fit)[3] * xreg[n]) +
coef(fit)[2] + coef(fit)[3] * newxreg[1]
for (i in seq(2, 5))
pred2[i] <- coef(fit)[1] * (pred2[i-1] - coef(fit)[2] - coef(fit)[3] * newxreg[i-1]) +
coef(fit)[2] + coef(fit)[3] * newxreg[i]
cbind(pred, pred2)
#         pred    pred2
# 101 2.966803 2.966803
# 102 1.822288 1.822288
# 103 1.991748 1.991748
# 104 2.095386 2.095386
# 105 2.855613 2.855613
all.equal(as.vector(pred), pred2)
#[1] TRUE

• this is the same with the equation I used, but the estimated value is different from R. – Sherin Oct 14 '14 at 14:44
• I overlooked the external regressors. I updated my answer. – javlacalle Oct 14 '14 at 17:53