1

caret与我一起使用时recipes出现错误说明:

Error in { : task 1 failed - "$ operator is invalid for atomic vectors"

我设法将其缩小到食谱的问题。但我不确定我做错了什么。有人见过这个吗?我发现的唯一相关信息是here,上面写着:

当模型对象失败并导致没有可用的配方时会发生这种情况

下面是我使用的代码。我无法共享数据,但使用 mtcars 时也会出现错误。

library(caret)
library(tidymodels)
library(embed)
library(doParallel)

cluster <- makeCluster(detectCores() - 1) 
registerDoParallel(cluster)

df <- mtcars %>% 
  as_tibble() %>% 
  mutate(cyl = factor(cyl)) # to have one nominal variable

set.seed(123)
cv_split <- initial_split(df)
df_train <- training(cv_split)

set.seed(123)
cv_folds <- vfold_cv(df_train, v = 10, repeats = 10)

cv_ind <- rsample2caret(cv_folds)

rec <- 
  recipe(mpg ~ ., data = df_train) %>% 
  step_nzv(all_predictors()) %>% 
  step_lencode_mixed(all_nominal(), outcome = vars(mpg))

ctrl <- 
  trainControl(
    method = "repeatedcv",
    repeats = 10,
    index = cv_ind$index,
    indexOut = cv_ind$indexOut,
    allowParallel = TRUE)

train(rec,
    data = df_train,
    method = "glmnet",
    tuneLength = 20,
    trControl = ctrl)`
4

0 回答 0