Search code examples
rdataframeapplylapplyanomaly-detection

How to make a simple script using apply family in R


I need to make Anomaly Detection using R but i think my code is really long. I need to get "Find Anomaly" and "Total Anomaly". Can someone make it simple using apply family?. Here the code

#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

Solution

  • A solution with purrr package:

    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])
    

    and base R solution using lapply

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

    re: comment, to have 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])