-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_AN_Rf47CutRangeMtrySelection_v1.R
101 lines (85 loc) · 3.42 KB
/
07_AN_Rf47CutRangeMtrySelection_v1.R
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
# Author: M.L.
# Note: this script to change the range of some land cover variables
# end
library(ggplot2)
library(cowplot)
library(foreach)
#library(doParallel)
library(randomForest)
library(tidyverse)
library(DALEX)
library(doSNOW)
library(tcltk)
library(pdp)
load("02_Data/SP_Data_47Variable_Weights_changeRangeOfLandCover.RData")
data_47_no_weights <- data_47 %>% dplyr::select(-weights)
accuracy.df <- data.frame(Doubles=double(),
Ints=integer(),
Factors=factor(),
Logicals=logical(),
Characters=character(),
stringsAsFactors=FALSE)
MTRY <- 11
while(MTRY < 16){
# do SNOW
cl <- makeSOCKcluster(12)
registerDoSNOW(cl)
getDoParWorkers()
ntasks <- 100
pb <- tkProgressBar(max=ntasks)
progress <- function(n) setTkProgressBar(pb, n)
opts <- list(progress=progress)
data.rf.47.weighted <-
foreach(ntree = rep(10, ntasks), .combine = randomForest::combine,
.multicombine=TRUE, .packages='randomForest',
.options.snow=opts) %dopar% {
randomForest(GHQ12 ~ ., data_47_no_weights,
na.action = na.omit, weights = data_47$weights,
ntree = ntree, importance = T, mtry = MTRY)
}
stopCluster(cl)
# do snow
### unified the model
explainer_data.rf.47.weighted = explain(data.rf.47.weighted, data = data_47_no_weights,
y = data_47_no_weights$GHQ12)
model_performance_data.rf.47.weighted <- model_performance(explainer_data.rf.47.weighted)
line.accuracy <- c(MTRY, model_performance_data.rf.47.weighted$measure$mse,
model_performance_data.rf.47.weighted$measure$rmse,
model_performance_data.rf.47.weighted$measure$r2,
model_performance_data.rf.47.weighted$measure$mad)
accuracy.df <- rbind(accuracy.df, line.accuracy)
MTRY <- MTRY + 1
}
MTRY <- 17
while(MTRY < 22){
# do SNOW
cl <- makeSOCKcluster(14)
registerDoSNOW(cl)
getDoParWorkers()
ntasks <- 100
pb <- tkProgressBar(max=ntasks)
progress <- function(n) setTkProgressBar(pb, n)
opts <- list(progress=progress)
data.rf.47.weighted <-
foreach(ntree = rep(10, ntasks), .combine = randomForest::combine,
.multicombine=TRUE, .packages='randomForest',
.options.snow=opts) %dopar% {
randomForest(GHQ12 ~ ., data_47_no_weights,
na.action = na.omit, weights = data_47$weights,
ntree = ntree, importance = T, mtry = MTRY)
}
stopCluster(cl)
# do snow
### unified the model
explainer_data.rf.47.weighted = explain(data.rf.47.weighted, data = data_47_no_weights,
y = data_47_no_weights$GHQ12)
model_performance_data.rf.47.weighted <- model_performance(explainer_data.rf.47.weighted)
line.accuracy <- c(MTRY, model_performance_data.rf.47.weighted$measure$mse,
model_performance_data.rf.47.weighted$measure$rmse,
model_performance_data.rf.47.weighted$measure$r2,
model_performance_data.rf.47.weighted$measure$mad)
accuracy.df <- rbind(accuracy.df, line.accuracy)
MTRY <- MTRY + 1
}
colnames(accuracy.df) <- c("mtry", "MSE", "RMSE", "R2", "MAD")
save(accuracy.df, file = "04_Results/08_Mtry_accuracy.df.RData")