在定义了一个recipe()之后,使用tidymodels方法调用recipes::prep()时,我得到一个错误。在食谱定义过程中,我似乎误用了recipes::step_num2factor(),但我不明白哪里出了问题。
加载包
library(tidyverse) # data wrangling
library(tidymodels) # modelling提供数据
data <-
tibble::tribble(
~Survived, ~Pclass,
1L, 1L,
1L, 2L,
0L, 3L
)定义食谱
titanic_recipe <-
# define model formula:
recipe(Survived ~ Pclass, data = data) %>%
# convert numeric outcome to nominal (factor):
step_num2factor(Survived,
levels = c("dead", "alive")) 准备食谱
prep(titanic_recipe) # THROWS ERROR上面的live抛出了这个错误:
Error: Assigned data `map_df(...)` must be compatible with existing data.
x Existing data has 3 rows.
x Assigned data has 2 rows.
ℹ Only vectors of size 1 are recycled.我不明白为什么会出现这个错误。
发布于 2020-12-15 02:17:20
因子级别不能为0,它们应从1开始。因此,您可以向step()函数添加transform参数以添加1。以下修改效果良好
titanic_recipe <-
recipe(Survived ~ Pclass, data = data) %>%
step_num2factor(Survived,
levels = c("dead", "alive"),
transform = function(x) x+1)
titanic_recipe
prep(titanic_recipe) %>% juice()# A tibble: 3 x 2
Pclass Survived
<int> <fct>
1 1 alive
2 2 alive
3 3 dead https://stackoverflow.com/questions/65264103
复制相似问题