business-science / anomalize

Tidy anomaly detection
https://business-science.github.io/anomalize/
339 stars 61 forks source link

Error when Applying Code to a Different Dataset #14

Open KathyGCY opened 6 years ago

KathyGCY commented 6 years ago

Dear Developer,

Thank you so much for the wonderful package and beautiful demo! I can't wait to use it!

I was replicating the code and testing it on my own dataset where and error occured.

library(data.table)
library(ggplot2)
library(scales)
old_signals = read.csv("~/Desktop/demo_signals.txt")
old_signals = na.omit(old_signals)
long_sig = melt(old_signals, id.vars = "FM")
long_sig$FM = as.Date(long_sig$FM, "%m/%d/%y")
long_sig$FM = as.Date(as.yearmon(long_sig$FM))

library(tidyverse)
library(anomalize)
long_sig %>%
  ggplot(aes(FM, value)) +
  geom_line(color = "#0000CD", alpha = 0.25) +
  facet_wrap(~ variable, scale = "free_y", ncol = 3) +
  theme_minimal() +
  scale_x_date(date_breaks = "12 month", date_labels = "%Y-%m", date_minor_breaks = "3 month")+
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  labs(title = "Validate anomalize package",
       subtitle = "Using Merge and Acquisition* signals")

This code generates the plot below:

validate anomalize package

I then converted it to tbl_time, instead of data.table using the code below:

names(long_sig)[1] = "FM"
long_sig$variable = as.character(long_sig$variable)
long_sig = prep_tbl_time(long_sig)

class(long_sig) <- c("grouped_tbl_time", class(long_sig))

long_sig %>%
  # Data Manipulation / Anomaly Detection
  time_decompose(value, method = "stl") %>%
  anomalize(remainder, method = "iqr") %>%
  time_recompose() %>%
  # Anomaly Visualization
  plot_anomalies(time_recomposed = TRUE, ncol = 3, alpha_dots = 0.25) +
  labs(title = "Tidyverse Anomalies", subtitle = "STL + IQR Methods") 

However, and error occured:

screen shot 2018-07-12 at 5 56 21 pm

I couldn't seem to figure out what went wrong. Help! Thank you so much for your time and have a wonderful day! All the best, Kathy Gao cg2908@columbia.edu

P.S. Below is the link to the demo dataset: demo_signals.txt