Fitted values in R forecast missing date / time component

久未见 提交于 2019-12-11 11:36:29

问题


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:


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:



来源:https://stackoverflow.com/questions/30988301/fitted-values-in-r-forecast-missing-date-time-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!