Skip to content

Commit

Permalink
Merge pull request twitter#2 from ChrisMuir/master
Browse files Browse the repository at this point in the history
replace calls to 'subset()' with '['
  • Loading branch information
hrbrmstr authored Apr 22, 2018
2 parents 4f33b11 + 5ea962f commit 347b576
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Authors@R: c(
comment = c(ORCID = "0000-0001-5670-2640")),
person("Yuan", "Tang", role = c("aut", "cph"),
email = "[email protected]",
comment = c(ORCID = "0000-0001-5243-233X"))
comment = c(ORCID = "0000-0001-5243-233X")),
person("Chris", "Muir", email = "[email protected]", role = c("ctb"))
)
Maintainer:
Owen S. Vallis <[email protected]>,
Expand Down
20 changes: 9 additions & 11 deletions R/ts_anom_detection.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = "pos",
end_date <- min(start_date + lubridate::days(num_days_in_period), x[[1]][length(x[[1]])])
# if there is at least 14 days left, subset it, otherwise subset last_date - 14days
if (difftime(end_date, start_date, units = "days") == as.difftime(num_days_in_period, units = "days")) {
all_data[[ceiling(j / (num_obs_in_period))]] <- subset(x, x[[1]] >= start_date & x[[1]] < end_date)
all_data[[ceiling(j / (num_obs_in_period))]] <- x[x[[1]] >= start_date & x[[1]] < end_date, ]
} else {
all_data[[ceiling(j / (num_obs_in_period))]] <-
subset(x, x[[1]] > (last_date - lubridate::days(num_days_in_period)) & x[[1]] <= last_date)
x[x[[1]] > (last_date - lubridate::days(num_days_in_period)) & x[[1]] <= last_date, ]
}
}
} else {
Expand Down Expand Up @@ -205,7 +205,7 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = "pos",

# -- Step 3: Use detected anomaly timestamps to extract the actual anomalies (timestamp and value) from the data
if (!is.null(s_h_esd_timestamps)) {
anoms <- subset(all_data[[i]], (all_data[[i]][[1]] %in% s_h_esd_timestamps))
anoms <- all_data[[i]][(all_data[[i]][[1]] %in% s_h_esd_timestamps), ]
} else {
anoms <- data.frame(timestamp = numeric(0), count = numeric(0))
}
Expand All @@ -224,7 +224,7 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = "pos",
thresh <- quantile(periodic_maxs, .99)
}
# Remove any anoms below the threshold
anoms <- subset(anoms, anoms[[2]] >= thresh)
anoms <- anoms[anoms[[2]] >= thresh, ]
}
all_anoms <- rbind(all_anoms, anoms)
seasonal_plus_trend <- rbind(seasonal_plus_trend, data_decomp)
Expand Down Expand Up @@ -254,10 +254,10 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = "pos",
}

# subset the last days worth of data
x_subset_single_day <- subset(x, (x[[1]] > start_anoms))
x_subset_single_day <- x[x[[1]] > start_anoms, ]

x_subset_week <- subset(x, ((x[[1]] <= start_anoms) & (x[[1]] > start_date)))
all_anoms <- subset(all_anoms, all_anoms[[1]] >= x_subset_single_day[[1]][1])
x_subset_week <- x[(x[[1]] <= start_anoms) & (x[[1]] > start_date), ]
all_anoms <- all_anoms[all_anoms[[1]] >= x_subset_single_day[[1]][1], ]
num_obs <- length(x_subset_single_day[[2]])
}

Expand All @@ -277,9 +277,7 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = "pos",
if (e_value) {
anoms <- data.frame(
timestamp = all_anoms[[1]], anoms = all_anoms[[2]],
expected_value = subset(seasonal_plus_trend[[2]],
as.POSIXlt(seasonal_plus_trend[[1]], tz = "UTC")
%in% all_anoms[[1]]),
expected_value = seasonal_plus_trend[[2]][as.POSIXlt(seasonal_plus_trend[[1]], tz = "UTC") %in% all_anoms[[1]]],
stringsAsFactors = FALSE
)
} else {
Expand All @@ -299,4 +297,4 @@ AnomalyDetectionTs <- function(x, max_anoms = 0.10, direction = "pos",

#' @rdname AnomalyDetectionTs
#' @export
ad_ts <- AnomalyDetectionTs
ad_ts <- AnomalyDetectionTs
15 changes: 7 additions & 8 deletions R/vec_anom_detection.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ AnomalyDetectionVec <- function(x, max_anoms=0.10, direction="pos",
# if there is at least longterm_period left, subset it, otherwise subset last_index - longterm_period
if ((end_index - start_index + 1) == longterm_period) {
all_data[[ceiling(j / (longterm_period))]] <-
subset(x, x[[1]] >= start_index & x[[1]] <= end_index)
x[x[[1]] >= start_index & x[[1]] <= end_index, ]
} else {
all_data[[ceiling(j / (longterm_period))]] <-
subset(x, x[[1]] > (num_obs - longterm_period) & x[[1]] <= num_obs)
x[x[[1]] > (num_obs - longterm_period) & x[[1]] <= num_obs, ]
}
}
} else {
Expand Down Expand Up @@ -134,7 +134,7 @@ AnomalyDetectionVec <- function(x, max_anoms=0.10, direction="pos",

# -- Step 3: Use detected anomaly timestamps to extract the actual anomalies (timestamp and value) from the data
if (!is.null(s_h_esd_timestamps)) {
anoms <- subset(all_data[[i]], (all_data[[i]][[1]] %in% s_h_esd_timestamps))
anoms <- all_data[[i]][all_data[[i]][[1]] %in% s_h_esd_timestamps, ]
} else {
anoms <- data.frame(timestamp = numeric(0), count = numeric(0))
}
Expand All @@ -157,7 +157,7 @@ AnomalyDetectionVec <- function(x, max_anoms=0.10, direction="pos",
thresh <- quantile(periodic_maxs, .99)
}
# Remove any anoms below the threshold
anoms <- subset(anoms, anoms[[2]] >= thresh)
anoms <- anoms[anoms[[2]] >= thresh, ]
}
all_anoms <- rbind(all_anoms, anoms)
seasonal_plus_trend <- rbind(seasonal_plus_trend, data_decomp)
Expand All @@ -182,7 +182,7 @@ AnomalyDetectionVec <- function(x, max_anoms=0.10, direction="pos",
data.frame(timestamp = x[[1]][(num_obs - past_obs + 1):(num_obs - period + 1)],
count = x[[2]][(num_obs - past_obs + 1):(num_obs - period + 1)])

all_anoms <- subset(all_anoms, all_anoms[[1]] >= x_subset_single_period[[1]][1])
all_anoms <- all_anoms[all_anoms[[1]] >= x_subset_single_period[[1]][1], ]
num_obs <- length(x_subset_single_period[[2]])
}

Expand All @@ -199,8 +199,7 @@ AnomalyDetectionVec <- function(x, max_anoms=0.10, direction="pos",
if (e_value) {
anoms <- data.frame(index = all_anoms[[1]], anoms = all_anoms[[2]],
expected_value =
subset(seasonal_plus_trend[[2]],
seasonal_plus_trend[[1]] %in% all_anoms[[1]]))
seasonal_plus_trend[[2]][seasonal_plus_trend[[1]] %in% all_anoms[[1]]])
} else {
anoms <- data.frame(index = all_anoms[[1]], anoms = all_anoms[[2]])
}
Expand All @@ -213,4 +212,4 @@ AnomalyDetectionVec <- function(x, max_anoms=0.10, direction="pos",

#' @rdname AnomalyDetectionVec
#' @export
ad_vec <- AnomalyDetectionVec
ad_vec <- AnomalyDetectionVec

0 comments on commit 347b576

Please sign in to comment.