I am currently applying the following recipe and workflow in order to fit a Random Forest using 5 folds cross validation using fit_resamples
. The workflow looks something like this:
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
)
)
When fitting the model I am prompted with the following failure message:
preprocessor 1/1: There are new levels in a factor: NA
preprocessor 1/1, model 1/1 (predictions): Missing data in columns: ethnicity_Asian ...
This looks limited to the one hot encoded columns and even with step_naomit(skip = TRUE)
. For this reason, I've erroneously thought that placing step_naomit
after step_mutate
would take care of it.
I am probably overlooking something fairly simple here, this is my first stab at {tidymodels}
after a long R hyathus.