3

我用这些数据加载了一个数据框(名为 stock):

day               value
2000-12-01 00:00:00 11.809242 
2000-12-01 06:00:00 10.919792 
2000-12-01 12:00:00 13.265208 
2000-12-01 18:00:00 13.005139 
2000-12-02 00:00:00 10.592222  
2000-12-02 06:00:00 8.873160 
2000-12-02 12:00:00 12.292847 
2000-12-02 18:00:00 12.609722 
2000-12-03 00:00:00 11.378299 
2000-12-03 06:00:00 10.510972  
2000-12-03 12:00:00 8.297222  
2000-12-03 18:00:00 8.110486  
2000-12-04 00:00:00 8.066154

我尝试使用 arima() 模型来实现预测:

requires(forecast)
fit <- Arima(stock$value,c(3,1,2))
fcast <- forecast(fit, h = 5)

之后,我想从预测过程中获取情节。但是,我尝试使 x 轴有日子。直到现在我有这个:

x = as.POSIXct( stock$day , format = "%Y-%m-%d %H:%M:%S" )

plot(fcast, xaxt="n")
a = seq(x[1], by="min", length=nrow(stock))
axis(1, at = a, labels = format(a, "%Y-%m-%d %H:%M:%S"), cex.axis=0.6)
4

1 回答 1

2

由于,您操作时间序列对象,最好使用 ts-packageszooxts,似乎该forecast包旨在与此类对象一起使用。

## read the zoo object
## I add a virtual colname aaa here 
## for some reason index=0:1 don't work
library(zoo)
stock <- read.zoo(text='aaa day value
2000-12-01 00:00:00 11.809242 
2000-12-01 06:00:00 10.919792 
2000-12-01 12:00:00 13.265208 
2000-12-01 18:00:00 13.005139 
2000-12-02 00:00:00 10.592222  
2000-12-02 06:00:00 8.873160 
2000-12-02 12:00:00 12.292847 
2000-12-02 18:00:00 12.609722 
2000-12-03 00:00:00 11.378299 
2000-12-03 06:00:00 10.510972  
2000-12-03 12:00:00 8.297222  
2000-12-03 18:00:00 8.110486  
2000-12-04 00:00:00 8.066154',header=TRUE,
                tz='',
                index=1:2)

##  arima works well with zoo objects
fit <- Arima(stock,c(3,1,2))
fcast <- forecast(fit, h = 20)
plot(fcast, xaxt="n")

要绘制日期轴,您应该使用axis.POSIXctor axis.Date。有等价axis于日期对象。但是您应该首先创建日期索引。在这里,我汇总了函数生成的原始日期和预测日期forecast

  a <- c(as.POSIXct(index(stock)),
         as.POSIXct(index(fcast$mean),origin='1970-01-01 00:00.00 UTC'))

然后我使用这样的东西绘制我的轴:

## Note the use of las to rotate the axis 
## you can play with format here
axis.POSIXct(1,at=a,format="%a %H",las=2,cex=0.5)

在此处输入图像描述

于 2013-06-15T11:14:55.757 回答