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


R recipes check_missing 檢查缺失值


check_missing 創建配方操作的規範,該規範將檢查變量是否包含缺失值。

用法

check_missing(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  columns = NULL,
  skip = FALSE,
  id = rand_id("missing")
)

參數

recipe

一個菜譜對象。該檢查將添加到該配方的操作序列中。

...

一個或多個選擇器函數用於選擇用於此檢查的變量。有關更多詳細信息,請參閱selections()

role

由於沒有創建新變量,因此此檢查未使用。

trained

... 中的選擇器是否已由 prep() 解析的邏輯。

columns

所選變量名稱的字符串。該字段是一個占位符,一旦使用 prep() 就會被填充。

skip

一個合乎邏輯的。當bake() 烘焙食譜時是否應該跳過檢查?雖然所有操作都是在 prep() 運行時烘焙的,但某些操作可能無法對新數據進行(例如處理結果變量)。使用skip = TRUE時應小心,因為它可能會影響後續操作的計算。

id

此檢查唯一的字符串,用於識別它。

recipe 的更新版本,將新檢查添加到任何現有操作的序列中。

細節

如果任何檢查列確實包含 NA 值,則此檢查將破壞 bake 函數。如果檢查通過,則數據不會發生任何更改。

tidy() 結果

當您tidy()進行此檢查時,將返回帶有terms列(所選的選擇器或變量)的tibble。

也可以看看

其他檢查:check_class()check_cols()check_new_values()check_range()

例子

data(credit_data, package = "modeldata")
is.na(credit_data) %>% colSums()
#>    Status Seniority      Home      Time       Age   Marital   Records 
#>         0         0         6         0         0         1         0 
#>       Job  Expenses    Income    Assets      Debt    Amount     Price 
#>         2         0       381        47        18         0         0 

# If the test passes, `new_data` is returned unaltered
recipe(credit_data) %>%
  check_missing(Age, Expenses) %>%
  prep() %>%
  bake(credit_data)
#> # A tibble: 4,454 × 14
#>    Status Seniority Home     Time   Age Marital Records Job       Expenses
#>    <fct>      <int> <fct>   <int> <int> <fct>   <fct>   <fct>        <int>
#>  1 good           9 rent       60    30 married no      freelance       73
#>  2 good          17 rent       60    58 widow   no      fixed           48
#>  3 bad           10 owner      36    46 married yes     freelance       90
#>  4 good           0 rent       60    24 single  no      fixed           63
#>  5 good           0 rent       36    26 single  no      fixed           46
#>  6 good           1 owner      60    36 married no      fixed           75
#>  7 good          29 owner      60    44 married no      fixed           75
#>  8 good           9 parents    12    27 single  no      fixed           35
#>  9 good           0 owner      60    32 married no      freelance       90
#> 10 bad            0 parents    48    41 married no      partime         90
#> # ℹ 4,444 more rows
#> # ℹ 5 more variables: Income <int>, Assets <int>, Debt <int>,
#> #   Amount <int>, Price <int>

# If your training set doesn't pass, prep() will stop with an error
if (FALSE) {
recipe(credit_data) %>%
  check_missing(Income) %>%
  prep()
}

# If `new_data` contain missing values, the check will stop `bake()`

train_data <- credit_data %>% dplyr::filter(Income > 150)
test_data <- credit_data %>% dplyr::filter(Income <= 150 | is.na(Income))

rp <- recipe(train_data) %>%
  check_missing(Income) %>%
  prep()

bake(rp, train_data)
#> # A tibble: 1,338 × 14
#>    Status Seniority Home   Time   Age Marital Records Job       Expenses
#>    <fct>      <int> <fct> <int> <int> <fct>   <fct>   <fct>        <int>
#>  1 bad           10 owner    36    46 married yes     freelance       90
#>  2 good           0 rent     60    24 single  no      fixed           63
#>  3 good           1 owner    60    36 married no      fixed           75
#>  4 good           8 owner    60    30 married no      fixed           75
#>  5 good          19 priv     36    37 married no      fixed           75
#>  6 good          15 priv     24    52 single  no      freelance       35
#>  7 good          33 rent     24    68 married no      freelance       65
#>  8 good           5 owner    60    22 single  no      fixed           45
#>  9 good          19 owner    60    43 single  no      fixed           75
#> 10 good          15 owner    36    43 married no      fixed           75
#> # ℹ 1,328 more rows
#> # ℹ 5 more variables: Income <int>, Assets <int>, Debt <int>,
#> #   Amount <int>, Price <int>
if (FALSE) {
bake(rp, test_data)
}
源代碼:R/missing.R

相關用法


注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Check for Missing Values。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。