I'm interested in trying this package for two reasons:
- To check out its intended purpose of time-series anomaly detection.
- To learn more about how they've done the embedding of ggplot into the returned object (that looks cool, I'd like to do the same).
It's a few years old (last github commit four years ago) so things in R may have changed a bit, but I get an error on their examples right out of the box.
I've tried to change the class on the timestamps from POSIXlt to POSIXct, as indicated by the error message, but still get the same error again.
Any suggestions appreciated.
library(devtools)
install_github("twitter/AnomalyDetection")
#> Skipping install of 'AnomalyDetection' from a github remote, the SHA1 (1f5deaa1) has not changed since last install.
#> Use `force = TRUE` to force installation
library(AnomalyDetection)
packageVersion("AnomalyDetection")
#> [1] '1.0'
# first example
data(raw_data)
res = AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', plot=TRUE)
res$plot
#> Error: Column `x` is a date/time and must be stored as POSIXct, not POSIXlt.
# what class are the timestamp elements?
class(res$anoms$timestamp)
#> [1] "POSIXlt" "POSIXt"
class(res$plot$data$timestamp)
#> [1] "POSIXlt" "POSIXt"
# convert them to POSIXct
library(magrittr)
res$anoms$timestamp %<>% as.POSIXct()
res$plot$data$timestamp %<>% as.POSIXct()
# what class are they now?
class(res$anoms$timestamp)
#> [1] "POSIXct" "POSIXt"
class(res$plot$data$timestamp)
#> [1] "POSIXct" "POSIXt"
# replot
res$plot
#> Error: Column `x` is a date/time and must be stored as POSIXct, not POSIXlt.
Created on 2019-02-13 by the reprex package (v0.2.1)
PS. I still get the same error if I try the type conversion on the input data.
library(AnomalyDetection)
library(magrittr)
str(raw_data)
#> 'data.frame': 14398 obs. of 2 variables:
#> $ timestamp: POSIXlt, format: "1980-09-25 14:01:00" "1980-09-25 14:02:00" ...
#> $ count : num 182 176 184 178 165 ...
raw_data$timestamp %<>% as.POSIXct()
str(raw_data)
#> 'data.frame': 14398 obs. of 2 variables:
#> $ timestamp: POSIXct, format: "1980-09-25 14:01:00" "1980-09-25 14:02:00" ...
#> $ count : num 182 176 184 178 165 ...
res = AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', plot=TRUE)
res$plot
#> Error: Column `x` is a date/time and must be stored as POSIXct, not POSIXlt.
Created on 2019-02-13 by the reprex package (v0.2.1)