-
Notifications
You must be signed in to change notification settings - Fork 24
/
vi_permute.R
287 lines (256 loc) · 9.26 KB
/
vi_permute.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
#' Permutation-Based Variable Importance
#'
#' Compute permutation-based variable importance scores for the predictors in a
#' model. (This function is meant for internal use only.)
#'
#' @param object A fitted model object (e.g., a \code{"randomForest"} object).
#'
#' @param train A matrix-like R object (e.g., a data frame or matrix)
#' containing the training data.
#'
#' @param target Either a character string giving the name (or position) of the
#' target column in \code{train} or, if \code{train} only contains feature
#' columns, a vector containing the target values used to train \code{object}.
#'
#' @param metric Either a function or character string specifying the
#' performance metric to use in computing model performance (e.g., RMSE for
#' regression or accuracy for binary classification). If \code{metric} is a
#' function, then it requires two arguments, \code{actual} and \code{predicted},
#' and should return a single, numeric value.
#'
#' @param smaller_is_better Logical indicating whether or not a smaller value
#' of \code{metric} is better. Default is \code{NULL}. Must be supplied if
#' \code{metric} is a user-supplied function.
#'
#' @param reference_class Character string specifying which response category
#' represents the "reference" class (i.e., the class for which the predicted
#' class probabilities correspond to). Only needed for binary classification
#' problems.
#'
#' @param pred_fun Optional prediction function that requires two arguments,
#' \code{object} and \code{newdata}. Default is \code{NULL}. Must be supplied
#' whenever \code{metric} is a custom function.
#'
#' @return A tidy data frame (i.e., a \code{"tibble"} object) with two columns:
#' \code{Variable} and \code{Importance}. For \code{"glm"}-like object, an
#' additional column, called \code{Sign}, is also included which gives the sign
#' (i.e., POS/NEG) of the original coefficient.
#'
#' @param verbose Logical indicating whether or not to print information during
#' the construction of variable importance scores. Default is \code{FALSE}.
#'
#' @param progress Character string giving the name of the progress bar to use.
#' See \code{\link[plyr]{create_progress_bar}} for details. Default is
#' \code{"none"}.
#'
#' @param parallel Logical indicating whether or not to run \code{vi_permute()}
#' in parallel (using a backend provided by the \code{foreach} package). Default
#' is \code{FALSE}. If \code{TRUE}, an appropriate backend must be provided by
#' \code{foreach}.
#'
#' @param paropts List containing additional options to be passed onto
#' \code{foreach} when \code{parallel = TRUE}.
#'
#' @param ... Additional optional arguments. (Currently ignored.)
#'
#' @details Coming soon!
#'
#' @rdname vi_permute
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # Load required packages
#' library(ggplot2) # for ggtitle() function
#' library(mlbench) # for ML benchmark data sets
#' library(nnet) # for fitting neural networks
#'
#' # Simulate training data
#' set.seed(101) # for reproducibility
#' trn <- as.data.frame(mlbench.friedman1(500)) # ?mlbench.friedman1
#'
#' # Inspect data
#' tibble::as.tibble(trn)
#'
#' # Fit PPR and NN models (hyperparameters were chosen using the caret package
#' # with 5 repeats of 5-fold cross-validation)
#' pp <- ppr(y ~ ., data = trn, nterms = 11)
#' set.seed(0803) # for reproducibility
#' nn <- nnet(y ~ ., data = trn, size = 7, decay = 0.1, linout = TRUE,
#' maxit = 500)
#'
#' # Plot VI scores
#' set.seed(2021) # for reproducibility
#' p1 <- vip(pp, method = "permute", target = "y", metric = "rsquared",
#' pred_fun = predict) + ggtitle("PPR")
#' p2 <- vip(nn, method = "permute", target = "y", metric = "rsquared",
#' pred_fun = predict) + ggtitle("NN")
#' grid.arrange(p1, p2, ncol = 2)
#'
#' # Mean absolute error
#' mae <- function(actual, predicted) {
#' mean(abs(actual - predicted))
#' }
#'
#' # Permutation-based VIP with user-defined MAE metric
#' set.seed(1101) # for reproducibility
#' vip(pp, method = "permute", target = "y", metric = mae,
#' smaller_is_better = TRUE,
#' pred_fun = function(object, newdata) predict(object, newdata) # wrapper
#' ) + ggtitle("PPR")
#' }
vi_permute <- function(object, ...) {
UseMethod("vi_permute")
}
#' @rdname vi_permute
#'
#' @export
vi_permute.default <- function(object, train, target, metric = "auto",
smaller_is_better = NULL, reference_class = NULL, pred_fun = NULL,
verbose = FALSE, progress = "none", parallel = FALSE, paropts = NULL, ...
) {
# Issue warning until this function is complete!
warning("Setting `method = \"permute\"` is experimental, use at your own ",
"risk!", call. = FALSE)
# Get training data, if not supplied
if (missing(train)) {
train <- get_training_data(object)
}
# Extract feature names and separate features from target (if necessary)
if (is.character(target)) {
feature_names <- setdiff(colnames(train), target)
train_x <- train[, feature_names]
train_y <- train[, target, drop = TRUE]
} else {
feature_names <- colnames(train)
train_x <- train
train_y <- target
}
# Metric
if (is.function(metric)) { # user-supplied function
# If `metric` is a user-supplied function, then `smaller_is_better` cannot
# be `NULL`.
if (is.null(smaller_is_better) || !is.logical(smaller_is_better)) {
stop("Please specify a logical value for `smaller_is_better`.",
call. = FALSE)
}
# If `metric` is a user-supplied function, then `pred_fun` cannot
# be `NULL`.
if (is.null(smaller_is_better)) {
stop("Please specify a logical value for `smaller_is_better`.",
call. = FALSE)
} else {
# Check prediction function arguments
if (!all(c("object", "newdata") %in% names(formals(pred_fun)))) {
stop("`pred_fun()` must be a function with arguments `object` and ",
"`newdata`.", call. = FALSE)
}
}
# Check prediction function arguments
if (!identical(c("actual", "predicted"), names(formals(metric)))) {
stop("`metric()` must be a function with arguments `actual` and ",
"`predicted`.", call. = FALSE)
}
# Performance function
perf_fun <- metric
} else {
# Performance metric
metric <- if (metric == "auto") {
get_default_metric(object)
} else {
tolower(metric)
}
# Performance function
perf_fun <- switch(metric,
# Classification
"auc" = perf_auc, # requires predicted class probabilities
"error" = perf_ce, # requires predicted class labels
"logloss" = perf_logLoss, # requires predicted class probabilities
"mauc" = perf_mauc, # requires predicted class probabilities
"mlogloss" = perf_mlogLoss, # requires predicted class probabilities
# Regression
"mse" = perf_mse,
"r2" = perf_rsquared,
"rsquared" = perf_rsquared,
"rmse" = perf_rmse,
# Return informative error
stop("Metric \"", metric, "\" is not supported.")
)
# Is smaller better?
smaller_is_better <- switch(metric,
"auto" = TRUE,
# Classification
"auc" = FALSE,
"error" = TRUE,
"logloss" = TRUE,
"mauc" = FALSE,
"mlogloss" = TRUE,
# Regression
"mse" = TRUE,
"r2" = FALSE,
"rsquared" = FALSE,
"rmse" = TRUE,
# Return informative error
stop("Metric \"", metric, "\" is not supported.")
)
# Get prediction function, if not supplied
prob_based_metrics <- c("auc", "mauc", "logloss", "mlogloss")
if (is.null(pred_fun)) {
type <- if (metric %in% prob_based_metrics) {
"prob"
} else {
"raw"
}
pred_fun <- get_predictions(object, type = type)
}
# Determine reference class (binary classification only)
if (is.null(reference_class) && metric %in% c("auc", "logloss")) {
stop("Please specify the reference class via the `reference_class` ",
"argument when using \"auc\" or \"logloss\".")
}
if (!is.null(reference_class) && metric %in% c("auc", "logloss")) {
train_y <- ifelse(train_y == reference_class, yes = 1, no = 0)
}
}
# Compute baseline metric for comparison
baseline <- perf_fun(
actual = train_y,
predicted = pred_fun(object, newdata = train_x)
)
# Construct VI scores
#
# Loop through each feature and do the following:
#
# 1. make a copy of the training data;
# 2. permute the values of the original feature;
# 3. get new predictions based on permuted data set;
# 4. record difference in accuracy.
vis <- unlist(plyr::llply(feature_names, .progress = progress,
.parallel = parallel, .paropts = paropts,
.fun = function(x) {
if (verbose && !parallel) {
message("Computing variable importance for ", x, "...")
}
train_x_permuted <- train_x # make copy
train_x_permuted[[x]] <- sample(train_x_permuted[[x]]) # permute values
permuted <- perf_fun(
actual = train_y,
predicted = pred_fun(object, newdata = train_x_permuted)
)
if (smaller_is_better) {
permuted - baseline
} else {
baseline - permuted
}
})
)
tib <- tibble::tibble(
"Variable" = feature_names,
"Importance" = vis
)
# Add variable importance type attribute
attr(tib, which = "type") <- "permutation"
# Return results
tib
}