DHSC-Capstone/ML/3_model_outputs.R

129 lines
2.9 KiB
R
Raw Permalink Normal View History

2023-03-22 16:44:36 -04:00
# The following script is for graphing of models
rm(list = ls(all.names = TRUE)) # Clear the memory of variables from previous run.
cat("\014") # Clear the console
# load packages -----------------------------------------------------------
box::use(
magrittr[`%>%`]
,here[here]
,readr
,gp2 = ggplot2[ggplot, aes]
,rsample
,r = recipes
,wf = workflows
,p = parsnip[tune]
,ys = yardstick
,d = dials
,rsamp = rsample
,tune
2023-04-09 19:59:56 -04:00
,workflowsets
2023-03-22 16:44:36 -04:00
)
# globals -----------------------------------------------------------------
set.seed(070823) #set seed for reproducible research
# load-data ---------------------------------------------------------------
screen_workflows_reg <- readr::read_rds(here("ML","outputs","workflowscreen_reg.rds"))
2023-03-27 06:24:05 -04:00
screen_workflows_class <- readr::read_rds(here("ML","outputs","workflowscreen_class.rds"))
2023-03-22 16:44:36 -04:00
# graphing ----------------------------------------------------------------
reg_results <- screen_workflows_reg %>%
workflowsets::rank_results() %>%
dplyr::filter(.metric == "rmse") %>%
dplyr::select(model, .config, rmse = mean, rank)
#TODO Save this for paper
ggplot2::autoplot(
screen_workflows_reg
,rank_metric = "rmse"
,metric = "rmse"
,select_best = TRUE
) +
ggplot2::geom_text(ggplot2::aes(y = mean, label = wflow_id)
# ,angle = 90
,hjust = -0.2
) +
ggplot2::theme_bw() +
ggplot2::scale_color_manual(values = rep("black", times = 5)) +
2023-05-27 16:46:49 -04:00
ggplot2::theme(legend.position = "none") +
ggplot2::labs(
title = "Regression Model Screening"
,y = "RMSE"
) +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
gp2$ggsave(
here("figures","reg_screen.emf")
,width = 7
,height = 7
,dpi = 300
,device = devEMF::emf
)
gp2$ggsave(
here("figures","reg_screen.png")
,width = 7
,height = 7
,dpi = 300
)
2023-03-22 16:44:36 -04:00
2023-03-27 06:24:05 -04:00
class_results <- screen_workflows_class %>%
workflowsets::rank_results()
ggplot2::autoplot(
screen_workflows_class
,rank_metric = "roc_auc"
,metric = "roc_auc"
,select_best = TRUE
) +
ggplot2::geom_text(ggplot2::aes(y = mean, label = wflow_id)
# ,angle = 90
,hjust = -0.2
) +
ggplot2::theme_bw() +
ggplot2::scale_color_manual(values = rep("black", times = 5)) +
2023-05-27 16:46:49 -04:00
ggplot2::theme(legend.position = "none") +
ggplot2::labs(
title = "Classification Model Screening"
,y = "Accuracy"
) +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))
gp2$ggsave(
here("figures","class_screen.emf")
,width = 7
,height = 7
,dpi = 300
,device = devEMF::emf
)
gp2$ggsave(
here("figures","class_screen.png")
,width = 7
,height = 7
,dpi = 300
)
2023-03-27 06:24:05 -04:00
# best results ------------------------------------------------------------
best_class_result <-
screen_workflows_class %>%
workflowsets::extract_workflow_set_result("forests_RF") %>%
tune::select_best(metric = "accuracy")
2023-03-22 16:44:36 -04:00