【问题标题】:How to use recipes package to replace missing values with a constant如何使用食谱包用常量替换缺失值
【发布时间】:2018-11-30 02:41:55
【问题描述】:

我不知道如何使用 recipes 包将缺失的数字变量替换为常量。

我确实考虑过使用 step_lowerimpute,但我认为我无法将它用于我的案例。 step_lowerimpute 用 0 和阈值之间的随机数替换 低于 给定阈值的缺失值。在我的情况下,这是行不通的。

例如,我有一些实验室变量,例如乳酸,它经常丢失。我想用极值替换缺失值,例如 -9999。

【问题讨论】:

  • 寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。展示你的尝试并描述它没有达到你想要的效果。

标签: r missing-data r-recipes


【解决方案1】:

您可以尝试使用step_unknown() 函数,该函数将缺失值NA 替换为用户可以提供的new_level

【讨论】:

    【解决方案2】:

    为什么您特别需要食谱包来执行此操作?只需将所有 NA 替换为常数值即可。

    library(imputeTS)
    na.replace(yourDataframe, fill = -9999)
    

    其他解决方案(无需额外包装):

    yourDataframe[is.na(yourDataframe)] <- -9999
    

    【讨论】:

    • 谢谢@stats0007! recipes 包能够自动确定输入数据集中所有变量的类型。我想一次将其应用于所有数值变量,并且我认为打包的食谱可能比“手动”更快。如果我在食谱中找不到方法,我可能不得不这样做。
    【解决方案3】:

    这是我查看食谱包的第一天(所以也许不是最可靠的答案......)。我有同样的问题,并认为以下工作符合要求:

    rec <-
        recipe( ~ ., data = airquality) %>%
        step_mutate(
            Ozone = tidyr::replace_na(Ozone, -9999)
        ) %>%
        prep(training = airquality, retain = TRUE)
    
    juice(rec)
    

    在遇到这种方法之前,我也尝试过创建自己的步骤,这似乎也可以,但上面要简单得多......

    step_nareplace <- 
        function(recipe, 
                ..., 
                role = NA, 
                trained = FALSE,  
                skip = FALSE,
                columns = NULL,
                replace = -9,
                id = rand_id("nareplace")) {
      add_step(
        recipe,
        step_nareplace_new(
          terms = ellipse_check(...),
          role = role,
          trained = trained,
          skip = skip,
          id = id,
          replace = replace,
          columns = columns
        )
      )
    }
    
    step_nareplace_new <- 
        function(terms, role, trained, skip, id, columns, replace) {
      step(
        subclass = "nareplace",
        terms = terms,
        role = role,
        trained = trained,
        skip = skip,
        id = id,
        columns = columns,
        replace = replace
      )
    }
    
    prep.step_nareplace <- function(x, training, info = NULL, ...) {
    
        col_names <- terms_select(x$terms, info = info)
    
        step_nareplace_new(
            terms = x$terms,
            role = x$role,
            trained = TRUE,
            skip = x$skip,
            id = x$id,
            columns = col_names,
            replace = x$replace
          )
    }
    
    bake.step_nareplace <- function(object, new_data, ...) {
      for (i in  object$columns) {
        if (any(is.na(new_data[, i])))
          new_data[is.na(new_data[, i]), i] <- object$replace
      }
      as_tibble(new_data)
    }
    
    print.step_nareplace <-
      function(x, width = max(20, options()$width - 30), ...) {
        cat("Replacing NA values in ", sep = "")
        cat(format_selectors(x$terms, wdth = width))
        cat("\n")
        invisible(x)
      }
    
    tidy.step_nareplace <- function(x, ...) {
      res <- simple_terms(x, ...)
      res$id <- x$id
      res
    }
    
    
    recipe(Ozone ~ ., data = airquality) %>%
       step_nareplace(Ozone, replace = -9999) %>%
       prep(airquality, verbose = FALSE, retain = TRUE) %>%
       juice()
    

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 1970-01-01
      • 2017-09-08
      • 2017-08-01
      • 2023-04-05
      • 2015-05-01
      • 2021-12-04
      • 2014-06-09
      • 1970-01-01
      相关资源
      最近更新 更多