如何使用-xts R包创建领先的滞后时间序列

时间:2017-07-27 11:57:49

标签: r statistics time-series xts lag

我有2年零5个月的月度数据。从2017年6月到2017年12月,我希望未来7个月(未来)的领先滞后价值......

## DataSet

                    nr_serie_titles_live
2015-01-01                  165
2015-02-01                  170
2015-03-01                  178
2015-04-01                  188
2015-05-01                  188
2015-06-01                  194
2015-07-01                  207
2015-08-01                  216
2015-09-01                  222
2015-10-01                  226
2015-11-01                  234
2015-12-01                  237
2016-01-01                  236
2016-02-01                  245
2016-03-01                  262
2016-04-01                  266
2016-05-01                  272
2016-06-01                  275
2016-07-01                  279
2016-08-01                  281
2016-09-01                  291
2016-10-01                  304
2016-11-01                  314
2016-12-01                  324
2017-01-01                  315
2017-02-01                  324
2017-03-01                  335
2017-04-01                  352
2017-05-01                  365

我希望7个月的价值

## values needed for 7 months
                    nr_serie_titles_live
01-06-2017               -
01-07-2017               -
01-08-2017               -
01-09-2017               -
01-10-2017               -
01-11-2017               -
01-12-2017               -

我试过" xts"包。

head(lag(date , 12), n = 7)

但我不明白。我怎样才能每月获得价值。所以我应该得到36个月的数据,其中包含29个月的原始值和7个月的未来滞后值。

数据输入

dput(xts_in_out_p_month)
structure(c(165, 170, 178, 188, 188, 194, 207, 216, 222, 226, 
234, 237, 236, 245, 262, 266, 272, 275, 279, 281, 291, 304, 314, 
324, 315, 324, 335, 352, 365), .Dim = c(29L, 1L), .Dimnames = list(
NULL, "nr_serie_titles_live"), index = structure(c(1420070400, 
1422748800, 1425168000, 1427846400, 1430438400, 1433116800, 1435708800, 
1438387200, 1441065600, 1443657600, 1446336000, 1448928000, 1451606400, 
1454284800, 1456790400, 1459468800, 1462060800, 1464739200, 1467331200, 
1470009600, 1472688000, 1475280000, 1477958400, 1480550400, 1483228800, 
1485907200, 1488326400, 1491004800, 1493596800), tzone = "UTC", 
tclass    = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", 
tclass = "Date", .indexTZ = "UTC", tzone = "UTC")

非常感谢您的帮助。 非常感谢提前!!!!

1 个答案:

答案 0 :(得分:1)

从你的问题来看,我老实说没有得到你想要的输出,但是 也许你想要这样的东西:

carrylastNForward <- function(x, n) {
  p <- periodicity(x) # get end date date and frequency
  xn <- length(x)     # length of current time series
  # create new time series time vector:
  newtimes <- seq(p$end, by = p$label, length.out = n + 1)[-1]
  new <- xts(x[(xn-n+1):length(x)], order.by = newtimes)
  c(x, new) # combine old and new time series
}

结果:

> carrylastNForward(x, 7) # x <- xts_in_out_p_month
           nr_serie_titles_live
2015-01-01                  165
2015-02-01                  170
2015-03-01                  178
2015-04-01                  188
2015-05-01                  188
2015-06-01                  194
2015-07-01                  207
2015-08-01                  216
2015-09-01                  222
2015-10-01                  226
2015-11-01                  234
2015-12-01                  237
2016-01-01                  236
2016-02-01                  245
2016-03-01                  262
2016-04-01                  266
2016-05-01                  272
2016-06-01                  275
2016-07-01                  279
2016-08-01                  281
2016-09-01                  291
2016-10-01                  304
2016-11-01                  314
2016-12-01                  324
2017-01-01                  315
2017-02-01                  324
2017-03-01                  335
2017-04-01                  352
2017-05-01                  365
2017-06-01                  314
2017-07-01                  324
2017-08-01                  315
2017-09-01                  324
2017-10-01                  335
2017-11-01                  352
2017-12-01                  365