Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions R/anomalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' @param verbose A boolean. If `TRUE`, will return a list containing useful information
#' about the anomalies. If `FALSE`, just returns the data expanded with the anomalies and
#' the lower (l1) and upper (l2) bounds.
#' @param na.rm Should NA values be removed when calculating outliers? Only
#' applicable to `"gesd"` method.
#'
#' @return Returns a `tibble` / `tbl_time` object or list depending on the value of `verbose`.
#'
Expand Down Expand Up @@ -88,7 +90,7 @@
#'
#' @export
anomalize <- function(data, target, method = c("iqr", "gesd"),
alpha = 0.05, max_anoms = 0.20, verbose = FALSE) {
alpha = 0.05, max_anoms = 0.20, verbose = FALSE, na.rm = FALSE) {
UseMethod("anomalize", data)
}

Expand All @@ -100,7 +102,7 @@ anomalize.default <- function(data, target, method = c("iqr", "gesd"),

#' @export
anomalize.tbl_df <- function(data, target, method = c("iqr", "gesd"),
alpha = 0.05, max_anoms = 0.20, verbose = FALSE) {
alpha = 0.05, max_anoms = 0.20, verbose = FALSE, na.rm = FALSE) {

# Checks
if (missing(target)) stop('Error in anomalize(): argument "target" is missing, with no default', call. = FALSE)
Expand Down Expand Up @@ -130,6 +132,7 @@ anomalize.tbl_df <- function(data, target, method = c("iqr", "gesd"),
outlier_list <- anomalize::gesd(x = x,
alpha = alpha,
max_anoms = max_anoms,
na.rm = na.rm,
verbose = TRUE)

} else {
Expand Down
10 changes: 5 additions & 5 deletions R/anomalize_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ iqr <- function(x, alpha = 0.05, max_anoms = 0.2, verbose = FALSE) {

#' @export
#' @rdname anomalize_methods
gesd <- function(x, alpha = 0.05, max_anoms = 0.2, verbose = FALSE) {
gesd <- function(x, alpha = 0.05, max_anoms = 0.2, verbose = FALSE, na.rm = FALSE) {

# Variables
n <- length(x)
Expand All @@ -151,12 +151,12 @@ gesd <- function(x, alpha = 0.05, max_anoms = 0.2, verbose = FALSE) {
for (i in seq_len(r)) {

# Compute test statistic
median_new[i] <- median(x_new)
mad_new[i] <- mad(x_new)
median_new[i] <- median(x_new, na.rm = na.rm)
mad_new[i] <- mad(x_new, na.rm = na.rm)

z <- abs(x_new - median(x_new)) / (mad(x_new) + .Machine$double.eps) # Z-scores
z <- abs(x_new - median(x_new, na.rm = na.rm)) / (mad(x_new, na.rm = na.rm) + .Machine$double.eps) # Z-scores

max_ind <- which(z == max(z), arr.ind = T)[1] # in case of ties, return first one
max_ind <- which(z == max(z, na.rm = na.rm), arr.ind = T)[1] # in case of ties, return first one
R[i] <- z[max_ind] # max Z-score
outlier_val[i] <- x_new[max_ind] # removed outlier observation values
outlier_ind[i] <- which(x_new[max_ind] == x, arr.ind = T)[1] # index of removed outlier observation values
Expand Down
20 changes: 15 additions & 5 deletions man/anomalize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/anomalize_methods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.