87 lines
2.1 KiB
R
87 lines
2.1 KiB
R
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
|
|
,ys = yardstick
|
|
)
|
|
|
|
|
|
|
|
# globals -----------------------------------------------------------------
|
|
|
|
set.seed(070823) #set seed for reproducible research
|
|
|
|
|
|
# load-data ---------------------------------------------------------------
|
|
|
|
model_data <- readr$read_rds(here("ML","data-unshared","model_data.RDS"))
|
|
|
|
|
|
# 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()
|
|
|
|
|
|
class_train <- ds_train %>% dplyr::select(-FT4) # training data for classification models
|
|
reg_train <- ds_train %>% dplyr::select(-ft4_dia) # training data for reg models predicting result
|
|
|
|
# random forest -----------------------------------------------------------
|
|
|
|
# base model - No Hyper Tuning
|
|
|
|
rf_model <- p$rand_forest(trees = 1900) %>%
|
|
p$set_engine("ranger") %>% p$set_mode("classification")
|
|
|
|
rf_recipe <- r$recipe(ft4_dia ~ . , data = class_train) %>%
|
|
r$update_role(subject_id, new_role = "id") %>%
|
|
r$update_role(charttime, new_role = "time") %>%
|
|
r$step_impute_bag(r$all_predictors())
|
|
|
|
|
|
rf_workflow <- wf$workflow() %>%
|
|
wf$add_model(rf_model) %>%
|
|
wf$add_recipe(rf_recipe)
|
|
|
|
rf_fit <- p$fit(rf_workflow, class_train)
|
|
|
|
rf_predict <- class_train %>%
|
|
dplyr::select(ft4_dia) %>%
|
|
dplyr::bind_cols(
|
|
predict(rf_fit, class_train)
|
|
,predict(rf_fit, class_train, type = "prob")
|
|
)
|
|
|
|
conf_mat_rf <- ys$conf_mat(rf_predict, ft4_dia, .pred_class)
|
|
|
|
# explainer_rf <- DALEXtra::explain_tidymodels(
|
|
# rf_fit
|
|
# ,data = class_train
|
|
# ,y = class_train$ft4_dia
|
|
# )
|
|
|
|
# this takes awhile to run
|
|
#vip_lm <- DALEX::model_parts(explainer_rf)
|
|
|
|
#plot(vip_lm)
|