-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathmiss-x-summary.R
More file actions
240 lines (218 loc) · 7.03 KB
/
Copy pathmiss-x-summary.R
File metadata and controls
240 lines (218 loc) · 7.03 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
#' Summarise the missingness in each variable
#'
#' Provide a summary for each variable of the number, percent missings, and
#' cumulative sum of missings of the order of the variables. By default,
#' it orders by the most missings in each variable.
#'
#' @param data a data.frame
#' @param order a logical indicating whether to order the result by `n_miss`.
#' Defaults to TRUE. If FALSE, order of variables is the order input.
#' @param add_cumsum logical indicating whether or not to add the cumulative
#' sum of missings to the data. This can be useful when exploring patterns
#' of nonresponse. These are calculated as the cumulative sum of the missings
#' in the variables as they are first presented to the function.
#' @param digits how many digits to display in `pct_miss` column. Useful when
#' you are working with small amounts of missing data.
#' @param ... extra arguments
#'
#' @note `n_miss_cumsum` is calculated as the cumulative sum of missings in the
#' variables in the order that they are given in the data when entering
#' the function
#'
#' @return a tibble of the percent of missing data in each variable
#'
#' @seealso [pct_miss_case()] [prop_miss_case()] [pct_miss_var()] [prop_miss_var()] [pct_complete_case()] [prop_complete_case()] [pct_complete_var()] [prop_complete_var()] [miss_prop_summary()] [miss_case_summary()] [miss_case_table()] [miss_summary()] [miss_var_prop()] [miss_var_run()] [miss_var_span()] [miss_var_summary()] [miss_var_table()] [n_complete()] [n_complete_row()] [n_miss()] [n_miss_row()] [pct_complete()] [pct_miss()] [prop_complete()] [prop_complete_row()] [prop_miss()]
#'
#' @export
#'
#' @examples
#'
#' miss_var_summary(airquality)
#' miss_var_summary(oceanbuoys, order = TRUE)
#'
#' \dontrun{
#' # works with group_by from dplyr
#' library(dplyr)
#' airquality %>%
#' group_by(Month) %>%
#' miss_var_summary()
#' }
#' @export
miss_var_summary <- function(
data,
order = FALSE,
add_cumsum = FALSE,
digits,
...
) {
test_if_null(data)
test_if_dataframe(data)
UseMethod("miss_var_summary")
}
#' @export
miss_var_summary.default <- function(
data,
order = TRUE,
add_cumsum = FALSE,
digits = NULL,
...
) {
col_n_miss <- colSums(is.na(data))
col_pct_miss <- as.numeric(colMeans(is.na(data)) * 100)
res <- tibble::tibble(
variable = names(col_n_miss),
n_miss = as.integer(col_n_miss),
pct_miss = tibble::num(
x = col_pct_miss,
# control how rounding is presented
digits = digits
)
)
if (add_cumsum) {
res <- res %>% dplyr::mutate(n_miss_cumsum = cumsum(n_miss))
}
if (order) {
return(dplyr::arrange(res, -n_miss))
}
return(res)
}
#' @export
miss_var_summary.grouped_df <- function(
data,
order = TRUE,
add_cumsum = FALSE,
digits = NULL,
...
) {
group_by_fun(
data,
.fun = miss_var_summary,
order = order,
add_cumsum = add_cumsum,
digits = digits
)
}
#' Summarise the missingness in each case
#'
#' Provide a summary for each case in the data of the number, percent missings,
#' and cumulative sum of missings of the order of the variables. By default,
#' it orders by the most missings in each variable.
#'
#' @param data a data.frame
#' @param order a logical indicating whether or not to order the result by
#' n_miss. Defaults to TRUE. If FALSE, order of cases is the order input.
#' @param ... extra arguments
#' @param add_cumsum logical indicating whether or not to add the cumulative
#' sum of missings to the data. This can be useful when exploring patterns
#' of nonresponse. These are calculated as the cumulative sum of the missings
#' in the variables as they are first presented to the function.
#'
#' @return a tibble of the percent of missing data in each case.
#'
#' @seealso [pct_miss_case()] [prop_miss_case()] [pct_miss_var()] [prop_miss_var()] [pct_complete_case()] [prop_complete_case()] [pct_complete_var()] [prop_complete_var()] [miss_prop_summary()] [miss_case_summary()] [miss_case_table()] [miss_summary()] [miss_var_prop()] [miss_var_run()] [miss_var_span()] [miss_var_summary()] [miss_var_table()] [n_complete()] [n_complete_row()] [n_miss()] [n_miss_row()] [pct_complete()] [pct_miss()] [prop_complete()] [prop_complete_row()] [prop_miss()]
#'
#' @export
#'
#' @examples
#'
#' miss_case_summary(airquality)
#'
#' \dontrun{
#' # works with group_by from dplyr
#' library(dplyr)
#' airquality %>%
#' group_by(Month) %>%
#' miss_case_summary()
#'}
#'
miss_case_summary <- function(data, order = TRUE, add_cumsum = FALSE, ...) {
test_if_null(data)
test_if_dataframe(data)
UseMethod("miss_case_summary")
}
#' @export
miss_case_summary.default <- function(
data,
order = TRUE,
add_cumsum = FALSE,
...
) {
res <- data
res[["pct_miss"]] <- rowMeans(is.na(res)) * 100
res[["n_miss"]] <- as.integer(rowSums(is.na(res)))
res[["case"]] <- seq_len(nrow(res))
if (add_cumsum) {
res[["n_miss_cumsum"]] <- cumsum(res[["n_miss"]])
res <- dplyr::as_tibble(res)
res <- dplyr::select(res, case, n_miss, pct_miss, n_miss_cumsum)
}
if (!add_cumsum) {
res <- dplyr::as_tibble(res)
res <- dplyr::select(res, case, n_miss, pct_miss)
}
if (order) {
return(dplyr::arrange(res, -n_miss))
}
if (!order) {
return(res)
}
}
#' @export
miss_case_summary.grouped_df <- function(
data,
order = TRUE,
add_cumsum = FALSE,
...
) {
group_by_fun(
data,
.fun = miss_case_summary,
order = order,
add_cumsum = add_cumsum
)
}
#' Collate summary measures from naniar into one tibble
#'
#' `miss_summary` performs all of the missing data helper summaries and puts
#' them into lists within a tibble
#'
#' @param data a dataframe
#' @param order whether or not to order the result by n_miss
#'
#' @return a tibble of missing data summaries
#'
#' @seealso [pct_miss_case()] [prop_miss_case()] [pct_miss_var()] [prop_miss_var()] [pct_complete_case()] [prop_complete_case()] [pct_complete_var()] [prop_complete_var()] [miss_prop_summary()] [miss_case_summary()] [miss_case_table()] [miss_summary()] [miss_var_prop()] [miss_var_run()] [miss_var_span()] [miss_var_summary()] [miss_var_table()] [n_complete()] [n_complete_row()] [n_miss()] [n_miss_row()] [pct_complete()] [pct_miss()] [prop_complete()] [prop_complete_row()] [prop_miss()]
#'
#' @export
#'
#' @examples
#'
#' s_miss <- miss_summary(airquality)
#' s_miss$miss_df_prop
#' s_miss$miss_case_table
#' s_miss$miss_var_summary
#' # etc, etc, etc.
#'
#' \dontrun{
#' library(dplyr)
#' s_miss_group <- group_by(airquality, Month) %>% miss_summary()
#' s_miss_group$miss_df_prop
#' s_miss_group$miss_case_table
#' # etc, etc, etc.
#' }
#'
miss_summary <- function(data, order = TRUE) {
test_if_null(data)
test_if_dataframe(data)
return(
tibble::tibble(
miss_df_prop = prop_miss(data),
miss_var_prop = prop_miss_var(data),
miss_case_prop = prop_miss_case(data),
miss_case_table = list(miss_case_table(data)),
miss_var_table = list(miss_var_table(data)),
miss_var_summary = list(miss_var_summary(data, order)),
miss_case_summary = list(miss_case_summary(data, order))
)
)
}