0

I am trying to check and plot accuracy of an ARIMA model on out-of-sample (test) data.

I have following time series.

> ts.all
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2014 145 135 137 137 120 119 188 130 119 140 125 126
2015 186 174 178 239 189 181 270 174 228 270 247 190
2016 236 203 193 250 178 214 242 174 194 210 160 184
2017 182 164 177 250 170 222 268 279 196 264 241 218
2018 252 203 160 263 169 165

EDIT: output of dput(ts.all)

> dput(ts.all)
structure(c(145, 135, 137, 137, 120, 119, 188, 130, 119, 140, 
125, 126, 186, 174, 178, 239, 189, 181, 270, 174, 228, 270, 247, 
190, 236, 203, 193, 250, 178, 214, 242, 174, 194, 210, 160, 184, 
182, 164, 177, 250, 170, 222, 268, 279, 196, 264, 241, 218, 252, 
203, 160, 263, 169, 165), .Tsp = c(2014, 2018.41666666667, 12
), class = "ts", .Names = c("Gross_Orders_Invoiced1", "Gross_Orders_Invoiced2", 
"Gross_Orders_Invoiced3", "Gross_Orders_Invoiced4", "Gross_Orders_Invoiced5", 
"Gross_Orders_Invoiced6", "Gross_Orders_Invoiced7", "Gross_Orders_Invoiced8", 
"Gross_Orders_Invoiced9", "Gross_Orders_Invoiced10", "Gross_Orders_Invoiced11", 
"Gross_Orders_Invoiced12", "Gross_Orders_Invoiced13", "Gross_Orders_Invoiced14", 
"Gross_Orders_Invoiced15", "Gross_Orders_Invoiced16", "Gross_Orders_Invoiced17", 
"Gross_Orders_Invoiced18", "Gross_Orders_Invoiced19", "Gross_Orders_Invoiced20", 
"Gross_Orders_Invoiced21", "Gross_Orders_Invoiced22", "Gross_Orders_Invoiced23", 
"Gross_Orders_Invoiced24", "Gross_Orders_Invoiced25", "Gross_Orders_Invoiced26", 
"Gross_Orders_Invoiced27", "Gross_Orders_Invoiced28", "Gross_Orders_Invoiced29", 
"Gross_Orders_Invoiced30", "Gross_Orders_Invoiced31", "Gross_Orders_Invoiced32", 
"Gross_Orders_Invoiced33", "Gross_Orders_Invoiced34", "Gross_Orders_Invoiced35", 
"Gross_Orders_Invoiced36", "Gross_Orders_Invoiced37", "Gross_Orders_Invoiced38", 
"Gross_Orders_Invoiced39", "Gross_Orders_Invoiced40", "Gross_Orders_Invoiced41", 
"Gross_Orders_Invoiced42", "Gross_Orders_Invoiced43", "Gross_Orders_Invoiced44", 
"Gross_Orders_Invoiced45", "Gross_Orders_Invoiced46", "Gross_Orders_Invoiced47", 
"Gross_Orders_Invoiced48", "Gross_Orders_Invoiced49", "Gross_Orders_Invoiced50", 
"Gross_Orders_Invoiced51", "Gross_Orders_Invoiced52", "Gross_Orders_Invoiced53", 
"Gross_Orders_Invoiced54"))

I have figured out an ARIMA model (using ACF/differencing/auto.arima/seasonality testing etc.), and do the forecast like this

ts.train = window(ts.all, start = c(2014,1), end = c(2017,6))
ts.test = window(ts.all, start = c(2017,7))
fit.train = arima(ts.train, order=c(1,1,0),seasonal = c(0,1,1))
pred.train = forecast(fit.train, h=12)
autoplot(ts.train) + autolayer(pred.train)

The last line throws following error

Error: Invalid input: date_trans works with objects of class Date only

I had a look at here and here, but I am at a loss on how to format the date part of my time series or how to modify the autolayer function to resolve this.

Version info:

R version 3.4.4 (2018-03-15)
Package: forecast
Version: 8.4
Package: ggplot2
Version: 3.0.0

PS: I have tried similar ARIMA prediction on goog which is daily time series in package fpp2. The autolayer function works there.

Edit: I am not particularly behind autoplot and autolayer functions, base graphics or any other functions/libraries are fine, however, I want to scale the labels on y axis (to show 1,000,000 as 1m) and need to show minor breaks on x-axis for each month and major breaks for each quarter, both of which I have already figured out for ggplot/autoplot.

Gaurav Singhal
  • 998
  • 2
  • 10
  • 25
  • I cannot reproduce your error using exactly the same package versions for forecast and ggplot2, although I'm using R v3.5.1. Please use dput(ts.all) to provide the data so the problem is reproducible. – Rob Hyndman Aug 18 '18 at 01:11
  • @RobHyndman, I have added the output of `dput(ts.all)`. Actually I get the same error on R v3.5.1 as well. Thanks – Gaurav Singhal Aug 18 '18 at 10:20
  • I have figured out an alternative, however, it is not an answer to my question. So adding it as a comment. `newdf2 = data.table(YearMonth = seq(as.Date("2014/1/1"), by = "month", length.out = 54), predicted = c(rep(NA,42),unclass(pred.train$mean)), actual = unclass(ts.all) ); meltdf = melt(newdf2,id.vars = 'YearMonth', variable.name = "type"); ggplot(meltdf,aes(YearMonth,value, color=type)) + geom_line()`. I can always write a wrapper function and use this generically, but if `autolayer/autoplot` works, that would be much more elegant/easier. – Gaurav Singhal Aug 20 '18 at 06:15
  • I still cannot replicate the problem using your code and the `ts.all` object you posted. – Rob Hyndman Aug 20 '18 at 07:37
  • @RobHyndman, I have found the cause of the issue. If I load the package `ggfortify`, I get this error message, otherwise I don't. I am using `ggfortify` Version: 0.4.5. – Gaurav Singhal Aug 20 '18 at 08:00
  • 1
    You do not need ggfortify, and it unhelpfully overrides the functions in the forecast package. – Rob Hyndman Aug 20 '18 at 08:04
  • @RobHyndman, if I do not load `ggfortify` my code breaks. Now I am not able to use `scale_x_date()` with `autoplot` on `stl` and `forecast` objects. Not sure if I should I edit my current question or ask a new question. – Gaurav Singhal Aug 20 '18 at 10:25
  • Load the ggplot2 package – Rob Hyndman Aug 20 '18 at 12:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178353/discussion-between-gaurav-singhal-and-rob-hyndman). – Gaurav Singhal Aug 20 '18 at 12:25
  • No thanks. I don't have time to provided extended consulting advice. Ask another question if you have one and someone might answer it. – Rob Hyndman Aug 20 '18 at 23:13
  • I have currently the same problem. I think it is not possible to use autolayer() in combination with ggfortify when using ts object like yours or mine. Definitely, I cannot specify scale_x_date parameters. I tried scale_x_date(date_breaks="1 year", date_labels="%Y") and scale_x_date(date_breaks="1 month", date_labels="%Y %m") in my case dealing with monthly data... In R studio, the object is presented in the form: Time-Series [1:180] from 2003 to 2018: 8704 ... etc. If someone have another solution, it would be great. Maybe something to adjust in the current ggfortify version. – J.marine Apr 05 '19 at 02:15

0 Answers0