我正在尝试编写一个适合从使用 step_ns() 的配方中重新采样的函数。出于某种原因,我收到错误消息:
Fold01: recipe: Error: Not all variables in the recipe are present in the supplied training set
等等所有的折叠。接着
警告信息:
All models failed in [fit_resamples()]. See the
.notes
column.
这是我的代码:
compare_basis_exp_to_base_mod <- function (data, outcome, metric, ...) {
outcome <- rlang::enquo(outcome)
metric <- rlang::enquo(metric)
pred_list <- colnames(data)
outcome_str <- substring(deparse(substitute(outcome)), 2)
outcome_str_id <- which(colnames(data) %in% outcome_str)
predictor <- pred_list[-outcome_str_id]
data <- data %>%
rename(prediction = !!outcome)
res <- tibble(splits = list(), id = character(), .metrics = list(),
.notes = list(), .predictions = list(), pred = character())
rec_without_splines <- recipe(prediction ~ ., data = data) %>%
prep()
rec_with_splines <- recipe(prediction ~ ., data = data) %>%
step_ns(all_predictors(), ...) %>%
prep()
folds_without_splines <- vfold_cv(juice(rec_without_splines), strata = prediction)
folds_with_splines <- vfold_cv(juice(rec_with_splines), strata = prediction)
mod <- linear_reg() %>%
set_engine("lm")
mod_without_splines <- fit_resamples(mod,
rec_without_splines,
folds_without_splines,
metrics = metric_set(!!metric),
control = control_resamples(save_pred = TRUE)) %>%
mutate(pred = "no_splines")
mod_with_splines <- fit_resamples(mod,
rec_with_splines,
folds_with_splines,
metrics = metric_set(!!metric),
control = control_resamples(save_pred = TRUE)) %>%
mutate(pred = "with_splines")
res <- mod_without_splines %>%
bind_rows(mod_with_splines)
return (res)
}
基本上,参数data
采用两列表,并且outcome
是结果列的名称。除了使用此功能(我只是在这里玩 tidymodels,因为我是新手),我只想了解导致此错误的原因以及如何修复它。评估时出现错误mod_with_splines
。
这里遇到了类似的问题。但我不知道这是否与我的问题有关。我不能在把菜谱传给之前先做好准备fit_resamples
。(或者我认为)
任何帮助,将不胜感激。谢谢。