-
Notifications
You must be signed in to change notification settings - Fork 53
/
impute_below.R
403 lines (347 loc) · 11.1 KB
/
impute_below.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#' Impute data with values shifted 10 percent below range.
#'
#' It can be useful in exploratory graphics to impute data outside the range of
#' the data. `impute_below` imputes variables with missings to have values
#' 10 percent below the range for numeric values, plus some jittered noise,
#' to separate repeated values, so that missing values can be visualised
#' along with the rest of the data. For character or factor
#' values, it adds a new string or label.
#'
#' @param x a variable of interest to shift
#' @param ... extra arguments to pass
#'
#' @seealso [add_shadow_shift()] [cast_shadow_shift()] [cast_shadow_shift_label()]
#'
#' @export
#' @examples
#'library(dplyr)
#'vec <- rnorm(10)
#'
#'vec[sample(1:10, 3)] <- NA
#'
#'impute_below(vec)
#'impute_below(vec, prop_below = 0.25)
#'impute_below(vec,
#' prop_below = 0.25,
#' jitter = 0.2)
#'
#'dat <- tibble(
#' num = rnorm(10),
#' int = as.integer(rpois(10, 5)),
#' fct = factor(LETTERS[1:10])
#') %>%
#' mutate(
#' across(
#' everything(),
#' \(x) set_prop_miss(x, prop = 0.25)
#' )
#' )
#'
#'dat
#'
#'dat %>%
#' nabular() %>%
#' mutate(
#' num = impute_below(num),
#' int = impute_below(int),
#' fct = impute_below(fct),
#' )
#'
#'dat %>%
#' nabular() %>%
#' mutate(
#' across(
#' where(is.numeric),
#' impute_below
#' )
#' )
#'
#'dat %>%
#' nabular() %>%
#' mutate(
#' across(
#' c("num", "int"),
#' impute_below
#' )
#' )
#'
#'
impute_below <- function(x, ...) UseMethod("impute_below")
#' @export
impute_below.NULL <- function(x, ...) NULL
#' @export
impute_below.default <- function(x, ...){
cli::cli_abort(
c(
"{.fun impute_below} does not know how to deal with data of class {.cls {class_glue(x)}}",
"Check if your input is more than length one, and that you are using the right function. Perhaps you meant to apply this to many variables in a data frame? See the examples dor details on doing this with {.fun across}"
)
)
}
# function to perform the shifting/imputing, which is used by later function
shift_values <- function(x,
xmin,
prop_below,
seed_shift,
jitter) {
# provide the amount of shift - default is 0.1
x_shift <- xmin - xmin * prop_below
# set the seed here
set.seed(seed_shift)
x_jitter <- (stats::runif(length(x)) - 0.50) * x_shift * jitter
# overwrite x
x <- ifelse(is.na(x),
yes = x_shift + x_jitter,
no = x)
return(x)
}
#' Impute numeric values below a range for graphical exploration
#'
#' @param x a variable of interest to shift
#' @param prop_below the degree to shift the values. default is
#' @param jitter the amount of jitter to add. default is 0.05
#' @param seed_shift a random seed to set, if you like
#' @param ... extra arguments to pass
#' @export
impute_below.numeric <- function(x,
prop_below = 0.1,
jitter = 0.05,
seed_shift = 2017-7-1-1850,
...){
# add an exception for cases with infinite values
if (any(is.infinite(x))) {
# use the minimum for the non infinite values
xmin <- min(x[!is.infinite(x)], na.rm = TRUE)
shifted_values <- shift_values(x,
xmin,
prop_below,
seed_shift,
jitter)
return(shifted_values)
}
# add an exception for when length x == 1 and variance is zero
if (n_complete(x) == 1 | stats::var(x, na.rm = TRUE) == 0) {
xmin <- min(x, na.rm = TRUE)
shifted_values <- shift_values(x,
xmin,
prop_below,
seed_shift,
jitter)
return(shifted_values)
# else, when there is more than 1 complete value
}
range_dist <- function(x) diff(range(x, na.rm = TRUE))
xrange <- range_dist(x)
xmin <- min(x, na.rm = TRUE)
# create the "jitter" to be added around the points.
set.seed(seed_shift)
x_jitter <- (stats::runif(length(x)) - 0.5) * xrange * jitter
x_shift <- xmin - xrange * prop_below
ifelse(is.na(x),
# add the jitter around the those values that are missing
yes = x_shift + x_jitter,
no = x)
} # close function
#' @export
impute_below.POSIXct <- function(x,
prop_below = 0.1,
jitter = 0.05,
seed_shift = 2017-7-1-1850,
...){
dates <- as.numeric(x)
imputed_vals <- impute_below(x = dates,
prop_below = prop_below,
jitter = jitter,
seed_shift = seed_shift,
...)
as.POSIXct(imputed_vals)
}
#' @export
impute_below.POSIXlt <- function(x,
prop_below = 0.1,
jitter = 0.05,
seed_shift = 2017-7-1-1850,
...){
dates <- as.numeric(x)
imputed_vals <- impute_below(x = dates,
prop_below = prop_below,
jitter = jitter,
seed_shift = seed_shift,
...)
as.POSIXlt(imputed_vals)
}
#' @export
impute_below.Date <- function(x,
prop_below = 0.1,
jitter = 0.05,
seed_shift = 2017-7-1-1850,
...){
dates <- as.numeric(x)
imputed_vals <- impute_below(x = dates,
prop_below = prop_below,
jitter = jitter,
seed_shift = seed_shift,
...)
as.Date(imputed_vals)
}
#' @export
impute_below.factor <- function(x, ...){
forcats::fct_na_value_to_level(x, level = "missing")
}
#' @export
impute_below.character <- function(x, ...){
dplyr::if_else(is.na(x),
true = "missing",
false = x)
}
#' Impute data with values shifted 10 percent below range.
#'
#' It can be useful in exploratory graphics to impute data outside the range of
#' the data. `impute_below_all` imputes all variables with missings to have
#' values 10\% below the range for numeric values, and for character or factor
#' values adds a new string or label.
#'
#' `r lifecycle::badge('superseded')`
#'
#' @param .tbl a data.frame
#' @param prop_below the degree to shift the values. default is
#' @param jitter the amount of jitter to add. default is 0.05
#' @param ... additional arguments
#'
#' @return an dataset with values imputed
#' @export
#'
#' @examples
#'
#' # you can impute data like so:
#' airquality %>%
#' impute_below_all()
#'
#' # However, this does not show you WHERE the missing values are.
#' # to keep track of them, you want to use `bind_shadow()` first.
#'
#' airquality %>%
#' bind_shadow() %>%
#' impute_below_all()
#'
#' # This identifies where the missing values are located, which means you
#' # can do things like this:
#'
#' \dontrun{
#' library(ggplot2)
#' airquality %>%
#' bind_shadow() %>%
#' impute_below_all() %>%
#' # identify where there are missings across rows.
#' add_label_shadow() %>%
#' ggplot(aes(x = Ozone,
#' y = Solar.R,
#' colour = any_missing)) +
#' geom_point()
#' # Note that this ^^ is a long version of `geom_miss_point()`.
#' }
#'
impute_below_all <- function(.tbl,
prop_below = 0.1,
jitter = 0.05,
...){
lifecycle::signal_stage("superseded", "impute_below_all()")
test_if_dataframe(.tbl)
test_if_null(.tbl)
dplyr::mutate_all(.tbl = .tbl,
.funs = impute_below,
prop_below = prop_below,
jitter = jitter)
}
#' Scoped variants of `impute_below`
#'
#' `impute_below` imputes missing values to a set percentage below the range
#' of the data. To impute many variables at once, we recommend that you use the
#' `across` function workflow, shown in the examples for [impute_below()].
#' `impute_below_all` operates on all variables. To only impute variables
#' that satisfy a specific condition, use the scoped variants,
#' `impute_below_at`, and `impute_below_if`. To use `_at` effectively,
#' you must know that `_at`` affects variables selected with a character
#' vector, or with `vars()`.
#'
#' `r lifecycle::badge('superseded')`
#'
#' @param .tbl a data.frame
#' @param .vars variables to impute
#' @param prop_below the degree to shift the values. default is
#' @param jitter the amount of jitter to add. default is 0.05
#' @param ... extra arguments
#'
#' @return an dataset with values imputed
#' @export
#'
#' @examples
#' # select variables starting with a particular string.
#' impute_below_at(airquality,
#' .vars = c("Ozone", "Solar.R"))
#'
#' impute_below_at(airquality, .vars = 1:2)
#'
#' \dontrun{
#' library(dplyr)
#' impute_below_at(airquality,
#' .vars = vars(Ozone))
#'
#' library(ggplot2)
#' airquality %>%
#' bind_shadow() %>%
#' impute_below_at(vars(Ozone, Solar.R)) %>%
#' add_label_shadow() %>%
#' ggplot(aes(x = Ozone,
#' y = Solar.R,
#' colour = any_missing)) +
#' geom_point()
#' }
#'
impute_below_at <- function(.tbl,
.vars,
prop_below = 0.1,
jitter = 0.05,
...){
lifecycle::signal_stage("superseded", "impute_below_at()")
test_if_dataframe(.tbl)
test_if_null(.tbl)
dplyr::mutate_at(.tbl = .tbl,
.vars = .vars,
.funs = impute_below,
prop_below = prop_below,
jitter = jitter)
}
#' Scoped variants of `impute_below`
#'
#' `impute_below` operates on all variables. To only impute variables
#' that satisfy a specific condition, use the scoped variants,
#' `impute_below_at`, and `impute_below_if`.
#'
#' @param .tbl data.frame
#' @param .predicate A predicate function (such as is.numeric)
#' @param prop_below the degree to shift the values. default is
#' @param jitter the amount of jitter to add. default is 0.05
#' @param ... extra arguments
#'
#' @return an dataset with values imputed
#' @export
#' @examples
#'
#' airquality %>%
#' impute_below_if(.predicate = is.numeric)
#'
impute_below_if <- function(.tbl,
.predicate,
prop_below = 0.1,
jitter = 0.05,
...){
lifecycle::signal_stage("superseded", "impute_below_if()")
test_if_dataframe(.tbl)
test_if_null(.tbl)
dplyr::mutate_if(.tbl = .tbl,
.predicate = .predicate,
.funs = impute_below,
prop_below = prop_below,
jitter = jitter)
}