-
Notifications
You must be signed in to change notification settings - Fork 13
/
ipred.R
144 lines (119 loc) · 2.69 KB
/
ipred.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
#' Axing a bagged tree.
#'
#' `*_bagg` objects are created from the \pkg{ipred} package, which
#' is used for bagging classification, regression and survival trees.
#'
#' @inheritParams butcher
#'
#' @return Axed `*_bagg` object.
#'
#' @examplesIf rlang::is_installed(c("ipred", "rpart"))
#' library(ipred)
#'
#' fit_mod <- function() {
#' boop <- runif(1e6)
#' bagging(y ~ x, data.frame(y = rnorm(1e4), x = rnorm(1e4)))
#' }
#'
#' mod_fit <- fit_mod()
#' mod_res <- butcher(mod_fit)
#'
#' weigh(mod_fit)
#' weigh(mod_res)
#'
#' @name axe-ipred
#' @aliases axe-regbagg axe-classbagg axe-survbagg
NULL
#' @rdname axe-ipred
#' @export
axe_call.regbagg <- function(x, verbose = FALSE, ...) {
old <- x
for (i in seq_along(x$mtrees)) {
x$mtrees[[i]]$btree <- axe_call(x$mtrees[[i]]$btree)
}
x <- exchange(x, "call", call("dummy_call"))
add_butcher_attributes(
x,
old,
disabled = c("print()", "summary()"),
verbose = verbose
)
}
#' @rdname axe-ipred
#' @export
axe_call.classbagg <- axe_call.regbagg
#' @rdname axe-ipred
#' @export
axe_call.survbagg <- axe_call.regbagg
#' @rdname axe-ipred
#' @export
axe_ctrl.regbagg <- function(x, verbose = FALSE, ...) {
old <- x
for (i in seq_along(x$mtrees)) {
x$mtrees[[i]]$btree <- axe_ctrl(x$mtrees[[i]]$btree)
}
add_butcher_attributes(
x,
old,
verbose = verbose
)
}
#' @rdname axe-ipred
#' @export
axe_ctrl.classbagg <- axe_ctrl.regbagg
#' @rdname axe-ipred
#' @export
axe_ctrl.survbagg <- axe_ctrl.regbagg
#' @rdname axe-ipred
#' @export
axe_data.regbagg <- function(x, verbose = FALSE, ...) {
old <- x
for (i in seq_along(x$mtrees)) {
x$mtrees[[i]]$btree <- axe_data(x$mtrees[[i]]$btree)
}
x$y <- numeric(0)
x$X <- data.frame(NA)
add_butcher_attributes(
x,
old,
verbose = verbose
)
}
# note that the class method doesn't wipe the `X` and `y` slots
# as does the regression method. predict.classbagg taps into this data.
#' @rdname axe-ipred
#' @export
axe_data.classbagg <- function(x, verbose = FALSE, ...) {
old <- x
for (i in seq_along(x$mtrees)) {
x$mtrees[[i]]$btree <- axe_data(x$mtrees[[i]]$btree)
}
add_butcher_attributes(
x,
old,
verbose = verbose
)
}
#' @rdname axe-ipred
#' @export
axe_data.survbagg <- axe_data.classbagg
#' @rdname axe-ipred
#' @export
axe_env.regbagg <- function(x, verbose = FALSE, ...) {
old <- x
for (i in seq_along(x$mtrees)) {
x$mtrees[[i]]$btree <- axe_env(x$mtrees[[i]]$btree)
}
x$terms <- axe_env(x$terms, ...)
add_butcher_attributes(
x,
old,
verbose = verbose
)
}
#' @rdname axe-ipred
#' @export
axe_env.classbagg <- axe_env.regbagg
#' @rdname axe-ipred
#' @export
axe_env.survbagg <- axe_env.regbagg