通过时间序列中的条件值查找时间 windows

find time windows by conditional values in a time series

我有一个很长的时间序列。时间戳是等距的。为简单起见,我省略了许多列和行:

library(data.table)
dt <- data.table(time = c(seq(0, 2, by = 0.1)),
                   value1 = c(1, 2, 1, 3, 2, 2, 1, 4, 6, 3, 2, 1, 2, 3, 2, 1, 3, 2, 3, 2, 1),
                   value2 = c(8, 7, 7, 6, 7, 8, 5, 6, 4, 1, 3, 2, 5, 7, 6, 8, 7, 8, 4, 1, 2))

数据图如下所示:

library(ggplot2)
ggplot(data = dt, aes(x = time)) +
  geom_line(aes(y = value1), color = "red", size = 2) +
  geom_line(aes(y = value2), color = "blue", size = 2)

现在我想找到满足特定条件的时间序列 windows。必须同时满足所有条件 window。在这个例子中:

我怎样才能在 R 中实现一个很长的时间序列和多个 columns/conditions? 目标是通过一组特殊条件在我的数据中找到相似的模式。

考虑这个解决方案。它为 window 中的每个有效时间戳定义了一个 window_id。如果时间戳不是有效 window 的一部分,window_idNA.

# value constraints
dt$value_cons_met <- inrange(dt$value1,1,3) & inrange(dt$value2,6,8)

# assign all potential sequential true timestamps a group id
dt$potential_win_id <- c(0,cumsum(abs(diff(dt$value_cons_met))))

# is the window big enough?
dt[,window_size_okay := max(time)-min(time) >= 0.5 ,by = potential_win_id]
# Other window dependent constraints can be put here

# Window "ID" is defined if the valid cons are met and window size is okay
# in that case copy potential window number as window id
dt[,window_id := ifelse(value_cons_met & window_size_okay, potential_win_id,NA)]


# sample plot
library(ggplot2)
ggplot(data = dt, aes(x = time)) +
  geom_line(aes(y = value1), color = "red", size = 2) +
  geom_line(aes(y = value2), color = "blue", size = 2) +
  geom_line(aes(y = window_id), color = "green", size =2)

这是一个示例图,其中检测到的 window_id 是 y 值