'"Error: Invalid input: time_trans works with objects of class POSIXct only"

The Problem

The plot here is not working, how can I show it?

# minimal runnable code
# source: https://blog.twitter.com/engineering/en_us/a/2015/introducing-practical-and-robust-anomaly-detection-in-a-time-series

install.packages("devtools") devtools::install_github("twitter/AnomalyDetection")
library(AnomalyDetection)
data(raw_data) 
res = AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', plot=TRUE) 
res$plot

I get the following error:

> res$plot
Error: Invalid input: time_trans works with objects of class POSIXct only

Additional Information

If it can be useful, this is the full output that I get once I press the button Source in RStudio:

# full output, including the error

> source("~/.active-rstudio-document", echo=TRUE)
> library(AnomalyDetection)
> data(raw_data) 
> res = AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', plot=TRUE) 
> res$plot
Error: Invalid input: time_trans works with objects of class POSIXct only
> 

In addition, here a sample of the dataset called raw_data:

# minimal dataset (printed)

> raw_data
              timestamp    count
1   1980-09-25 14:01:00 182.4780
2   1980-09-25 14:02:00 176.2310
3   1980-09-25 14:03:00 183.9170
4   1980-09-25 14:04:00 177.7980
5   1980-09-25 14:05:00 165.4690
6   1980-09-25 14:06:00 181.8780
7   1980-09-25 14:07:00 184.5020
8   1980-09-25 14:08:00 183.3030
9   1980-09-25 14:09:00 177.5780
10  1980-09-25 14:10:00 171.6410

Still the sample of my dataset calledraw_data, by using dput:

# minimal dataset (dput)

> dput(raw_data[1:10, ])

structure(list(timestamp = structure(list(sec = c(0, 0, 0, 0, 
0, 0, 0, 0, 0, 0), min = 1:10, hour = c(14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L), mday = c(25L, 25L, 25L, 25L, 25L, 25L, 
25L, 25L, 25L, 25L), mon = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L), year = c(80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 80L, 
80L), wday = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), yday = c(268L, 
268L, 268L, 268L, 268L, 268L, 268L, 268L, 268L, 268L), isdst = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = c("POSIXlt", "POSIXt"
), tzone = "UTC"), count = c(182.478, 176.231, 183.917, 177.798, 
165.469, 181.878, 184.502, 183.303, 177.578, 171.641)), row.names = c(NA, 
10L), class = "data.frame")


Solution 1:[1]

I'm not familiar with the AnomnalyDetection package but the sample of your dataset shows the timestamp column is of class POSIXlt and the error suggests you need it as POSIXct instead. Try using as.POSIXct() e.g.

raw_data$timestamp <- as.POSIXct(raw_data$timestamp)

before running the AnomalyDetectionTs() function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cnbrownlie