当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R recipes update_role_requirements 更新角色特定要求


update_role_requirements() 允许您微调配方中可能遇到的各种角色的要求(有关角色的一般信息,请参阅update_role())。角色要求只能针对提供给 recipe() 的原始数据中存在的角色进行更改,它们不适用于通过步骤计算的列。

update_role() 一样,update_role_requirements() 会立即应用于配方,这与 step_*() 函数在 prep() 时间完成大部分工作不同。

用法

update_role_requirements(recipe, role, ..., bake = NULL)

参数

recipe

一个食谱。

role

表示您要修改其要求的角色的字符串。这必须是配方中已经存在的角色。

...

这些点用于将来的扩展,并且必须为空。

bake

bake() 时,是否应该进行检查以确保提供给 recipe() 的该角色的所有列也存在于提供给 bake()new_data 中?

必须是单个 TRUEFALSE 。默认值 NULL 不会修改此要求。

以下表示特定类型角色的默认烘焙时间要求:

  • "outcome":烘烤时不需要。无法改变。

  • "predictor":烘烤时需要。无法改变。

  • "case_weights":默认情况下烘烤时不需要。

  • NA:默认在烘焙时需要。

  • 自定义角色:默认情况下在烘焙时需要。

例子

df <- tibble(y = c(1, 2, 3), x = c(4, 5, 6), var = c("a", "b", "c"))

# Let's assume that you have a `var` column that isn't used in the recipe.
# We typically recommend that you remove this column before passing the
# `data` to `recipe()`, but for now let's pass it through and assign it an
# `"id"` role.
rec <- recipe(y ~ ., df) %>%
  update_role(var, new_role = "id") %>%
  step_center(x)

prepped <- prep(rec, df)

# Now assume you have some "new data" and you are ready to `bake()` it
# to prepare it for prediction purposes. Here, you might not have `var`
# available as a column because it isn't important to your model.
new_data <- df[c("y", "x")]

# By default `var` is required at `bake()` time because we don't know if
# you actually use it in the recipe or not
try(bake(prepped, new_data))
#> Error in bake(prepped, new_data) : 
#>   The following required columns are missing from `new_data`: "var".
#> ℹ These columns have one of the following roles, which are required at `bake()` time: "id".
#> ℹ If these roles are not required at `bake()` time, use `update_role_requirements(role = "your_role", bake = FALSE)`.

# You can turn off this check by using `update_role_requirements()` and
# setting `bake = FALSE` for the `"id"` role. We recommend doing this on
# the original unprepped recipe, but it will also work on a prepped recipe.
rec <- update_role_requirements(rec, "id", bake = FALSE)
prepped <- prep(rec, df)

# Now you can `bake()` on `new_data` even though `var` is missing
bake(prepped, new_data)
#> # A tibble: 3 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1    -1     1
#> 2     0     2
#> 3     1     3

相关用法


注:本文由纯净天空筛选整理自Max Kuhn等大神的英文原创作品 Update role specific requirements。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。