【问题标题】:Tidymodels: Impute missing values in a Date column?Tidymodels:在日期列中估算缺失值?
【发布时间】:2021-06-16 07:00:00
【问题描述】:

此问题与Tidymodels: What is the correct way to impute missing values in a Date column? 重复 问题结束后,我提供了reprex 并再次提出问题。

我有点为日期列中的缺失值而苦恼。 在我的预处理管道 (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.

【问题讨论】:

  • 我不认为step_impute_knn() 对日期有效,但我相信step_impute_linear() 会。试试看!
  • 嗨@JuliaSilge!感谢您的评论(以及精彩的截屏视频)。如果我在Plucked 列上使用step_impute_linear,不幸的是仍然有一个错误,虽然是一个不同的错误。我得到:"preprocessor 1/1: Error: Variable 'Plucked' chosen for linear regression imputation must be of type numeric."

标签: r tidymodels r-recipes


【解决方案1】:

我怀疑step_impute_knn 不适用于日期格式。您可能必须先将其转换为因子。你可以试试下面的代码吗?

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

iris_n[45, 2] <- NA
iris_n[37, 3] <- NA
iris_n[78, 4] <- NA
iris_n[9, 5] <- NA
iris_n[15, 6] <- NA

set.seed(456)

iris_split <- iris_n %>%
  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) %>% #might not need this step anymore
  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)

【讨论】:

  • 嗨@mar​​qui,谢谢你的回答。不幸的是,您的代码对我来说失败了。最后一行代码导致此错误:Error in summary.connection(connection) : invalid connection
  • 这很奇怪,我可以毫无问题地访问iris_rf_wkfl_fit 对象。从快速谷歌搜索你的错误可能与一些并行计算工作有关。您可以尝试 a) 清理您的工作区,重新启动 RStudio 并再次运行工作流程 b) 也可以尝试获取最新的 tidymodel 版本(我使用的是 0.1.3.9000)。
  • 谢谢,更新后错误消失了。我不确定我将如何继续。由于我的数据集非常大,因此将日期列转换为因子并进行一次性编码(或类似的东西)并不是一个真正的选择。不过还是谢谢你。
  • 不客气。根据您的用例,您还可以考虑将其转换为数字,例如毫秒等或自某个时间起源以来的任何时间。
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多