Repository navigation
Expand file tree
/
Copy pathshadows.R
More file actions
306 lines (275 loc) · 8.08 KB
/
Copy pathshadows.R
File metadata and controls
306 lines (275 loc) · 8.08 KB
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
#' Create shadows
#'
#' Return a tibble in shadow matrix form, where the variables are the same but
#' have a suffix _NA attached to distinguish them.
#'
#' Representing missing data structure is achieved using the shadow matrix,
#' introduced in [Swayne and Buja](https://www.researchgate.net/publication/2758672_Missing_Data_in_Interactive_High-Dimensional_Data_Visualization). The shadow
#' matrix is the same dimension as the data, and consists of binary indicators
#' of missingness of data values, where missing is represented as "NA", and not
#' missing is represented as "!NA". Although these may be represented as 1 and
#' 0, respectively.
#'
#' @param data dataframe
#' @param ... selected variables to use
#'
#' @return appended shadow with column names
#' @examples
#'
#' as_shadow(airquality)
#' @export
as_shadow <- function(data, ...) {
test_if_null(data)
test_if_dataframe(data)
data_shadow <- purrr::map_dfc(data, shade)
names(data_shadow) <- paste0(names(data), "_NA")
return(data_shadow)
}
#' Bind a shadow dataframe to original data
#'
#' Binding a shadow matrix to a regular dataframe helps visualise and work with
#' missing data.
#'
#' @param data a dataframe
#' @param only_miss logical - if FALSE (default) it will bind a dataframe with
#' all of the variables duplicated with their shadow. Setting this to TRUE
#' will bind variables only those variables that contain missing values.
#' See the examples for more details.
#' @param ... extra options to pass to [recode_shadow()] - a work in progress.
#'
#' @return data with the added variable shifted and the suffix `_NA`
#' @export
#'
#' @examples
#'
#' bind_shadow(airquality)
#'
#' # bind only the variables that contain missing values
#' bind_shadow(airquality, only_miss = TRUE)
#'
#' aq_shadow <- bind_shadow(airquality)
#'
#' \dontrun{
#' # explore missing data visually
#' library(ggplot2)
#'
#' # using the bounded shadow to visualise Ozone according to whether Solar
#' # Radiation is missing or not.
#'
#' ggplot(data = aq_shadow,
#' aes(x = Ozone)) +
#' geom_histogram() +
#' facet_wrap(~Solar.R_NA,
#' ncol = 1)
#' }
#'
bind_shadow <- function(data, only_miss = FALSE, ...) {
# If you want only the missing values to be added
if (only_miss) {
# I want to only select columns that contain a missing value.
miss_vars <- rlang::syms(miss_var_which(data))
shadow_vars <- dplyr::as_tibble(as_shadow(dplyr::select(
data,
!!!miss_vars
)))
data <- tibble::as_tibble(data)
shadow_data <- dplyr::bind_cols(data, shadow_vars)
return(shadow_data)
}
if (!only_miss) {
data_shadow <- tibble::as_tibble(as_shadow(data))
data <- tibble::as_tibble(data)
shadow_data <- dplyr::bind_cols(data, data_shadow)
if (!missing(...)) {
shadow_data <- shadow_data %>% recode_shadow(...)
}
return(shadow_data)
}
}
#' Unbind (remove) shadow from data, and vice versa
#'
#' Remove the shadow variables (which end in `_NA`) from the data, or vice versa.
#' This will also remove the `nabular` class from the data.
#'
#' @param data data.frame containing shadow columns (created by [bind_shadow()])
#'
#' @return `data.frame` without shadow columns if using [unbind_shadow()], or
#' without the original data, if using [unbind_data()].
#' @name unbinders
#'
#' @export
#'
#' @examples
#'
#' # bind shadow columns
#' aq_sh <- bind_shadow(airquality)
#'
#' # print data
#' aq_sh
#'
#' # remove shadow columns
#' unbind_shadow(aq_sh)
#'
#' # remove data
#' unbind_data(aq_sh)
#'
#' # errors when you don't use data with shadows
#' \dontrun{
#' unbind_data(airquality)
#' unbind_shadow(airquality)
#' }
#'
unbind_shadow <- function(data) {
test_if_any_shade(data)
temp <- dplyr::select(data, -dplyr::ends_with("_NA"))
return(temp)
}
#' @rdname unbinders
#' @export
unbind_data <- function(data) {
test_if_any_shade(data)
dplyr::select(data, dplyr::ends_with("_NA"))
}
#' Long form representation of a shadow matrix
#'
#' `gather_shadow` is a long-form representation of binding the shadow matrix to
#' your data, producing variables named `case`, `variable`, and `missing`, where
#' `missing` contains the missing value representation.
#'
#' @param data a dataframe
#'
#' @return dataframe in long, format, containing information about the missings
#'
#' @export
#'
#' @examples
#'
#' gather_shadow(airquality)
#'
gather_shadow <- function(data) {
as_shadow(data) %>%
dplyr::mutate(rows = seq_len(nrow(.))) %>%
tidyr::pivot_longer(
cols = -rows,
names_to = "variable",
values_to = "missing"
) %>%
dplyr::rename(case = rows)
}
#' Reshape shadow data into a long format
#'
#' Once data is in `nabular` form, where the shadow is bound to the data, it
#' can be useful to reshape it into a long format with the shadow columns
#' in a separate grouping - so you have `variable`, `value`, and
#' `variable_NA` and `value_NA`.
#'
#' @param shadow_data a data.frame
#' @param ... bare name of variables that you want to focus on
#' @param fn_value_transform function to transform the "value" column. Default
#' is NULL, which defaults to `as.character`. Be aware that `as.numeric` may
#' fail for some instances if it cannot coerce the value into numeric. See
#' the examples.
#' @param only_main_vars logical - do you want to filter down to main variables?
#'
#' @return data in long format, with columns `variable`, `value`, `variable_NA`, and `value_NA`.
#' @export
#'
#' @examples
#'
#' aq_shadow <- nabular(airquality)
#'
#' shadow_long(aq_shadow)
#'
#' # then filter only on Ozone
#' shadow_long(aq_shadow, Ozone)
#'
#' shadow_long(aq_shadow, Ozone, Solar.R)
#'
#' # ensure `value` is numeric
#' shadow_long(aq_shadow, fn_value_transform = as.numeric)
#' shadow_long(aq_shadow, Ozone, Solar.R, fn_value_transform = as.numeric)
#'
#'
shadow_long <- function(
shadow_data,
...,
fn_value_transform = NULL,
only_main_vars = TRUE
) {
test_if_null(shadow_data)
test_if_any_shade(shadow_data)
if (is.null(fn_value_transform)) {
fn_value_transform <- as.character
}
shadow_data_names <- names(which_are_shade(shadow_data))
longer_one <- tidyr::pivot_longer(
shadow_data,
cols = -dplyr::one_of(shadow_data_names),
names_to = "variable",
values_to = "value",
values_transform = list(value = fn_value_transform)
)
longer_one_shade_names <- names(which_are_shade(longer_one))
gathered_df <- tidyr::pivot_longer(
longer_one,
cols = dplyr::one_of(longer_one_shade_names),
names_to = "variable_NA",
values_to = "value_NA"
)
if (only_main_vars) {
gathered_df <- dplyr::filter(
gathered_df,
variable_NA == paste0(variable, "_NA")
)
}
if (!missing(...)) {
df_vars <- purrr::map_chr(ensyms(...), as_string)
gathered_df <- gathered_df %>%
dplyr::filter(variable %in% df_vars)
}
return(gathered_df)
}
#' Convert data into shadow format for doing an upset plot
#'
#' Upset plots are a way of visualising common sets, this function transforms
#' the data into a format that feeds directly into an upset plot
#'
#' @param data a data.frame
#'
#' @return a data.frame
#'
#' @examples
#'
#' \dontrun{
#'
#' library(UpSetR)
#' airquality %>%
#' as_shadow_upset() %>%
#' upset()
#' }
#'
#' @export
as_shadow_upset <- function(data) {
if (n_var_miss(data) <= 1) {
if (n_var_miss(data) == 1) {
glu_st <- glue::glue(
"upset plots for missing data requre at least two \\
variables to have missing data, only one variable, \\
'{miss_var_which(data)}' has missing values."
)
}
if (n_var_miss(data) == 0) {
glu_st <- glue::glue(
"upset plots for missing data requre at least two \\
variables to have missing data, there are no missing \\
values in your data! This is probably a good thing."
)
}
rlang::abort(message = glu_st)
}
test_if_null(data)
test_if_dataframe(data)
data_shadow <- as.data.frame(is.na(data) * 1)
names(data_shadow) <- paste0(names(data), "_NA")
dplyr::mutate_if(data_shadow, is.numeric, as.integer)
}