Forecast plot with actual days
My data have the df format:
library(zoo)
dat <- read.zoo(text='day,price
"2010-03-21 01:00:00",32
"2010-03-21 02:00:00",36
"2010-03-21 03:00:00",33
"2010-03-21 04:00:00",31
"2010-03-21 05:00:00",37
"2010-03-21 06:00:00",39
"2010-03-21 07:00:00",33
"2010-03-21 08:00:00",35',tz ='' , format = "%Y-%m-%d %H:%M:%S",header=TRUE,
sep=',')
df = data.frame(price=coredata(dat),day=index(dat))
The forecast:
library(forecast)
fit <- Arima(df$price,c(3,1,2))
fcast <- forecast(fit, h = 7)
Try to take the x-axis with day values:
library(reshape2)
day1 <- melt(df$day)
day2 <- melt(as.POSIXct( as.character( tail( df$day , 1 ) ) , format =
"%Y-%m-%d %H:%M:%S" )+(1:7))
day = rbind(day1, day2)
And the forecast plot:
plot(fcast, xaxt="n")
axis(1, day)
And the error:
Error in axis(1, day) : (list) object cannot be coerced to type 'double'
Using update 3 from here http://stackoverflow.com/a/10347205/2366057:
library(lubridate)
day = as.Date(strptime(day$value, "%Y-%m-%d %H:%M:%S"))
plot(fcast, xaxt="n")
axis(1, at = decimal_date(day), labels = format(day, "%Y-%m-%d
%H:%M:%S"), cex.axis=0.6)
The error doesn't exist anymore but the x axis doesn't have values.
No comments:
Post a Comment