forked from tidymodels/parsnip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultinom_reg.R
333 lines (285 loc) · 10.2 KB
/
multinom_reg.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
#' General Interface for Multinomial Regression Models
#'
#' `multinom_reg()` is a way to generate a _specification_ of a model
#' before fitting and allows the model to be created using
#' different packages in R, keras, or Spark. The main arguments for the
#' model are:
#' \itemize{
#' \item \code{penalty}: The total amount of regularization
#' in the model. Note that this must be zero for some engines.
#' \item \code{mixture}: The mixture amounts of different types of
#' regularization (see below). Note that this will be ignored for some engines.
#' }
#' These arguments are converted to their specific names at the
#' time that the model is fit. Other options and arguments can be
#' set using `set_engine()`. If left to their defaults
#' here (`NULL`), the values are taken from the underlying model
#' functions. If parameters need to be modified, `update()` can be used
#' in lieu of recreating the object from scratch.
#' @inheritParams boost_tree
#' @param mode A single character string for the type of model.
#' The only possible value for this model is "classification".
#' @param penalty A non-negative number representing the total
#' amount of regularization (`glmnet`, `keras`, and `spark` only).
#' For `keras` models, this corresponds to purely L2 regularization
#' (aka weight decay) while the other models can be a combination
#' of L1 and L2 (depending on the value of `mixture`).
#' @param mixture A number between zero and one (inclusive) that is the
#' proportion of L1 regularization (i.e. lasso) in the model. When
#' `mixture = 1`, it is a pure lasso model while `mixture = 0` indicates that
#' ridge regression is being used. (`glmnet` and `spark` only).
#' @details
#' For `multinom_reg()`, the mode will always be "classification".
#'
#' The model can be created using the `fit()` function using the
#' following _engines_:
#' \itemize{
#' \item \pkg{R}: `"glmnet"` (the default), `"nnet"`
#' \item \pkg{Stan}: `"stan"`
#' \item \pkg{keras}: `"keras"`
#' }
#'
#' @includeRmd man/rmd/multinom-reg.Rmd details
#'
#' @note For models created using the spark engine, there are
#' several differences to consider. First, only the formula
#' interface to via `fit()` is available; using `fit_xy()` will
#' generate an error. Second, the predictions will always be in a
#' spark table format. The names will be the same as documented but
#' without the dots. Third, there is no equivalent to factor
#' columns in spark tables so class predictions are returned as
#' character columns. Fourth, to retain the model object for a new
#' R session (via `save()`), the `model$fit` element of the `parsnip`
#' object should be serialized via `ml_save(object$fit)` and
#' separately saved to disk. In a new session, the object can be
#' reloaded and reattached to the `parsnip` object.
#'
#' @seealso [fit()]
#' @examples
#' show_engines("multinom_reg")
#'
#' multinom_reg()
#' # Parameters can be represented by a placeholder:
#' multinom_reg(penalty = varying())
#' @export
#' @importFrom purrr map_lgl
multinom_reg <-
function(mode = "classification",
penalty = NULL,
mixture = NULL) {
args <- list(
penalty = enquo(penalty),
mixture = enquo(mixture)
)
new_model_spec(
"multinom_reg",
args = args,
eng_args = NULL,
mode = mode,
method = NULL,
engine = NULL
)
}
#' @export
print.multinom_reg <- function(x, ...) {
cat("Multinomial Regression Model Specification (", x$mode, ")\n\n", sep = "")
model_printer(x, ...)
if (!is.null(x$method$fit$args)) {
cat("Model fit template:\n")
print(show_call(x))
}
invisible(x)
}
#' @export
translate.multinom_reg <- translate.linear_reg
# ------------------------------------------------------------------------------
#' @inheritParams update.boost_tree
#' @param object A multinomial regression model specification.
#' @examples
#' model <- multinom_reg(penalty = 10, mixture = 0.1)
#' model
#' update(model, penalty = 1)
#' update(model, penalty = 1, fresh = TRUE)
#' @method update multinom_reg
#' @rdname multinom_reg
#' @export
update.multinom_reg <-
function(object,
parameters = NULL,
penalty = NULL, mixture = NULL,
fresh = FALSE, ...) {
eng_args <- update_engine_parameters(object$eng_args, ...)
if (!is.null(parameters)) {
parameters <- check_final_param(parameters)
}
args <- list(
penalty = enquo(penalty),
mixture = enquo(mixture)
)
args <- update_main_parameters(args, parameters)
if (fresh) {
object$args <- args
object$eng_args <- eng_args
} else {
null_args <- map_lgl(args, null_value)
if (any(null_args))
args <- args[!null_args]
if (length(args) > 0)
object$args[names(args)] <- args
if (length(eng_args) > 0)
object$eng_args[names(eng_args)] <- eng_args
}
new_model_spec(
"multinom_reg",
args = object$args,
eng_args = object$eng_args,
mode = object$mode,
method = NULL,
engine = object$engine
)
}
# ------------------------------------------------------------------------------
check_args.multinom_reg <- function(object) {
args <- lapply(object$args, rlang::eval_tidy)
if (all(is.numeric(args$penalty)) && any(args$penalty < 0))
rlang::abort("The amount of regularization should be >= 0.")
if (is.numeric(args$mixture) && (args$mixture < 0 | args$mixture > 1))
rlang::abort("The mixture proportion should be within [0,1].")
invisible(object)
}
# ------------------------------------------------------------------------------
organize_multnet_class <- function(x, object) {
x[,1]
}
organize_multnet_prob <- function(x, object) {
x <- x[,,1]
as_tibble(x)
}
organize_nnet_prob <- function(x, object) {
format_classprobs(x)
}
# ------------------------------------------------------------------------------
# glmnet call stack for multinomial regression using `predict` when object has
# classes "_multnet" and "model_fit" (for class predictions):
#
# predict()
# predict._multnet(penalty = NULL) <-- checks and sets penalty
# predict.model_fit() <-- checks for extra vars in ...
# predict_class()
# predict_class._multnet()
# predict.multnet()
# glmnet call stack for multinomial regression using `multi_predict` when object has
# classes "_multnet" and "model_fit" (for class predictions):
#
# multi_predict()
# multi_predict._multnet(penalty = NULL)
# predict._multnet(multi = TRUE) <-- checks and sets penalty
# predict.model_fit() <-- checks for extra vars in ...
# predict_raw()
# predict_raw._multnet()
# predict_raw.model_fit(opts = list(s = penalty))
# predict.multnet()
# ------------------------------------------------------------------------------
#' @export
predict._multnet <-
function(object, new_data, type = NULL, opts = list(), penalty = NULL, multi = FALSE, ...) {
# See discussion in https://github.com/tidymodels/parsnip/issues/195
if (is.null(penalty) & !is.null(object$spec$args$penalty)) {
penalty <- object$spec$args$penalty
}
object$spec$args$penalty <- check_penalty(penalty, object, multi)
object$spec <- eval_args(object$spec)
res <- predict.model_fit(
object = object,
new_data = new_data,
type = type,
opts = opts
)
res
}
#' @importFrom dplyr full_join as_tibble arrange
#' @importFrom tidyr gather
#' @export
#' @rdname multi_predict
multi_predict._multnet <-
function(object, new_data, type = NULL, penalty = NULL, ...) {
if (any(names(enquos(...)) == "newdata"))
rlang::abort("Did you mean to use `new_data` instead of `newdata`?")
if (is_quosure(penalty))
penalty <- eval_tidy(penalty)
dots <- list(...)
if (is.null(penalty)) {
# See discussion in https://github.com/tidymodels/parsnip/issues/195
if (!is.null(object$spec$args$penalty)) {
penalty <- object$spec$args$penalty
} else {
penalty <- object$fit$lambda
}
}
dots$s <- penalty
if (is.null(type))
type <- "class"
if (!(type %in% c("class", "prob", "link", "raw"))) {
rlang::abort("`type` should be either 'class', 'link', 'raw', or 'prob'.")
}
if (type == "prob")
dots$type <- "response"
else
dots$type <- type
object$spec <- eval_args(object$spec)
pred <- predict.model_fit(object, new_data = new_data, type = "raw", opts = dots)
format_probs <- function(x) {
x <- as_tibble(x)
names(x) <- paste0(".pred_", names(x))
nms <- names(x)
x$.row <- 1:nrow(x)
x[, c(".row", nms)]
}
if (type == "prob") {
pred <- apply(pred, 3, format_probs)
names(pred) <- NULL
pred <- map_dfr(pred, function(x) x)
pred$penalty <- rep(penalty, each = nrow(new_data))
} else {
pred <-
tibble(
.row = rep(1:nrow(new_data), length(penalty)),
.pred_class = factor(as.vector(pred), levels = object$lvl),
penalty = rep(penalty, each = nrow(new_data))
)
}
pred <- arrange(pred, .row, penalty)
.row <- pred$.row
pred$.row <- NULL
pred <- split(pred, .row)
names(pred) <- NULL
tibble(.pred = pred)
}
#' @export
predict_class._multnet <- function(object, new_data, ...) {
object$spec <- eval_args(object$spec)
predict_class.model_fit(object, new_data = new_data, ...)
}
#' @export
predict_classprob._multnet <- function(object, new_data, ...) {
object$spec <- eval_args(object$spec)
predict_classprob.model_fit(object, new_data = new_data, ...)
}
#' @export
predict_raw._multnet <- function(object, new_data, opts = list(), ...) {
object$spec <- eval_args(object$spec)
predict_raw.model_fit(object, new_data = new_data, opts = opts, ...)
}
# ------------------------------------------------------------------------------
# This checks as a pre-processor in the model data object
check_glmnet_lambda <- function(dat, object) {
if (length(object$fit$lambda) > 1)
rlang::abort(
glue::glue(
"`predict()` doesn't work with multiple penalties (i.e. lambdas). ",
"Please specify a single value using `penalty = some_value` or use ",
"`multi_predict()` to get multiple predictions per row of data."
)
)
dat
}