Question: How can I update the code below to: a ) Manually specify the cost complexity paramente with a specific value. for example, create a regular

How can I update the code below to:
a) Manually specify the cost complexity paramente with a specific value. for example, create a regular grid with only one value for the complexity parameter?
b ) Prune the tree. Create a regular or random grid with some values for the cost complexity parameter, re-run the hyperparameter tuning step, and investigate the performance of the different hyperparameter values?
c) Select the best one according to for example ROC_AUC or Accuracy?
d) Create the training set, validation, and test prediction using the pruned decision tree.
e) Visualize the pruned decision tree?
dataset <- read_csv("Census.csv")
str(dataset)
dataset$Income <- as.factor(dataset$Income)
dataset$Education.num <- as.factor(dataset$Education.num)
dataset$Race <- as.factor(dataset$Race)
dataset$Sex <- as.factor(dataset$Sex)
dataset = select(dataset,-3)
dataset$Income <- factor(dataset$Income, levels = c("Low","High"))
str(dataset)
summary(dataset)
plot_intro(dataset)
set.seed(1)
data_split <- initial_split(data = dataset,
prop =0.8,
strata = Income)
data_train <- training(data_split)
data_test <- testing(data_split)
target_var <- "Income"
model_form <- Income ~ Age + Education.num + Race + Sex + Hours.per.week
positive_class <- 'Low'
model_recipe <- recipe(formula = model_form, data = data_train)%>%
step_novel(all_nominal_predictors())%>%
step_unknown(all_predictors(),-all_numeric())%>%
step_dummy(all_nominal_predictors())%>%
step_zv(all_predictors())
class_tree_model <-
decision_tree(cost_complexity = tune())%>%
set_engine("rpart")%>%
set_mode("classification")
class_tree_workflow <- workflow()%>%
add_recipe(model_recipe)%>%
add_model(class_tree_model)
all_models <- workflow_set(
preproc = list(model_recipe),
models = list(class_tree_rpart = class_tree_model)
)
set.seed(1)
cv_folds <- vfold_cv(data_train, v=5)
control <- control_resamples(save_workflow = TRUE,
save_pred = TRUE,
event_level = "second")
classification_metrics <- metric_set(yardstick::accuracy,
yardstick::kap,
yardstick::roc_auc,
yardstick::mn_log_loss,
yardstick::sens,
yardstick::spec,
yardstick::f_meas,
yardstick::precision,
yardstick::recall)
tGrid_random <- grid_random(cost_complexity(),
size =50)
models_trained <- all_models %>%
workflow_map("tune_grid",
resamples = cv_folds,
grid = tGrid_random,
metrics = classification_metrics,
verbose = TRUE,
control = control)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!