user3833190
user3833190

Reputation:

How to plot outliers and original series?

Hi I would like to define a function which returns a plot for outlier(defined below) based on a specified date range and simultaneously plots the original series(and accounts in that context for possible ratios):

Defing outliers:

  anomaly <- function(x)
               {   tt <- 1:length(x)  
                   resid <- residuals(loess(x ~ tt)) 
                   resid.q <- quantile(resid,prob=c(0.25,0.75)) 
                   iqr <- diff(resid.q) 
                   limits <- resid.q + 1.5*iqr*c(-1,1)  
                   score <- abs(pmin((resid-limits[1])/iqr,0) + pmax((resid -                   limits[2])/iqr,0)) 

                   return(score)
            }
   # defining dates
     dates <- as.POSIXct(seq(as.Date("2015-08-20"), as.Date("2015-10-08"), by = "days"))

Some data:

     a<-runif(50, 5.0, 7.5)
     b<-runif(50, 4, 8)
     c<-runif(50, 1, 2)
     d<-runif(50, 3, 3.5)
     ca<-c/a
     cb<-c/b
     df<-data.frame(dates,a,b,c,d,ca,cb)

Introducing outlier

       df[49,4]<-0
       df[50,6]<-0

Loop over the data to find anomalies

      new<-lapply(df[,2:7],anomaly)
       library(stringi) # binding list with differing rows
     # from list to data frame
       res <- as.data.frame((stri_list2matrix(new)))
     # rename columns
       colnames(res) <- names(new)
     # depends on dates at the beginning 
      res<-(cbind(dates,res[,1:6])) 
     # melt to plot
       library(reshape)
       library(reshape2)
       new <- melt(res , id.vars = 'dates', variable.name = 'series')

Defing plot with a specified date range(last 4 days):

       library(ggplot2)

       nrdays <- 4
       a.plot<-ggplot(subset(new, new$dates >= as.POSIXct(max(new$dates)- (nrdays*60*60*24))),
         aes(x=dates,y=value,colour=variable,group=variable)) + 
         geom_line() + 
         facet_grid(variable ~ ., scales = "free_y")+
         ylab("Outliers")+
         xlab("Date")

Defining check data function:

          check_data <- function(df) { 
          if(tail(df, 1) > 0) { # check only last date

            return(a.plot)

           # and the corresponding original series

        }
      }
        # check and plot data
          check_data(df)

My problem is that I have hundreds of features and I would like only plot those where a outlierhas happened. As you can see in the graph, I'm able to come up with a plot which returns all time series including the series with the outlier rather those where only the outliertook place. Additionally, I would like to report the original series as well(including ratios, that is, given an outlier in the ratio ca I would like to get the original series cand a too)...how may I approach that problem. So the output may look like that:

including original series:

enter image description here

and the outlier as well:

enter image description here

Upvotes: 10

Views: 565

Answers (1)

Volodymyr
Volodymyr

Reputation: 908

you need to specify in subset that you want only outliers, the one not equal to 0. so you can replace

a.plot<-ggplot(subset(new, new$dates >= as.POSIXct(max(new$dates)- (nrdays*60*60*24)) &  new$variable %in% new$variable[!new$value %in% 0 & new$dates >= as.POSIXct(max(new$dates)- (nrdays*60*60*24))]),
           aes(x=dates,y=value,colour=variable,group=variable)) + 
  geom_line() + 
  facet_grid(variable ~ ., scales = "free_y")+
  ylab("Outliers")+
  xlab("Date")

This should help. Also you can clean it a bit so it is more readable

Another option would be to join original data and outliers and plot them together. First you create a data.frame, then subset and pass it to ggplot. So after yours loop over the data you can do something like this

orig <- melt(df , id.vars = 'dates', variable.name = 'series')

data.df <- merge(new, orig, by = c("dates", "variable"))
colnames(data.df)[2:4] <- c("group","index", "original")
data.df$index <- as.numeric(as.character(data.df$index)) # replace factor with numeric

nrdays <- 4
data.subs <- subset(data.df, data.df$dates >= as.POSIXct(max(data.df$dates)- (nrdays*60*60*24)) & 
                  data.df$group %in% data.df$group[!data.df$index %in% 0 & data.df$dates >= as.POSIXct(max(data.df$dates)- (nrdays*60*60*24))])
data.subs <- melt(data.subs, id = c('dates', "group"))

a.plot<-ggplot(data.subs)+
  geom_line(aes(x=dates,y=value, colour = variable, group = variable))+
  facet_grid(group ~ ., scales = "free_y")+
  ylab("Outliers")+
  xlab("Date")

a.plot

enter image description here

Upvotes: 5

Related Questions