-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsy-data-varimp.R
289 lines (231 loc) · 7.92 KB
/
psy-data-varimp.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
################################################################################
# Script: psy-data-varimp
# Class: Psychology Collaboration
# Topic: Tenure and Job Performance
#
################################################################################
# load libraries
# detach("package:machinelearningtools", character.only = TRUE)
# devtools::install_github("agilebean/machinelearningtools", force = TRUE)
libraries <- c(
"magrittr"
, "caret"
, "RColorBrewer"
, "machinelearningtools"
, "knitr"
, "tidyverse"
, "gbm"
)
sapply(libraries, require, character.only = TRUE)
source("_labels.R")
source("_common.R")
getOption("digits")
################################################################################
# get data of models.list
################################################################################
get_models_varimp <- function(models_list) {
models_list %>%
names %>%
# tricky: avoid glmnet by squeezing ^lm$
# str_detect("^ranger|rf|gbm|lm$") %>%
# str_detect("^rf|gbm|lm$") %>%
str_detect("^RF|GBM|LR$") %>%
# select specific list elements by name
purrr::keep(models_list, .)
}
################################################################################
# MAIN: single model
################################################################################
# 1) get config: from model.permutations.list by model index
model.index = 5
data.labels <- model.permutations.labels[model.index,] %>%
unlist() %>% as.vector() %T>% print
# 2) get data
models.list <- read_models_list(data.labels) %>% print
models.varimp <- models.list %>% get_models_varimp()
library(gbm)
models.varimp$GBM %>% varImp()
models.varimp$RF %>% varImp()
# # doesn't work!
# models.varimp$svmRadial %>% varImp()
# models.varimp$svmRadial %>% varImp(useModel = FALSE, nonpara = FALSE)
# models.varimp$svmRadial %>% varImp(useModel = FALSE)
# models.varimp$svmRadial %>% varImp(nonpara = FALSE)
# models.varimp$svmRadial %>% varImp(scale = FALSE)
#
# models.varimp$svmRadial$finalModel %>% varImp()
# models.varimp$svmRadial$finalModel %>% class
# methods(varImp)
#
# rminer::Importance(M = models.varimp$svmRadial$finalModel,
# data = models.varimp$svmRadial$trainingData,
# method = "sens")
# 3) correlation matrix
models.varimp$GBM %>%
print_correlation_table_from_model(digits = 2)
# 4) visualize feature importance
system.time(
plot.fi <- visualize_importance(
models.varimp$GBM, relative = TRUE, text_labels = TRUE,
# save = TRUE,
axis_limit = 25.5
)
)
plot.fi
plot1 <- visualize_importance(
models.varimp$GBM,
# text_labels = TRUE,
relative = TRUE,
)
models.varimp$LR %>% .$finalModel %>% summary()
models.varimp$GBM %>% varImp()
# problem ranger:
# for varImp(), ranger needs explicit importance = "impurity" argument
# -> implemented this in benchmark_algorithms on July 12, 2020:
# https://github.com/agilebean/machinelearningtools/commit/427e0d5
################################################################################
# MAIN: all models
################################################################################
# step1)
# read models.lists from all datasets
# NEW <- TRUE
NEW <- FALSE
data.label.all <- "data/models.list.PERF10.ALL.rds"
if (NEW) {
system.time(
datasets.models.list <- model.permutations.strings %>%
map(~ read_models_list(.x)) %>%
set_names(model.permutations.strings)
) # 6.8s
system.time(
datasets.models.list %>% saveRDS(data.label.all)
) # 31s
} else {
system.time(
datasets.models.list <- readRDS(data.label.all)
) # 5.3s
}
datasets.models.list
#####################################################
# create correlation matrices
#####################################################
# step1: create correlation tables (html + data)
correlation.list <-
map(datasets.models.list,
~ .x %>% # tricky: start with .x
pluck("LR") %>%
print_correlation_table_from_model(digits = 2)
) %>%
set_names(model.permutations.strings)
# 0.68s
correlation.list$PERF10.big5composites.all
# step2: save correlation html tables
map2(correlation.list, names(correlation.list),
function(correlation_result, jobtype_label) {
correlation_result$html.table %>%
cat(., file = paste0(
c("tables/corrtable", features.set.labels.list,
jobtype_label, correlation_result$method, "html"),
collapse = "."))
})
#####################################################
# create feature importance tables & plots
#####################################################
# step1: extract varImp-able models from each models.list
models.varimp.list <- datasets.models.list %>%
# tricky: start with .x
map( ~ .x %>% get_models_varimp() )
# select model label
model.label.publish <- "GBM"
# # try single model
# models.varimp.list$PERF10.big5composites.all %>%
# pluck(model.label.publish) %>%
# visualize_importance(relative = TRUE, labels = TRUE)
# step2: create varimp plots+tables for each models.list
varimp.list <- models.varimp.list %>%
map(~ .x %>% # .x = models.list
map( ~ visualize_importance(
.x, relative = TRUE, labels = TRUE
)))
# get varImp plots - only for model.label.publish
varimp.list %>% map(
~ .x %>%
pluck(model.label.publish) %>%
pluck("importance.plot") )
# for BigFive Factors (oo, cc, ee, aa, nn)
# options(digits = 3)
varimp.list$PERF10.big5composites.all$GBM
varimp.list$`PERF10.big5composites.R&D`$GBM
varimp.list$PERF10.big5composites.sales$GBM
varimp.list$PERF10.big5composites.support$GBM
# for BigFive Facets (oo1~5, cc1~5, ee1~5, aa1~5, nn1~5)
# options(digits = 2)
varimp.list$PERF10.big5items.all$GBM
varimp.list$`PERF10.big5items.R&D`$GBM
varimp.list$PERF10.big5items.sales$GBM
varimp.list$PERF10.big5items.support$GBM
# step3: save varImp plots
# # this works - but does not consider axis_limits
# map2(
# varimp.list, names(varimp.list),
# ~ .x %>%
# pluck(model.label.publish) %>%
# pluck("importance.plot") %>%
# ggsave(
# filename = paste0(c("figures/importance",
# .y, model.label.publish, "png"),
# collapse = "."),
# plot = .,
# dpi = 300,
# width = 7, height = 3
# )
# )
# save varImp plots with different axis_limits for composites/items
plot_varImp_list_of_lists <- function(dataset_type) {
if (dataset_type == "items") {
list.of.lists <-
get_listelements_by_string(models.varimp.list, "items")
width = 6
height = 6
axis_limit = 48 # items
axis_label = "facet"
} else if (dataset_type == "composites") {
list.of.lists <-
get_listelements_by_string(models.varimp.list, "composites")
width = 7
height = 3
axis_limit = 103 # composites
axis_label = "factor"
}
map2(list.of.lists, names(list.of.lists),
function(models_list, models_list_label) {
map2(models_list, names(models_list),
function(model_object, model_label) {
filename <- paste0(
c("figures/importance",
models_list_label, model_label, "png"),
collapse = ".") %>% print
visualize_importance(
model_object,
relative = TRUE,
text_labels = TRUE,
axis_label = axis_label,
axis_tick_labels = data.labels.long,
save_label = filename,
width = width,
height = height,
axis_limit = axis_limit
)
}
)
}
)
}
# change axis labels for factors/facets
data.labels.long
varimp.list$PERF10.big5items.all$GBM$importance.plot +
scale_x_discrete(labels = data.labels.long)
get_listelements_by_string(models.varimp.list, "composites")
get_listelements_by_string(models.varimp.list, "items")
plot_varImp_list_of_lists("composites")
plot_varImp_list_of_lists("items")