|
3 | 3 | #' @param x A data frame that can be coerced into a [tibble][tibble::tibble]. |
4 | 4 | #' @param null The null hypothesis. Options include `"independence"` and |
5 | 5 | #' `"point"`. |
6 | | -#' @param ... Arguments passed to downstream functions. |
| 6 | +#' @param p The true proportion of successes (a number between 0 and 1). To be used with point null hypotheses when the specified response |
| 7 | +#' variable is categorical. |
| 8 | +#' @param mu The true mean (any numerical value). To be used with point null |
| 9 | +#' hypotheses when the specified response variable is continuous. |
| 10 | +#' @param med The true median (any numerical value). To be used with point null |
| 11 | +#' hypotheses when the specified response variable is continuous. |
| 12 | +#' @param sigma The true standard deviation (any numerical value). To be used with |
| 13 | +#' point null hypotheses. |
7 | 14 | #' |
8 | 15 | #' @return A tibble containing the response (and explanatory, if specified) |
9 | 16 | #' variable data with parameter information stored as well. |
|
17 | 24 | #' generate(reps = 100, type = "permute") %>% |
18 | 25 | #' calculate(stat = "F") |
19 | 26 | #' |
| 27 | +#' @importFrom purrr compact |
20 | 28 | #' @export |
21 | | -hypothesize <- function(x, null, ...) { |
22 | | - hypothesize_checks(x, null) |
| 29 | +hypothesize <- function(x, null, p = NULL, mu = NULL, med = NULL, sigma = NULL) { |
23 | 30 |
|
| 31 | + # Custom logic, because using match.arg() would give a default value when |
| 32 | + # the user didn't specify anything. |
| 33 | + null <- match_null_hypothesis(null) |
24 | 34 | attr(x, "null") <- null |
25 | 35 |
|
26 | | - dots <- list(...) |
27 | | - |
28 | | - if ((null == "point") && (length(dots) == 0)) { |
29 | | - stop_glue( |
30 | | - "Provide a parameter and a value to check such as `mu = 30` for the ", |
31 | | - "point hypothesis." |
32 | | - ) |
33 | | - } |
34 | | - |
35 | | - if ((null == "independence") && (length(dots) > 0)) { |
36 | | - warning_glue( |
37 | | - "Parameter values are not specified when testing that two variables are ", |
38 | | - "independent." |
39 | | - ) |
40 | | - } |
41 | | - |
42 | | - if ((length(dots) > 0) && (null == "point")) { |
43 | | - params <- parse_params(dots, x) |
44 | | - attr(x, "params") <- params |
45 | | - |
46 | | - if (any(grepl("p.", attr(attr(x, "params"), "names")))) { |
47 | | - # simulate instead of bootstrap based on the value of `p` provided |
48 | | - attr(x, "type") <- "simulate" |
49 | | - } else { |
50 | | - attr(x, "type") <- "bootstrap" |
51 | | - } |
| 36 | + hypothesize_checks(x, null) |
52 | 37 |
|
53 | | - } |
| 38 | + dots <- compact(list(p = p, mu = mu, med = med, sigma = sigma)) |
54 | 39 |
|
55 | | - if (!is.null(null) && (null == "independence")) { |
56 | | - attr(x, "type") <- "permute" |
57 | | - } |
| 40 | + switch( |
| 41 | + null, |
| 42 | + independence = { |
| 43 | + params <- sanitize_hypothesis_params_independence(dots) |
| 44 | + attr(x, "type") <- "permute" |
| 45 | + }, |
| 46 | + point = { |
| 47 | + params <- sanitize_hypothesis_params_point(dots, x) |
| 48 | + attr(x, "params") <- unlist(params) |
58 | 49 |
|
59 | | - # Check one proportion test set up correctly |
60 | | - if (null == "point") { |
61 | | - if (is.factor(response_variable(x))) { |
62 | | - if (!any(grepl("p", attr(attr(x, "params"), "names")))) { |
63 | | - stop_glue( |
64 | | - 'Testing one categorical variable requires `p` to be used as a ', |
65 | | - 'parameter.' |
66 | | - ) |
| 50 | + if (!is.null(params$p)) { |
| 51 | + # simulate instead of bootstrap based on the value of `p` provided |
| 52 | + attr(x, "type") <- "simulate" |
| 53 | + } else { |
| 54 | + # Check one proportion test set up correctly |
| 55 | + if (is.factor(response_variable(x))) { |
| 56 | + stop_glue( |
| 57 | + 'Testing one categorical variable requires `p` to be used as a ', |
| 58 | + 'parameter.' |
| 59 | + ) |
| 60 | + } |
| 61 | + attr(x, "type") <- "bootstrap" |
67 | 62 | } |
68 | 63 | } |
69 | | - } |
70 | | - |
71 | | - # Check one numeric test set up correctly |
72 | | - ## Not currently able to reach in testing as other checks |
73 | | - ## already produce errors |
74 | | - # if (null == "point") { |
75 | | - # if ( |
76 | | - # !is.factor(response_variable(x)) |
77 | | - # & !any(grepl("mu|med|sigma", attr(attr(x, "params"), "names"))) |
78 | | - # ) { |
79 | | - # stop_glue( |
80 | | - # 'Testing one numerical variable requires one of ', |
81 | | - # '`mu`, `med`, or `sd` to be used as a parameter.' |
82 | | - # ) |
83 | | - # } |
84 | | - # } |
85 | | - |
86 | | - tibble::as_tibble(x) |
| 64 | + ) |
| 65 | + append_infer_class(tibble::as_tibble(x)) |
87 | 66 | } |
0 commit comments