/
categorical.R
359 lines (310 loc) · 10.3 KB
/
categorical.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
#' Tabulate frequencies
#'
#' Tabulates frequencies for one or more categorical variable, including relative,
#' and cumulative frequencies.
#'
#' @param data a [tibble][tibble::tibble-package] or a [tdcmm] model
#' @param ... Variables to tabulate
#'
#' @return a [tdcmm] model
#'
#' @examples
#' WoJ %>% tab_frequencies(employment)
#' WoJ %>% tab_frequencies(employment, country)
#'
#' @family categorical
#'
#' @export
tab_frequencies <- function(data, ...) {
vars <- grab_vars(data, enquos(...))
vars_str <- purrr::map_chr(vars, as_label)
grouping <- dplyr::groups(data)
d <- data %>%
dplyr::group_by(..., !!!grouping) %>%
dplyr::summarise(n = dplyr::n()) %>%
dplyr::group_by(!!!grouping) %>%
dplyr::mutate(percent = n / sum(n)) %>%
dplyr::arrange(!!!grouping)
out <- d %>%
dplyr::bind_cols(d %>%
dplyr::select(!!!grouping,
cum_n = "n",
cum_percent = "percent") %>%
dplyr::mutate_at(dplyr::vars(-dplyr::group_cols()), cumsum) %>%
dplyr::ungroup() %>%
dplyr::select("cum_n", "cum_percent")
)
return(new_tdcmm_ctgrcl(new_tdcmm(out,
func = "tab_frequencies",
data = data,
params = list(vars = vars_str))))
}
#' Crosstab variables
#'
#' Computes contingency table for one independent (column) variable and one or
#' more dependent (row) variables.
#'
#' @param data a [tibble][tibble::tibble-package] or a [tdcmm] model
#' @param col_var Independent (column) variable.
#' @param ... Dependent (row) variables.
#' @param add_total Logical indicating whether a 'Total' column should be
#' computed. Defaults to `FALSE`.
#' @param percentages Logical indicating whether to output column-wise
#' percentages instead of absolute values. Defaults to `FALSE`.
#' @param chi_square Logical indicating whether a Chi-square test should be computed.
#' Test results will be reported via message(). Defaults to `FALSE`.
#'
#' @return a [tdcmm] model
#'
#' @examples
#' WoJ %>% crosstab(reach, employment)
#' WoJ %>% crosstab(reach, employment, add_total = TRUE, percentages = TRUE, chi_square = TRUE)
#'
#' @family categorical
#'
#' @export
crosstab <- function(data, col_var, ..., add_total = FALSE,
percentages = FALSE, chi_square = FALSE) {
# Checks
if (dplyr::is_grouped_df(data)) {
warning("Grouping variable(s) present in data will be ignored.",
call. = FALSE)
}
vars <- grab_vars(data, enquos(...))
vars_str <- purrr::map_chr(vars, as_label)
cross_vars <- length(quos(...))
if (cross_vars < 1) {
stop("Must provide at least one variable to crosstabulate.")
}
# Prepare crosstab
xt <- data %>%
dplyr::group_by({{ col_var }}, ...) %>%
dplyr::count() %>%
tidyr::spread({{ col_var }}, n, fill = 0) %>%
dplyr::ungroup()
xt_cross_vars <- xt %>%
dplyr::select(1:tidyselect::all_of(cross_vars))
xt_col_vars <- xt %>%
dplyr::select(-(1:tidyselect::all_of(cross_vars)))
# Estimate Chi-square test
if (chi_square) {
chi2 <- xt_col_vars %>%
as.matrix() %>%
chisq.test()
}
# Augment
if (add_total) {
xt_col_vars <- xt_col_vars %>%
dplyr::mutate(Total = rowSums(xt_col_vars))
}
if (percentages) {
xt_col_vars <- xt_col_vars %>%
dplyr::mutate_all(col_percs)
}
# Output
out <- xt_cross_vars %>%
dplyr::bind_cols(xt_col_vars)
if (chi_square) {
return(new_tdcmm_ctgrcl(
new_tdcmm(out,
func = "crosstab",
data = data,
params = list(vars = vars_str,
col_var = as_name(enquo(col_var)),
add_total = add_total,
percentages = percentages,
chi_square = chi_square),
model = list(chi2))))
} else {
return(new_tdcmm_ctgrcl(
new_tdcmm(out,
func = "crosstab",
data = data,
params = list(vars = vars_str,
col_var = as_name(enquo(col_var)),
add_total = add_total,
percentages = percentages,
chi_square = chi_square))))
}
}
#' @rdname visualize
#' @export
visualize.tdcmm_ctgrcl <- function(x, ..., .design = design_lmu()) {
if (attr(x, "func") == "tab_frequencies") {
return(visualize_tab_frequencies(x, .design))
}
if (attr(x, "func") == "crosstab") {
return(visualize_crosstab(x, .design))
}
return(warn_about_missing_visualization(x))
}
# Internal functions ----
## Compute Cramer's V
##
## Computes Cramer's V
##
## @param chi2 Output from a `chisq.test()`.
##
## @return a `dbl`
##
## @family categorical
##
## @keywords internal
cramer_V <- function(chi2) {
X2 <- chi2$statistic
N <- sum(chi2$observed)
k = min(dim(chi2$observed))
unname(sqrt(X2 / (N * (k - 1))))
}
## Compute column percentages
##
## Computes column percentages
##
## @param x Numeric vector
##
## @return a `dbl`
col_percs <- function(x) {
x / sum(x, na.rm = TRUE)
}
## Visualize `tab_frequencies()` as one or many histogram(s)
##
## @param x a [tdcmm] model
##
## @return a [ggplot2] object
##
## @family tdcmm visualize
##
## @keywords internal
visualize_tab_frequencies <- function(x, design = design_lmu()) {
var_names <- attr(x, "params")$vars
num_histograms <- length(var_names)
# collect data
data <- NULL
for (variable in var_names) {
data <- data %>%
rbind(attr(x, "data") %>%
tab_frequencies(!!sym(variable)) %>%
dplyr::mutate(var = variable,
level = forcats::as_factor(.data[[variable]])) %>%
dplyr::select(var, level, percent))
}
# visualize
g <- data %>%
ggplot2::ggplot(ggplot2::aes(x = level, y = percent)) +
ggplot2::geom_bar(stat = "identity",
fill = design$main_color_1) +
ggplot2::facet_wrap(dplyr::vars(var),
scales = "free_x") +
ggplot2::scale_x_discrete(NULL) +
ggplot2::scale_y_continuous(NULL,
labels = percentage_labeller,
limits = c(0, 1),
breaks = seq(0, 1, .1)) +
design$theme()
# wrap depending on number of variables
if (num_histograms >= 5) {
warning(glue("Visualizing too many histograms at once might strongly ",
"inhibit readability. Consider reducing the number of ",
"variables in tab_frequencies() before calling visualize()."),
call. = FALSE)
}
return(g)
}
## Visualize `crosstab()` as horizontal stacked bar plot, either absolute or
## relative (depending on `percentages`).
##
## @param x a [tdcmm] model
##
## @return a [ggplot2] object
##
## @family tdcmm visualize
#
## @keywords internal
visualize_crosstab <- function(x, design = design_lmu()) {
independent_var_string <- attr(x, "params")$col_var
dependent_var_strings <- attr(x, "params")$vars
dependent_var_string <- dependent_var_strings[1]
if (length(dependent_var_strings) > 1) {
stop(glue("Visualizing multiple crosstabs at once looks overwhelming. ",
"Consider reducing the number of variables in crosstab() to ",
"two before calling visualize()."),
call. = FALSE)
}
data <- x %>%
tidyr::pivot_longer(!c(!!sym(dependent_var_string)),
names_to = "label_independent") %>%
dplyr::mutate(label_independent =
forcats::as_factor(label_independent),
label_independent_desc =
forcats::fct_rev(label_independent)) %>%
dplyr::rename(level = !!sym(dependent_var_string))
if (length(dplyr::n_distinct(data$label_independent)) > 12) {
stop(glue("Cannot visualize crosstabs with more than 12 levels of the ",
"independent variable ({independent_var_string})."),
call. = FALSE)
}
# visualize
g <- data %>%
ggplot2::ggplot(ggplot2::aes(x = value,
y = label_independent_desc,
fill = level)) +
ggplot2::geom_bar(stat = "identity",
position = "stack")
if (attr(x, "params")$percentages) {
g <- g +
ggplot2::geom_text(ggplot2::aes(label = percentage_labeller(value),
color = level),
position = ggplot2::position_stack(vjust = .5)) +
ggplot2::scale_x_continuous(NULL,
labels = percentage_labeller,
limits = c(0, 1),
breaks = seq(0, 1, .1))
} else {
g <- g +
ggplot2::geom_text(ggplot2::aes(label = value,
color = level),
position = ggplot2::position_stack(vjust = .5)) +
ggplot2::scale_x_continuous('N',
limits = c(0, NA),
n.breaks = 10)
}
g <- g +
ggplot2::scale_y_discrete(NULL) +
ggplot2::scale_fill_manual(NULL,
values = design$main_colors,
guide = ggplot2::guide_legend(reverse = TRUE)) +
ggplot2::scale_color_manual(NULL,
values = design$main_contrasts,
guide = NULL) +
design$theme() +
ggplot2::theme(legend.position = "bottom")
return(g)
}
# Constructors ----
new_tdcmm_ctgrcl <- function(x) {
stopifnot(is_tdcmm(x))
structure(
x,
class = c("tdcmm_ctgrcl", class(x))
)
}
# Formatting ----
#' @export
tbl_format_footer.tdcmm_ctgrcl <- function(x, ...) {
default_footer <- NextMethod()
if (attr(x, "func") != "crosstab" |
length(attr(x, "params")) == 0 |
is.null(attr(x, "model"))) {
return(default_footer)
}
# Get values
chi2 <- model(x)
# Format test string
test_string <- glue("Chi-square = {format_value(chi2$statistic, 3)}, ",
"df = {format(chi2$parameter, digits = 4)}, ",
"{format_pvalue(chi2$p.value)}, ",
"V = {format_value(cramer_V(chi2), 3)}")
# Add to footer and display
test_footer <- style_subtle(glue("# {test_string}"))
c(default_footer, test_footer)
}