forked from tidymodels/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_missing.R
155 lines (145 loc) · 4.11 KB
/
filter_missing.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
#' Missing Value Column Filter
#'
#' `step_filter_missing` creates a *specification* of a recipe
#' step that will potentially remove variables that have too many missing
#' values.
#'
#' @inheritParams step_center
#' @param threshold A value for the threshold of missing values in column. The
#' step will remove the columns where the proportion of missing values
#' exceeds the threshold.
#' @param removals A character string that contains the names of
#' columns that should be removed. These values are not determined
#' until [prep()] is called.
#' @template step-return
#' @template filter-steps
#' @family variable filter steps
#' @export
#'
#' @details This step will remove variables if the proportion of missing
#' values exceeds the `threshold`.
#'
#' All variables with missing values will be removed for `threshold = 0`.
#'
#' # Tidying
#'
#' When you [`tidy()`][tidy.recipe()] this step, a tibble with column
#' `terms` (the columns that will be removed) is returned.
#'
#' @template case-weights-unsupervised
#'
#' @examplesIf rlang::is_installed("modeldata")
#' data(credit_data, package = "modeldata")
#'
#' rec <- recipe(Status ~ ., data = credit_data) %>%
#' step_filter_missing(all_predictors(), threshold = 0)
#'
#' filter_obj <- prep(rec)
#'
#' filtered_te <- bake(filter_obj, new_data = NULL)
#'
#' tidy(rec, number = 1)
#' tidy(filter_obj, number = 1)
step_filter_missing <- function(recipe,
...,
role = NA,
trained = FALSE,
threshold = 0.1,
removals = NULL,
skip = FALSE,
id = rand_id("filter_missing")) {
add_step(
recipe,
step_filter_missing_new(
terms = enquos(...),
role = role,
trained = trained,
threshold = threshold,
removals = removals,
skip = skip,
id = id,
case_weights = NULL
)
)
}
step_filter_missing_new <-
function(terms, role, trained, threshold, removals, skip, id, case_weights) {
step(
subclass = "filter_missing",
terms = terms,
role = role,
trained = trained,
threshold = threshold,
removals = removals,
skip = skip,
id = id,
case_weights = case_weights
)
}
#' @export
prep.step_filter_missing <- function(x, training, info = NULL, ...) {
col_names <- recipes_eval_select(x$terms, training, info)
wts <- get_case_weights(info, training)
were_weights_used <- are_weights_used(wts, unsupervised = TRUE)
if (isFALSE(were_weights_used)) {
wts <- NULL
}
if (length(col_names) > 1) {
filter <- filter_missing_fun(
x = training[, col_names],
threshold = x$threshold,
wts = wts
)
} else {
filter <- character(0)
}
step_filter_missing_new(
terms = x$terms,
role = x$role,
trained = TRUE,
threshold = x$threshold,
removals = filter,
skip = x$skip,
id = x$id,
case_weights = were_weights_used
)
}
#' @export
bake.step_filter_missing <- function(object, new_data, ...) {
if (length(object$removals) > 0) {
new_data <- new_data[, !(colnames(new_data) %in% object$removals)]
}
new_data
}
print.step_filter_missing <-
function(x, width = max(20, options()$width - 36), ...) {
if (x$trained) {
title <- "Missing value column filter removed "
} else {
title <- "Missing value column filter on "
}
print_step(x$removals, x$terms, x$trained, title, width,
case_weights = x$case_weights)
invisible(x)
}
filter_missing_fun <- function(x, threshold, wts) {
x_na <- purrr::map_dfc(x, is.na)
missing <- averages(x_na, wts = wts)
removal_ind <- which(missing > threshold)
names(x)[removal_ind]
}
#' @rdname tidy.recipe
#' @export
tidy.step_filter_missing <- tidy_filter
#' @export
tunable.step_filter_missing <- function(x, ...) {
tibble::tibble(
name = "threshold",
call_info = list(
list(pkg = "dials", fun = "threshold", range = c(0.05, 1.00))
),
source = "recipe",
component = "step_filter_missing",
component_id = x$id
)
}