问题
I have a gold price data set with "DATE" and "GOLD PRICE" variables.After doing all the pre processing steps in R,I convert the data frame object to time series by ts or xts function and check for stationary through adf test.
Now by enabling forecast library I run auto.arima function and forecast next ten values.
x <- "DATE" "GOLD PRICE"
01-01-2006 1326
x.xts <- xts(x$GOLD PRICE,X$DATE),
fit <- auto.arima(x.xts)
forecast <- forecast(fit,h=10)
Now when I plot the forecast I get some values plotted in x instead of actual dates.I am able to get the date from x.xts
through index(x.xts)
. But I want to extract it from forecast to get it plotted in graph for better understanding.
Someone please help me through this with the R codes.
回答1:
You need to explicitly note the date when creating the ts
(or xts
) object. Using a reproducible example:
library("forecast")
data("gas")
# gas is already a TS object.
# We remove it and recreate it to show the appropriate method
gas2 <- vector(gas); rm(gas)
gas <- ts(gas2, start= c(1956,1), frequency= 12)
fit <- auto.arima(gas)
forecast(fit, h= 10)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Sep 1995 57178.66 54885.61 59471.71 53671.74 60685.58
Oct 1995 53080.77 50466.09 55695.46 49081.96 57079.59
Nov 1995 50940.76 48086.64 53794.87 46575.77 55305.75
Dec 1995 40923.84 37931.85 43915.84 36347.99 45499.70
Jan 1996 43739.23 40654.37 46824.09 39021.35 48457.12
Feb 1996 43706.56 40557.77 46855.34 38890.91 48522.20
Mar 1996 47849.24 44653.96 51044.52 42962.48 52736.00
Apr 1996 50204.88 46974.32 53435.44 45264.16 55145.60
May 1996 56691.41 53432.91 59949.91 51707.96 61674.86
Jun 1996 61053.42 57771.93 64334.92 56034.81 66072.04
来源:https://stackoverflow.com/questions/40831546/how-to-get-the-actual-date-plotted-in-x-axis-while-plotting-the-auto-arima-forec