1
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ Yes it does, it was just for the data I was using, which is replaced by dummy data here. $\endgroup$
    – Waqas
    Commented Aug 2, 2016 at 11:29
  • $\begingroup$ This question has already been adequately answered here. $\endgroup$
    – Billywob
    Commented Aug 2, 2016 at 11:50

2 Answers 2

1
$\begingroup$

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`).
$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – Waqas
    Commented Aug 2, 2016 at 12:23
  • $\begingroup$ So fitting the same model will give identical results? $\endgroup$
    – Waqas
    Commented Aug 2, 2016 at 12:26
  • $\begingroup$ @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. $\endgroup$
    – Qaswed
    Commented Aug 2, 2016 at 12:39
0
$\begingroup$

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"
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.