如何在预测后保留 xts 时间序列数据中的日期 [英] How to preserve dates from xts time series data after forecasting

查看:36
本文介绍了如何在预测后保留 xts 时间序列数据中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑这个小数据集:

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)

到目前为止,沿 x 轴的日期(索引)被保留,但只要我调用预测,沿 x 轴的日期就会转换为整数.见下文:

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)

问题:如何保留 myxts 中的索引?有没有办法告诉 forecastauto.arima 保留 myxts 的日期(索引)?

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?

推荐答案

问题是您在两个不同的时间系统中工作:xts 是不规则的(使用不需要周期性的日期)而 forecast/ts 系统是规则的(使用均匀间隔的数字序列).我们通过创建一个可以映射到预测的未来日期序列来解决这个问题.

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.

这是一个详细的解决方案.forecastxts 包用于重新创建预测.timekit 包用于创建未来日期.ggplot2 包用于绘图.

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.

问题的关键是创建未来日期.请注意,您拥有的是不规则间隔的.tk_make_future_timeseries() 使用匹配您输入时间索引的周期性.如果这不正确,您可以根据需要分别使用 skip_valuesinsert_values 删除和插入日期.

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)

这篇关于如何在预测后保留 xts 时间序列数据中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆