Tidymodels:执行PCR错误:错误:无法子集不存在的列 [英] Tidymodels : problem performing PCR Error: Can't subset columns that don't exist

查看:89
本文介绍了Tidymodels:执行PCR错误:错误:无法子集不存在的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tidymodels进行PCR,但是我一直遇到这个问题.我知道也有类似的帖子,但是那边的解决方案不适用于我的情况.

I'm trying to do a PCR with tidymodels however i'm keep runing into this problem. I know there is a similar post but the solution over there, doesn't work form my case.

我的数据

library(AppliedPredictiveModeling)
data(solubility)

train = solTrainY %>% bind_cols(solTrainXtrans) %>% rename(solubility = ...1)

我的PCR分析

train %<>% mutate_all(., as.numeric) %>% glimpse()
tidy_rec = recipe(solubility ~ ., data = train) %>%
  step_corr(all_predictors(), threshold = 0.9) %>%
  step_pca(all_predictors(), num_comp = ncol(train)-1) %>% 
  prep()

tidy_rec %>% tidy(2) %>% select(terms) %>% distinct()

tidy_predata = tidy_rec %>% juice()

# Re-sampling
tidy_folds = vfold_cv(train, v = 10)

# Set model
tidy_rlm = linear_reg() %>% 
  set_mode("regression") %>% 
  set_engine("lm")

# Set workflow
tidy_wf = workflow() %>% 
  add_recipe(tidy_rec) %>% 
  add_model(tidy_rlm) 

# Fit model
tidy_fit = tidy_wf %>% 
  fit_resamples(tidy_folds) 

tidy_fit %>% collect_metrics()

错误

x Fold01: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`, `PC2`, `PC3`, `PC4`, and `PC5` don't exist.
x Fold02: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`, `PC2`, `PC3`, `PC4`, and `PC5` don't exist.
x Fold03: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`, `PC2`, `PC3`, `PC4`, and `PC5` don't exist.
x Fold04: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`, `PC2`, `PC3`, `PC4`, and `PC5` don't exist.
x Fold05: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`, `PC2`, `PC3`, `PC4`, and `PC5` don't exist.
x Fold06: recipe: Error: Can't subset columns that don't exist.
.
.
.

推荐答案

这是因为 workflow 需要未准备的配方规范.

It is because workflow needs a recipe specification that is not prepped.

因此,在您的代码中,从配方规范中删除 prep()将消除该错误.

So, in your code, removing the prep() from the recipe specification will eliminate the error.

tidy_rec <- recipe(solubility ~ ., data = train) %>%
  step_corr(all_predictors(), threshold = 0.9) %>%
  step_pca(all_predictors(), num_comp = ncol(train)-1) 
  # remove the prep() method

这篇关于Tidymodels:执行PCR错误:错误:无法子集不存在的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆