问题
Please consider this small dataset:
library(xts)
library(ggplot2)
library(forecast)
data <- data.frame(idDate = c("12-12-2012", "13-12-2012", "14-12-2012", "16-12-2012", "19-12-2012"), score= c(110, 120, 130, 200, 180))
date <- as.Date(as.character(data$idDate), "%d-%m-%Y")
score <- as.numeric(data$score)
myxts <- xts(score, date)
autoplot(myxts)
So far the date (Index) along the x axis is preserved but as soon as I call forecast, the date along my x axis gets converted to integer. see below:
d.arima <- auto.arima(myxts)
d.forecast <- forecast(d.arima, level = c(95), h = 3)
d.forecast
autoplot(d.forecast)
questions:
How can the index from myxts
be kept?
Is there a way to tell forecast
or auto.arima
to preserve the date (Index) from myxts
?
回答1:
The problem is you are working in two different time systems: xts
is irregular (uses dates with no required periodicity) while forecast
/ ts
system is regular (uses evenly spaced numeric sequence). We get around this by creating a future date sequence that can be mapped to the forecast.
Here's a detailed solution. The forecast
and xts
packages are used for recreating the forecast. The timekit
package is use for creating future dates. The ggplot2
package is for plotting.
The key to your problem is creating the future dates. Note that what you have is irregularly spaced. tk_make_future_timeseries()
uses matches the periodicity of your input time index. If this is not correct, you can remove and insert dates as necessary using skip_values
and insert_values
, respectively.
library(forecast)
library(xts)
library(ggplot2)
library(timekit)
# Recreate xts data, d.arima and d.forecast
data <- data.frame(idDate = c("12-12-2012", "13-12-2012", "14-12-2012", "16-12-2012",
"19-12-2012"),
score= c(110, 120, 130, 200, 180))
date <- as.Date(as.character(data$idDate), "%d-%m-%Y")
score <- as.numeric(data$score)
myxts <- xts(score, date)
d.arima <- auto.arima(myxts)
d.forecast <- forecast(d.arima, level = c(95), h = 3)
# Extract index
idx <- tk_index(myxts)
idx
#> [1] "2012-12-12" "2012-12-13" "2012-12-14" "2012-12-16" "2012-12-19"
# Make future index
idx_future <- tk_make_future_timeseries(idx, n_future = 3)
idx_future
#> [1] "2012-12-20" "2012-12-22" "2012-12-23"
# Build xts object from forecast
myts_future <- cbind(y = d.forecast$mean, y.lo = d.forecast$lower, y.hi = d.forecast$upper)
myxts_future <- xts(myts_future, idx_future)
myxts_future
#> y y.lo y.hi
#> 2012-12-20 148 70.33991 225.6601
#> 2012-12-22 148 70.33991 225.6601
#> 2012-12-23 148 70.33991 225.6601
# Format original xts object
myxts_reformatted <- cbind(y = myxts, y.lo = NA, y.hi = NA)
myxts_final <- rbind(myxts_reformatted, myxts_future)
# Plot forecast - Note ggplot uses data frames, tk_tbl() converts to df
tk_tbl(myxts_final) %>%
ggplot(aes(x = index, y = y)) +
geom_point() +
geom_line() +
geom_ribbon(aes(ymin = y.lo, ymax = y.hi), alpha = 0.2)
来源:https://stackoverflow.com/questions/44478893/how-to-preserve-dates-from-xts-time-series-data-after-forecasting