Since , you manipulate time series objects it is better to use ts-packages like zoo
or xts
, It seems that forecast
package was designed to work with such objects.
## 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")
To plot dates axis , you should use axis.POSIXct
or axis.Date
. There are equivalent of axis
for dates objects. But you should first create date indexes. Here I aggregate the original dates and the forecated dates generated by forecast
function.
a <- c(as.POSIXct(index(stock)),
as.POSIXct(index(fcast$mean),origin='1970-01-01 00:00.00 UTC'))
Then I plot my axis using something like this:
## 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)