Search code examples
rdplyrrollapply

R- rolling customized function dplyr


I need to extract outliers from a ts. I wanted to use a customized function like:

es_outlier<-function(vect){
  m=mean(vect)
  s=sd(vect)
  vector_final=abs(vect)>abs(m+s*1.5)
return(vector_final )
}

My table would be (a short example):

tbl<-data.frame(aa=c('a','b', 'a', 'a','a', 'b', 'b', 'b', 'a', 'b', 'a'), 
                fecha=seq.Date(from=as.Date('01-01-2001', format='%d-%m-%Y'), 
                               to=as.Date('01-11-2001',format='%d-%m-%Y'), by='month'),
                cant=c(runif(10),1000))

I would like as a result a table with an extra column with zeros (or False) except for the values that are outliers (True or 1) like:

  aa      fecha         cant  outl
  a 2001-01-01 7.586968e-01    NA
  a 2001-03-01 9.939139e-01    NA
  a 2001-04-01 6.064410e-01    NA
  a 2001-05-01 2.937717e-02    NA
  a 2001-09-01 4.321826e-02 FALSE
  a 2001-11-01 1.000000e+03  TRUE
  b 2001-02-01 9.572499e-01    NA
  b 2001-06-01 3.364454e-01    NA
  b 2001-07-01 2.776581e-01    NA
  b 2001-08-01 1.171976e-01    NA
  b 2001-10-01 3.703098e-01 FALSE

So to apply it I used rollapply:

library(dplyr)
tbl%>%group_by(aa)%>%arrange(aa,fecha) %>%
  mutate(outl=rollapply(cant,5, es_outlier, align='right', fill=NA))

But I got the following error:

Error in mutate_impl(.data, dots) : Column outl must be length 6 (the group size) or one, not 30

The function returns a vector with T or F for each element of the group passed.


Solution

  • My mistake was that the function was creating a vector for each observation. And I need to get only the last one. So the change is:

    es_outlier<-function(vect){
      m=mean(vect)
      s=sd(vect)
      vector_final=abs(vect)>abs(m+s*1.5)
    return(vector_final[length(vect)] )
    }
    

    And the result:

    tbl%>%group_by(aa)%>%arrange(aa,fecha) %>%
      mutate(outl=rollapply(cant,5, es_outlier, align='right', fill=NA))
    
    # Groups:   aa [2]
           aa      fecha         cant  outl
       <fctr>     <date>        <dbl> <lgl>
           a 2001-01-01 7.586968e-01    NA
           a 2001-03-01 9.939139e-01    NA
           a 2001-04-01 6.064410e-01    NA
           a 2001-05-01 2.937717e-02    NA
           a 2001-09-01 4.321826e-02 FALSE
           a 2001-11-01 1.000000e+03  TRUE
           b 2001-02-01 9.572499e-01    NA
           b 2001-06-01 3.364454e-01    NA
           b 2001-07-01 2.776581e-01    NA
           b 2001-08-01 1.171976e-01    NA
           b 2001-10-01 3.703098e-01 FALSE