From 716d47d46fa708a35f52c9681b39bd6b643df9a6 Mon Sep 17 00:00:00 2001 From: Matt Dancho Date: Sat, 28 Oct 2023 20:06:41 -0400 Subject: [PATCH] plot_anomalies_decomp --- R/anomalize-plotting.R | 15 +++++----- R/anomalize.R | 62 ++++++++++++++++++++++++++++++++---------- _pkgdown.yml | 2 +- man/anomalize.Rd | 8 +++--- man/plot_anomalies.Rd | 12 ++++---- 5 files changed, 65 insertions(+), 34 deletions(-) diff --git a/R/anomalize-plotting.R b/R/anomalize-plotting.R index c8085ad7..dff40e5a 100644 --- a/R/anomalize-plotting.R +++ b/R/anomalize-plotting.R @@ -2,7 +2,7 @@ #' Visualize Anomalies for One or More Time Series #' -#' An interactive and scalable function for visualizing anomalies in time series data. +#' `plot_anomalies()` is an interactive and scalable function for visualizing anomalies in time series data. #' Plots are available in interactive `plotly` (default) and static `ggplot2` format. #' #' @param .data A `tibble` or `data.frame` that has been anomalized by `anomalize()` @@ -47,6 +47,7 @@ #' #' #' @examples +#' # Plot Anomaly #' library(dplyr) #' #' walmart_sales_weekly %>% @@ -360,8 +361,8 @@ plot_anomalies.grouped_df <- function( # 2.0 PLOT ANOMALIES DECOMP ---- #' Visualize Anomaly Decomposition #' -#' An interactive and scalable function for visualizing anomaly decomposition in time series data. -#' Plots are available in interactive `plotly` (default) and static `ggplot2` format. +#' `plot_anomalies_decomp` function takes in data from the `anomalize()` +#' function, and returns a plot of the anomaly decomposition. #' #' @param .data A `tibble` or `data.frame` that has been anomalized by `anomalize()` #' @param .date_var A column containing either date or date-time values @@ -400,11 +401,8 @@ plot_anomalies.grouped_df <- function( #' - `scales`: use `facet_scales` #' - `as_plotly`: use `.interactive` #' -#' -#' @return A `plotly` or `ggplot2` visualization -#' -#' #' @examples +#' # Plot Anomaly Decomposition #' library(dplyr) #' #' walmart_sales_weekly %>% @@ -610,6 +608,7 @@ plot_anomalies_decomp.grouped_df <- function( return(g) - } + + diff --git a/R/anomalize.R b/R/anomalize.R index 785e15d0..9bb085b9 100644 --- a/R/anomalize.R +++ b/R/anomalize.R @@ -101,11 +101,22 @@ #' @name anomalize #' @export #' -anomalize <- function(.data, .date_var, .value, - .frequency = "auto", .trend = "auto", - .method = "stl", - .iqr_alpha = 0.05, .clean_alpha = 0.75, .max_anomalies = 0.2, - .message = TRUE) { +anomalize <- function( + .data, + .date_var, + .value, + + .frequency = "auto", + .trend = "auto", + + method = "stl", + + .iqr_alpha = 0.05, + .clean_alpha = 0.75, + .max_anomalies = 0.2, + + .message = TRUE +) { # Tidyeval Setup date_var_expr <- rlang::enquo(.date_var) @@ -132,11 +143,22 @@ anomalize <- function(.data, .date_var, .value, #' @export -anomalize.data.frame <- function(.data, .date_var, .value, - .frequency = "auto", .trend = "auto", - .method = "stl", - .iqr_alpha = 0.05, .clean_alpha = 0.75, .max_anomalies = 0.2, - .message = TRUE) { +anomalize.data.frame <- function( + .data, + .date_var, + .value, + + .frequency = "auto", + .trend = "auto", + + method = "stl", + + .iqr_alpha = 0.05, + .clean_alpha = 0.75, + .max_anomalies = 0.2, + + .message = TRUE +) { # STL Decomposition (observed, season, trend, remainder, seasadj) ret <- .data %>% @@ -181,12 +203,22 @@ anomalize.data.frame <- function(.data, .date_var, .value, } #' @export -anomalize.grouped_df <- function(.data, .date_var, .value, - .frequency = "auto", .trend = "auto", - .method = "stl", - .iqr_alpha = 0.05, .clean_alpha = 0.75, .max_anomalies = 0.2, - .message = TRUE) { +anomalize.grouped_df <- function( + .data, + .date_var, + .value, + + .frequency = "auto", + .trend = "auto", + + method = "stl", + + .iqr_alpha = 0.05, + .clean_alpha = 0.75, + .max_anomalies = 0.2, + .message = TRUE +) { # Tidy Eval Setup value_expr <- rlang::enquo(.value) diff --git a/_pkgdown.yml b/_pkgdown.yml index b7787676..73ef1942 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -114,7 +114,7 @@ reference: - subtitle: Anomaly Detection contents: - anomalize - - plot_anomalies + - contains("plot_anomalies") - subtitle: Function Operations contents: slidify - subtitle: Vector Operations diff --git a/man/anomalize.Rd b/man/anomalize.Rd index 9daca6fc..1e2f7943 100644 --- a/man/anomalize.Rd +++ b/man/anomalize.Rd @@ -10,7 +10,7 @@ anomalize( .value, .frequency = "auto", .trend = "auto", - .method = "stl", + method = "stl", .iqr_alpha = 0.05, .clean_alpha = 0.75, .max_anomalies = 0.2, @@ -33,9 +33,6 @@ Refer to \code{\link[=tk_get_frequency]{tk_get_frequency()}}.} For STL, trend controls the sensitivity of the LOESS smoother, which is used to remove the remainder. Refer to \code{\link[=tk_get_trend]{tk_get_trend()}}.} -\item{.method}{The outlier detection method. Default: "stl". Currently -"stl" is the only method. "twitter" is planned.} - \item{.iqr_alpha}{Controls the width of the "normal" range. Lower values are more conservative while higher values are less prone to incorrectly classifying "normal" observations.} @@ -48,6 +45,9 @@ depending on the direction of the anomaly.} \item{.message}{A boolean. If \code{TRUE}, will output information related to automatic frequency and trend selection (if applicable).} + +\item{.method}{The outlier detection method. Default: "stl". Currently +"stl" is the only method. "twitter" is planned.} } \value{ A \code{tibble} or \code{data.frame} with the following columns: diff --git a/man/plot_anomalies.Rd b/man/plot_anomalies.Rd index 1871ef1f..8c66de14 100644 --- a/man/plot_anomalies.Rd +++ b/man/plot_anomalies.Rd @@ -117,18 +117,17 @@ The only parameters that cannot be passed are: }} } \value{ -A \code{plotly} or \code{ggplot2} visualization - A \code{plotly} or \code{ggplot2} visualization } \description{ -An interactive and scalable function for visualizing anomalies in time series data. +\code{plot_anomalies()} is an interactive and scalable function for visualizing anomalies in time series data. Plots are available in interactive \code{plotly} (default) and static \code{ggplot2} format. -An interactive and scalable function for visualizing anomaly decomposition in time series data. -Plots are available in interactive \code{plotly} (default) and static \code{ggplot2} format. +\code{plot_anomalies_decomp} function takes in data from the \code{anomalize()} +function, and returns a plot of the anomaly decomposition. } \examples{ +# Plot Anomaly library(dplyr) walmart_sales_weekly \%>\% @@ -137,12 +136,13 @@ walmart_sales_weekly \%>\% anomalize(Date, Weekly_Sales) \%>\% plot_anomalies(Date, .facet_ncol = 2, .ribbon_alpha = 0.25, .interactive = FALSE) +# Plot Anomaly Decomposition library(dplyr) walmart_sales_weekly \%>\% filter(id \%in\% c("1_1", "1_3")) \%>\% group_by(id) \%>\% - anomalize(Date, Weekly_Sales) \%>\% + anomalize(Date, Weekly_Sales, .message = FALSE) \%>\% plot_anomalies_decomp(Date, .interactive = FALSE) }