In [ ]:
library(AnomalyDetection)

In [ ]:
source("./lib/alerts_data.R")

In [ ]:
alertsData <- AlertsData("../../cocUptoJuly2016.csv")

In [ ]:
cityLevel <- alertsData$cityLevel("hour")
plot(cityLevel)

In [ ]:
# subset one year of data 
subset <- window(cityLevel, start="2015-01-01 00:00:00", end="2015-12-31 23:59:59")
plot(subset)

In [ ]:
cityLevel[cityLevel == max(cityLevel)]

In [ ]:
construct.frame <- function(xtsobj) {
    data.frame(time=index(xtsobj), values=coredata(xtsobj))
}
cityLevelFrame <- construct.frame(subset)

In [ ]:
adt <- AnomalyDetectionTs(cityLevelFrame, plot=T)
adt$plot

In [ ]:
# pick an even smaller subset, a 60 day subset
subset <- window(cityLevel, start="2015-09-01 00:00:00", end="2015-11-30 23:59:59")
plot(subset)

In [ ]:
adt <- AnomalyDetectionTs(construct.frame(subset), plot=T)
adt$plot

In [ ]:
adt <- AnomalyDetectionTs(construct.frame(subset), plot=T, alpha = 0.5)
adt$plot

In [ ]:
library(dygraphs)

In [ ]:
# write a function for finding anomalies around in one day
anomalies.around <- function(xtsobj, date, window.size=60) {
    end_time <- date
    start_time <- date - (24 * 60 * 60 * window.size)
    subset <- window(xtsobj, start=start_time, end=end_time)
    AnomalyDetectionVec(drop(coredata(subset)),period=24, plot=T, only_last = T)
}
anoms <- anomalies.around(cityLevel, as.POSIXct("2014-11-22 23:59:59"))
anoms$plot

In [ ]:
# try to see anomalies for the dates with most number of complaints
plots <- list()
i <- 1
for(d in index(cityLevel[order(-coredata(cityLevel))[1:5]])) {
    plots[[i]] <- anomalies.around(cityLevel, d)$plot
    i <- i + 1
}
plots