Robust Scaler in recipes Package

Viewed 13

Is there a robust scaler method in the recipes package in the R programming language? As a result of my research, I could not find this method.

1 Answers

I'm assuming you are referring to the RobustScaler from scikit-learn. You are correct that there isn't a similar step in the recipes package.

It is implemented in the extrasteps package which you can install with

# install.packages("devtools")
devtools::install_github("EmilHvitfeldt/extrasteps")

Then you can use the step_robust() which will do what you are expecting.

library(recipes)
library(extrasteps)

rec <- recipe(~., data = mtcars) %>%
  step_robust(all_predictors()) %>%
  prep()

rec %>%
  bake(new_data = NULL)
#> # A tibble: 32 × 11
#>        mpg   cyl   disp     hp     drat     wt   qsec    vs    am  gear  carb
#>      <dbl> <dbl>  <dbl>  <dbl>    <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  0.244    0   -0.177 -0.156  0.244   -0.685 -0.623     0     1     0   1  
#>  2  0.244    0   -0.177 -0.156  0.244   -0.437 -0.344     0     1     0   1  
#>  3  0.488   -0.5 -0.430 -0.359  0.185   -0.977  0.448     1     1     0  -0.5
#>  4  0.298    0    0.301 -0.156 -0.732   -0.107  0.862     1     0    -1  -0.5
#>  5 -0.0678   0.5  0.798  0.623 -0.649    0.112 -0.344     0     0    -1   0  
#>  6 -0.149    0    0.140 -0.216 -1.11     0.131  1.25      1     0    -1  -0.5
#>  7 -0.664    0.5  0.798  1.46  -0.577    0.238 -0.932     0     0    -1   1  
#>  8  0.705   -0.5 -0.242 -0.731 -0.00595 -0.131  1.14      1     0     0   0  
#>  9  0.488   -0.5 -0.271 -0.335  0.268   -0.170  2.59      1     0     0   0  
#> 10  0        0   -0.140  0      0.268    0.112  0.294     1     0     0   1  
#> # … with 22 more rows

tidy(rec, 1)
#> # A tibble: 33 × 4
#>    terms statistic value id          
#>    <chr> <chr>     <dbl> <chr>       
#>  1 mpg   lower      15.4 robust_hS9q6
#>  2 mpg   median     19.2 robust_hS9q6
#>  3 mpg   higher     22.8 robust_hS9q6
#>  4 cyl   lower       4   robust_hS9q6
#>  5 cyl   median      6   robust_hS9q6
#>  6 cyl   higher      8   robust_hS9q6
#>  7 disp  lower     121.  robust_hS9q6
#>  8 disp  median    196.  robust_hS9q6
#>  9 disp  higher    326   robust_hS9q6
#> 10 hp    lower      96.5 robust_hS9q6
#> # … with 23 more rows

rec <- recipe(~., data = mtcars) %>%
  step_robust(all_predictors(), range = c(0.1, 0.9)) %>%
  prep()

rec %>%
  bake(new_data = NULL)
#> # A tibble: 32 × 11
#>        mpg   cyl    disp      hp     drat      wt   qsec    vs    am  gear
#>      <dbl> <dbl>   <dbl>   <dbl>    <dbl>   <dbl>  <dbl> <dbl> <dbl> <dbl>
#>  1  0.114    0   -0.115  -0.0732  0.171   -0.337  -0.281     0     1   0  
#>  2  0.114    0   -0.115  -0.0732  0.171   -0.215  -0.155     0     1   0  
#>  3  0.229   -0.5 -0.280  -0.169   0.129   -0.480   0.202     1     1   0  
#>  4  0.140    0    0.196  -0.0732 -0.512   -0.0526  0.388     1     0  -0.5
#>  5 -0.0317   0.5  0.519   0.293  -0.453    0.0550 -0.155     0     0  -0.5
#>  6 -0.0698   0    0.0910 -0.101  -0.778    0.0645  0.563     1     0  -0.5
#>  7 -0.311    0.5  0.519   0.687  -0.403    0.117  -0.420     0     0  -0.5
#>  8  0.330   -0.5 -0.157  -0.344  -0.00416 -0.0645  0.514     1     0   0  
#>  9  0.229   -0.5 -0.176  -0.158   0.187   -0.0837  1.16      1     0   0  
#> 10  0        0   -0.0910  0       0.187    0.0550  0.132     1     0   0  
#> # … with 22 more rows, and 1 more variable: carb <dbl>

tidy(rec, 1)
#> # A tibble: 33 × 4
#>    terms statistic value id          
#>    <chr> <chr>     <dbl> <chr>       
#>  1 mpg   lower      14.3 robust_MygTA
#>  2 mpg   median     19.2 robust_MygTA
#>  3 mpg   higher     30.1 robust_MygTA
#>  4 cyl   lower       4   robust_MygTA
#>  5 cyl   median      6   robust_MygTA
#>  6 cyl   higher      8   robust_MygTA
#>  7 disp  lower      80.6 robust_MygTA
#>  8 disp  median    196.  robust_MygTA
#>  9 disp  higher    396   robust_MygTA
#> 10 hp    lower      66   robust_MygTA
#> # … with 23 more rows
Related