1

如何在配方中保持characterID 变量不变?PERSON_ID我尝试update_role(PERSON_ID , new_role = "id variable")并尝试将其从 step_dummy 中排除step_dummy(all_nominal_predictors(), -all_numeric_predictors(), -all_outcomes(), -has_role(match = "id variable")。这没用。它仍然将 PERSON_ID 转换为因子。有什么建议吗?

4

1 回答 1

0

这似乎是一个令人困惑的问题。按照配方函数文档,step_factor2string 应该将因子转换为字符串。

但是,当您瞥见配方时,它会为 PERSON_ID 声明“fct”。另一方面,如果您将 strings_as_factors 设置为 FALSE,则会出现错误,说明 PERSON_ID 不是一个因素:

library(tibble)
library(tidymodels)

data_input <- tibble(target = rep(1,9),
               num_var = rep(2,9),
               char = c(rep("a", 6),rep("b",3)),
               PERSON_ID = as.character(c(rep("W",3),rep("D",6))),
               logi = rep(c(TRUE,FALSE,FALSE),3),
               fac = as.factor(c(rep("1",6),rep("2",3)))
               )
             
recipe_spec <- recipe(target ~ ., data = data_input) %>% 
  update_role("PERSON_ID", new_role = "id variable") %>%
  step_dummy(all_nominal_predictors(),-all_numeric_predictors(),-all_outcomes(),-has_role(match = "id variable")) %>% 
  step_factor2string(PERSON_ID)

recipe_spec %>%  prep() %>%  juice()  %>%  glimpse()

recipe_spec %>%  prep(strings_as_factors = FALSE) %>%  juice()  %>%  glimpse()

 
        
于 2022-03-04T22:03:12.973 回答