不规则时间序列的定期分析

10

我有一个不规则的时间序列(R中的xts),我想对其应用一些时间窗口。例如,给定以下时间序列,我想计算在从2009-09-22 00:00:00开始的每个离散的3小时窗口中有多少个观测值:

library(lubridate)
s <- xts(c("OK", "Fail", "Service", "OK", "Service", "OK"),
         ymd_hms(c("2009-09-22 07:43:30", "2009-10-01 03:50:30",
                   "2009-10-01 08:45:00", "2009-10-01 09:48:15",
                   "2009-11-11 10:30:30", "2009-11-11 11:12:45")))

显然我不能使用period.apply()split()来处理,因为它们会省略没有观测到的周期,而我也无法给它一个起始时间。

对于简单计数问题的期望输出(当然,我的实际任务每个细分都更加复杂!),如果我每次聚合3天,则会得到类似以下的结果:

2009-09-22    1
2009-09-25    0
2009-09-28    0
2009-10-01    3
2009-10-04    0
2009-10-07    0
2009-10-10    0
2009-10-13    0
2009-10-16    0
2009-10-19    0
2009-10-22    0
2009-10-25    0
2009-10-28    0
2009-10-31    0
2009-11-03    0
2009-11-06    0
2009-11-09    2

感谢任何指导。

1个回答

11

使用 align.times 的索引放入你感兴趣的时间段,然后使用 period.apply 找出每个3小时窗口的长度。然后与一个包含你所需所有索引值的空 xts 对象合并。

# align index into 3-hour blocks
a <- align.time(s, n=60*60*3)
# find the number of obs in each block
count <- period.apply(a, endpoints(a, "hours", 3), length)
# create an empty xts object with the desired index
e <- xts(,seq(start(a),end(a),by="3 hours"))
# merge the counts with the empty object and fill with zeros
out <- merge(e,count,fill=0)

那不是我要找的 - 让我对原问题添加更多细节。 - Ken Williams
也许 merge() 这个想法是我需要的 - 创建一个具有所需间隔端点的序列,然后将其合并到序列中? - Ken Williams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接