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?
 A: 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`).

A: 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"

