我正在尝试编写一个函数来拟合使用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是结果列的名称。除了使用这个函数(我只是在这里使用tidymodel,因为我是新手),我只想了解是什么导致了这个错误,以及如何修复它。在评估mod_with_splines时出现错误。
here也遇到了类似的问题。但我不知道这是否和我的问题有关。在传递给fit_resamples之前我不能不准备食谱。(或者我是这么认为的)
任何帮助都将不胜感激。谢谢。
发布于 2020-09-03 13:37:36
您的问题来自于试图在已经通过相同配方运行的数据集上应用配方。
如果我们假设预测变量是X1和X2,那么rec_with_splines应该是这些变量。但由于folds_with_splines包含rec_with_splines的优化结果,因此folds_with_splines实际上包含X1_ns_1、X1_ns_2、X2_ns_1和X2_ns_2。不是X1和X2。
我建议使用workflows将预处理和建模步骤结合起来。并将原始数据传递给vfold_cv()。
library(tidymodels)
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)
rec_without_splines <- recipe(prediction ~ ., data = data) %>%
prep()
rec_with_splines <- recipe(prediction ~ ., data = data) %>%
step_ns(all_predictors(), ...)
mod <- linear_reg() %>%
set_engine("lm")
wf_without_splines <- workflow() %>%
add_recipe(rec_without_splines) %>%
add_model(mod)
wf_with_splines <- workflow() %>%
add_recipe(rec_with_splines) %>%
add_model(mod)
data_folds <- vfold_cv(data, strata = prediction)
mod_without_splines <- fit_resamples(wf_without_splines,
data_folds,
metrics = metric_set(!!metric),
control = control_resamples(save_pred = TRUE)) %>%
mutate(pred = "no_splines")
mod_with_splines <- fit_resamples(wf_with_splines,
data_folds,
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)
}
res <- compare_basis_exp_to_base_mod(mtcars, mpg, rmse)https://stackoverflow.com/questions/63700398
复制相似问题