/
wrappers.R
executable file
·586 lines (534 loc) · 19.5 KB
/
wrappers.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# Wrapper functions
# Different shortcuts to doing traditional hypothesis tests & confidence
# intervals in R as well as calculating test statistics, following a pipe-able
# framework
#' Tidy t-test
#'
#' @description
#'
#' A tidier version of [t.test()][stats::t.test()] for two sample tests.
#'
#' @param x A data frame that can be coerced into a [tibble][tibble::tibble].
#' @inheritParams specify
#' @param order A string vector of specifying the order in which the levels of
#' the explanatory variable should be ordered for subtraction, where `order =
#' c("first", "second")` means `("first" - "second")`.
#' @param alternative Character string giving the direction of the alternative
#' hypothesis. Options are `"two-sided"` (default), `"greater"`, or `"less"`.
#' @param mu A numeric value giving the hypothesized null mean value for a one
#' sample test and the hypothesized difference for a two sample test.
#' @param conf_int A logical value for whether to include the confidence
#' interval or not. `TRUE` by default.
#' @param conf_level A numeric value between 0 and 1. Default value is 0.95.
#' @param ... For passing in other arguments to [t.test()][stats::t.test()].
#'
#' @examples
#' library(tidyr)
#'
#' # t test for number of hours worked per week
#' # by college degree status
#' gss %>%
#' tidyr::drop_na(college) %>%
#' t_test(formula = hours ~ college,
#' order = c("degree", "no degree"),
#' alternative = "two-sided")
#'
#' # see vignette("infer") for more explanation of the
#' # intuition behind the infer package, and vignette("t_test")
#' # for more examples of t-tests using infer
#'
#' @importFrom rlang f_lhs
#' @importFrom rlang f_rhs
#' @importFrom rlang new_formula
#' @importFrom stats as.formula
#' @family wrapper functions
#' @export
t_test <- function(x, formula,
response = NULL,
explanatory = NULL,
order = NULL,
alternative = "two-sided",
mu = 0,
conf_int = TRUE,
conf_level = 0.95,
...) {
check_conf_level(conf_level)
# convert all character and logical variables to be factor variables
x <- standardize_variable_types(x)
# parse response and explanatory variables
response <- enquo(response)
explanatory <- enquo(explanatory)
x <- parse_variables(x = x, formula = formula,
response = response, explanatory = explanatory)
# match with old "dot" syntax in t.test
if (alternative %in% c("two-sided", "two_sided", "two sided", "two.sided")) {
alternative <- "two.sided"
}
# two sample
if (has_explanatory(x)) {
order <- check_order(x, order, in_calculate = FALSE, stat = NULL)
x <- reorder_explanatory(x, order)
prelim <- stats::t.test(formula = new_formula(response_expr(x), explanatory_expr(x)),
data = x,
alternative = alternative,
mu = mu,
conf.level = conf_level,
...) %>%
broom::glance()
} else { # one sample
prelim <- stats::t.test(response_variable(x),
alternative = alternative,
mu = mu,
conf.level = conf_level) %>%
broom::glance()
}
if (conf_int) {
results <- prelim %>%
dplyr::select(
statistic, t_df = parameter, p_value = p.value, alternative,
estimate, lower_ci = conf.low, upper_ci = conf.high
)
} else {
results <- prelim %>%
dplyr::select(
statistic, t_df = parameter, p_value = p.value, alternative, estimate
)
}
results
}
#' Tidy t-test statistic
#'
#' @description
#'
#' A shortcut wrapper function to get the observed test statistic for a t test.
#' This function has been deprecated in favor of the more general [observe()].
#'
#' @param x A data frame that can be coerced into a [tibble][tibble::tibble].
#' @inheritParams specify
#' @param order A string vector of specifying the order in which the levels of
#' the explanatory variable should be ordered for subtraction, where `order =
#' c("first", "second")` means `("first" - "second")`.
#' @param mu A numeric value giving the hypothesized null mean value for a one
#' sample test and the hypothesized difference for a two sample test.
#' @inheritParams t_test
#' @param ... Pass in arguments to infer functions.
#'
#' @examples
#' library(tidyr)
#'
#' # t test statistic for true mean number of hours worked
#' # per week of 40
#' gss %>%
#' t_stat(response = hours, mu = 40)
#'
#' # t test statistic for number of hours worked per week
#' # by college degree status
#' gss %>%
#' tidyr::drop_na(college) %>%
#' t_stat(formula = hours ~ college,
#' order = c("degree", "no degree"),
#' alternative = "two-sided")
#'
#' @family wrapper functions
#' @family functions for calculating observed statistics
#' @export
t_stat <- function(x, formula,
response = NULL,
explanatory = NULL,
order = NULL,
alternative = "two-sided",
mu = 0,
conf_int = FALSE,
conf_level = 0.95,
...) {
lifecycle::deprecate_warn(
when = "1.0.0",
what = "t_stat()",
with = "observe()"
)
check_conf_level(conf_level)
# convert all character and logical variables to be factor variables
x <- standardize_variable_types(x)
# parse response and explanatory variables
response <- enquo(response)
explanatory <- enquo(explanatory)
x <- parse_variables(x = x, formula = formula,
response = response, explanatory = explanatory)
# match with old "dot" syntax in t.test
if (alternative %in% c("two-sided", "two_sided", "two sided", "two.sided")) {
alternative <- "two.sided"
}
# two sample
if (has_explanatory(x)) {
order <- check_order(x, order, in_calculate = FALSE, stat = NULL)
x <- reorder_explanatory(x, order)
prelim <- stats::t.test(formula = new_formula(response_expr(x), explanatory_expr(x)),
data = x,
alternative = alternative,
mu = mu,
conf.level = conf_level,
...) %>%
broom::glance()
} else { # one sample
prelim <- stats::t.test(response_variable(x),
alternative = alternative,
mu = mu,
conf.level = conf_level) %>%
broom::glance()
}
# removed unnecessary if(conf_int) clause; only the statistic itself
# was returned regardless
results <- prelim %>%
dplyr::select(statistic) %>%
pull()
results
}
#' Tidy chi-squared test
#'
#' @description
#'
#' A tidier version of [chisq.test()][stats::chisq.test()] for goodness of fit
#' tests and tests of independence.
#'
#' @param x A data frame that can be coerced into a [tibble][tibble::tibble].
#' @inheritParams specify
#' @param ... Additional arguments for [chisq.test()][stats::chisq.test()].
#'
#' @examples
#' # chi-squared test of independence for college completion
#' # status depending on one's self-identified income class
#' chisq_test(gss, college ~ finrela)
#'
#' # chi-squared goodness of fit test on whether self-identified
#' # income class follows a uniform distribution
#' chisq_test(gss,
#' response = finrela,
#' p = c("far below average" = 1/6,
#' "below average" = 1/6,
#' "average" = 1/6,
#' "above average" = 1/6,
#' "far above average" = 1/6,
#' "DK" = 1/6))
#'
#' @family wrapper functions
#' @export
chisq_test <- function(x, formula, response = NULL,
explanatory = NULL, ...) {
# Parse response and explanatory variables
response <- enquo(response)
explanatory <- enquo(explanatory)
x <- standardize_variable_types(x)
x <- parse_variables(x = x, formula = formula,
response = response, explanatory = explanatory)
if (!(class(response_variable(x)) %in% c("logical", "character", "factor"))) {
cli_abort(
'The response variable of `{response_name(x)}` is not appropriate \\
since the response variable is expected to be categorical.'
)
}
if (has_explanatory(x) &&
!(class(explanatory_variable(x)) %in% c("logical", "character", "factor"))) {
cli_abort(
'The explanatory variable of `{explanatory_name(x)}` is not appropriate \\
since the explanatory variable is expected to be categorical.'
)
}
x <- x %>%
select(any_of(c(response_name(x), explanatory_name(x))))
stats::chisq.test(table(x), ...) %>%
broom::glance() %>%
dplyr::select(statistic, chisq_df = parameter, p_value = p.value)
}
#' Tidy chi-squared test statistic
#'
#' @description
#'
#' A shortcut wrapper function to get the observed test statistic for a chisq
#' test. Uses [chisq.test()][stats::chisq.test()], which applies a continuity
#' correction. This function has been deprecated in favor of the more
#' general [observe()].
#'
#' @param x A data frame that can be coerced into a [tibble][tibble::tibble].
#' @inheritParams specify
#' @param ... Additional arguments for [chisq.test()][stats::chisq.test()].
#'
#' @examples
#' # chi-squared test statistic for test of independence
#' # of college completion status depending and one's
#' # self-identified income class
#' chisq_stat(gss, college ~ finrela)
#'
#' # chi-squared test statistic for a goodness of fit
#' # test on whether self-identified income class
#' # follows a uniform distribution
#' chisq_stat(gss,
#' response = finrela,
#' p = c("far below average" = 1/6,
#' "below average" = 1/6,
#' "average" = 1/6,
#' "above average" = 1/6,
#' "far above average" = 1/6,
#' "DK" = 1/6))
#'
#' @family wrapper functions
#' @family functions for calculating observed statistics
#' @export
chisq_stat <- function(x, formula, response = NULL,
explanatory = NULL, ...) {
lifecycle::deprecate_warn(
when = "1.0.0",
what = "chisq_stat()",
with = "observe()"
)
# Parse response and explanatory variables
response <- enquo(response)
explanatory <- enquo(explanatory)
x <- standardize_variable_types(x)
x <- parse_variables(x = x, formula = formula,
response = response, explanatory = explanatory)
if (!(class(response_variable(x)) %in% c("logical", "character", "factor"))) {
cli_abort(
'The response variable of `{response_name(x)}` is not appropriate \\
since the response variable is expected to be categorical.'
)
}
if (has_explanatory(x) &&
!(class(explanatory_variable(x)) %in% c("logical", "character", "factor"))) {
cli_abort(
'The explanatory variable of `{explanatory_name(x)}` is not appropriate \\
since the response variable is expected to be categorical.'
)
}
x <- x %>%
select(any_of(c(response_name(x), explanatory_name(x))))
suppressWarnings(stats::chisq.test(table(x), ...)) %>%
broom::glance() %>%
dplyr::select(statistic) %>%
pull()
}
check_conf_level <- function(conf_level, call = caller_env()) {
if (
(!inherits(conf_level, "numeric")) | (conf_level < 0) | (conf_level > 1)
) {
cli_abort(
"The `conf_level` argument must be a number between 0 and 1.",
call = call
)
}
}
#' Tidy proportion test
#'
#' @description
#'
#' A tidier version of [prop.test()][stats::prop.test()] for equal or given
#' proportions.
#'
#' @param x A data frame that can be coerced into a [tibble][tibble::tibble].
#' @inheritParams specify
#' @param order A string vector specifying the order in which the proportions
#' should be subtracted, where `order = c("first", "second")` means
#' `"first" - "second"`. Ignored for one-sample tests, and optional for two
#' sample tests.
#' @param alternative Character string giving the direction of the alternative
#' hypothesis. Options are `"two-sided"` (default), `"greater"`, or `"less"`.
#' Only used when testing the null that a single proportion equals a given
#' value, or that two proportions are equal; ignored otherwise.
#' @param p A numeric vector giving the hypothesized null proportion of
#' success for each group.
#' @inheritParams t_test
#' @param success The level of `response` that will be considered a success, as
#' a string. Only used when testing the null that a single
#' proportion equals a given value, or that two proportions are equal;
#' ignored otherwise.
#' @param correct A logical indicating whether Yates' continuity correction
#' should be applied where possible. If `z = TRUE`, the `correct` argument will
#' be overwritten as `FALSE`. Otherwise defaults to `correct = TRUE`.
#' @param z A logical value for whether to report the statistic as a standard
#' normal deviate or a Pearson's chi-square statistic. \eqn{z^2} is distributed
#' chi-square with 1 degree of freedom, though note that the user will likely
#' need to turn off Yates' continuity correction by setting `correct = FALSE`
#' to see this connection.
#' @param ... Additional arguments for [prop.test()][stats::prop.test()].
#'
#' @details
#' When testing with an explanatory variable with more than two levels, the
#' `order` argument as used in the package is no longer well-defined. The function
#' will thus raise a warning and ignore the value if supplied a non-NULL `order`
#' argument.
#'
#' The columns present in the output depend on the output of both [prop.test()]
#' and [broom::glance.htest()]. See the latter's documentation for column
#' definitions; columns have been renamed with the following mapping:
#'
#' * `chisq_df` = `parameter`
#' * `p_value` = `p.value`
#' * `lower_ci` = `conf.low`
#' * `upper_ci` = `conf.high`
#'
#' @examples
#' # two-sample proportion test for difference in proportions of
#' # college completion by respondent sex
#' prop_test(gss,
#' college ~ sex,
#' order = c("female", "male"))
#'
#' # one-sample proportion test for hypothesized null
#' # proportion of college completion of .2
#' prop_test(gss,
#' college ~ NULL,
#' p = .2)
#'
#' # report as a z-statistic rather than chi-square
#' # and specify the success level of the response
#' prop_test(gss,
#' college ~ NULL,
#' success = "degree",
#' p = .2,
#' z = TRUE)
#'
#' @family wrapper functions
#' @export
prop_test <- function(x, formula,
response = NULL,
explanatory = NULL,
p = NULL,
order = NULL,
alternative = "two-sided",
conf_int = TRUE,
conf_level = 0.95,
success = NULL,
correct = NULL,
z = FALSE,
...) {
# Parse response and explanatory variables
response <- enquo(response)
explanatory <- enquo(explanatory)
x <- standardize_variable_types(x)
x <- parse_variables(x = x, formula = formula,
response = response, explanatory = explanatory)
correct <- if (z) {FALSE} else if (is.null(correct)) {TRUE} else {correct}
if (!(class(response_variable(x)) %in% c("logical", "character", "factor"))) {
cli_abort(
'The response variable of `{response_name(x)}` is not appropriate \\
since the response variable is expected to be categorical.'
)
}
if (has_explanatory(x) &&
!(class(explanatory_variable(x)) %in% c("logical", "character", "factor"))) {
cli_abort(
'The explanatory variable of `{explanatory_name(x)}` is not appropriate \\
since the explanatory variable is expected to be categorical.'
)
}
# match with old "dot" syntax in t.test
if (alternative %in% c("two-sided", "two_sided", "two sided", "two.sided")) {
alternative <- "two.sided"
}
# process "success" arg
lvls <- levels(factor(response_variable(x)))
if (length(lvls) > 2) {
cli_abort(
"This test is not defined for response variables with more than 2 levels."
)
}
if (!is.null(success)) {
check_type(success, rlang::is_string)
if (!(success %in% lvls)) {
cli_abort('{success} is not a valid level of {response_name(x)}.')
}
lvls <- c(success, lvls[lvls != success])
} else {
success <- lvls[1]
}
# two sample
if (has_explanatory(x)) {
# make a summary table to supply to prop.test
sum_table <- x %>%
select(explanatory_name(x), response_name(x)) %>%
table()
length_exp_levels <- length(levels(explanatory_variable(x)))
if (length_exp_levels == 2) {
order <- check_order(x, order, in_calculate = FALSE, stat = NULL)
# reorder according to the order and success arguments
sum_table <- sum_table[order, lvls]
} else if (length_exp_levels >= 3 && !is.null(order)) {
cli_warn(c(
"The `order` argument will be ignored as it is not well-defined \\
for explanatory variables with more than 2 levels. ",
i = "To silence this message, avoid passing the `order` argument."
))
# reorder according to the success argument
sum_table <- sum_table[, lvls]
}
prelim <- stats::prop.test(x = sum_table,
alternative = alternative,
conf.level = conf_level,
p = p,
correct = correct,
...)
} else { # one sample
response_tbl <- response_variable(x) %>%
factor() %>%
stats::relevel(success) %>%
table()
if (is.null(p)) {
cli_inform(
"No `p` argument was hypothesized, so the test will \\
assume a null hypothesis `p = .5`."
)
}
prelim <- stats::prop.test(x = response_tbl,
alternative = alternative,
conf.level = conf_level,
p = p,
correct = correct,
...)
}
if (length(prelim$estimate) <= 2) {
if (conf_int & is.null(p)) {
results <- prelim %>%
broom::glance() %>%
dplyr::select(statistic,
chisq_df = parameter,
p_value = p.value,
alternative,
lower_ci = conf.low,
upper_ci = conf.high)
} else {
results <- prelim %>%
broom::glance() %>%
dplyr::select(statistic,
chisq_df = parameter,
p_value = p.value,
alternative)
}
} else {
results <- prelim %>%
broom::glance() %>%
dplyr::select(statistic,
chisq_df = parameter,
p_value = p.value)
}
if (z) {
results <- calculate_z(x, results, success, p, order)
}
results
}
calculate_z <- function(x, results, success, p, order) {
exp <- if (has_explanatory(x)) {explanatory_expr(x)} else {NULL}
form <- new_formula(response_expr(x), exp)
stat <- x %>%
specify(formula = form, success = success) %>%
hypothesize(
null = if (has_explanatory(x)) {"independence"} else {"point"},
p = if (is.null(p) && !has_explanatory(x)) {.5} else {p}
) %>%
calculate(
stat = "z",
order = if (has_explanatory(x)) {order} else {NULL}
) %>%
dplyr::pull()
results$statistic <- stat
results$chisq_df <- NULL
results
}