Anomalize Quick Start Guide

Business Science

2019-09-20

The anomalize package is a feature rich package for performing anomaly detection. It’s geared towards time series analysis, which is one of the biggest needs for understanding when anomalies occur. We have a quick start section called “5-Minutes to Anomalize” for those looking to jump right in. We also have a detailed section on parameter adjustment for those looking to understand what nobs they can turn. Finally, for those really looking to get under the hood, we have another vignette called “Anomalize Methods” that gets into a deep discussion on STL, Twitter, IQR and GESD methods that are used to power anomalize.

Anomalize Intro on YouTube

As a first step, you may wish to watch our anomalize introduction video on YouTube.

Anomalize

Check out our entire Software Intro Series on YouTube!

5-Minutes To Anomalize

Load libraries.

library(tidyverse)
library(tibbletime)
library(anomalize)

Get some data. We’ll use the tidyverse_cran_downloads data set that comes with anomalize. A few points:

tidyverse_cran_downloads
#> # A time tibble: 6,375 x 3
#> # Index:  date
#> # Groups: package [15]
#>    date       count package
#>    <date>     <dbl> <chr>  
#>  1 2017-01-01   873 tidyr  
#>  2 2017-01-02  1840 tidyr  
#>  3 2017-01-03  2495 tidyr  
#>  4 2017-01-04  2906 tidyr  
#>  5 2017-01-05  2847 tidyr  
#>  6 2017-01-06  2756 tidyr  
#>  7 2017-01-07  1439 tidyr  
#>  8 2017-01-08  1556 tidyr  
#>  9 2017-01-09  3678 tidyr  
#> 10 2017-01-10  7086 tidyr  
#> # … with 6,365 more rows

We can use the general workflow for anomaly detection, which involves three main functions:

  1. time_decompose(): Separates the time series into seasonal, trend, and remainder components
  2. anomalize(): Applies anomaly detection methods to the remainder component.
  3. time_recompose(): Calculates limits that separate the “normal” data from the anomalies!
tidyverse_cran_downloads_anomalized <- tidyverse_cran_downloads %>%
    time_decompose(count, merge = TRUE) %>%
    anomalize(remainder) %>%
    time_recompose()

tidyverse_cran_downloads_anomalized %>% glimpse()
#> Observations: 6,375
#> Variables: 12
#> Groups: package [15]
#> $ package       <chr> "tidyr", "tidyr", "tidyr", "tidyr", "tidyr", "tidy…
#> $ date          <date> 2017-01-01, 2017-01-02, 2017-01-03, 2017-01-04, 2…
#> $ count         <dbl> 873, 1840, 2495, 2906, 2847, 2756, 1439, 1556, 367…
#> $ observed      <dbl> 8.730000e+02, 1.840000e+03, 2.495000e+03, 2.906000…
#> $ season        <dbl> -2761.4637, 901.0113, 1459.7147, 1429.7532, 1238.7…
#> $ trend         <dbl> 5052.582, 5046.782, 5040.983, 5035.183, 5029.383, …
#> $ remainder     <dbl> -1418.11842, -4107.79368, -4005.69726, -3558.93599…
#> $ remainder_l1  <dbl> -3747.904, -3747.904, -3747.904, -3747.904, -3747.…
#> $ remainder_l2  <dbl> 3707.661, 3707.661, 3707.661, 3707.661, 3707.661, …
#> $ anomaly       <chr> "No", "Yes", "Yes", "No", "No", "No", "No", "No", …
#> $ recomposed_l1 <dbl> -1456.786, 2199.890, 2752.793, 2717.032, 2520.278,…
#> $ recomposed_l2 <dbl> 5998.779, 9655.454, 10208.358, 10172.597, 9975.843…

Let’s explain what happened:

  1. time_decompose(count, merge = TRUE): This performs a time series decomposition on the “count” column using seasonal decomposition. It created four columns:
    • “observed”: The observed values (actuals)
    • “season”: The seasonal or cyclic trend. The default for daily data is a weekly seasonality.
    • “trend”: This is the long term trend. The default is a Loess smoother using spans of 3-months for daily data.
    • “remainder”: This is what we want to analyze for outliers. It is simply the observed minus both the season and trend.
    • Setting merge = TRUE keeps the original data with the newly created columns.
  2. anomalize(remainder): This performs anomaly detection on the remainder column. It creates three new columns:
    • “remainder_l1”: The lower limit of the remainder
    • “remainder_l2”: The upper limit of the remainder
    • “anomaly”: Yes/No telling us whether or not the observation is an anomaly
  3. time_recompose(): This recomposes the season, trend and remainder_l1 and remainder_l2 columns into new limits that bound the observed values. The two new columns created are:
    • “recomposed_l1”: The lower bound of outliers around the observed value
    • “recomposed_l2”: The upper bound of outliers around the observed value

We can then visualize the anomalies using the plot_anomalies() function.

tidyverse_cran_downloads_anomalized %>%
    plot_anomalies(ncol = 3, alpha_dots = 0.25)

Parameter Adjustment

Now that you have an overview of the package, you can begin to adjust the parameter settings. The first settings you may wish to explore are related to time series decomposition: trend and seasonality. The second are related to anomaly detection: alpha and max anoms.

Adjusting Decomposition Trend and Seasonality

Adjusting the trend and seasonality are fundamental to time series analysis and specifically time series decomposition. With anomalize, it’s simple to make adjustments because everything is done with date or datetime information so you can intuitively select increments by time spans that make sense (e.g. “5 minutes” or “1 month”).

To get started, let’s isolate one of the time series packages: lubridate.

Next, let’s perform anomaly detection.

First, notice that a frequency and a trend were automatically selected for us. This is by design. The arguments frequency = "auto" and trend = "auto" are the defaults. We can visualize this decomposition using plot_anomaly_decomposition().

When “auto” is used, a get_time_scale_template() is used to determine logical frequency and trend spans based on the scale of the data. You can uncover the logic:

What this means is that if the scale is 1 day (meaning the difference between each data point is 1 day), then the frequency will be 7 days (or 1 week) and the trend will be around 90 days (or 3 months). This logic tends to work quite well for anomaly detection, but you may wish to adjust it. There are two ways:

  1. Local parameter adjustment
  2. Global parameter adjustment

Local Parameter Adjustment

Local parameter adjustment can be performed by tweaking the in-function parameters. Below we adjust trend = "2 weeks" which makes for a quite overfit trend.

Global Parameter Adjustement

We can also adjust globally by using set_time_scale_template() to update the default template to one that we prefer. We’ll change the “3 month” trend to “2 weeks” for time scale = “day”. Use time_scale_template() to retrieve the time scale template that anomalize begins with, them mutate() the trend field in the desired location, and use set_time_scale_template() to update the template in the global options. We can retrieve the updated template using get_time_scale_template() to verify the change has been executed properly.

Finally we can re-run the time_decompose() with defaults, and we can see that the trend is “2 weeks”.

Let’s reset the time scale template defaults back to the original defaults.

Adjusting Anomaly Detection Alpha and Max Anoms

The alpha and max_anoms are the two parameters that control the anomalize() function. Here’s how they work.