tidymodels - Fehler in fit_resamples mit step_ns ()
Ich versuche, eine Funktion zu schreiben, die zu Resamples eines Rezepts passt, das step_ns () verwendet. Aus irgendeinem Grund erhalte ich folgende Fehlermeldungen:
Fold01: recipe: Error: Not all variables in the recipe are present in the supplied training set
und so weiter für alle Falten. Und dann
Warnmeldung: All models failed in [fit_resamples()]. See the .notes column.
Das ist mein Code:
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)
}
Grundsätzlich nimmt das Argument dataeine zweispaltige Tabelle auf und outcomeist der Name der Ergebnisspalte. Abgesehen von der Verwendung dieser Funktion (ich spiele hier nur mit Tidymodellen herum, da ich neu darin bin) möchte ich nur verstehen, was diesen Fehler verursacht und wie er behoben werden kann. Der Fehler tritt bei der Auswertung auf mod_with_splines.
Ein ähnliches Problem wurde hier festgestellt . Aber ich weiß nicht, ob es mit meinem Problem zusammenhängt. Ich kann das Rezept nicht vorbereiten, bevor ich es weitergebe fit_resamples. (Zumindest denke ich)
Jede Hilfe wäre dankbar. Vielen Dank.
Antworten
Ihr Problem ergibt sich aus dem Versuch, ein Rezept auf einen Datensatz anzuwenden, der bereits dasselbe Rezept durchlaufen hat.
Wenn wir annehmen, dass die Prädiktorvariablen X1und waren X2, dann werden rec_with_splinesdiese Variablen erwartet. Da aber folds_with_splinesenthält die entsaftet Ergebnisse rec_with_splinesdann folds_with_splinestatsächlich enthält X1_ns_1, X1_ns_2, X2_ns_1, und X2_ns_2. Nicht X1und X2.
Ich würde vorschlagen, Workflows zu verwenden, um den Vorverarbeitungs- und Modellierungsschritt zu kombinieren. Und um die Rohdaten weiterzugeben 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)