2023-03-17 16:13:34 -04:00
|
|
|
# The following script is for training and testing Regression 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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# globals -----------------------------------------------------------------
|
|
|
|
|
|
|
|
set.seed(070823) #set seed for reproducible research
|
|
|
|
|
|
|
|
|
|
|
|
# load-data ---------------------------------------------------------------
|
|
|
|
|
2023-03-19 20:22:04 -04:00
|
|
|
model_data <- readr$read_rds(here("ML","data-unshared","model_data.RDS")) %>%
|
|
|
|
dplyr::select(-subject_id, -charttime)
|
2023-03-17 16:13:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
# split data --------------------------------------------------------------
|
|
|
|
|
|
|
|
model_data_split <- rsample$initial_split(
|
|
|
|
model_data
|
|
|
|
,prop = 0.80
|
|
|
|
,strata = ft4_dia
|
|
|
|
)
|
|
|
|
|
|
|
|
ds_train <- rsample$training(model_data_split)
|
|
|
|
ds_test <- rsample$testing(model_data_split)
|
|
|
|
|
|
|
|
# verify distribution of data
|
|
|
|
table(ds_train$ft4_dia) %>% prop.table()
|
|
|
|
table(ds_test$ft4_dia) %>% prop.table()
|
|
|
|
|
|
|
|
|
|
|
|
ds_train <- ds_train %>% dplyr::select(-ft4_dia)
|
|
|
|
ds_test <- ds_test %>% dplyr::select(-ft4_dia)
|
|
|
|
|
|
|
|
data_folds <- rsamp$vfold_cv(ds_train, repeats = 5)
|
|
|
|
|
|
|
|
|
2023-03-17 16:43:13 -04:00
|
|
|
|
2023-03-17 16:13:34 -04:00
|
|
|
# recipes ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
# Neural Net, KNN
|
2023-03-21 08:11:18 -04:00
|
|
|
normalized_rec <- recipes::recipe(FT4 ~ ., data = ds_train) %>%
|
|
|
|
recipes::step_impute_bag(recipes::all_predictors()) %>%
|
|
|
|
# recipes::step_corr(recipes::all_numeric_predictors()) %>%
|
|
|
|
recipes::step_normalize(recipes::all_numeric_predictors() , -anchor_age) %>%
|
|
|
|
recipes::step_dummy(gender)
|
2023-03-17 16:13:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Random Forest and Boasted Tree
|
2023-03-21 08:11:18 -04:00
|
|
|
rf_rec <- recipes::recipe(FT4 ~ . , data = ds_train) %>%
|
|
|
|
recipes::step_impute_bag(recipes::all_predictors())
|
2023-03-17 16:13:34 -04:00
|
|
|
|
2023-03-22 12:06:45 -04:00
|
|
|
boost_rec <- recipes::recipe(FT4 ~ . , data = ds_train) %>%
|
|
|
|
recipes::step_impute_bag(recipes::all_predictors()) %>%
|
|
|
|
recipes::step_dummy(gender)
|
2023-03-17 16:13:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
# models ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
nnet_spec <-
|
|
|
|
p$mlp(hidden_units = tune(), penalty = tune(), epochs = tune()) %>%
|
|
|
|
p$set_engine("nnet", MaxNWts = 2600) %>%
|
|
|
|
p$set_mode("regression")
|
|
|
|
|
|
|
|
|
|
|
|
knn_spec <-
|
|
|
|
p$nearest_neighbor(neighbors = tune(), dist_power = tune(), weight_func = tune()) %>%
|
|
|
|
p$set_engine("kknn") %>%
|
|
|
|
p$set_mode("regression")
|
|
|
|
|
|
|
|
|
|
|
|
rf_spec <-
|
2023-03-19 20:22:04 -04:00
|
|
|
p$rand_forest(mtry = tune(), min_n = tune(), trees = tune()) %>%
|
2023-03-17 16:13:34 -04:00
|
|
|
p$set_engine("ranger") %>%
|
|
|
|
p$set_mode("regression")
|
|
|
|
|
|
|
|
|
|
|
|
xgb_spec <-
|
|
|
|
p$boost_tree(tree_depth = tune(), learn_rate = tune(), loss_reduction = tune(),
|
2023-03-17 16:43:13 -04:00
|
|
|
min_n = tune(), sample_size = tune(), trees = tune()) %>%
|
2023-03-17 16:13:34 -04:00
|
|
|
p$set_engine("xgboost") %>%
|
|
|
|
p$set_mode("regression")
|
|
|
|
|
|
|
|
|
2023-03-21 08:11:18 -04:00
|
|
|
svm_r_spec <-
|
|
|
|
p$svm_rbf(cost = tune(), rbf_sigma = tune()) %>%
|
|
|
|
p$set_engine("kernlab") %>%
|
|
|
|
p$set_mode("regression")
|
|
|
|
|
|
|
|
svm_p_spec <-
|
|
|
|
p$svm_poly(cost = tune(), degree = tune()) %>%
|
|
|
|
p$set_engine("kernlab") %>%
|
|
|
|
p$set_mode("regression")
|
|
|
|
|
|
|
|
|
2023-03-17 16:13:34 -04:00
|
|
|
|
|
|
|
nnet_param <-
|
|
|
|
nnet_spec %>%
|
|
|
|
tune$extract_parameter_set_dials() %>%
|
|
|
|
update(hidden_units = d$hidden_units(c(1, 27)))
|
|
|
|
|
|
|
|
|
2023-03-19 20:22:04 -04:00
|
|
|
rf_param <-
|
2023-03-17 16:43:13 -04:00
|
|
|
rf_spec %>%
|
|
|
|
tune$extract_parameter_set_dials() %>%
|
2023-03-19 20:22:04 -04:00
|
|
|
d$finalize(ds_train)
|
2023-03-17 16:43:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-17 16:13:34 -04:00
|
|
|
|
|
|
|
# workflows ---------------------------------------------------------------
|
|
|
|
|
|
|
|
normalized <-
|
|
|
|
workflowsets::workflow_set(
|
|
|
|
preproc = list(normalized = normalized_rec),
|
2023-03-19 20:22:04 -04:00
|
|
|
models = list(
|
2023-03-22 12:06:45 -04:00
|
|
|
SVM_radial = svm_r_spec,
|
2023-03-21 08:11:18 -04:00
|
|
|
# SVM_poly = svm_p_spec,
|
2023-03-20 14:54:03 -04:00
|
|
|
KNN = knn_spec,
|
2023-03-19 20:22:04 -04:00
|
|
|
neural_network = nnet_spec)
|
2023-03-17 16:13:34 -04:00
|
|
|
) %>%
|
|
|
|
workflowsets::option_add(param_info = nnet_param, id = "normalized_neural_network")
|
|
|
|
|
2023-03-22 12:06:45 -04:00
|
|
|
forest <-
|
2023-03-17 16:13:34 -04:00
|
|
|
workflowsets::workflow_set(
|
|
|
|
preproc = list(forests = rf_rec),
|
2023-03-22 12:06:45 -04:00
|
|
|
models = list(RF = rf_spec)
|
2023-03-19 20:22:04 -04:00
|
|
|
) %>%
|
|
|
|
workflowsets::option_add(param_info = rf_param, id = "forests_RF")
|
2023-03-17 16:13:34 -04:00
|
|
|
|
2023-03-22 12:06:45 -04:00
|
|
|
boost <-
|
|
|
|
workflowsets::workflow_set(
|
|
|
|
preproc = list(boost = boost_rec),
|
|
|
|
models = list(boosting = xgb_spec)
|
|
|
|
)
|
2023-03-17 16:13:34 -04:00
|
|
|
|
|
|
|
all_workflows <-
|
2023-03-22 12:06:45 -04:00
|
|
|
dplyr::bind_rows(normalized, forest, boost) %>%
|
|
|
|
dplyr::mutate(wflow_id = gsub("(forest_)|(normalized_)|(boost_)", "", wflow_id))
|
2023-03-17 16:43:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-21 08:11:18 -04:00
|
|
|
# workflow screening ------------------------------------------------------
|
2023-03-22 12:06:45 -04:00
|
|
|
num_cores <- parallel::detectCores() - 2
|
2023-03-21 08:11:18 -04:00
|
|
|
doParallel::registerDoParallel(cores = num_cores)
|
|
|
|
|
|
|
|
screen_workflows <- all_workflows %>%
|
|
|
|
workflowsets::workflow_map(
|
|
|
|
resamples = data_folds,
|
|
|
|
verbose = TRUE
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-25 08:36:00 -04:00
|
|
|
saveRDS(
|
|
|
|
screen_workflows, here::here("ML", "outputs", "workflowscreen.rds")
|
|
|
|
,compress = TRUE)
|
2023-03-21 08:11:18 -04:00
|
|
|
|