-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelling-accessors.R
370 lines (295 loc) · 10.4 KB
/
modelling-accessors.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#' Modelling accessor methods
#' @rdname modelling-accessors
#' @description Methods for accessing modelling results.
#' @param x S4 object of class `AnalysisData`,`RandomForest`, `Univariate`, `Analysis` or a list.
#' @param cls sample information column to use
#' @param metric importance metric for which to retrieve explanatory features
#' @param threshold threshold below which explanatory features are extracted
#' @param idx sample information column to use for sample names. If `NULL`, the sample row number will be used. Sample names should be unique for each row of data.
#' @param ... arguments to parse to method for specific class
#' @section Methods:
#' * `binaryComparisons`: Return a vector of all possible binary comparisons for a given sample information column.
#' * `type`: Return the type of random forest analysis.
#' * `response`: Return the response variable name used for a random forest analysis.
#' * `metrics`: Retrieve the model performance metrics for a random forest analysis
#' * `importanceMetrics`: Retrieve the available feature importance metrics for a random forest analysis.
#' * `importance`: Retrieve feature importance results.
#' * `proximity`: Retrieve the random forest sample proximities.
#' * `explanatoryFeatures`: Retrieve explanatory features.
#' @examples
#' library(metaboData)
#'
#' d <- analysisData(abr1$neg[,200:300],abr1$fact)
#'
#' ## Return possible binary comparisons for the 'day' column
#' binaryComparisons(d,cls = 'day')
#'
#' ## Perform random forest analysis
#' rf_analysis <- randomForest(d,cls = 'day')
#'
#' ## Return the type of random forest
#' type(rf_analysis)
#'
#' ## Return the response variable name used
#' response(rf_analysis)
#'
#' ## Retrieve the model performance metrics
#' metrics(rf_analysis)
#'
#' ## Show the available feature importance metrics
#' importanceMetrics(rf_analysis)
#'
#' ## Retrieve the feature importance results
#' importance(rf_analysis)
#'
#' ## Retrieve the sample proximities
#' proximity(rf_analysis)
#'
#' ## Retrieve the explanatory features
#' explanatoryFeatures(rf_analysis,metric = 'FalsePositiveRate',threshold = 0.05)
#' @export
setGeneric('binaryComparisons',function(x,cls = 'class')
standardGeneric('binaryComparisons'))
#' @rdname modelling-accessors
#' @importFrom utils combn
setMethod('binaryComparisons',signature = 'AnalysisData',
function(x,cls = 'class'){
x %>%
clsExtract(cls) %>%
as.character() %>%
unique() %>%
sort() %>%
combn(2) %>%
apply(2,str_c,collapse = '~')
}
)
#' @rdname modelling-accessors
#' @export
setGeneric("type", function(x)
standardGeneric("type"))
#' @rdname modelling-accessors
setMethod('type',signature = 'RandomForest',function(x){
x@type
})
#' @rdname modelling-accessors
#' @export
setGeneric("response", function(x)
standardGeneric("response")
)
#' @rdname modelling-accessors
setMethod('response',signature = 'RandomForest',function(x){
x@response
})
#' @rdname modelling-accessors
#' @export
setGeneric("metrics", function(x)
standardGeneric("metrics")
)
#' @rdname modelling-accessors
setMethod('metrics',signature = 'RandomForest',
function(x){
x@results$measures
}
)
#' @rdname modelling-accessors
setMethod('metrics',signature = 'list',
function(x){
object_classes <- x %>%
map_chr(class)
if (FALSE %in% (object_classes == 'RandomForest')) {
message(
str_c('All objects contained within supplied list ',
'that are not of class RandomForest will be ignored.'))
}
x <- x[object_classes == 'RandomForest']
if (length(x) > 0) {
x %>%
map(metrics) %>%
bind_rows()
} else {
tibble()
}
})
#' @rdname modelling-accessors
setMethod('metrics',signature = 'Analysis',
function(x){
x %>%
analysisResults('modelling') %>%
metrics()
})
#' @rdname modelling-accessors
#' @export
setGeneric("importanceMetrics", function(x)
standardGeneric("importanceMetrics")
)
#' @rdname modelling-accessors
setMethod('importanceMetrics',signature = 'RandomForest',function(x){
x %>%
importance() %>%
.$Metric %>%
unique() %>%
sort()
})
#' @rdname modelling-accessors
#' @export
setGeneric("importance", function(x)
standardGeneric("importance")
)
#' @rdname modelling-accessors
setMethod('importance',signature = 'RandomForest',
function(x){
x@results$importances %>%
ungroup()
}
)
#' @rdname modelling-accessors
setMethod('importance',signature = 'Univariate',
function(x){
x@results %>%
ungroup()
}
)
#' @rdname modelling-accessors
setMethod('importance',signature = 'list',
function(x){
object_classes <- x %>%
map_chr(class)
if (FALSE %in% (object_classes == 'RandomForest' |
object_classes == 'Univariate')) {
stop(
str_c('All objects contained within supplied list ',
'should be of class RandomForest or Univariate'),
call. = FALSE)
}
x %>%
map(importance) %>%
bind_rows(.id = 'Method')
})
#' @rdname modelling-accessors
setMethod('importance',signature = 'Analysis',
function(x){
x %>%
analysisResults(element = 'modelling') %>%
importance()
})
#' @rdname modelling-accessors
#' @export
setGeneric("proximity", function(x,idx = NULL)
standardGeneric("proximity")
)
#' @rdname modelling-accessors
#' @importFrom dplyr relocate
setMethod('proximity',signature = 'RandomForest',
function(x,idx = NULL){
group_vars <- switch(type(x),
classification = c('Response','Comparison'),
regression = 'Response',
unsupervised = NULL) %>%
c(.,'Sample1','Sample2')
proximities <- x@proximities %>%
group_by_at(group_vars) %>%
summarise(Proximity = mean(Proximity),
.groups = 'drop')
if (!is.null(idx)){
sample_idx <- x %>%
clsExtract(cls = idx)
if (any(duplicated(sample_idx))){
stop(str_c('Duplicated sample names found in sample information column `',
idx,
'`. The specified sample names should be unique to each sample.'),
call. = FALSE)
}
sample_idx <- sample_idx %>%
tibble(idx = .) %>%
rowid_to_column(var = 'row')
proximities <- proximities %>%
left_join(sample_idx,
by = c('Sample1' = 'row')) %>%
rename(idx_1 = idx) %>%
left_join(sample_idx,
by = c('Sample2' = 'row')) %>%
rename(idx_2 = idx) %>%
select(-Sample1,
-Sample2,
Sample1 = idx_1,
Sample2 = idx_2) %>%
relocate(Proximity,.after = Sample2)
}
return(proximities)
}
)
#' @rdname modelling-accessors
setMethod('proximity',signature = 'list',
function(x,idx = NULL){
object_classes <- x %>%
map_chr(class)
if (FALSE %in% (object_classes == 'RandomForest')) {
message(
str_c('All objects contained within supplied list ',
'that are not of class RandomForest will be ignored.'))
}
x <- x[object_classes == 'RandomForest']
if (length(x) > 0) {
x %>%
map(proximity,idx = idx) %>%
bind_rows()
} else {
tibble()
}
})
#' @rdname modelling-accessors
setMethod('proximity',signature = 'Analysis',
function(x,idx = NULL){
x %>%
analysisResults(element = 'modelling') %>%
proximity(idx = idx)
})
#' @rdname modelling-accessors
#' @export
setGeneric('explanatoryFeatures', function(x,...)
standardGeneric("explanatoryFeatures")
)
#' @rdname modelling-accessors
#' @importFrom dplyr arrange
setMethod('explanatoryFeatures',signature = 'Univariate',
function(x,threshold = 0.05){
importance(x) %>%
filter(adjusted.p.value < threshold) %>%
arrange(adjusted.p.value)
}
)
#' @rdname modelling-accessors
setMethod('explanatoryFeatures',signature = 'RandomForest',
function(x,metric = 'FalsePositiveRate', threshold = 0.05){
typ <- type(x)
if (typ %in% c('unsupervised','classification')) {
explan <- explanatoryFeaturesClassification(x,metric,threshold)
}
if (typ == 'regression') {
explan <- explanatoryFeaturesRegression(x,metric,threshold)
}
return(explan)
}
)
#' @rdname modelling-accessors
setMethod('explanatoryFeatures',signature = 'list',
function(x,...){
object_classes <- x %>%
map_chr(class)
if (FALSE %in% (object_classes == 'RandomForest' |
object_classes == 'Univariate')) {
stop(str_c('All objects contained within supplied ',
'list should be of class RandomForest or Univariate'),
call. = FALSE)
}
x %>%
map(explanatoryFeatures,...) %>%
bind_rows(.id = 'Method')
})
#' @rdname modelling-accessors
setMethod('explanatoryFeatures',signature = 'Analysis',
function(x,...){
x %>%
analysisResults(element = 'modelling') %>%
explanatoryFeatures(...)
})