# Difference in Forecast and Fitted method in R

I have the following piece of code:

library(forecast)
set.seed(1234)
y <- ts(sort(rnorm(30)), start = 1978, frequency = 1) # annual data
fcasts <- numeric(10)
for (i in 1:10) { # start rolling forecast
# start from 1997, every time one more year included
win.y <- window(y, end = 1996 + i)
fit <- auto.arima(win.y)
fcasts[i] <- forecast(fit, h = 1)$mean } train <- window(y,end=1997) fit<- auto.arima(train) refit <- Arima(y, model=fit) fc <- window(fitted(refit), start=1998)  I thought both should give same results, but why fcasts and fc give different results? • Yes it does, it was just for the data I was using, which is replaced by dummy data here. – Waqas Aug 2 '16 at 11:29 • This question has already been adequately answered here. – Billywob Aug 2 '16 at 11:50 ## 2 Answers If you shorten you code, it becomes: library(forecast) set.seed(1234) y <- ts(sort(rnorm(30)), start = 1978, frequency = 1) # annual data fcasts <- numeric(10) for (i in 1:10) { fcasts[i] <- forecast(auto.arima(window(y, end = 1996 + i) ), h = 1)$mean
}
fc <- window(fitted(Arima(y, model = auto.arima(window(y, end = 1997)))), start = 1998)


And here you can see, that fc always relies on the constant auto.arima(window(y, end = 1997)), where the values in fcasts rely on the changing auto.arima(window(y, end = 1996 + i)). Because both forcasts for 1998 are based on the identical model for 1997, they are identical. But the others are not.

To adress the comment whether both models give the same result:

fcasts <- numeric(10)
fc <- numeric(10)

for (i in 1:10) {
fcasts[i] <- forecast(auto.arima(window(y, end = 1996 + i) ), h = 1)$mean fc <- window(fitted.Arima(Arima(y, model = auto.arima(window(y, end = 1996 + i)))), start = 1998)[i] } fcasts-fc # is identical (beside very small numerical differences of e-14).  • For the first (n-1) terms difference was subtle, and I thought if they are the same then I can avoid the loop, which is computationally more efficient, thank you for the explanation. – Waqas Aug 2 '16 at 12:23 • So fitting the same model will give identical results? – Waqas Aug 2 '16 at 12:26 • @Vic, if you change the generation of fc in a manner that you fit the same auto.arima model: yes, they provide identical results. I added this to my answer. – Qaswed Aug 2 '16 at 12:39 Apart from the difference between forecast and fitted pointed by @Billywob in the comments, your loop is using different models to forecast: fcasts <- numeric(10) modls<-list() for (i in 1:10) { # start rolling forecast # start from 1997, every time one more year included win.y <- window(y, end = 1996 + i) fit <- auto.arima(win.y) modls[[i]]<- names(fit$coef)
fcasts[i] <- forecast(fit, h = 1)$mean } > modls [[1]] [1] "ar1" [[2]] [1] "ar1" [[3]] [1] "ar1" [[4]] [1] "ar1" [[5]] [1] "ar1" [[6]] [1] "ar1" [[7]] [1] "ar1" [[8]] [1] "ar1" [[9]] [1] "ar1" "drift" [[10]] [1] "ar1" "drift"  and your fitted only uses one model. > names(fit$coef)
[1] "ar1"