Skip to content

Commit 0cb4dfa

Browse files
authored
Updating CI functions (#181)
**What changes are proposed in this pull request?** * Style this entry in a way that can be copied directly into `NEWS.md`. (#<issue number>, @<username>) Provide more detail here as needed. **Reference GitHub issue associated with pull request.** _e.g., 'closes #<issue number>'_ -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [ ] If a bug was fixed, a unit test was added. - [ ] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [ ] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [ ] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom", reference_pkg = "cardx")` has been set in the function call and the following added to the roxygen comments: `@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom"", reference_pkg = "cardx"))` - [ ] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cardx (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
1 parent c6b0998 commit 0cb4dfa

23 files changed

+329
-147
lines changed

NAMESPACE

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
S3method(ard_attributes,survey.design)
44
S3method(ard_categorical,survey.design)
5+
S3method(ard_categorical_ci,data.frame)
6+
S3method(ard_categorical_ci,survey.design)
57
S3method(ard_continuous,survey.design)
8+
S3method(ard_continuous_ci,data.frame)
9+
S3method(ard_continuous_ci,survey.design)
610
S3method(ard_dichotomous,survey.design)
711
S3method(ard_missing,survey.design)
812
S3method(ard_regression,default)
@@ -18,15 +22,16 @@ export(ard_attributes)
1822
export(ard_car_anova)
1923
export(ard_car_vif)
2024
export(ard_categorical)
25+
export(ard_categorical_ci)
2126
export(ard_continuous)
27+
export(ard_continuous_ci)
2228
export(ard_dichotomous)
2329
export(ard_effectsize_cohens_d)
2430
export(ard_effectsize_hedges_g)
2531
export(ard_effectsize_paired_cohens_d)
2632
export(ard_effectsize_paired_hedges_g)
2733
export(ard_emmeans_mean_difference)
2834
export(ard_missing)
29-
export(ard_proportion_ci)
3035
export(ard_regression)
3136
export(ard_regression_basic)
3237
export(ard_smd_smd)
@@ -46,8 +51,6 @@ export(ard_stats_t_test)
4651
export(ard_stats_t_test_onesample)
4752
export(ard_stats_wilcox_test)
4853
export(ard_stats_wilcox_test_onesample)
49-
export(ard_survey_categorical_ci)
50-
export(ard_survey_continuous_ci)
5154
export(ard_survey_svychisq)
5255
export(ard_survey_svyranktest)
5356
export(ard_survey_svyttest)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ard_moodtest() -> ard_stats_mood_test()
1818

1919
### New Features
2020

21-
* The `ard_proportion_ci(value)` argument has been added. Previously, only binary variables (0/1 or TRUE/FALSE) could be summarized. When a value is not supplied, each level of the variable is summarized independently. By default, binary variables will have the 1/TRUE level summarized.
21+
* The `ard_categorical_ci(value)` argument has been added. Previously, only binary variables (0/1 or TRUE/FALSE) could be summarized. When a value is not supplied, each level of the variable is summarized independently. By default, binary variables will have the 1/TRUE level summarized.
2222

2323
* Added the following functions for calculating Analysis Results Data (ARD).
2424
- `ard_stats_aov()` for calculating ANOVA results using `stats::aov()`. (#3)
Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#' Default is `0.95`
1515
#' @param method (`string`)\cr
1616
#' string indicating the type of confidence interval to calculate.
17-
#' Must be one of `r formals(ard_proportion_ci)[["method"]] |> eval() |> shQuote("sh")`.
17+
#' Must be one of `r formals(ard_categorical_ci)[["method"]] |> eval() |> shQuote("sh")`.
1818
#' See `?proportion_ci` for details.
1919
#' @param strata,weights,max.iterations arguments passed to `proportion_ci_strat_wilson()`,
2020
#' when `method='strat_wilson'`
@@ -25,29 +25,42 @@
2525
#' columns coded as `0`/`1` and `TRUE`/`FALSE` will summarize the `1` and `TRUE` levels.
2626
#'
2727
#' @return an ARD data frame
28-
#' @export
28+
#' @name ard_categorical_ci
2929
#'
3030
#' @examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom", reference_pkg = "cardx"))
3131
#' # compute CI for binary variables
32-
#' ard_proportion_ci(mtcars, variables = c(vs, am), method = "wilson")
32+
#' ard_categorical_ci(mtcars, variables = c(vs, am), method = "wilson")
3333
#'
3434
#' # compute CIs for each level of a categorical variable
35-
#' ard_proportion_ci(mtcars, variables = cyl, method = "jeffreys")
36-
ard_proportion_ci <- function(data,
37-
variables,
38-
by = dplyr::group_vars(data),
39-
method = c(
40-
"waldcc", "wald", "clopper-pearson",
41-
"wilson", "wilsoncc",
42-
"strat_wilson", "strat_wilsoncc",
43-
"agresti-coull", "jeffreys"
44-
),
45-
conf.level = 0.95,
46-
value = list(where(is_binary) ~ 1L, where(is.logical) ~ TRUE),
47-
strata = NULL,
48-
weights = NULL,
49-
max.iterations = 10) {
35+
#' ard_categorical_ci(mtcars, variables = cyl, method = "jeffreys")
36+
NULL
37+
38+
#' @rdname ard_categorical_ci
39+
#' @export
40+
ard_categorical_ci <- function(data, ...) {
41+
check_not_missing(data)
42+
UseMethod("ard_categorical_ci")
43+
}
44+
45+
#' @rdname ard_categorical_ci
46+
#' @export
47+
ard_categorical_ci.data.frame <- function(data,
48+
variables,
49+
by = dplyr::group_vars(data),
50+
method = c(
51+
"waldcc", "wald", "clopper-pearson",
52+
"wilson", "wilsoncc",
53+
"strat_wilson", "strat_wilsoncc",
54+
"agresti-coull", "jeffreys"
55+
),
56+
conf.level = 0.95,
57+
value = list(where(is_binary) ~ 1L, where(is.logical) ~ TRUE),
58+
strata = NULL,
59+
weights = NULL,
60+
max.iterations = 10,
61+
...) {
5062
set_cli_abort_call()
63+
check_dots_empty()
5164

5265
# check installed packages ---------------------------------------------------
5366
check_pkg_installed(pkg = "broom", reference_pkg = "cardx")
@@ -137,8 +150,8 @@ ard_proportion_ci <- function(data,
137150
unique_levels <-
138151
# styler: off
139152
if (is.logical(data[[variable]])) c(TRUE, FALSE)
140-
else if (is.factor(data[[variable]])) factor(levels(data[[variable]]), levels = levels(data[[variable]]))
141-
else unique(data[[variable]]) |> sort()
153+
else if (is.factor(data[[variable]])) factor(levels(data[[variable]]), levels = levels(data[[variable]]))
154+
else unique(data[[variable]]) |> sort()
142155
# styler: on
143156

144157
if (!is_empty(value) && !value %in% unique_levels) {
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
#' [`survey::svyciprop()`].
55
#'
66
#' @inheritParams ard_continuous.survey.design
7+
#' @inheritParams ard_categorical_ci.data.frame
78
#' @param method (`string`)\cr
89
#' Method passed to `survey::svyciprop(method)`
9-
#' @param conf.level (scalar `numeric`)\cr
10-
#' confidence level for confidence interval. Default is `0.95`.
1110
#' @param df (`numeric`)\cr
1211
#' denominator degrees of freedom, passed to `survey::svyciprop(df)`.
1312
#' Default is `survey::degf(data)`.
@@ -20,16 +19,18 @@
2019
#' data(api, package = "survey")
2120
#' dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
2221
#'
23-
#' ard_survey_categorical_ci(dclus1, variables = sch.wide)
24-
#' ard_survey_categorical_ci(dclus1, variables = sch.wide, method = "xlogit")
25-
ard_survey_categorical_ci <- function(data,
26-
variables,
27-
by = NULL,
28-
method = c("logit", "likelihood", "asin", "beta", "mean", "xlogit"),
29-
conf.level = 0.95,
30-
df = survey::degf(data),
31-
...) {
22+
#' ard_categorical_ci(dclus1, variables = sch.wide)
23+
#' ard_categorical_ci(dclus1, variables = sch.wide, value = sch.wide ~ "Yes", method = "xlogit")
24+
ard_categorical_ci.survey.design <- function(data,
25+
variables,
26+
by = NULL,
27+
method = c("logit", "likelihood", "asin", "beta", "mean", "xlogit"),
28+
conf.level = 0.95,
29+
value = list(where(is_binary) ~ 1L, where(is.logical) ~ TRUE),
30+
df = survey::degf(data),
31+
...) {
3232
set_cli_abort_call()
33+
check_dots_empty()
3334

3435
# check inputs ---------------------------------------------------------------
3536
check_not_missing(data)
@@ -41,6 +42,10 @@ ard_survey_categorical_ci <- function(data,
4142
variables = {{ variables }},
4243
by = {{ by }}
4344
)
45+
cards::process_formula_selectors(
46+
data = data$variables,
47+
value = value
48+
)
4449
check_scalar(by, allow_empty = TRUE)
4550
check_scalar_range(conf.level, range = c(0, 1))
4651
method <- arg_match(method)
@@ -54,11 +59,12 @@ ard_survey_categorical_ci <- function(data,
5459
conf.level = conf.level,
5560
method = method,
5661
df = df,
62+
value = value,
5763
...
5864
)
5965
}
6066

61-
.calculate_ard_onesample_survey_ci <- function(FUN, data, variables, by, conf.level, ...) {
67+
.calculate_ard_onesample_survey_ci <- function(FUN, data, variables, by, conf.level, value, ...) {
6268
# return empty data frame if no variables to process -------------------------
6369
if (is_empty(variables)) return(dplyr::tibble()) # styler: off
6470

@@ -72,14 +78,15 @@ ard_survey_categorical_ci <- function(data,
7278
variable = variable,
7379
by = by,
7480
conf.level = conf.level,
81+
value = value[[variable]],
7582
...
7683
)
7784
}
7885
) |>
7986
dplyr::bind_rows()
8087
}
8188

82-
.calculate_one_ard_categorical_survey_ci <- function(FUN, data, variable, by, conf.level, ...) {
89+
.calculate_one_ard_categorical_survey_ci <- function(FUN, data, variable, by, conf.level, value, ...) {
8390
variable_levels <- .unique_values_sort(data$variables, variable = variable)
8491
if (!is_empty(by)) {
8592
by_levels <- .unique_values_sort(data$variables, variable = by)
@@ -126,7 +133,7 @@ ard_survey_categorical_ci <- function(data,
126133
list(),
127134
warning = .data$lst_result["warning"] |> unname(),
128135
error = .data$lst_result["error"] |> unname(),
129-
context = "survey_categorical_ci"
136+
context = "categorical_ci"
130137
) |>
131138
dplyr::select(-"lst_result") |>
132139
dplyr::ungroup() |>
@@ -137,6 +144,14 @@ ard_survey_categorical_ci <- function(data,
137144
) |>
138145
cards::tidy_ard_column_order() %>%
139146
structure(., class = c("card", class(.)))
147+
148+
# if a value was passed for the variable, subset on those results
149+
if (!is_empty(value)) {
150+
df_full <- df_full |>
151+
dplyr::filter(.data$variable_level %in% .env$value)
152+
}
153+
154+
df_full
140155
}
141156

142157

R/ard_continuous_ci.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#' ARD continuous CIs
2+
#'
3+
#' One-sample confidence intervals for continuous variable means and medians.
4+
#'
5+
#' @inheritParams ard_stats_t_test
6+
#' @param method (`string`)\cr
7+
#' a string indicating the method to use for the confidence interval
8+
#' calculation. Must be one of `"t.test"` or `"wilcox.test"`
9+
#' @param ... arguments passed to `t.test()` or `wilcox.test()`
10+
#'
11+
#' @return ARD data frame
12+
#' @name ard_continuous_ci
13+
#'
14+
#' @examples
15+
#' ard_continuous_ci(mtcars, variables = c(mpg, hp), method = "wilcox.test")
16+
#' ard_continuous_ci(mtcars, variables = mpg, by = am, method = "t.test")
17+
NULL
18+
19+
#' @rdname ard_continuous_ci
20+
#' @export
21+
ard_continuous_ci <- function(data, ...) {
22+
check_not_missing(data)
23+
UseMethod("ard_continuous_ci")
24+
}
25+
26+
#' @rdname ard_continuous_ci
27+
#' @export
28+
ard_continuous_ci.data.frame <- function(data, variables, by = dplyr::group_vars(data), conf.level = 0.95, method = c("t.test", "wilcox.test"), ...) {
29+
set_cli_abort_call()
30+
31+
# check inputs ---------------------------------------------------------------
32+
method <- arg_match(method)
33+
34+
# calculate CIs --------------------------------------------------------------
35+
switch(method,
36+
"t.test" =
37+
ard_stats_t_test_onesample(
38+
data = data,
39+
variables = {{ variables }},
40+
by = {{ by }},
41+
conf.level = conf.level,
42+
...
43+
),
44+
"wilcox.test" =
45+
ard_stats_wilcox_test_onesample(
46+
data = data,
47+
variables = {{ variables }},
48+
by = {{ by }},
49+
conf.level = conf.level,
50+
conf.int = TRUE,
51+
...
52+
)
53+
) |>
54+
dplyr::mutate(context = "continuous_ci")
55+
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' ARD survey categorical CIs
1+
#' ARD survey continuous CIs
22
#'
33
#' One-sample confidence intervals for continuous variables' means and medians.
44
#' Confidence limits are calculated with `survey::svymean()` and `survey::svyquantile()`.
@@ -23,15 +23,15 @@
2323
#' data(api, package = "survey")
2424
#' dclus1 <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
2525
#'
26-
#' ard_survey_continuous_ci(dclus1, variables = api00)
27-
#' ard_survey_continuous_ci(dclus1, variables = api00, method = "xlogit")
28-
ard_survey_continuous_ci <- function(data,
29-
variables,
30-
by = NULL,
31-
method = c("svymean", "mean", "beta", "xlogit", "asin", "score"),
32-
conf.level = 0.95,
33-
df = survey::degf(data),
34-
...) {
26+
#' ard_continuous_ci(dclus1, variables = api00)
27+
#' ard_continuous_ci(dclus1, variables = api00, method = "svymedian.xlogit")
28+
ard_continuous_ci.survey.design <- function(data,
29+
variables,
30+
by = NULL,
31+
method = c("svymean", "svymedian.mean", "svymedian.beta", "svymedian.xlogit", "svymedian.asin", "svymedian.score"),
32+
conf.level = 0.95,
33+
df = survey::degf(data),
34+
...) {
3535
set_cli_abort_call()
3636

3737
# check inputs ---------------------------------------------------------------
@@ -183,7 +183,7 @@ ard_survey_continuous_ci <- function(data,
183183
svyquantile <-
184184
survey::svyquantile(
185185
x = reformulate2(variable), design = data, quantiles = 0.5,
186-
na.rm = TRUE, interval.type = method
186+
na.rm = TRUE, interval.type = str_remove(method, pattern = "^svymedian\\.")
187187
)
188188

189189
lst_svyquantile <- svyquantile |>

R/ard_stats_t_test.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#' column name of the subject or participant ID
1515
#' @param conf.level (scalar `numeric`)\cr
1616
#' confidence level for confidence interval. Default is `0.95`.
17-
#' @param ... arguments passed to `t.test(...)`
17+
#' @param ... arguments passed to `t.test()`
1818
#'
1919
#' @return ARD data frame
2020
#' @name ard_stats_t_test

R/ard_stats_wilcox_test_onesample.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ard_stats_wilcox_test_onesample <- function(data, variables, by = dplyr::group_v
4242
data = data,
4343
variables = all_of(variables),
4444
by = all_of(by),
45-
statistic = all_of(variables) ~ list(t_test_onesample = \(x) stats::wilcox.test(x = x, conf.level = conf.level, ...) |> broom::tidy())
45+
statistic = all_of(variables) ~ list(wilcox_test_onesample = \(x) stats::wilcox.test(x = x, conf.level = conf.level, ...) |> broom::tidy())
4646
) |>
4747
cards::bind_ard(
4848
cards::ard_continuous(

R/proportion_ci.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' Functions to calculate different proportion confidence intervals for use in `ard_proportion()`.
44
#'
5-
#' @inheritParams ard_proportion_ci
5+
#' @inheritParams ard_categorical_ci
66
#' @param x vector of a binary values, i.e. a logical vector, or numeric with values `c(0, 1)`
77
#' @return Confidence interval of a proportion.
88
#'

_pkgdown.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ reference:
6767
- ard_dichotomous.survey.design
6868
- ard_missing.survey.design
6969
- ard_attributes.survey.design
70-
- ard_survey_continuous_ci
71-
- ard_survey_categorical_ci
70+
- ard_continuous_ci.survey.design
71+
- ard_categorical_ci.survey.design
7272
- ard_survey_svychisq
7373
- ard_survey_svyranktest
7474
- ard_survey_svyttest
@@ -81,7 +81,8 @@ reference:
8181

8282
- subtitle: "Other ARD functions"
8383
- contents:
84-
- ard_proportion_ci
84+
- ard_continuous_ci.data.frame
85+
- ard_categorical_ci.data.frame
8586
- ard_regression
8687
- ard_regression_basic
8788

0 commit comments

Comments
 (0)