将 recipes::step_dummy 与 caret::train 一起使用时出现以下错误(第一次尝试组合这两个包):
错误:并非配方中的所有变量都存在于提供的训练集中
不确定是什么导致了错误,也不确定调试的最佳方法。帮助训练模型将不胜感激。
library(caret)
library(tidyverse)
library(recipes)
library(rsample)
data("credit_data")
## Split the data into training (75%) and test sets (25%)
set.seed(100)
train_test_split <- initial_split(credit_data)
credit_train <- training(train_test_split)
credit_test <- testing(train_test_split)
# Create recipe for data pre-processing
rec_obj <- recipe(Status ~ ., data = credit_train) %>%
step_knnimpute(all_predictors()) %>%
#step_other(Home, Marital, threshold = .2, other = "other") %>%
#step_other(Job, threshold = .2, other = "others") %>%
step_dummy(Records) %>%
step_center(all_numeric()) %>%
step_scale(all_numeric()) %>%
prep(training = credit_train, retain = TRUE)
train_data <- juice(rec_obj)
test_data <- bake(rec_obj, credit_test)
set.seed(1055)
# the glm function models the second factor level.
lrfit <- train(rec_obj, data = train_data,
method = "glm",
trControl = trainControl(method = "repeatedcv",
repeats = 5))