我不会说有错误,而是说功能有forecast::plot.forecast
不适合与以下设备一起使用axis.Date
or axis.POSIXct
(包中未使用forecast
).
调用函数之前axis.Date
and axis.POSIXct
,时间点必须明确传递给plot
作为一个序列Date
or POSIXct
对象。如果我们将时间序列绘制为plot(t)
那么 x 轴似乎没有被正确定义为供上述函数使用。
请参阅下面的代码以及变量如何time
创建并传递给plot
. plot(time, x)
被使用而不是仅仅plot(x)
。因此函数axis
将能够显示时间轴的标签。 (在这个例子中,置信区间看起来不像那些
显示者forecast::plot.forecast
.)
library("forecast")
require(zoo)
t <- ts(
c(
4410.0, 6435.0,
4939.0, 6487.0, 25521.0, 18764.0,
12223.0, 18590.0, 36898.0, 28826.0,
20329.0
)
, frequency = 4
, start=c(2011, 7, 1)
)
cast <- meanf(t, h=4)
x <- ts(c(t, cast$mean), start = c(start(t), 1), frequency = frequency(t))
time <- as.yearqtr(seq(start(x)[1], end(x)[1] + 1, 1/frequency(x)))
time <- time[seq_along(x)]
time <- as.POSIXct(time)
plot(time, x, type = "n", xaxt = "n", ylim = range(c(cast$lower, cast$upper)))
lines(time[seq_along(t)], t)
lines(time[-seq_along(t)], cast$mean, col = "blue")
lines(time[-seq_along(t)], cast$upper[,2], col = "red")
lines(time[-seq_along(t)], cast$lower[,2], col = "red")
ticks <- seq(as.POSIXct("2011-07-01"), by = "3 month", length.out=length(cast) + 4)
axis(side = 1, at = ticks, labels = ticks)