fza fzt vc lc Ra
1 0.50 0.10 40 emulsion 1.915556
2 1.25 0.10 40 emulsion 3.515556
3 0.50 0.20 40 emulsion 1.065556
4 1.25 0.20 40 emulsion 1.965556
5 0.50 0.15 20 emulsion 1.765556
6 1.25 0.15 20 emulsion 3.265556
Ra modeling with SVM radial - Helical milling of Inconel 718 with round carbide inserts
Loading libraries, defining experimental design, and getting measurement results.
The same as done previously.
Tuning best model again with a wider grid
The best model was the SVM radial. Tuning with a wider grid is performed to improve model performance. A grid of 10 values of both cost and \(\sigma\) is considered.
<-
normalized_rec recipe(Ra ~ ., data = plan_train) %>%
step_normalize(fza,fzt,vc) %>%
step_dummy(all_nominal_predictors())
<-
svm_r_spec svm_rbf(cost = tune(), rbf_sigma = tune()) %>%
set_engine("kernlab") %>%
set_mode("regression")
<-
svm_wflow workflow() %>%
add_model(svm_r_spec) %>%
add_recipe(normalized_rec)
<- parameters(cost(), rbf_sigma())
p
<- grid_regular(p, levels = 10)
param_grid
<- tune_grid(
tune_res
svm_wflow, resamples = plan_folds,
grid = param_grid
)
autoplot(tune_res) + theme_bw()
collect_metrics(tune_res) %>%
arrange(mean)
# A tibble: 200 × 8
cost rbf_sigma .metric .estimator mean n std_err .config
<dbl> <dbl> <chr> <chr> <dbl> <int> <dbl> <chr>
1 3.17 0.0774 rmse standard 0.438 20 0.0673 Preprocessor1_Model088
2 32 0.00599 rmse standard 0.454 20 0.0676 Preprocessor1_Model080
3 1 0.0774 rmse standard 0.459 20 0.0749 Preprocessor1_Model087
4 10.1 0.0774 rmse standard 0.501 20 0.0638 Preprocessor1_Model089
5 10.1 0.00599 rmse standard 0.507 20 0.0697 Preprocessor1_Model079
6 10.1 1 rsq standard 0.535 20 0.0680 Preprocessor1_Model099
7 32 1 rsq standard 0.535 20 0.0680 Preprocessor1_Model100
8 3.17 1 rsq standard 0.535 20 0.0679 Preprocessor1_Model098
9 32 0.0774 rmse standard 0.566 20 0.0697 Preprocessor1_Model090
10 1 1 rsq standard 0.572 20 0.0786 Preprocessor1_Model097
# ℹ 190 more rows
<-
best_rmse %>%
tune_res select_best(metric = "rmse")
best_rmse
# A tibble: 1 × 3
cost rbf_sigma .config
<dbl> <dbl> <chr>
1 3.17 0.0774 Preprocessor1_Model088
<-
best_rsq %>%
tune_res select_best(metric = "rsq")
best_rsq
# A tibble: 1 × 3
cost rbf_sigma .config
<dbl> <dbl> <chr>
1 3.17 0.0774 Preprocessor1_Model088
The best model considering both rmse and rsq is with cost = 3.174802 and \(\sigma = 0.07742637\).
<- finalize_workflow(svm_wflow, best_rmse)
svm_r_final
<- fit(svm_r_final, data = plan_train) svm_r_final_fit
Final model is then defined with these parameters’ levels.
augment(svm_r_final_fit, new_data = plan_test) %>%
rsq(truth = Ra, estimate = .pred)
# A tibble: 1 × 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 rsq standard 0.894
augment(svm_r_final_fit, new_data = plan_test) %>%
rmse(truth = Ra, estimate = .pred)
# A tibble: 1 × 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 rmse standard 0.300
Evaluating the model in the whole training set
The model is evaluated in the whole training data set.
<- predict(svm_r_final_fit, new_data = plan_train %>% select(-Ra))
svm_r_res <- bind_cols(svm_r_res, plan_train %>% select(Ra))
svm_r_res head(svm_r_res)
# A tibble: 6 × 2
.pred Ra
<dbl> <dbl>
1 1.87 1.92
2 3.61 3.52
3 0.967 1.07
4 2.06 1.97
5 3.17 3.27
6 1.26 1.17
ggplot(svm_r_res, aes(x = Ra, y = .pred)) +
# Create a diagonal line:
geom_abline(lty = 2) +
geom_point(alpha = 0.5) +
coord_obs_pred() + theme_bw()
Model interpretation
Effects plots to interpret the model according to process aspects.
##########
<- seq(min(plan_train$fza), max(plan_train$fza), length = 50)
x1_grid <- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_em fzt = 0.15,
vc = 40,
lc = "emulsion"))
<- data.frame(fza = x1_grid,
data_p1_em Ra = ypred_fza_em$.pred,
fzt = 0.15,
vc = 40,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_mql fzt = 0.15,
vc = 40,
lc = "mql"))
<- data.frame(fza = x1_grid,
data_p1_mql Ra = ypred_fza_mql$.pred,
fzt = 0.15,
vc = 40,
lc = "mql")
<- rbind(data_p1_em, data_p1_mql)
data_p1
<- ggplot(data = data_p1, mapping = aes(x = fza, y = Ra, group = lc)) +
p1 geom_line(aes(colour = lc, linetype = lc), linewidth = 1.2) +
ylim(.8,2.9) +
scale_color_manual(values = c("red", "blue")) +
theme_bw()
##########
<- seq(min(plan_train$fzt), max(plan_train$fzt), length = 50)
x2_grid <- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_fzt_em fzt = x2_grid,
vc = 40,
lc = "emulsion"))
<- data.frame(fza = 0.875,
data_p2_em Ra = ypred_fzt_em$.pred,
fzt = x2_grid,
vc = 40,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_fzt_mql fzt = x2_grid,
vc = 40,
lc = "mql"))
<- data.frame(fza = 0.875,
data_p2_mql Ra = ypred_fzt_mql$.pred,
fzt = x2_grid,
vc = 40,
lc = "mql")
<- rbind(data_p2_em, data_p2_mql)
data_p2
<- ggplot(data = data_p2, mapping = aes(x = fzt, y = Ra, group = lc)) +
p2 geom_line(aes(colour = lc, linetype = lc), linewidth = 1.2) +
ylim(.8,2.9) +
scale_color_manual(values = c("red", "blue")) +
theme_bw()
##########
<- seq(min(plan_train$vc), max(plan_train$vc), length = 50)
x3_grid <- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_vc_em fzt = 0.15,
vc = x3_grid,
lc = "emulsion"))
<- data.frame(fza = 0.875,
data_p3_em Ra = ypred_fzt_em$.pred,
fzt = 0.15,
vc = x3_grid,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_vc_mql fzt = 0.15,
vc = x3_grid,
lc = "mql"))
<- data.frame(fza = 0.875,
data_p3_mql Ra = ypred_vc_mql$.pred,
fzt = 0.15,
vc = x3_grid,
lc = "mql")
<- rbind(data_p3_em, data_p3_mql)
data_p3
<- ggplot(data = data_p3, mapping = aes(x = vc, y = Ra, group = lc)) +
p3 geom_line(aes(colour = lc, linetype = lc), linewidth = 1.2) +
ylim(.8,2.9) +
scale_color_manual(values = c("red", "blue")) +
theme_bw()
ggarrange(p1, p2 , p3, common.legend = TRUE, nrow = 1)
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_em_a fzt = 0.1,
vc = 40,
lc = "emulsion"))
<- data.frame(fza = x1_grid,
data_p1_em_a Ra = ypred_fza_em_a$.pred,
fzt = 0.1,
vc = 40,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_mql_a fzt = 0.1,
vc = 40,
lc = "mql"))
<- data.frame(fza = x1_grid,
data_p1_mql_a Ra = ypred_fza_mql_a$.pred,
fzt = 0.1,
vc = 40,
lc = "mql")
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_em_b fzt = 0.2,
vc = 40,
lc = "emulsion"))
<- data.frame(fza = x1_grid,
data_p1_em_b Ra = ypred_fza_em_b$.pred,
fzt = 0.2,
vc = 40,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_mql_b fzt = 0.2,
vc = 40,
lc = "mql"))
<- data.frame(fza = x1_grid,
data_p1_mql_b Ra = ypred_fza_mql_b$.pred,
fzt = 0.2,
vc = 40,
lc = "mql")
<- rbind(data_p1, data_p1_em_a, data_p1_mql_a,
data_p1_fza_fzt
data_p1_em_b, data_p1_mql_b)
<- ggplot(data_p1_fza_fzt, aes(y = Ra, x = fza, group = fzt)) +
pp12 geom_line(aes(color = fzt), linewidth = 1.2) +
# scale_fill_binned(type = "viridis") +
scale_color_gradient(low="blue", high="red") +
facet_grid(cols = vars(lc), scales = "free") +
ylim(.6,3.8) +
theme_bw()
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_em_c fzt = 0.15,
vc = 20,
lc = "emulsion"))
<- data.frame(fza = x1_grid,
data_p1_em_c Ra = ypred_fza_em_c$.pred,
fzt = 0.15,
vc = 20,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_mql_c fzt = 0.15,
vc = 20,
lc = "mql"))
<- data.frame(fza = x1_grid,
data_p1_mql_c Ra = ypred_fza_mql_c$.pred,
fzt = 0.15,
vc = 20,
lc = "mql")
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_em_d fzt = 0.15,
vc = 60,
lc = "emulsion"))
<- data.frame(fza = x1_grid,
data_p1_em_d Ra = ypred_fza_em_d$.pred,
fzt = 0.15,
vc = 60,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = x1_grid,
ypred_fza_mql_d fzt = 0.15,
vc = 60,
lc = "mql"))
<- data.frame(fza = x1_grid,
data_p1_mql_d Ra = ypred_fza_mql_d$.pred,
fzt = 0.15,
vc = 60,
lc = "mql")
<- rbind(data_p1, data_p1_em_c, data_p1_mql_c,
data_p1_fza_vc
data_p1_em_d, data_p1_mql_d)
<- ggplot(data_p1_fza_vc, aes(y = Ra, x = fza, group = vc)) +
pp13 geom_line(aes(color = vc), linewidth = 1.2) +
scale_color_gradient(low="blue", high="red") +
facet_grid(cols = vars(lc), scales = "free") +
ylim(.6,3.8) +
theme_bw()
<- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_fzt_em_c fzt = x2_grid,
vc = 20,
lc = "emulsion"))
<- data.frame(fza = 0.875,
data_p2_em_c fzt = x2_grid,
Ra = ypred_fzt_em_c$.pred,
vc = 20,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_fzt_mql_c fzt = x2_grid,
vc = 20,
lc = "mql"))
<- data.frame(fza = 0.875,
data_p2_mql_c fzt = x2_grid,
Ra = ypred_fzt_mql_c$.pred,
vc = 20,
lc = "mql")
<- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_fzt_em_d fzt = x2_grid,
vc = 60,
lc = "emulsion"))
<- data.frame(fza = 0.875,
data_p2_em_d fzt = x2_grid,
Ra = ypred_fzt_em_d$.pred,
vc = 60,
lc = "emulsion")
<- predict(svm_r_final_fit, new_data = data.frame(fza = 0.875,
ypred_fzt_mql_d fzt = x2_grid,
vc = 60,
lc = "mql"))
<- data.frame(fza = 0.875,
data_p2_mql_d fzt = x2_grid,
Ra = ypred_fzt_mql_d$.pred,
vc = 60,
lc = "mql")
<- rbind(data_p2, data_p2_em_c, data_p2_mql_c,
data_p2_fzt_vc
data_p2_em_d, data_p2_mql_d)
<- ggplot(data_p2_fzt_vc, aes(y = Ra, x = fzt, group = vc)) +
pp23 geom_line(aes(color = vc), linewidth = 1.2) +
scale_color_gradient(low="blue", high="red") +
facet_grid(cols = vars(lc), scales = "free") +
ylim(.6,3.8) +
theme_bw()
ggarrange(pp12,pp13,pp23, nrow = 3)
Contour plots.
#######################
<- expand.grid(fza = x1_grid,
grid_12_em fzt = x2_grid,
vc = 40, lc = "emulsion")
<- predict(svm_r_final_fit, new_data = grid_12_em)
y_hat_12_em $Ra <- y_hat_12_em$.pred
grid_12_em
<- expand.grid(fza = x1_grid,
grid_12_mql fzt = x2_grid,
vc = 40, lc = "mql")
<- predict(svm_r_final_fit, new_data = grid_12_mql)
y_hat_12_mql $Ra <- y_hat_12_mql$.pred
grid_12_mql
<- rbind(grid_12_em, grid_12_mql)
grid_12
<- ggplot(data = grid_12,
cp12 mapping = aes(x = fza, y = fzt, z = Ra)) +
geom_tile(aes(fill=Ra)) +
facet_grid(cols = vars(lc), scales = "free") +
scale_fill_distiller(palette = "RdBu",
direction = -1) +
geom_contour(color = "black") +
theme_bw()
#######################
<- expand.grid(fza = x1_grid,
grid_13_em fzt = 0.15,
vc = x3_grid, lc = "emulsion")
<- predict(svm_r_final_fit, new_data = grid_13_em)
y_hat_13_em $Ra <- y_hat_13_em$.pred
grid_13_em
<- expand.grid(fza = x1_grid,
grid_13_mql fzt = 0.15,
vc = x3_grid, lc = "mql")
<- predict(svm_r_final_fit, new_data = grid_13_mql)
y_hat_13_mql $Ra <- y_hat_13_mql$.pred
grid_13_mql
<- rbind(grid_13_em, grid_13_mql)
grid_13
<- ggplot(data = grid_13,
cp13 mapping = aes(x = fza, y = vc, z = Ra)) +
geom_tile(aes(fill=Ra)) +
facet_grid(cols = vars(lc), scales = "free") +
scale_fill_distiller(palette = "RdBu",
direction = -1) +
geom_contour(color = "black") +
theme_bw()
#######################
<- expand.grid(fza = 0.875,
grid_23_em fzt = x2_grid,
vc = x3_grid, lc = "emulsion")
<- predict(svm_r_final_fit, new_data = grid_23_em)
y_hat_23_em $Ra <- y_hat_23_em$.pred
grid_23_em
<- expand.grid(fza = 0.875,
grid_23_mql fzt = x2_grid,
vc = x3_grid, lc = "mql")
<- predict(svm_r_final_fit, new_data = grid_23_mql)
y_hat_23_mql $Ra <- y_hat_23_mql$.pred
grid_23_mql
<- rbind(grid_23_em, grid_23_mql)
grid_23
<- ggplot(data = grid_23,
cp23 mapping = aes(x = fzt, y = vc, z = Ra)) +
geom_tile(aes(fill=Ra)) +
facet_grid(cols = vars(lc), scales = "free") +
scale_fill_distiller(palette = "RdBu",
direction = -1) +
geom_contour(color = "black") +
theme_bw()
ggarrange(cp12,cp13,cp23, nrow = 3)
Variance importance plots.
<- c("fza", "fzt", "vc", "lc")
vip_features
<-
vip_train %>%
plan_train select(all_of(vip_features))
<-
explainer_svm_rexplain_tidymodels(
svm_r_final_fit, data = plan_train %>% select(-Ra),
y = plan_train$Ra,
verbose = FALSE
)
set.seed(1803)
<- model_parts(explainer_svm_r, loss_function = loss_root_mean_square) vip_svm
<- function(...) {
ggplot_imp <- list(...)
obj <- attr(obj[[1]], "loss_name")
metric_name <- paste(metric_name,
metric_lab "after permutations\n(higher indicates more important)")
<- bind_rows(obj) %>%
full_vip filter(variable != "_baseline_")
<- full_vip %>%
perm_vals filter(variable == "_full_model_") %>%
group_by(label) %>%
summarise(dropout_loss = mean(dropout_loss))
<- full_vip %>%
p filter(variable != "_full_model_") %>%
mutate(variable = fct_reorder(variable, dropout_loss)) %>%
ggplot(aes(dropout_loss, variable))
if(length(obj) > 1) {
<- p +
p facet_wrap(vars(label)) +
geom_vline(data = perm_vals, aes(xintercept = dropout_loss, color = label),
linewidth = 1.4, lty = 2, alpha = 0.7) +
geom_boxplot(aes(color = label, fill = label), alpha = 0.2)
else {
} <- p +
p geom_vline(data = perm_vals, aes(xintercept = dropout_loss),
linewidth = 1.4, lty = 2, alpha = 0.7) +
geom_boxplot(fill = "#91CBD765", alpha = 0.4)
}+
p theme(legend.position = "none") +
labs(x = metric_lab,
y = NULL, fill = NULL, color = NULL)
}
ggplot_imp(vip_svm) + labs(x = "RMSE after permutations") + theme_bw()