Skip to content

Commit

Permalink
plot_anomalies_decomp
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 committed Oct 29, 2023
1 parent f810004 commit 716d47d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
15 changes: 7 additions & 8 deletions R/anomalize-plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down Expand Up @@ -47,6 +47,7 @@
#'
#'
#' @examples
#' # Plot Anomaly
#' library(dplyr)
#'
#' walmart_sales_weekly %>%
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 %>%
Expand Down Expand Up @@ -610,6 +608,7 @@ plot_anomalies_decomp.grouped_df <- function(

return(g)


}



62 changes: 47 additions & 15 deletions R/anomalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 %>%
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ reference:
- subtitle: Anomaly Detection
contents:
- anomalize
- plot_anomalies
- contains("plot_anomalies")
- subtitle: Function Operations
contents: slidify
- subtitle: Vector Operations
Expand Down
8 changes: 4 additions & 4 deletions man/anomalize.Rd

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

12 changes: 6 additions & 6 deletions man/plot_anomalies.Rd

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

0 comments on commit 716d47d

Please sign in to comment.