Update 2-modeling.R

This commit is contained in:
Kyle Belanger 2023-01-31 15:29:27 -05:00
parent cd755881f2
commit 3fb002a12f

View file

@ -13,6 +13,7 @@ box::use(
,r = recipes ,r = recipes
,wf = workflows ,wf = workflows
,p = parsnip ,p = parsnip
,ys = yardstick
) )
@ -48,13 +49,14 @@ table(ds_test$ft4_dia) %>% prop.table()
# random forest ----------------------------------------------------------- # random forest -----------------------------------------------------------
rf_model <- p$rand_forest(trees = 1900) %>% rf_model <- p$rand_forest(trees = 1900) %>%
p$set_engine("ranger") %>% p$set_mode("classification") p$set_engine("ranger") %>% p$set_mode("classification")
rf_recipe <- r$recipe(ft4_dia ~ . , data = ds_train) %>% rf_recipe <- r$recipe(ft4_dia ~ . , data = ds_train) %>%
r$update_role(subject_id, new_role = "id") %>% r$update_role(subject_id, new_role = "id") %>%
r$update_role(charttime, new_role = "time") %>% r$update_role(charttime, new_role = "time") %>%
r$step_impute_knn(r$all_predictors()) r$step_impute_bag(r$all_numeric())
@ -63,3 +65,13 @@ rf_workflow <- wf$workflow() %>%
wf$add_recipe(rf_recipe) wf$add_recipe(rf_recipe)
rf_fit <- p$fit(rf_workflow, ds_train) rf_fit <- p$fit(rf_workflow, ds_train)
rf_predict <- ds_train %>%
dplyr::select(ft4_dia) %>%
dplyr::bind_cols(
predict(rf_fit, ds_train)
,predict(rf_fit, ds_train, type = "prob")
)
ys$accuracy(rf_predict, ft4_dia, .pred_class)
ys$conf_mat(rf_predict, ft4_dia, .pred_class)