在R中拟合`arima()`模型后进行预测和绘图

4
刚开始了解时间序列,并使用这篇R-bloggers文章作为下面的练习指导:企图预测股市未来回报率...只是理解时间序列概念的练习。
问题在于,当我绘制预测值时,得到的是一条常数线,与历史数据不符。下面是它在平稳化后的历史道琼斯日回报率的尾部的蓝色线。 enter image description here 实际上,我想要更加“乐观”的视觉效果,或者是一个“重新趋势化”的绘图,就像我对航空旅客预测数量所得到的那个: enter image description here 以下是代码:
library(quantmod)
library(tseries)
library(forecast)
getSymbols("^DJI")
d = DJI$DJI.Adjusted
chartSeries(DJI)
adf.test(d) 
dow = 100 * diff(log(d))[-1]
adf.test(dow)
train = dow[1 : (0.9 * length(dow))]
test  = dow[(0.9 * length(dow) + 1):  length(dow)]
fit = arima(train, order = c(2, 0, 2))
predi = predict(fit, n.ahead = (length(dow) - (0.9*length(dow))))$pred
fore  = forecast(fit, h = 500)
plot(fore)

很不幸,如果我尝试将同样的代码用于航空旅客预测,就会出现错误。例如:

fit = arima(log(AirPassengers), c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12))
pred <- predict(fit, n.ahead = 10*12)
ts.plot(AirPassengers,exp(pred$pred), log = "y", lty = c(1,3))

应用于当前问题的解决方案可能是这样的:
fit2 = arima(log(d), c(2, 0, 2))
pred = predict(fit2, n.ahead = 500)
ts.plot(d,exp(pred$pred), log = "y", lty = c(1,3))
Error in .cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = TRUE) : non-time series not of the correct length
1个回答

2
取得一些进展,而且原帖太长了。
  1. 从完全不工作到有所进展: 或者为什么我会得到“非时间序列长度不正确”和其他难以理解的错误信息……嗯,在不知道细节的情况下,我只是想检查我正在尝试用 cbind.ts 组合的内容:is.ts(d) [1] FALSE 啊哈!即使 d 是一个 xts 对象,它也不是一个时间序列。所以我只需要运行 as.ts(d)。问题解决了!
  2. 不现实的平稳市场预测: 现在将其绘制为

    fit2 = arima(log(d), c(2, 1, 2)); pred = predict(fit2, n.ahead = 365 * 5) ts.plot(as.ts(d),exp(pred$pred), log = "y", col= c(4,2),lty = c(1,3), main="VIX = 0 Market Conditions", ylim=c(6000,20000))

enter image description here

好的...这个平淡的前景没有为高盛公司找到工作的机会。我需要吸引一些投资者。让我们再炮制一下这种蛇油:

  1. 让线变平:让我们把“季节性”加起来,然后我们就可以像1999年那样聚会了:

    fit3 = arima(log(d), c(2, 1, 2), seasonal=list(order = c(0, 1, 1), period=12))

    pred = predict(fit3, n.ahead = 365 * 5) ts.plot(as.ts(d),exp(pred$pred), log = "y", col= c(2,4),lty = c(1,3), main="投资者说明书- Maddoff & Co.,Inc.")

enter image description here

几乎完成了:我访问了John Oliver的页面并打印了一张正式的财务顾问证书,所以我已经准备好接受你的退休金。为了这样做,我只需要对未来五年的看涨前景表达一些健康的不确定性。很容易... fore = forecast(fit2, h = 365 * 5); plot(fore),然后就完成了...

enter image description here

哦,不!我不可能让任何笨蛋根据这个现实状况来投资... 幸好我没有辞掉我的白天工作... 等一下,我在输入模型时弄错了:fore = forecast(fit3, h = 365 * 5) plot(fore):

enter image description here

我要去斯台普斯(一家办公用品店)...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接