R tidymodels-使用步骤()进行拟合重采样时出错
我正在尝试编写一个函数,该函数适用于使用step_ns()的配方的重采样。由于某些原因,我收到了错误消息:R tidymodels-使用步骤()进行拟合重采样时出错,r,tidymodels,R,Tidymodels,我正在尝试编写一个函数,该函数适用于使用step_ns()的配方的重采样。由于某些原因,我收到了错误消息: Fold01:配方:错误:并非配方中的所有变量都存在于提供的培训集中 所有的褶皱都是如此。然后 警告信息: 所有模型在[fit_resamples()]中失败。请参阅.notes列。 这是我的代码: compare_basis_exp_to_base_mod <- function (data, outcome, metric, ...) { outcome <- rla
Fold01:配方:错误:并非配方中的所有变量都存在于提供的培训集中
所有的褶皱都是如此。然后
警告信息:
所有模型在[fit_resamples()]中失败。请参阅
.notes
列。
这是我的代码:
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)
}
compare_basis_exp_to_base_mod您的问题来自尝试在已经运行相同配方的数据集上应用配方
如果我们假设预测变量是X1
和X2
,那么rec\u和_样条
就是这些变量。但是由于folds\u with_spline
包含rec\u with_spline
的丰富结果,那么folds\u with_spline
实际上包含X1_ns_1
,X1_ns_2
,X2_ns_1
和X2_ns_2
。不是X1
和X2
我建议使用将预处理和建模步骤结合起来。并将原始数据传递到vfold\u cv()
库(tidymodels)
比较基础经验和基础模型