【问题标题】:Time series Anomaly detection in R using AnomalyDetectionTs package使用 AnomalyDetectionTs 包在 R 中进行时间序列异常检测
【发布时间】:2020-03-12 21:54:45
【问题描述】:

我在 R 中使用 AnomalyDetectionTs 包在 275 天内检测异常,但有些观察不到 275 天

这是数据框

这里是每个类别的观察次数

但是当我运行异常检测代码时,我会看到以下错误消息:

Error in do.ply(i) : 
  task 88 failed - "With longterm=TRUE, AnomalyDetection splits the data into 2 week periods by default. You have 103 observations in a period, which is too few. Set a higher piecewise_median_period_weeks."

我在 40 周内设置了分段中位数_周期_周 如下:

AnomalyDetectionTs(df, max_anoms = 0.002, direction = "both", alpha = 0.05, e_value = FALSE, plot = FALSE, y_log = FALSE,longterm=TRUE,piecewise_median_period_weeks=40) 

当我只过滤超过 103 个观察的类别时,它工作正常。 您能否帮我定义这个可以检测所有观察的参数(piecewise_median_period_weeks)。

【问题讨论】:

  • 我发现的一种方法是根据每个类别的计数将动态值分配给piecewise_median_period_weeks()
  • 我在使用这个包的 Python 版本时遇到了这个问题。解决这个问题的一种方法是使用替代函数 detect_vec 而不是 detect_ts

标签: r anomaly-detection


【解决方案1】:

我遇到了同样的问题,我用下面的方法解决了它。我正在使用 future_lapply 形式的包 future.apply 来提高性能。

future_lapply(ValuesToIterate, function(x) {
  k = 2
  repeat {
    outlier <- 
      try(
        ad_ts(y[y$var_local == x, c(var_date, var_quantityt)], max_anoms = 0.05, direction = "both",
              longterm = TRUE, na.rm = TRUE, only_last = NULL, piecewise_median_period_weeks = k)
      )
    # If there is no error return
    if (!(inherits(outlier, "try-error"))) {
      return(outlier)
    } 
    k = k + 1
    # Define a threshold
    if(k > max_k) {
      return(tibble::tibble(timestamp = NA))
    }
  }
})

在您的示例中: var_local 是 类别

ValuesToIterate 是

unique(category)

我也使用return(tibble::tibble(timestamp = NA)),因为然后我添加purrr:::map_dfr 来获得一个包含 3 列的数据框,其中某些系列没有异常,所以它们是 NA

我想使用 data.table 来解决这个问题,但我不能,因为 ad_ts 返回的时间戳数字会破坏代码。

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 2015-07-23
    • 2019-10-13
    • 2017-10-26
    • 2021-08-25
    • 2012-12-25
    • 2014-09-05
    • 2017-12-21
    相关资源
    最近更新 更多