當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。