business-science / anomalize

Tidy anomaly detection
https://business-science.github.io/anomalize/
338 stars 60 forks source link

native STL can detect anomaly but time_decompose not #36

Open edvardoss opened 5 years ago

edvardoss commented 5 years ago
dfr <- tibble(ds = as.Date(c('2017-03-01', '2017-04-01', '2017-05-01', '2017-06-01', '2017-07-01', '2017-08-01', '2017-09-01',
                                  '2017-10-01', '2017-11-01', '2017-12-01', '2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01', '2018-05-01', '2018-06-01',
                                  '2018-07-01', '2018-08-01', '2018-09-01', '2018-10-01', '2018-11-01', '2018-12-01', '2019-01-01', '2019-02-01', '2019-03-01',
                                  '2019-04-01', '2019-05-01', '2019-06-01')),

                  y=c(12.95, 1.12, 4.48, 17.85, 0.14, 0.7, 1.75, 3.43, 5.18, 14.91, 4.27, 1.82, 4.83, 2.94, 3.22, 6.72, 3.36, 2.52,
                      5.88, 23.1, 13.44, 1244.22, 1.26, 9.66, 22.05, 2.94, 6.3, 1.26))
# outlier in 2018-12-01
decmp <- time_decompose(data = dfr,target = y,method = "stl", frequency = "12 months",trend = "auto",message = FALSE)
fit <- decmp %>% anomalize(remainder,method = "gesd",max_anoms = .3,verbose = FALSE)
# 1244 in 2018-12-01 is seasonality!!!!! and remainder = -0.2793775
fit %>% time_recompose() %>% plot_anomalies()
fit %>% time_recompose() %>% plot_anomaly_decomposition()

#try native STL

t <- ts(data = dfr$y,start = c(2017,3),frequency = 12)
library(highcharter)
hchart(stl(t,s.window = "periodic"))
# in 2018-12-01  remainder is 558 and in this case is anomaly!!!!

# try twitter with same results
decmp <- time_decompose(data = dfr,target = y,method = "twitter", frequency = "12 months",trend = "auto",message = FALSE)
fit <- decmp %>% anomalize(remainder,method = "gesd",max_anoms = .3,verbose = FALSE)
fit

# try original twitter
# devtools::install_github("twitter/AnomalyDetection")
library(AnomalyDetection)
AnomalyDetectionTs(x = dfr)
# timestamp               anoms
# 1 2018-10-01 03:00:00   23.10
# 2 2018-12-01 03:00:00 1244.22
ranalytics commented 4 years ago

Log-transform the values of y, it'll work just fine.