-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntroduction to Random Forest in R_3.R
More file actions
127 lines (92 loc) · 3.76 KB
/
Copy pathIntroduction to Random Forest in R_3.R
File metadata and controls
127 lines (92 loc) · 3.76 KB
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
# 1) LOADING DATA ----
library(dplyr) # For glimpse()
library(randomForest)
library(caret) # For confusionMatrix()
library(e1071) # ML library
data_train <- read.csv("https://raw.githubusercontent.com/guru99-edu/R-Programming/master/train.csv")
glimpse(data_train)
data_test <- read.csv("https://raw.githubusercontent.com/guru99-edu/R-Programming/master/test.csv")
glimpse(data_test)
# 2.a) DEFINE K-FOLD CROSS-VALIDATION ----
trControl <- trainControl(method = "cv", # Specify method as "cross-validation"
number = 10, # Set folds to 10
search = "grid") # Search all combinations of hyperparameters
# 2.b) BUILD DEFAULT MODEL ----
set.seed(1234)
rf_default <- train(Survived ~ ., # Use all the other variables as predictors of survival
data = data_train,
method = "rf", # Specify training method as "random forest"
metric = "Accuracy", # Chose the best model using "Accuracy" measure
trControl = trControl) # Use k-fold developed above to train the model
# 2.c) SEARCH THE BEST MODEl ----
set.seed(1234)
tuneGrid <- expand.grid(.mtry = c(1: 10))
rf_mtry <- train(Survived~.,
data = data_train,
method = "rf",
metric = "Accuracy",
tuneGrid = tuneGrid,
trControl = trControl,
importance = TRUE,
nodesize = 14,
ntree = 300)
# 2.d) FIND BEST VALUE OF mtry ----
best_mtry <- rf_mtry$bestTune$mtry
# 2.e) TO CHECK Accuracy
Acc <- max(rf_mtry$results$Accuracy)
# 3) FIND THE BEST maxnodes ----
store_maxnode <- list()
tuneGrid <- expand.grid(.mtry = best_mtry)
for (maxnodes in c(5: 15)) {
set.seed(1234)
rf_maxnode <- train(survived~.,
data = data_train,
method = "rf",
metric = "Accuracy",
tuneGrid = tuneGrid,
trControl = trControl,
importance = TRUE,
nodesize = 14,
maxnodes = maxnodes,
ntree = 300)
current_iteration <- toString(maxnodes)
store_maxnode[[current_iteration]] <- rf_maxnode
}
results_mtry <- resamples(store_maxnode)
summary(results_mtry)
# 4) FIND THE BEST ntrees ----
store_maxtrees <- list()
for (ntree in c(250, 300, 350, 400, 450, 500, 550, 600, 800, 1000, 2000)) {
set.seed(5678)
rf_maxtrees <- train(survived~.,
data = data_train,
method = "rf",
metric = "Accuracy",
tuneGrid = tuneGrid,
trControl = trControl,
importance = TRUE,
nodesize = 14,
maxnodes = 24,
ntree = ntree)
key <- toString(ntree)
store_maxtrees[[key]] <- rf_maxtrees
}
results_tree <- resamples(store_maxtrees)
summary(results_tree)
# 5) RETRAIN THE MODEL USING OPTIMAL PARAMETERS ----
fit_rf <- train(survived~.,
data_train,
method = "rf",
metric = "Accuracy",
tuneGrid = tuneGrid,
trControl = trControl,
importance = TRUE,
nodesize = 14,
ntree = 800, # Change ntree to optimal value, here it is 800
maxnodes = 24) # Change maxnodes to optimal value, here it is 24
# 6) EVALUATE THE MODEL ----
prediction <-predict(fit_rf, data_test)
confusionMatrix(prediction, data_test$survived)
# 7) VISUALISE THE MODEL ----
varImpPlot(fit_rf)
# THE END ----