【发布时间】: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