forked from tidymodels/parsnip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmars.R
211 lines (179 loc) · 5.93 KB
/
mars.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
#' Multivariate adaptive regression splines (MARS)
#'
#' @description
#'
#' `mars()` defines a generalized linear model that uses artificial features for
#' some predictors. These features resemble hinge functions and the result is
#' a model that is a segmented regression in small dimensions. This function can
#' fit classification and regression models.
#'
#' \Sexpr[stage=render,results=rd]{parsnip:::make_engine_list("mars")}
#'
#' More information on how \pkg{parsnip} is used for modeling is at
#' \url{https://www.tidymodels.org/}.
#'
#' @inheritParams boost_tree
#' @param num_terms The number of features that will be retained in the
#' final model, including the intercept.
#' @param prod_degree The highest possible interaction degree.
#' @param prune_method The pruning method.
#'
#' @template spec-details
#'
#' @template spec-references
#'
#' @seealso \Sexpr[stage=render,results=rd]{parsnip:::make_seealso_list("mars")}
#'
#' @examples
#' show_engines("mars")
#'
#' mars(mode = "regression", num_terms = 5)
#' @export
mars <-
function(mode = "unknown", engine = "earth",
num_terms = NULL, prod_degree = NULL, prune_method = NULL) {
args <- list(
num_terms = enquo(num_terms),
prod_degree = enquo(prod_degree),
prune_method = enquo(prune_method)
)
new_model_spec(
"mars",
args = args,
eng_args = NULL,
mode = mode,
method = NULL,
engine = engine
)
}
#' @export
print.mars <- function(x, ...) {
cat("MARS 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)
}
# ------------------------------------------------------------------------------
#' @method update mars
#' @rdname parsnip_update
#' @export
update.mars <-
function(object,
parameters = NULL,
num_terms = NULL, prod_degree = NULL, prune_method = NULL,
fresh = FALSE, ...) {
args <- list(
num_terms = enquo(num_terms),
prod_degree = enquo(prod_degree),
prune_method = enquo(prune_method)
)
update_spec(
object = object,
parameters = parameters,
args_enquo_list = args,
fresh = fresh,
cls = "mars",
...
)
}
# ------------------------------------------------------------------------------
#' @export
translate.mars <- function(x, engine = x$engine, ...) {
if (is.null(engine)) {
message("Used `engine = 'earth'` for translation.")
engine <- "earth"
}
# If classification is being done, the `glm` options should be used. Check to
# see if it is there and, if not, add the default value.
if (x$mode == "classification") {
if (!("glm" %in% names(x$eng_args))) {
x$eng_args$glm <- rlang::quo(list(family = stats::binomial))
}
}
x <- translate.default(x, engine, ...)
x
}
# ------------------------------------------------------------------------------
check_args.mars <- function(object) {
args <- lapply(object$args, rlang::eval_tidy)
if (is.numeric(args$prod_degree) && args$prod_degree < 0)
rlang::abort("`prod_degree` should be >= 1.")
if (is.numeric(args$num_terms) && args$num_terms < 0)
rlang::abort("`num_terms` should be >= 1.")
if (!is_varying(args$prune_method) &&
!is.null(args$prune_method) &&
!is.character(args$prune_method))
rlang::abort("`prune_method` should be a single string value.")
invisible(object)
}
# ------------------------------------------------------------------------------
earth_submodel_pred <- function(object, new_data, terms = 2:3, ...) {
load_libs(object, quiet = TRUE, attach = TRUE)
map_dfr(terms, earth_reg_updater, object = object, newdata = new_data, ...)
}
earth_reg_updater <- function(num, object, new_data, ...) {
object <- update(object, nprune = num)
pred <- predict(object, new_data, ...)
if (ncol(pred) == 1) {
res <- tibble::tibble(.pred = pred[, 1], nprune = num)
} else {
names(res) <- paste0(".pred_", names(res))
res <- tibble::as_tibble(res)
res$nprune <- num
}
res
}
# earth helpers ----------------------------------------------------------------
#' @rdname multi_predict
#' @param num_terms An integer vector for the number of MARS terms to retain.
#' @export
multi_predict._earth <-
function(object, new_data, type = NULL, num_terms = NULL, ...) {
if (any(names(enquos(...)) == "newdata"))
rlang::abort("Did you mean to use `new_data` instead of `newdata`?")
load_libs(object, quiet = TRUE, attach = TRUE)
if (is.null(num_terms))
num_terms <- object$fit$selected.terms[-1]
num_terms <- sort(num_terms)
# update.earth uses the values in the call so evaluate them if
# they are quosures
call_names <- names(object$fit$call)
call_names <- call_names[!(call_names %in% c("", "x", "y"))]
for (i in call_names) {
if (is_quosure(object$fit$call[[i]]))
object$fit$call[[i]] <- eval_tidy(object$fit$call[[i]])
}
msg <-
paste("Please use `keepxy = TRUE` as an option to enable submodel",
"predictions with `earth`.")
if (any(names(object$fit$call) == "keepxy")) {
if (!isTRUE(object$fit$call$keepxy))
rlang::abort(msg)
} else {
rlang::abort(msg)
}
if (is.null(type)) {
if (object$spec$mode == "classification")
type <- "class"
else
type <- "numeric"
}
res <-
map_df(num_terms, earth_by_terms, object = object,
new_data = new_data, type = type, ...)
res <- arrange(res, .row, num_terms)
res <- split(res[, -1], res$.row)
names(res) <- NULL
tibble(.pred = res)
}
earth_by_terms <- function(num_terms, object, new_data, type, ...) {
object$fit <- update(object$fit, nprune = num_terms)
pred <- predict(object, new_data = new_data, type = type)
nms <- names(pred)
pred[["num_terms"]] <- num_terms
pred[[".row"]] <- 1:nrow(new_data)
pred[, c(".row", "num_terms", nms)]
}