I have read the questions about the ARIMA
and ARMA
prediction here and here, and also here.
I'd like to make an one-step ahead forecast in-sample with the ARIMA(p=1,d=1,q=0)
model. I have used the forecast
packages:
library(forecast)
set.seed(1)
n<-252
mydata1 <- runif(n, 9000, 10000)
fit1<-Arima(mydata1[1:(n-1)], order=c(1,1,0))
forecast(fit1, h=1)$mean[1]
# 9850.593
Then I have tried to use the predict()
function
predict(fit1, n.ahead=1)$pred[1]
# 9850.593
The reasults are equal. The ARIMA(1,1,0)
model has only one coefficient ar1
:
fit1$coef[1]
# ar1
# -0.4896545
I have tried to write the one-step ahead prediction: $$\hat Y_{n|n-1} = \hat \mu + \hat{ar_1} \cdot (Y_{n-1} - \hat \mu).$$
and then make the calculation in R:
mean(mydata1[n-1]) + coef(fit1)[1] * (mydata1[n-1] - mean(mydata1[n-1]))
# ar1
# 9761.974
The manual result is 9761.974
and it is not equal to 9850.593
.
I think my mistake in the formula because I should use the first difference of time series (d=1
) but not the original time series.
Question. Could anyone guide me in the manual calculation?