Skip to content

Commit

Permalink
Merge pull request #72 from olivroy/clean
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 authored Dec 6, 2023
2 parents 2633d15 + 827973b commit 133cfe2
Show file tree
Hide file tree
Showing 53 changed files with 234 additions and 215 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
^CRAN-RELEASE$
^CRAN-SUBMISSION$
^\.github$
^pkgdown$
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: anomalize
Type: Package
Title: Tidy Anomaly Detection
Version: 0.3.0
Version: 0.3.0.9000
Authors@R: c(
person("Matt", "Dancho", email = "[email protected]", role = c("aut", "cre")),
person("Davis", "Vaughan", email = "[email protected]", role = c("aut"))
Expand All @@ -18,7 +18,7 @@ Description:
and generalized extreme studentized deviation ("gesd"). These methods are based on
those used in the 'forecast' package and the Twitter 'AnomalyDetection' package.
Refer to the associated functions for specific references for these methods.
URL: https://github.com/business-science/anomalize
URL: https://business-science.github.io/anomalize/, https://github.com/business-science/anomalize
BugReports: https://github.com/business-science/anomalize/issues
License: GPL (>= 3)
Encoding: UTF-8
Expand All @@ -35,14 +35,14 @@ Imports:
rlang,
tibble,
tidyr (>= 1.0.0),
ggplot2
ggplot2 (>= 3.4.0)
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
Suggests:
tidyverse,
tidyquant,
stringr,
testthat (>= 2.1.0),
testthat (>= 3.0.0),
knitr,
rmarkdown
VignetteBuilder: knitr
Config/testthat/edition: 3
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ importFrom(ggplot2,"%+replace%")
importFrom(rlang,"!!!")
importFrom(rlang,"!!")
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(stats,as.formula)
importFrom(stats,mad)
importFrom(stats,median)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# anomalize (development version)

* anomalize works better with ggplot2 3.4.0

* anomalize no longer depends on tidyverse, devtools and roxygen2 (@olivroy, #70)

# anomalize 0.3.0

Prepare for supercession by `timetk`. Note that `anomalize` R package will be maintained for backwards compatibility. Users may wish to add these 2 lines of code to existing codebases that use the legacy anomalize R package:
Expand Down
15 changes: 7 additions & 8 deletions R/anomalize-package.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#' anomalize: Tidy anomaly detection
#'
#' @description
#' The 'anomalize' package enables a "tidy" workflow for detecting anomalies in data.
#' The main functions are time_decompose(), anomalize(), and time_recompose().
#' When combined, it's quite simple to decompose time series, detect anomalies,
Expand All @@ -14,14 +13,14 @@
#'
#' To learn more about `anomalize`, start with the vignettes:
#' `browseVignettes(package = "anomalize")`
#'
#'
#' @docType package
#' @name anomalize_package
#'
#' @aliases anomalize-package
#' @importFrom rlang := !! !!!
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom rlang := !! !!! .data
#' @importFrom dplyr %>% n row_number contains quo_name
#' @importFrom stats median mad qt as.formula
#' @import ggplot2
## usethis namespace: end
NULL
8 changes: 3 additions & 5 deletions R/anomalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@
#' # Needed to pass CRAN check / This is loaded by default
#' set_time_scale_template(time_scale_template())
#'
#' data(tidyverse_cran_downloads)
#'
#' tidyverse_cran_downloads %>%
#' time_decompose(count, method = "stl") %>%
#' anomalize(remainder, method = "iqr")
Expand Down Expand Up @@ -145,8 +143,8 @@ anomalize.tbl_df <- function(data, target, method = c("iqr", "gesd"),

# Returns
ret <- data %>%
dplyr::mutate(!! paste0(dplyr::quo_name(target_expr), "_l1") := limit_lower,
!! paste0(dplyr::quo_name(target_expr), "_l2") := limit_upper) %>%
dplyr::mutate(!!paste0(dplyr::quo_name(target_expr), "_l1") := limit_lower,
!!paste0(dplyr::quo_name(target_expr), "_l2") := limit_upper) %>%
tibble::add_column(anomaly = outlier)

if (verbose) {
Expand Down Expand Up @@ -181,7 +179,7 @@ anomalize.grouped_df <- function(data, target, method = c("iqr", "gesd"),
method = method[[1]],
alpha = alpha,
max_anoms = max_anoms,
verbose = F,
verbose = FALSE,
...)

return(ret)
Expand Down
7 changes: 3 additions & 4 deletions R/plot_anomalies.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#' library(dplyr)
#' library(ggplot2)
#'
#' data(tidyverse_cran_downloads)
#'
#' #### SINGLE TIME SERIES ####
#' tidyverse_cran_downloads %>%
Expand Down Expand Up @@ -80,7 +79,7 @@ plot_anomalies.tbl_time <- function(data, time_recomposed = FALSE, ncol = 1,
date_col <- tibbletime::get_index_char(data)

g <- data %>%
ggplot2::ggplot(ggplot2::aes_string(x = date_col, y = "observed"))
ggplot2::ggplot(ggplot2::aes(x = .data[[date_col]], y = .data[["observed"]]))


if (time_recomposed) {
Expand All @@ -94,8 +93,8 @@ plot_anomalies.tbl_time <- function(data, time_recomposed = FALSE, ncol = 1,
}

g <- g +
ggplot2::geom_point(ggplot2::aes_string(color = "anomaly"), size = size_dots, alpha = alpha_dots) +
ggplot2::geom_point(ggplot2::aes_string(x = date_col, y = "observed", color = "anomaly"),
ggplot2::geom_point(ggplot2::aes(color = .data[["anomaly"]]), size = size_dots, alpha = alpha_dots) +
ggplot2::geom_point(ggplot2::aes(x = .data[[date_col]], y = .data[["observed"]], color = .data[["anomaly"]]),
size = size_circles, shape = 1, alpha = alpha_circles,
data = data %>% dplyr::filter(anomaly == "Yes"),
inherit.aes = FALSE) +
Expand Down
6 changes: 2 additions & 4 deletions R/plot_anomaly_decomposition.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#' library(dplyr)
#' library(ggplot2)
#'
#' data(tidyverse_cran_downloads)
#'
#' tidyverse_cran_downloads %>%
#' filter(package == "tidyquant") %>%
#' ungroup() %>%
Expand Down Expand Up @@ -78,11 +76,11 @@ plot_anomaly_decomposition.tbl_time <- function(data, ncol = 1, color_no = "#2c3
date_col <- tibbletime::get_index_char(data)

data_anomaly_tbl <- data %>%
dplyr::select(!! date_expr, observed:remainder, anomaly) %>%
dplyr::select(!!date_expr, observed:remainder, anomaly) %>%
tidyr::gather(key = key, value = value, -dplyr::one_of(c(!! date_col, 'anomaly')), factor_key = T)

g <- data_anomaly_tbl %>%
ggplot2::ggplot(ggplot2::aes_string(x = date_col, y = "value", color = "anomaly")) +
ggplot2::ggplot(ggplot2::aes(x = .data[[date_col]], y = .data$value, color = .data$anomaly)) +
# Points
ggplot2::geom_point(size = size_dots, alpha = alpha_dots) +
# Circles
Expand Down
24 changes: 12 additions & 12 deletions R/tidyquant_theme_compat.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ theme_tq <- function(base_size = 11, base_family = "") {
ggplot2::theme(

# Base Inherited Elements
line = ggplot2::element_line(colour = blue, size = 0.5, linetype = 1,
line = ggplot2::element_line(colour = blue, linewidth = 0.5, linetype = 1,
lineend = "butt"),
rect = ggplot2::element_rect(fill = white, colour = blue,
size = 0.5, linetype = 1),
linewidth = 0.5, linetype = 1),
text = ggplot2::element_text(family = base_family, face = "plain",
colour = blue, size = base_size,
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
Expand All @@ -27,14 +27,14 @@ theme_tq <- function(base_size = 11, base_family = "") {
# Axes
axis.line = ggplot2::element_blank(),
axis.text = ggplot2::element_text(size = ggplot2::rel(0.8)),
axis.ticks = ggplot2::element_line(color = grey, size = ggplot2::rel(1/3)),
axis.ticks = ggplot2::element_line(color = grey, linewidth = ggplot2::rel(1/3)),
axis.title = ggplot2::element_text(size = ggplot2::rel(1.0)),

# Panel
panel.background = ggplot2::element_rect(fill = white, color = NA),
panel.border = ggplot2::element_rect(fill = NA, size = ggplot2::rel(1/2), color = blue),
panel.grid.major = ggplot2::element_line(color = grey, size = ggplot2::rel(1/3)),
panel.grid.minor = ggplot2::element_line(color = grey, size = ggplot2::rel(1/3)),
panel.border = ggplot2::element_rect(fill = NA, linewidth = ggplot2::rel(1/2), color = blue),
panel.grid.major = ggplot2::element_line(color = grey, linewidth = ggplot2::rel(1/3)),
panel.grid.minor = ggplot2::element_line(color = grey, linewidth = ggplot2::rel(1/3)),
panel.grid.minor.x = ggplot2::element_blank(),
panel.spacing = ggplot2::unit(.75, "cm"),

Expand Down Expand Up @@ -70,12 +70,12 @@ theme_tq_dark <- function(base_size = 11, base_family = "") {
ggplot2::theme(

# Axes
axis.ticks = ggplot2::element_line(color = blue, size = ggplot2::rel(1/3)),
axis.ticks = ggplot2::element_line(color = blue, linewidth = ggplot2::rel(1/3)),

# Panel
panel.background = ggplot2::element_rect(fill = grey, color = NA),
panel.grid.major = ggplot2::element_line(color = white, size = ggplot2::rel(1/3)),
panel.grid.minor = ggplot2::element_line(color = white, size = ggplot2::rel(1/3)),
panel.grid.major = ggplot2::element_line(color = white, linewidth = ggplot2::rel(1/3)),
panel.grid.minor = ggplot2::element_line(color = white, linewidth = ggplot2::rel(1/3)),

# Complete theme
complete = TRUE
Expand All @@ -95,12 +95,12 @@ theme_tq_green <- function(base_size = 11, base_family = "") {
ggplot2::theme(

# Axes
axis.ticks = ggplot2::element_line(color = blue, size = ggplot2::rel(1/3)),
axis.ticks = ggplot2::element_line(color = blue, linewidth = ggplot2::rel(1/3)),

# Panel
panel.background = ggplot2::element_rect(fill = green, color = NA),
panel.grid.major = ggplot2::element_line(color = white, size = ggplot2::rel(1/3)),
panel.grid.minor = ggplot2::element_line(color = white, size = ggplot2::rel(1/3)),
panel.grid.major = ggplot2::element_line(color = white, linewidth = ggplot2::rel(1/3)),
panel.grid.minor = ggplot2::element_line(color = white, linewidth = ggplot2::rel(1/3)),

# Complete theme
complete = TRUE
Expand Down
2 changes: 0 additions & 2 deletions R/time_apply.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#'
#' library(dplyr)
#'
#' data(tidyverse_cran_downloads)
#'
#' # Basic Usage
#' tidyverse_cran_downloads %>%
#' time_apply(count, period = "1 week", .fun = mean, na.rm = TRUE)
Expand Down
2 changes: 0 additions & 2 deletions R/time_decompose.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@
#'
#' library(dplyr)
#'
#' data(tidyverse_cran_downloads)
#'
#' # Basic Usage
#' tidyverse_cran_downloads %>%
#' time_decompose(count, method = "stl")
Expand Down
4 changes: 2 additions & 2 deletions R/time_decompose_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ decompose_twitter <- function(data, target, frequency = "auto", trend = "auto",
tibble::add_column(!! date_col_name := date_col_vals, .after = 0) %>%
purrr::set_names(c(date_col_name, "observed", "season", "trend", "remainder")) %>%
dplyr::mutate(seasadj = observed - season) %>%
dplyr::select(!! date_col_expr, observed, season, seasadj, trend, remainder)
dplyr::select(!!date_col_expr, observed, season, seasadj, trend, remainder)

# Median Span Logic
trnd <- time_trend(data, period = trend, message = FALSE)
Expand All @@ -74,7 +74,7 @@ decompose_twitter <- function(data, target, frequency = "auto", trend = "auto",
med_span <- decomp_tbl %>%
dplyr::count(median_spans) %>%
dplyr::pull(n) %>%
median(na.rm = TRUE)
stats::median(na.rm = TRUE)

med_scale <- decomp_tbl %>%
timetk::tk_index() %>%
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

grouped_mapper <- function(data, target, .f, ...) {

data <- prep_tbl_time(data, message = F)
data <- prep_tbl_time(data, message = FALSE)

target_expr <- dplyr::enquo(target)

Expand Down
7 changes: 3 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ knitr::opts_chunk$set(
warning = F
)
library(anomalize)
library(dplyr) # for pipe
```


Expand Down Expand Up @@ -79,12 +80,11 @@ install.packages("anomalize")

## Getting Started

Load the `tidyverse` and `anomalize` packages.
Load the `anomalize` package. Usually, you will also load the tidyverse as well!

```{r, eval = F}
library(tidyverse)
library(anomalize)
library(tidyverse)
# NOTE: timetk now has anomaly detection built in, which
# will get the new functionality going forward.
# Use this script to prevent overwriting legacy anomalize:
Expand All @@ -99,7 +99,6 @@ Next, let's get some data. `anomalize` ships with a data set called `tidyverse_
Suppose we want to determine which daily download "counts" are anomalous. It's as easy as using the three main functions (`time_decompose()`, `anomalize()`, and `time_recompose()`) along with a visualization function, `plot_anomalies()`.

```{r tidyverse_anoms_1, fig.height=8}
library(magrittr)
tidyverse_cran_downloads %>%
# Data Manipulation / Anomaly Detection
time_decompose(count, method = "stl") %>%
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ install.packages("anomalize")

## Getting Started

Load the `tidyverse` and `anomalize` packages.
Load the `anomalize` package. Usually, you will also load the tidyverse
as well!

``` r
library(tidyverse)
library(anomalize)

library(tidyverse)
# NOTE: timetk now has anomaly detection built in, which
# will get the new functionality going forward.
# Use this script to prevent overwriting legacy anomalize:
Expand All @@ -102,7 +102,6 @@ anomalous. It’s as easy as using the three main functions
visualization function, `plot_anomalies()`.

``` r
library(magrittr)
tidyverse_cran_downloads %>%
# Data Manipulation / Anomaly Detection
time_decompose(count, method = "stl") %>%
Expand Down
27 changes: 2 additions & 25 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
template:
bootstrap: 5
bootswatch: flatly
params:
bootswatch: flatly
ganalytics: UA-76139189-1

navbar:
title: "anomalize"
left:
- text: "Home"
href: index.html
- text: "Function Reference"
href: reference/index.html
- text: "Vignettes"
href: articles/index.html
menu:
- text: "Anomalize Quick Start Guide"
href: articles/anomalize_quick_start_guide.html
- text: "Anomalize Methods"
href: articles/anomalize_methods.html
- text: "Reduce Forecast Error by Cleaning Anomalies"
href: articles/forecasting_with_cleaned_anomalies.html
- text: "News"
href: news/index.html

right:
- icon: fa-github
href: https://github.com/business-science/anomalize

reference:
- title: General
contents:
- anomalize_package
- tidyverse_cran_downloads
- title: Anomalize workflow
desc: __The main functions used to anomalize time series data.__
Expand Down
4 changes: 4 additions & 0 deletions anomalize.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace

UseNativePipeOperator: No

SpellingDictionary: en_US
Loading

0 comments on commit 133cfe2

Please sign in to comment.