我正在尝试找出forecast
和predict
方法之间的区别。
我有以下内容。可以在DataMarket网站上找到数据。
对于具有完整数据的预测,两个函数都给出相同的值,但对于样本外技术则不然。既然Arima
是arima
的包装,那应该不一样吗?
> library(forecast)
> library(tidyverse)
>
> data = read.csv("Data/monthly-lake-erie-levels-1921-19.csv", sep = ",", header = TRUE)
> data = data[-dim(data)[1],]
>
> y = ts(data$Monthly.Lake.Erie.Levels.1921...1970.,
+ frequency = 12,
+ start = c(1921,1))
>
> model.arima = arima(y, order = c(0, 1, 1), seasonal = c(0, 1, 0))
> model.Arima = Arima(y, order = c(0, 1, 1), seasonal = c(0, 1, 0))
>
> preds.arima = predict(model.arima, n.ahead=1)$pred[1]
> preds.Arima = forecast(model.Arima, h=1)$mean[1]
>
> preds.Arima
[1] 15.51397
> preds.arima
[1] 15.51397
>
> S = round(0.75 * length(y))
> h = 1
> errors.a = c()
> errors.A = c()
> preds.a = c()
> preds.A = c()
>
> for (i in S:(length(y) - h))
+ {
+ model.sub.a = arima(y[1:i], order = c(0, 1, 1), seasonal = c(0, 1, 0))
+ pred.a = predict(model.sub.a, n.ahead = h)$pred[h]
+ preds.a = c(preds.a, pred.a)
+ errors.a = c(errors.a, y[i + h] - pred.a)
+
+ model.sub.A = Arima(y[1:i], order = c(0, 1, 1), seasonal = c(0, 1, 0))
+ pred.A = forecast(model.sub.A, h = h)$mean[h]
+ preds.A = c(preds.A, pred.A)
+ errors.A = c(errors.A, y[i + h] - pred.A)
+ }
>
> all = cbind(errors.A, errors.a, preds.A, preds.a)
>
> head(all)
errors.A errors.a preds.A preds.a
[1,] 0.4352598 0.27795891 14.53674 14.69404
[2,] -0.1875257 -0.42878261 15.15953 15.40078
[3,] -0.3183146 -0.49988640 14.89131 15.07289
[4,] -0.6598827 -0.51497911 14.43588 14.29098
[5,] -0.4743845 -0.08133969 13.49138 13.09834
[6,] -0.2121536 0.32318767 12.81215 12.27681
已找到差异的来源。 y[1:i]
对象不再是时间序列对象,而SARIMA模型需要指示工作频率。 arima
和Arima
都在做某事,但显然不是同一件事。更改这两行
# ...
model.sub.a = arima(y[1:i], order = c(0, 1, 1), seasonal = c(0, 1, 0))
# ...
model.sub.a = Arima(y[1:i], order = c(0, 1, 1), seasonal = c(0, 1, 0))
# ...
通过这两个方法可以解决问题。
# ...
model.sub.a = arima(ts(y[1:i], frequency=12), order = c(0, 1, 1), seasonal = c(0, 1, 0))
# ...
model.sub.a = Arima(ts(y[1:i], frequency=12), order = c(0, 1, 1), seasonal = c(0, 1, 0))
# ...