【问题标题】:Identifying repeated sequences of numbers in time series识别时间序列中重复的数字序列
【发布时间】:2018-02-06 11:11:59
【问题描述】:

我有一个长时间序列,我需要在其中识别和标记 R 中重复的值序列。假设我有以下向量:

a <- c(1,2,3,4,88,443,756,2,453,6,21,98,1,2,3,4,65)

请注意,序列 1、2、3、4 在开头重复,几乎在结尾重复。我想在很长的时间序列中识别和标记 n 的序列(n 可以设置)重复数字。这就是为什么我需要一个强大的方法来做到这一点。

非常感谢。

【问题讨论】:

  • ...错误是什么?您的代码在哪里显示您尝试过的内容?
  • 我只想知道如何用R语言来做
  • 或许你需要rle(c(TRUE, diff(a) == 1))
  • @PoGibas 我们仍在测试它。我会告诉你。谢谢!!
  • @agenis 恐怕我不想找到重复数字的序列(2,2,2,2,3,3,3,3),而是重复的序列数字(2,43,12,3,2,43,12,3)。请注意细微差别;)

标签: r vector


【解决方案1】:

你可以使用这个功能:

identRptSeq <- function(x, N = 4) {
    # Create groups to split input vector in
    splits <- ceiling(seq_along(x) / N)
    # Use data.table shift to create overlapping windows
    foo <- lapply(data.table::shift(x, 0:(N-1), type = "lead"), function(x) {
                  res <- split(x, splits)
                  res[lengths(res) == N]})
    foo <- na.omit(t(as.data.frame(foo)))
    # Find duplicated windows
    foo[duplicated(foo), ]
}

# OPs input
a <- c(1,2,3,4,88,443,756,2,453,6,21,98,1,2,3,4,65)

# Duplicated sequence when N = 4
identRptSeq(a, 4)
[1] 1 2 3 4

# Duplicated sequences when N = 3
identRptSeq(a, 3)
     [,1] [,2] [,3]
X5      1    2    3
X5.1    2    3    4

PS,请记住,当 N = 1 时它不起作用(R 中有其他方法)

【讨论】:

    【解决方案2】:

    如果你有完全重复的模式,这只是 O(n)。 (只需散列序列并查找冲突)

    如果您有几乎重复的模式(并且正在通过欧几里德距离或相关性来衡量相似性),那么这是 O(N^2),但 Matrix Profile 算法非常快 [a]。

    [a]http://www.cs.ucr.edu/~eamonn/MatrixProfile.html

    【讨论】:

    • 我正在尝试查找用于转换为 R 的 sn-p finder 的 c++ 和数据集,但 [Snippet-Finder] (sites.google.com/site/snippetfinder) 它的 404. That’s an error.。很抱歉在 SOF 上跟踪你。
    猜你喜欢
    • 2014-12-09
    • 1970-01-01
    • 2015-09-28
    • 2012-07-29
    • 2019-05-11
    • 2019-06-19
    • 1970-01-01
    • 2021-06-25
    相关资源
    最近更新 更多