You don't need the loop here. The one-step forecasts are the same as fitted values in a time series model. So the following should do what you want:
library(forecast)
model <- auto.arima(y)
newfit <- Arima(c(y,new.data), model=model)
onestep.for <- fitted(newfit)[1001:1010]
forecast
always produces forecasts beyond the end of the data.
So forecast(fit)
produces forecasts for observations 401, 402, ...
and forecast(refit)
produces forecasts for observations 501, 502, ...
fitted
produces one-step in-sample (i.e., training data) "forecasts". That is, it gives a forecast of observation t using observations up to time t-1 for each t in the data.
So fitted(fit)
gives one-step forecasts of observations 1, 2, ... It is possible to produce a "forecast" for observation 1 as a forecast is simply the expected value of that observation given the model and any preceding history.
fitted(refit)
gives one-step forecasts of observations 401, 402, .... So it uses the model estimated on observations 1...400, but it uses the data from time 401...500.
Note that forecast(fit)$mean[1]
will not be the same as fitted(refit)[1]
due to differences in what they are conditioning on. forecast(fit)$mean[1]
conditions on the training data (observations 1...400) while fitted(refit)
conditions only on the test data and it does not "know" what the training data were. So fitted(refit)[1]
is the estimate of observation 401 given the model but no history, while forecast(fit)$mean[1]
is the estimation of observation 401 given the model and the data up to time 400.
Update
Note that the model is actually
\begin{align}
y_t &= \mu + n_t \\
n_t &= \phi n_{t-1} + e_t
\end{align}
where $\mu$ is the estimated "intercept" and $\phi$ is the ar coefficient.
So if you write it in the more usual way,
$$
y_t = (1-\phi)\mu + \phi y_{t-1} + e_t
$$
Thus forecasts are given by
> phi <- coef(fit)['ar1']
> mu <- coef(fit)['intercept']
> (by.hand <- phi*test.5 + (1-phi)*mu)
[1] 1.318043 0.010579 0.628453 -0.515169 -2.010278
>
> (auto <- c(forecast(fit)$mean[1], fitted(refit)[2:5]))
[1] 1.318043 0.010579 0.628453 -0.515169 -2.010278
Best Answer
If it is one-step forecast then both forecasts are the same. The difference arises when forecasting further: "dynamic forecast" will take previously forecasted values while ""static forecast" will take actual values to make next step forecast.