r - 使用 tidymodels 配方包添加缺失的指标列

标签 r r-recipes tidymodels

我想使用 recipes 创建一个食谱该软件包既可以估算缺失的数据,又可以添加指示哪些值缺失的指标列。如果有一个选项可以选择为原始数据框中的每一列包含一个指标列,或者只包含原始数据框中缺少数据的列的指标列,那也很好。我知道我可以很容易地用 来估算缺失值食谱 ,但是否有内置的方法来添加缺失的指标列?

例如,如果我有一个这样的数据框:

> data.frame(x = c(1, NA, 3), y = 4:6)
   x y
1  1 4
2 NA 5
3  3 6

我希望插补和添加缺失的指标列后的输出看起来像这样:
   x y x_missing
1  1 4     FALSE
2  2 5      TRUE
3  3 6     FALSE

当然,对于这样的简单示例,我可以手动完成。但是在机器学习管道中处理大型数据集时,有一种自动化的方式来做到这一点会很有帮助。

根据 recipes::check_missing 的文档,有一个columns争论,

columns A character string of variable names that will be populated (eventually) by the terms argument.



但我不确定这意味着什么,因为没有 terms论据 check_missing .

作为引用,我正在寻找的功能是在 中实现的。 scikit-learn MissingIndicator类(class)。

最佳答案

可以通过创建自定义步骤来做到这一点。遵循 vignettes 之一中描述的过程,创建定义步骤的函数,然后定义 prepbake自定义步骤的方法。

以下代码定义了创建缺失值指标的新步骤。添加了一个带有后缀 _missing 的新列附加到名称。

step_missing_ind <- function(recipe, 
                             ...,
                             role = NA, 
                             trained = FALSE,
                             columns = NULL,
                             skip = FALSE,
                             id = rand_id("missing_ind")) {
  terms <- ellipse_check(...)
  add_step(
    recipe,
    step_missing_ind_new(
      terms = terms, 
      trained = trained,
      role = role, 
      columns = columns,
      skip = skip,
      id = id
    )
  )
}

step_missing_ind_new <- function(terms, 
                                 role, 
                                 trained, 
                                 columns, 
                                 skip, 
                                 id) {
  step(
    subclass = "missing_ind",
    terms = terms,
    role = role,
    trained = trained,
    columns = columns,
    skip = skip,
    id = id
  )
}

print.step_missing_ind <- function(x, width = max(20, options()$width), ...) {
  cat("Missing indicator on ")
  cat(format_selectors(x$terms, width = width))
  if (x$trained) cat(" [trained]\n") else cat("\n")
  invisible(x)
}

prep.step_missing_ind <- function(x, training, info = NULL, ...) {
  col_names <- terms_select(terms = x$terms, info = info)
  step_missing_ind_new(
    terms = x$terms,
    trained = TRUE,
    role = x$role,
    columns = col_names,
    skip = x$skip,
    id = x$id
  )
}

bake.step_missing_ind <- function(object, new_data, ...) {
  for (var in object$columns) {
    new_data[[paste0(var, "_missing")]] <- is.na(new_data[[var]])
  }
  as_tibble(new_data)
}

然后我们可以在配方管道中使用这个缺失的指标步骤,如下例所示,我们添加一个缺失值指标并执行均值插补。缺失指标和插补步骤的顺序很重要:缺失指标步骤必须在插补步骤之前。
library(recipes)

data <- tribble(
  ~x, ~y, ~z,
  1, 4, 7,
  NA, 5, 8,
  3, 6, NA
)

recipe(~ ., data = data) %>%
  step_missing_ind(x, y, z) %>%
  step_meanimpute(x, y, z) %>%
  prep() %>%
  juice()

#> # A tibble: 3 x 6
#>       x     y     z x_missing y_missing z_missing
#>   <dbl> <dbl> <dbl> <lgl>     <lgl>     <lgl>    
#> 1     1     4   7   FALSE     FALSE     FALSE    
#> 2     2     5   8   TRUE      FALSE     FALSE    
#> 3     3     6   7.5 FALSE     FALSE     TRUE

关于r - 使用 tidymodels 配方包添加缺失的指标列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59939421/

相关文章:

r - 使用 R 有选择地删除遵循模式的字符

r - 如何在 rvest 的 html_session 中使用 post

r - 如何从 R 中的 (9) 个重复值创建新变量?我需要循环吗?

r - 使用自定义范围(或值)调整工作流程集中的配方

r - 调整 LASSO 模型并使用 tidymodels 进行预测

R Shiny 使用 updateCheckboxGroupInput() 和 selectinput() 缓存值

r - Tidymodels 工作流程使用 add_formula() 或 add_variables() 但不使用 add_recipe()

r - 在 r 中,使用 tidymodels : Warning message: "All models failed in [fit_resamples()]. See the ` . 注释`列 ."internal: Error: In metric: ` roc_auc`

r - 在 tidymodels 中从 rsample 实现 loo_cv