Fitted values in R forecast missing date / time component

1.2k views Asked by At

I've been doing a variety of models in R with time series data (in XTS format) and I keep running into the same issue where there's no date / time component to the fitted values / forecasts and thus I can't graph them on the same graph as the original data.

Using the following code, from everything I see online I SHOULD get two line graphs on the same graph. However, I just get the original graph from the plot command and the second line never appears.

library("stats")
library("forecast")
model_a1 <- auto.arima(GED$Mfg.Shipments.Total..USA.)
plot(GED$Mfg.Shipments.Total..USA.)
lines(fitted(model_a1), col = 2)

If I plot just the residuals:

plot(fitted(model_a1))

I get a graph, but instead of having the date / time, it just says "Index" with a range of 0 - 1,2000 on the x-axis.

Any ideas as to what's causing this? I can't find anything on why an XTS time series would result in residuals with no date / time component. Also, I tried to post images but apparently there's a silly rule where if you don't post at least 10 times, you can't post images to further explain your problem.....

str(GED) 
An ‘xts’ object on 2018-01-01/2115-12-01 containing: 
Data: num [1:1176, 1:472] NA NA NA NA NA NA NA NA NA NA ... 
  - attr(*, "dimnames")=List of 2 
        ..$ : NULL
        ..$ : chr [1:472] "SRPM..USA." "WTI" "CU..Argentina." "CU..Australia."  
... Indexed by objects of class: [Date] TZ: UTC 
   xts Attributes: NULL
1

There are 1 answers

0
Robert On BEST ANSWER

Do not use the dates in your plot, use a numeric sequence as x axis. You can use the dates as labels. Try something like this:

y=GED$Mfg.Shipments.Total..USA.
n=length(y)
model_a1 <- auto.arima(y)
plot(x=1:n,y,xaxt="n",xlab="")
axis(1,at=seq(1,n,length.out=20),labels=index(y)[seq(1,n,length.out=20)],
     las=2,cex.axis=.5)
lines(fitted(model_a1), col = 2)

The result depending on your data will be something similar:

enter image description here