rfunctionoutliersrstatix

How to build a customed function for detecting outliers


I am trying to build a customized function to detect outliers on a dataset. The output that should return is the same as the function identify_outliers in rstatix()

identify_outliers_custom <- function(dt, values) {
  q1 <- quantile(dt)[2]
  q3 <- quantile(dt)[4]
  iqr <- q3 - q1
  lower_bound <- q1 - 1.5* iqr
  upper_bound <- q3 + 1.5* iqr
  dt$is.outlier <- ifelse(dt > upper_bound | dt < lower_bound, TRUE, FALSE)
  dt$is.extreme <- ifelse(dt > q3 + 3 * iqr | dt < q1 - 3 * iqr, TRUE, FALSE)
  return(dt)
}

Anyway, by applying this function on a dataset it turns back an error

mtcars %>% identify_outliers_custom(mpg)
Error in xtfrm.data.frame(x) : it is not possible xtfrm the data frame

How could it be corrected?


Solution

  • You are using dt at wrong places in your custom function. The error can be reproduced by passing a dataframe to quantile function.

    quantile(mtcars)
    

    Error in xtfrm.data.frame(x) : cannot xtfrm data frames

    You may try this function.

    identify_outliers_custom <- function(dt, values) {
      # replace dt with values since we want to get quantile of one column
      q1 <- quantile(values)[2]
      q3 <- quantile(values)[4]
      iqr <- q3 - q1
      lower_bound <- q1 - 1.5* iqr
      upper_bound <- q3 + 1.5* iqr
      # Compare with values and not dt. Also no need for ifelse
      dt$is.outlier <- values > upper_bound | values < lower_bound
      dt$is.extreme <- values > q3 + 3 * iqr | values < q1 - 3 * iqr
      return(dt)
    }
    
    mtcars %>% identify_outliers_custom(.$mpg)
    
    #                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb is.outlier is.extreme
    #Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4      FALSE      FALSE
    #Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4      FALSE      FALSE
    #Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1      FALSE      FALSE
    #Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1      FALSE      FALSE
    #Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2      FALSE      FALSE
    #...
    #...