【问题标题】:How to make a simple script using apply family in R如何在 R 中使用 apply 系列制作一个简单的脚本
【发布时间】:2018-03-07 06:17:49
【问题描述】:

我需要使用 R 进行异常检测,但我认为我的代码真的很长。我需要得到“Find Anomaly”和“Total Anomaly”。有人可以使用应用家庭使其变得简单吗?代码在这里

#Library
library(AnomalyDetection)
# Data Preparation
set.seed(1)
datex <- seq(as.Date("2017/01/01"), as.Date("2019/01/01"), by = "day")
x1 <- rf(731,3,4); x2 <- rf(731,3,4); x3 <- rf(731,3,4)
data.train <- data.frame(datex,x1,x2,x3,x4,x5)
# Find Anomaly x1
find.anomaly.x1 <- AnomalyDetectionTs(data.train[,c(1,2)],
                                      max_anoms = 0.3,
                                      direction = "both",
                                      alpha = 0.05,
                                      plot = F); find.anomaly.x1$anoms
# Find Anomaly x2
find.anomaly.x2 <- AnomalyDetectionTs(data.train[,c(1,3)],
                                      max_anoms = 0.3,
                                      direction = "both",
                                      alpha = 0.05,
                                      plot = F);find.anomaly.x2$anoms
# Find Anomaly x3
find.anomaly.x3 <- AnomalyDetectionTs(data.train[,c(1,4)],
                                      max_anoms = 0.3,
                                      direction = "both",
                                      alpha = 0.05,
                                      plot = F); find.anomaly.x3$anoms
# List Find Anomaly
find.anomaly

## Total Anomaly
# Total Anomaly x1
total.anomaly.x1 <- dim(find.anomaly.x1$anoms)[1]; total.anomaly.x1
# Total Anomaly x2
total.anomaly.x2 <- dim(find.anomaly.x2$anoms)[1]; total.anomaly.x2
# Total Anomaly x3
total.anomaly.x3 <- dim(find.anomaly.x3$anoms)[1]; total.anomaly.x3
# Total Anomaly
var.anom <- c("x1","x2","x3")
tot.anom.x <- c(total.anomaly.x1,total.anomaly.x2,total.anomaly.x3)
total.anomaly <- data.frame(var.anom,tot.anom.x)
total.anomaly

【问题讨论】:

    标签: r dataframe apply lapply anomaly-detection


    【解决方案1】:

    带有purrr包的解决方案:

    library(purrr)
    
    anomaly <- function(data, col) {
      x <- AnomalyDetectionTs(data[,c(1, col)],
                         max_anoms = 0.3,
                         direction = "both",
                         alpha = 0.05,
                         plot = F)
      x$anoms
    }
    
    find.anomaly <- map(2:4, ~ anomaly(data.train, col = .))
    total.anomaly <- map(find.anomaly, ~ dim(.)[1])
    

    和使用 lapply 的基本 R 解决方案

    find.anomaly <- lapply(2:4, function(x) anomaly(data.train, col = x))
    total.anomaly <- lapply(find.anomaly, function(x) dim(x)[1])
    

    re: comment,total.anomaly as df do:

    find.anomaly <- map(2:6, ~ anomaly(data.train, col = .)) %>% setNames(names(data.train)[2:6])  
    total.anomaly <- map_df(find.anomaly, ~ dim(.)[1])
    

    【讨论】:

    • 你好,很酷。对于“Find Anomaly”,它可以工作,但对于“Total Anomaly”,这不是我想要的。我需要一个数据框先生。我可以有其他解决方案吗?非常感谢
    • 你好.. 干得好,但我可以从数据训练列 (x1,x2,x3) 名称中获取“总异常”的名称 (x1,x2,x3)。谢谢
    • 你好 sebdalgarno.. 你做了很棒的解决方案。非常感谢你
    • 你好 sebdalgarno.. 我的技巧有问题,我的“查找异常”表之一为 NULL,因此“总异常”中的数据不显示结果并给出这样的错误“bind_rows_(x, .id) 中的错误:参数 1 是一个列表,必须包含原子向量”。请帮忙,谢谢
    猜你喜欢
    • 2020-03-20
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多