【问题标题】:Tidymodels: What is the correct way to impute missing values in a Date column?Tidymodels:在 Date 列中估算缺失值的正确方法是什么?
【发布时间】:2021-06-14 12:49:06
【问题描述】:

我有点为日期列中的缺失值而苦恼。 在我的预处理管道 (recipe-object) 中,我使用了step_impute_knn 函数来填充我所有日期列中的缺失值。不幸的是,我收到以下错误:

分配的数据 pred_vals 必须与现有数据兼容。?列 avg_begin_first_contract .x 发生错误无法将双精度转换为日期

这是一个reprex,用于我在多个列中估算值的版本,包括Date 列。如果我只将值估算到 Date 列,对我来说并不重要。结果是一样的。下面有一个reprex,没有通过错误,因为没有使用Datecolumn。

以前有人遇到过这个问题吗?

library(tidyverse)
library(tidymodels)

iris <- iris %>%
  mutate(Plucked = sample(seq(as.Date("1999/01/01"), as.Date("2000/01/01"),
    by = "day"
  ), size = 150))

iris[45, 2] <- as.numeric(NA)
iris[37, 3] <- as.numeric(NA)
iris[78, 4] <- as.numeric(NA)
iris[9, 5] <- as.numeric(NA)
iris[15, 6] <- as.factor(NA)

set.seed(456)

iris_split <- iris %>%
  initial_split(strata = Sepal.Length)


iris_training <- training(iris_split)
iris_testing <- testing(iris_split)

iris_rf_model <- rand_forest(
  mtry = 10,
  min_n = 10,
  trees = 500
) %>%
  set_engine("ranger") %>%
  set_mode("regression")


base_rec <- recipe(Sepal.Length ~ .,
  data = iris_training
) %>%
  step_impute_knn(Sepal.Width, Petal.Length, Petal.Width, Species, Plucked) %>%
  step_date(Plucked) %>%
  step_dummy(Species)

iris_workflow <- workflow() %>%
  add_model(iris_rf_model) %>%
  add_recipe(base_rec)

iris_rf_wkfl_fit <- iris_workflow %>%
  last_fit(iris_split)
#> x train/test split: preprocessor 1/1: Error: Assigned data `pred_vals` must be compatible wi...
#> Warning: All models failed. See the `.notes` column.
Created on 2021-06-15 by the reprex package (v2.0.0)

这是reprex,它不会通过错误:

library(tidyverse)
library(tidymodels)

iris[45, 2] <- as.numeric(NA)
iris[37 ,3] <- as.numeric(NA)
iris[78, 4] <- as.numeric(NA)
iris[9, 5] <- as.numeric(NA)

set.seed(123)

iris_split <- iris %>% 
  initial_split(strata = Sepal.Length)

iris_training <- training(iris_split)
iris_testing <- testing(iris_split)

iris_rf_model <- rand_forest(
  mtry = 5,
  min_n = 5,
  trees = 500) %>%
  set_engine("ranger") %>%
  set_mode("regression")


base_rec <- recipe(Sepal.Length ~ .,
                   data = iris_training) %>% 
  step_impute_knn(Sepal.Width, Petal.Length, Petal.Width, Species) %>%
  step_dummy(Species)

iris_workflow <- workflow() %>% 
  add_model(iris_rf_model) %>% 
  add_recipe(base_rec)

iris_rf_wkfl_fit <- iris_workflow %>%
  last_fit(split = iris_split)
Created on 2021-06-15 by the reprex package (v2.0.0)

提前致谢! M.

【问题讨论】:

  • 很难说没有最小的可重复示例,但也许你只需要一个 as.Date() 调用围绕你的插补。例如,as,Date(1,23) 给我们 1970 年 1 月 2 日,正如预期的那样(假设零的纪元是 1970 年 1 月 1 日)。如果这是为您的模型做的正确事情,只有您可以判断,但它应该为您提供正确的 type
  • 感谢您的回答,非常感谢。我更新了问题并在原始数据上提供了reprex,但我可以重现错误。

标签: r tidymodels r-recipes


【解决方案1】:

我想我找到了答案并想与您分享。关键是将日期转换为数值。然后估算很容易。这是reprex

library(tidyverse)
library(tidymodels)

iris <- iris %>%
  mutate(Plucked = sample(seq(as.Date("1999/01/01"), as.Date("2000/01/01"),
    by = "day"
  ), size = 150))

iris[45, 2] <- as.numeric(NA)
iris[37, 3] <- as.numeric(NA)
iris[78, 4] <- as.numeric(NA)
iris[9, 5] <- as.numeric(NA)
iris[15, 6] <- as.factor(NA)

set.seed(456)

iris_split <- iris %>%
  initial_split(strata = Sepal.Length)


iris_training <- training(iris_split)
iris_testing <- testing(iris_split)

iris_rf_model <- rand_forest(
  mtry = 10,
  min_n = 10,
  trees = 500
) %>%
  set_engine("ranger") %>%
  set_mode("regression")


base_rec <- recipe(Sepal.Length ~ .,
  data = iris_training
) %>% 
  step_mutate_at(
    where(lubridate::is.Date),
    fn = ~ as.numeric(lubridate::ymd(.x))
  ) %>%
  step_impute_bag(c("Plucked")) %>% 
  step_impute_knn(Sepal.Width, Petal.Length, Petal.Width, Species) %>%
  step_dummy(Species)

iris_workflow <- workflow() %>%
  add_model(iris_rf_model) %>%
  add_recipe(base_rec)

iris_rf_wkfl_fit <- iris_workflow %>%
  last_fit(iris_split)
#> ! train/test split: preprocessor 1/1, model 1/1: 10 columns were requested but there were 6 ...
Created on 2021-06-29 by the reprex package (v2.0.0)

如果您想在拟合之前从数字恢复到日期,可以通过在代码中添加以下行来实现:

step_mutate_at(c("Plucked"), fn = ~ as.Date(.x, origin = "1970-01-01 UTC"))

再次感谢, M.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多