step_outliers_univariate creates a specification of a recipe step that will calculate the outlier score using the user supplied outlier_function on a univariate basis the combination is of multiple variables is handled by the combination_function

step_outliers_univariate(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  outlier_score = NULL,
  columns = NULL,
  name_mutate = ".outliers_univariate",
  outlier_function = function(x) {
     abs((percent_rank(x) - 0.5)) * 2
 },
  combination_function = mean,
  skip = TRUE,
  id = rand_id("outliers_univariate")
)

# S3 method for step_outliers_univariate
tidy(x, ...)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

One or more selector functions to choose which variables will be transformed. See selections() for more details. For the tidy method, these are not currently used.

role

not defined for this function

trained

A logical to indicate if the quantities for preprocessing have been estimated.

outlier_score

a placeholder for the exit of this function don't change

columns

A character string of variable names that will be populated (eventually) by the terms argument.

name_mutate

the name of the generated column with univariate scores

outlier_function

the function used on a column basis to calculate the outlier score.

combination_function

the function used on a row basis to combine different outlier function results.

skip

A logical. Should the step be skipped when the recipe is baked by bake()? While all operations are baked when prep() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations.

id

A character string that is unique to this step to identify it.

x

A step_outliers_univariate object.

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any), with the name on name_mutate and the scores calculated. For the tidy method, a tibble with columns index (the row indexes of the tibble) and outlier_score (the scores).

Details

All columns in the data are sampled and returned by juice() and bake().

All columns used in this step must be numeric with no missing data.

When used in modeling, users should strongly consider using the option skip = TRUE so that this operation is not conducted outside of the training set.

Examples

library(recipes)
library(tidy.outliers)
rec <-
  recipe(mpg ~ ., data = mtcars) %>%
  step_outliers_univariate(all_numeric_predictors()) %>%
  prep(mtcars)

bake(rec, new_data = NULL)
#> # A tibble: 32 × 12
#>      cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb   mpg .outliers…¹
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>       <dbl>
#>  1     6  160    110  3.9   2.62  16.5     0     1     4     4  21         0.381
#>  2     6  160    110  3.9   2.88  17.0     0     1     4     4  21         0.335
#>  3     4  108     93  3.85  2.32  18.6     1     1     4     1  22.8       0.497
#>  4     6  258    110  3.08  3.22  19.4     1     0     3     1  21.4       0.503
#>  5     8  360    175  3.15  3.44  17.0     0     0     3     2  18.7       0.555
#>  6     6  225    105  2.76  3.46  20.2     1     0     3     1  18.1       0.613
#>  7     8  360    245  3.21  3.57  15.8     0     0     3     4  14.3       0.639
#>  8     4  147.    62  3.69  3.19  20       1     0     4     2  24.4       0.490
#>  9     4  141.    95  3.92  3.15  22.9     1     0     4     2  22.8       0.535
#> 10     6  168.   123  3.92  3.44  18.3     1     0     4     4  19.2       0.265
#> # … with 22 more rows, and abbreviated variable name ¹​.outliers_univariate

tidy(rec, number = 1)
#> # A tibble: 32 × 3
#>    index outlier_score id                       
#>    <int>         <dbl> <chr>                    
#>  1     1         0.381 outliers_univariate_LnzXx
#>  2     2         0.335 outliers_univariate_LnzXx
#>  3     3         0.497 outliers_univariate_LnzXx
#>  4     4         0.503 outliers_univariate_LnzXx
#>  5     5         0.555 outliers_univariate_LnzXx
#>  6     6         0.613 outliers_univariate_LnzXx
#>  7     7         0.639 outliers_univariate_LnzXx
#>  8     8         0.490 outliers_univariate_LnzXx
#>  9     9         0.535 outliers_univariate_LnzXx
#> 10    10         0.265 outliers_univariate_LnzXx
#> # … with 22 more rows