如何将规范化的数字变量(library(recipes))转换回r中的原始值



在放入决策树模型以预测结果之前,我在R中通过库(食谱)规范化了数字变量。现在,我有了决策树,年龄是节点中重要的变量之一,像>1.5和<1.5. 我想把-1.5转换回一个非标准化的值,以便能够给它一个实际的意义(如年龄>50或

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是r中recipes包中规范化的变量之一。现在,我正在努力将最终模型中的规范化数据转换回非规范化值,以便能够赋予其实际意义。我该怎么做呢?

您可以使用菜谱和菜谱步骤的tidy()方法访问这些估计值。点击这里和这里查看更多细节。

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

由reprex包(v2.0.0)在2021-08-07创建

最新更新