-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRondom Forest.Rmd
133 lines (124 loc) · 4 KB
/
Rondom Forest.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
title: "Random forest"
author: "Guiquan"
date: "2021/9/2"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Development of Random Forest-based candidate strategy models.
# Bayesian Optimization for Hyper Parameter.
## **PORSM**
```{r}
library(tidymodels)
# Data preparation and specifications.
porsm_mod_spec <-
rand_forest(mode = "classification",
trees = tune(),
mtry = tune(),
min_n = tune()) %>%
set_engine("ranger")
porsm_workflow_spec <- workflow() %>%
add_model(porsm_mod_spec) %>%
add_formula(POR ~.)
porsm_bayes_params <- parameters(porsm_workflow_spec) %>%
update(mtry = finalize(mtry(), porsm_train),
trees = trees(range = c(100L, 1000L)))
# Create cross-validation resamples.
set.seed(777)
porsm_valset <- vfold_cv(porsm_train, v = 5, strata = POR)
# Start tuning.
cl <- parallel::makeCluster(8)
doParallel::registerDoParallel(cl)
porsm_bayes_regs <- tune_bayes(
porsm_workflow_spec,
resamples = porsm_valset,
param_info = porsm_bayes_params,
iter = 100,
metrics = metric_set(roc_auc, mn_log_loss),
control = control_bayes(no_improve = 50, verbose = TRUE)
)
parallel::stopCluster(cl)
```
## **HORSM**
```{r}
# Data preparation and specifications.
horsm_mod_spec <-
rand_forest(mode = "classification",
trees = tune(),
mtry = tune(),
min_n = tune()) %>%
set_engine("ranger")
horsm_workflow_spec <- workflow() %>%
add_model(horsm_mod_spec) %>%
add_formula(HOR ~.)
horsm_bayes_params <- parameters(horsm_workflow_spec) %>%
update(mtry = finalize(mtry(), horsm_train),
trees = trees(range = c(100L, 1000L)))
# Create cross-validation resamples.
set.seed(777)
horsm_valset <- vfold_cv(horsm_train, v = 5, strata = HOR)
# Start tuning.
cl <- parallel::makeCluster(8)
doParallel::registerDoParallel(cl)
horsm_bayes_regs <- tune_bayes(
horsm_workflow_spec,
resamples = horsm_valset,
param_info = horsm_bayes_params,
iter = 100,
metrics = metric_set(roc_auc, mn_log_loss),
control = control_bayes(no_improve = 50, verbose = TRUE)
)
parallel::stopCluster(cl)
```
# Construction of Strategy models and calculate AUC/Brier score.
## Developing models.
```{r}
set.seed(777)
# Strategy models on train data.
porsm <- select_best(porsm_bayes_regs, "roc_auc") %>%
finalize_workflow(porsm_workflow_spec, .) %>%
fit(., porsm_train)
horsm <- select_best(horsm_bayes_regs, "roc_auc") %>%
finalize_workflow(horsm_workflow_spec, .) %>%
fit(., horsm_train)
```
## Calculate AUC and Brier score.
```{r}
# AUC and 95%CI.
set.seed(777)
roc_porsm <- porsm_test %>%
select(POR) %>%
bind_cols(predict(porsm, porsm_test, type = "prob")) %>%
sjmisc::rec(POR, rec = "No = 0; Yes = 1") %>%
select(-c(.pred_Yes, POR)) %>%
pROC::roc(POR_r, .pred_No, auc = TRUE)
roc_porsm %>% pROC::ci.auc(method = "bootstrap")
roc_horsm <- horsm_test %>%
select(HOR) %>%
bind_cols(predict(horsm, horsm_test, type = "prob")) %>%
sjmisc::rec(HOR, rec = "No = 0; Yes = 1") %>%
select(-c(.pred_Yes, HOR)) %>%
pROC::roc(HOR_r, .pred_No, auc = TRUE)
roc_horsm %>% pROC::ci.auc(method = "bootstrap")
# Brier score.
brier_score <- function(preds, obs) {
mean((obs - preds)^2)
}
preds_porsm <- predict(porsm, porsm_test, type = "prob") %>% .[[".pred_Yes"]]
obs_porsm <- porsm_test %>% select(POR) %>% sjmisc::rec(., rec = "No = 0; Yes = 1") %>% .[["POR_r"]] %>%
as.character()%>% as.numeric()
brier_score(obs_porsm, preds_porsm)
preds_horsm <- predict(horsm, horsm_test, type = "prob") %>% .[[".pred_Yes"]]
obs_horsm <- horsm_test %>% select(HOR) %>% sjmisc::rec(., rec = "No = 0; Yes = 1") %>% .[["HOR_r"]] %>%
as.character()%>% as.numeric()
brier_score(obs_horsm, preds_horsm)
```
# Export roc objects for plotting ROC.
```{r}
roc_rf_porsm <- roc_porsm
roc_rf_horsm <- roc_horsm
save(roc_rf_porsm, roc_rf_horsm,
file = "roc_rf.RData")
```