如何解决Tidymodels:执行PCR错误:错误:无法子集不存在的列
我正在尝试使用tidymodels进行PCR,但是我一直遇到这个问题。我知道有类似的帖子,但是那边的解决方案不适用于我的情况。
我的数据
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`,and `PC5` don't exist.
x Fold03: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`,and `PC5` don't exist.
x Fold04: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`,and `PC5` don't exist.
x Fold05: recipe: Error: Can't subset columns that don't exist.
x Columns `PC1`,and `PC5` don't exist.
x Fold06: recipe: Error: Can't subset columns that don't exist.
.
.
.
解决方法
这是因为workflow
需要的配方规范没有准备。
因此,在您的代码中,从配方规范中删除prep()
将消除错误。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。