Skip to content

Commit 1f5deaa

Browse files
committed
Merge pull request twitter#51 from terrytangyuan/bugfix
fixed bug and added error handling when there are NAs in timestamp
2 parents 855454b + e5c596b commit 1f5deaa

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

R/ts_anom_detection.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
#' of the daily max values (med_max), the 95th percentile of the daily max values (p95), and the
3434
#' 99th percentile of the daily max values (p99).
3535
#' @param title Title for the output plot.
36-
#' @param verbose Enable debug messages
36+
#' @param verbose Enable debug messages.
37+
#' @param na.rm Remove any NAs in timestamps.(default: FALSE)
3738
#' @return The returned value is a list with the following components.
3839
#' @return \item{anoms}{Data frame containing timestamps, values, and optionally expected values.}
3940
#' @return \item{plot}{A graphical object if plotting was requested by the user. The plot contains
@@ -63,7 +64,7 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = 'pos',
6364
alpha = 0.05, only_last = NULL, threshold = 'None',
6465
e_value = FALSE, longterm = FALSE, piecewise_median_period_weeks = 2, plot = FALSE,
6566
y_log = FALSE, xlabel = '', ylabel = 'count',
66-
title = NULL, verbose=FALSE){
67+
title = NULL, verbose=FALSE, na.rm = FALSE){
6768

6869
# Check for supported inputs types
6970
if(!is.data.frame(x)){
@@ -81,6 +82,19 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = 'pos',
8182
if (any((names(x) == c("timestamp", "count")) == FALSE)) {
8283
colnames(x) <- c("timestamp", "count")
8384
}
85+
86+
if(!is.logical(na.rm)){
87+
stop("na.rm must be either TRUE (T) or FALSE (F)")
88+
}
89+
90+
# Deal with NAs in timestamps
91+
if(any(is.na(x$timestamp))){
92+
if(na.rm){
93+
x <- x[-which(is.na(x$timestamp)), ]
94+
} else {
95+
stop("timestamp contains NAs, please set na.rm to TRUE or remove the NAs manually.")
96+
}
97+
}
8498

8599
# Sanity check all input parameters
86100
if(max_anoms > .49){

0 commit comments

Comments
 (0)