Created
June 18, 2018 12:59
-
-
Save yabyzq/10f34bedef592fdbc76504c8112ea4f6 to your computer and use it in GitHub Desktop.
R anomaly detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Install packages | |
install_github("petermeissner/wikipediatrend") | |
install_github("twitter/AnomalyDetection") | |
library(devtools) | |
library(Rcpp) | |
library(wikipediatrend) | |
library(AnomalyDetection) | |
#load data | |
fifa_data_wikipedia = wikipediatrend::wp_trend("fifa", from = "2013-03-18", lang = "en") | |
head(fifa_data_wikipedia) | |
#Plotting data | |
library(ggplot2) | |
ggplot(fifa_data_wikipedia, aes(x = date, y = views, | |
color = views)) + geom_line() | |
# Keep only date & page views and discard all other variables | |
columns_to_keep=c("date","views") | |
fifa_data_wikipedia=fifa_data_wikipedia[,columns_to_keep] | |
#Anomaly detection | |
anomalies = AnomalyDetectionTs(fifa_data_wikipedia, direction = "pos",#positive | |
plot = TRUE) | |
anomalies$plot | |
install_github("business-science/anomalize") | |
library(anomalize) | |
library(tidyverse) | |
library(coindeskr) | |
bitcoin_data <- get_historic_price(start = "2017-01-01") | |
bitcoin_data_ts = bitcoin_data %>% rownames_to_column() %>% | |
as.tibble() %>% mutate(date = as.Date(rowname)) %>% select(-one_of('rowname')) | |
#decompose | |
bitcoin_data_ts %>% time_decompose(Price, method = "stl", frequency = "auto", trend = "auto") %>% | |
anomalize(remainder, method = "gesd", alpha = 0.05, max_anoms = 0.1) %>% plot_anomaly_decomposition() | |
#removed trend and sensonality | |
bitcoin_data_ts %>% time_decompose(Price) %>% anomalize(remainder) %>% | |
time_recompose() %>% plot_anomalies(time_recomposed = TRUE, ncol = 3, alpha_dots = 0.5) | |
#getting the annomalies | |
bitcoin_data_ts %>% time_decompose(Price) %>% | |
anomalize(remainder) %>% time_recompose() %>% filter(anomaly == 'Yes') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment