Created
May 28, 2015 22:50
-
-
Save alexwoolford/5bc6ea859de3e33b3d75 to your computer and use it in GitHub Desktop.
ARIMA upper limit alerter
This file contains hidden or 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
| #!/usr/bin/env r | |
| ####################################################################################################################### | |
| # | |
| # This script was inspired by this post: | |
| # http://stats.stackexchange.com/questions/152644/what-algorithm-should-i-use-to-detect-anomalies-on-time-series | |
| # | |
| # It creates a timeseries model for all the previous periods except the most recent one, calculates confidence | |
| # intervals for alerts amd warnings, and then returns: | |
| # 2: most recent value falls outside the alert upper limit | |
| # 1: most recent value falls outside the warning upper limit | |
| # 0: most revent value is within both upper limits | |
| # | |
| ####################################################################################################################### | |
| suppressWarnings(suppressPackageStartupMessages(library(RMySQL))) | |
| suppressWarnings(suppressPackageStartupMessages(library(dplyr))) | |
| suppressWarnings(suppressPackageStartupMessages(library(zoo))) | |
| suppressWarnings(suppressPackageStartupMessages(library(forecast))) | |
| con <- dbConnect(MySQL(), host = "fqdn.of.host", user = "myuser", pass = "Mr. Creosote", dbname = "metrics") | |
| sql <- "select timestamp, someMetric from [someTable] order by timestamp" | |
| suppressWarnings(data <- dbGetQuery(con, sql)) | |
| data <- data[, c('timestamp', 'someMetric')] | |
| actual <- tail(data, n = 1)$someMetric | |
| dbDisconnect(con) | |
| data <- head(data, nrow(data) - 1) | |
| data <- data[, c('timestamp', 'someMetric')] | |
| data.zoo <- zoo(data$someMetric, order.by=data$timestamp) | |
| data.model <- auto.arima(data.zoo, ic = c("bic")) | |
| data.prediction.warningLimits <- forecast(data.model, h=1, level=0.99) | |
| data.prediction.alertLimits <- forecast(data.model, h=1, level=0.999) | |
| ifelse( | |
| actual < as.numeric(data.prediction.warningLimits$upper), | |
| resultCode <- 0, | |
| ifelse( | |
| actual < as.numeric(data.prediction.alertLimits$upper), | |
| resultCode <- 1, | |
| resultCode <- 2 | |
| ) | |
| ) | |
| cat(resultCode) | |
| cat("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment