Search code examples
ranomaly-detection

Anomaly detection In R


I am used to using the qcc package in R to detect outliers in the data. I recently came across the AnomalyDetection package. Found here: https://github.com/twitter/AnomalyDetection

My dataset is below:

date_start<-as.Date(c('2017-10-17','2017-10-18',
                '2017-10-19','2017-10-20',
                '2017-10-21','2017-10-22',
                '2017-10-23','2017-10-24',
                '2017-10-25','2017-10-26',
                '2017-10-27','2017-10-28',
                '2017-10-29','2017-10-30',
                '2017-10-31','2017-11-01',
                '2017-11-02','2017-11-03',
                '2017-11-04','2017-11-05',
                '2017-11-06','2017-11-07',
                '2017-11-08','2017-11-09',
                '2017-11-10','2017-11-11',
                '2017-11-12'))

count <- c(NA, 3828,
                3532,3527,
                3916,4303,
                3867,3699,
                3439,3099,
                3148,3310,
                3904,3525,
                2962,3398,
                2935,3013,
                3005,3516,
                3010,2848,
                2689,2573,
                2569,2946,
                2713)

df<-data.frame(date_start,count)

head(df)

  date_start count
1 2017-10-17    NA
2 2017-10-18  3828
3 2017-10-19  3532
4 2017-10-20  3527
5 2017-10-21  3916
6 2017-10-22  4303

When I test out this dataset with the AnomalyDetection package, the response is NULL and no plot appears. Any idea why this may be?

library(AnomalyDetection)
res = AnomalyDetectionTs(df, max_anoms=0.02, direction='both', plot=TRUE)
res$plot

NULL

Solution

  • This is caused by the fact no anomalies were detected.

    When one manually changes:

    count[13] <- 5671 
    

    it is detected.

    Additionally for the plot to work the time stamps need to be class POSIXct

    df <- data.frame(date_start = as.POSIXct(date_start),
                     count)
    
    res <- AnomalyDetectionTs(df,
                              max_anoms = 0.02,
                              direction = 'both',
                              plot = TRUE)  
    
    #output
    
    $anoms
                timestamp anoms
    1 2017-10-29 02:00:00  5671
    
    $plot
    

    enter image description here