【发布时间】:2021-06-22 08:50:10
【问题描述】:
我目前正在应用以下配方和工作流程,以便使用 fit_resamples 使用 5 折交叉验证来拟合随机森林。工作流程如下所示:
library(tidymodels)
# import data and convert response to factor
train <- read.csv('https://pastebin.com/raw/LJQqdEEE')
train$accepted <- as.factor(train$accepted)
# Train/test split
new_split <- initial_split(train, prop = 0.7)
new_train <- training(new_split)
new_test <- testing(new_split)
# Feature engineering and data prep
admission_rec <-
recipe(accepted ~ ., data = new_train) %>%
step_impute_median(sat) %>%
step_mutate(
ap_scores = strsplit(as.character(ap_scores), ';'),
ap_score_max = max(as.numeric(unlist(ap_scores))),
ap_score_avg = mean(as.numeric(unlist(ap_scores))),
ap_score_min = min(as.numeric(unlist(ap_scores))),
ap_score_med = median(as.numeric(unlist(ap_scores)))
) %>%
step_dummy(ethnicity, one_hot = T) %>%
step_center(c(essay_strength, family_income, sat), skip = T) %>%
step_scale(c(essay_strength, family_income, sat), skip = T) %>%
step_naomit(everything(), skip = T) %>%
step_rm(ap_scores)
# Random forest model and workflow
rf_spec <-
rand_forest() %>%
set_engine('ranger') %>%
set_mode('classification')
rf_workflow <-
workflow() %>%
add_recipe(admission_rec) %>%
add_model(rf_spec)
# Cross validation
cv_folds <-
vfold_cv(new_train, v = 5)
# Fit model
rf_res <- rf_workflow %>%
fit_resamples(
resamples = cv_folds,
metrics = metric_set(
recall, precision, f_meas, accuracy,
kap, roc_auc, sens, spec
)
)
在拟合模型时,系统会提示我以下失败消息:
preprocessor 1/1: There are new levels in a factor: NA
preprocessor 1/1, model 1/1 (predictions): Missing data in columns: ethnicity_Asian ...
这看起来仅限于一个热编码列,甚至是step_naomit(skip = TRUE)。出于这个原因,我错误地认为将step_naomit 放在step_mutate 之后会解决这个问题。
我可能在这里忽略了一些相当简单的事情,这是我在长时间的 R hyathus 之后第一次尝试{tidymodels}。
【问题讨论】:
标签: r tidymodels r-recipes r-ranger