Wisit C
Wisit C

Reputation: 1

How to convert normalized numeric variable (library(recipes)) back to original value in R

I normalized the numeric variables by library(recipes) in R before putting into Decision Tree models to predict outcome. Now, I have decision tree, and age is one of important variables in the node, like >1.5 and < 1.5. I want to convert that -1.5 back into a non-normalized value to be able to give it a practical meaning (like age >50 or </= 50 years old). I have searched and cannot find the answer.

library(recipes)
recipe_obj <- dataset %>%
  recipe(formula = anyaki ~.) %>% #specify formula
  step_center(all_numeric()) %>% #center data (0 mean)
  step_scale(all_numeric()) %>% #std = 1
  prep(data = dataset)
  dataset_scaled <- bake(recipe_obj, new_data = dataset)

Age is one of variables that have been normalized in recipes package in R. Now, I am struggling to convert the normalized data that I have in the final model back to into a non-normalized value to be able to give it a practical meaning. How can I do this?

Upvotes: 0

Views: 365

Answers (1)

Julia Silge
Julia Silge

Reputation: 11613

You can access these kind of estimated values using the tidy() method for recipes and recipe steps. Check out more details here and here.

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip
data(penguins)

penguin_rec <- recipe(~ ., data = penguins) %>%
  step_other(all_nominal(), threshold = 0.2, other = "another") %>%
  step_normalize(all_numeric()) %>%
  step_dummy(all_nominal())

tidy(penguin_rec)
#> # A tibble: 3 × 6
#>   number operation type      trained skip  id             
#>    <int> <chr>     <chr>     <lgl>   <lgl> <chr>          
#> 1      1 step      other     FALSE   FALSE other_ZNJ2R    
#> 2      2 step      normalize FALSE   FALSE normalize_ogEvZ
#> 3      3 step      dummy     FALSE   FALSE dummy_YVCBo
tidy(penguin_rec, number = 1)
#> # A tibble: 1 × 3
#>   terms         retained id         
#>   <chr>         <chr>    <chr>      
#> 1 all_nominal() <NA>     other_ZNJ2R


penguin_prepped <- prep(penguin_rec, training = penguins)
#> Warning: There are new levels in a factor: NA
tidy(penguin_prepped)
#> # A tibble: 3 × 6
#>   number operation type      trained skip  id             
#>    <int> <chr>     <chr>     <lgl>   <lgl> <chr>          
#> 1      1 step      other     TRUE    FALSE other_ZNJ2R    
#> 2      2 step      normalize TRUE    FALSE normalize_ogEvZ
#> 3      3 step      dummy     TRUE    FALSE dummy_YVCBo


tidy(penguin_prepped, number = 1)
#> # A tibble: 6 × 3
#>   terms   retained id         
#>   <chr>   <chr>    <chr>      
#> 1 species Adelie   other_ZNJ2R
#> 2 species Gentoo   other_ZNJ2R
#> 3 island  Biscoe   other_ZNJ2R
#> 4 island  Dream    other_ZNJ2R
#> 5 sex     female   other_ZNJ2R
#> 6 sex     male     other_ZNJ2R
tidy(penguin_prepped, number = 2)
#> # A tibble: 8 × 4
#>   terms             statistic   value id             
#>   <chr>             <chr>       <dbl> <chr>          
#> 1 bill_length_mm    mean        43.9  normalize_ogEvZ
#> 2 bill_depth_mm     mean        17.2  normalize_ogEvZ
#> 3 flipper_length_mm mean       201.   normalize_ogEvZ
#> 4 body_mass_g       mean      4202.   normalize_ogEvZ
#> 5 bill_length_mm    sd           5.46 normalize_ogEvZ
#> 6 bill_depth_mm     sd           1.97 normalize_ogEvZ
#> 7 flipper_length_mm sd          14.1  normalize_ogEvZ
#> 8 body_mass_g       sd         802.   normalize_ogEvZ

Created on 2021-08-07 by the reprex package (v2.0.0)

Upvotes: 2

Related Questions