`recipes::step_num2factor()` 错误 - tidymodels
error for `recipes::step_num2factor()` - tidymodels
在使用 tidymodels
方法定义 recipe()
后调用 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.
我不明白为什么会出现这个错误。
因子水平不能为零,它们应该从 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
在使用 tidymodels
方法定义 recipe()
后调用 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.
我不明白为什么会出现这个错误。
因子水平不能为零,它们应该从 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