Skip to content

Commit

Permalink
Rename internal functions sensibly (#274)
Browse files Browse the repository at this point in the history
* rename internal functions sensibly

* Apply automatic changes

* no need to suppress warnings for meta analysis

---------

Co-authored-by: IndrajeetPatil <[email protected]>
  • Loading branch information
IndrajeetPatil and IndrajeetPatil authored Mar 22, 2024
1 parent 5eb24a8 commit 18d5e83
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 41 deletions.
1 change: 1 addition & 0 deletions API
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_expression_col(data, paired = FALSE, statistic.text = NULL, effsize.text = N
centrality_description(data, x, y, type = "parametric", conf.level = NULL, tr = 0.2, digits = 2L, ...)
contingency_table(data, x, y = NULL, paired = FALSE, type = "parametric", counts = NULL, ratio = NULL, alternative = "two.sided", digits = 2L, conf.level = 0.95, sampling.plan = "indepMulti", fixed.margin = "rows", prior.concentration = 1, ...)
corr_test(data, x, y, type = "parametric", digits = 2L, conf.level = 0.95, tr = 0.2, bf.prior = 0.707, ...)
extract_stats_type(type)
long_to_wide_converter(data, x, y, subject.id = NULL, paired = TRUE, spread = TRUE, ...)
meta_analysis(data, type = "parametric", random = "mixture", digits = 2L, conf.level = 0.95, ...)
one_sample_test(data, x, type = "parametric", test.value = 0, alternative = "two.sided", digits = 2L, conf.level = 0.95, tr = 0.2, bf.prior = 0.707, effsize.type = "g", ...)
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: statsExpressions
Title: Tidy Dataframes and Expressions with Statistical Details
Version: 1.5.4
Version: 1.5.4.9000
Authors@R:
person(given = "Indrajeet",
family = "Patil",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(as_tibble)
export(centrality_description)
export(contingency_table)
export(corr_test)
export(extract_stats_type)
export(long_to_wide_converter)
export(meta_analysis)
export(one_sample_test)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# statsExpressions 1.5.4.9000

# statsExpressions 1.5.4

## MAJOR CHANGES
Expand Down
4 changes: 2 additions & 2 deletions R/add-expression-col.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ add_expression_col <- function(
df_expr <- data %>% # convert needed columns to character type
.data_to_char(digits, digits.df, digits.df.error) %>%
mutate(
statistic.text = statistic.text %||% stat_text_switch(tolower(method)),
es.text = effsize.text %||% estimate_type_switch(tolower(effectsize)),
statistic.text = statistic.text %||% extract_statistic_text(tolower(method)),
es.text = effsize.text %||% extract_estimate_type(tolower(effectsize)),
prior.distribution = prior_switch(tolower(method)),
n.obs = .prettyNum(n.obs)
)
Expand Down
2 changes: 1 addition & 1 deletion R/centrality-description.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ centrality_description <- function(

# styler: off
centrality <- case_match(
stats_type_switch(type),
extract_stats_type(type),
"parametric" ~ "mean",
"nonparametric" ~ "median",
"robust" ~ "trimmed",
Expand Down
2 changes: 1 addition & 1 deletion R/contingency-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contingency_table <- function(
...) {
# data -------------------------------------------

type <- stats_type_switch(type)
type <- extract_stats_type(type)
test <- ifelse(quo_is_null(enquo(y)), "1way", "2way")

data %<>%
Expand Down
2 changes: 1 addition & 1 deletion R/corr-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ corr_test <- function(
tr = 0.2,
bf.prior = 0.707,
...) {
type <- stats_type_switch(type)
type <- extract_stats_type(type)

stats_df <- correlation::correlation(
data = select(ungroup(data), {{ x }}, {{ y }}) %>% tidyr::drop_na(),
Expand Down
5 changes: 2 additions & 3 deletions R/meta-analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#'
#' # ----------------------- Bayesian ----------------------------------
#'
#' suppressWarnings(meta_analysis(dat, type = "bayes"))
#' meta_analysis(dat, type = "bayes")
#'
#' @export
meta_analysis <- function(
Expand All @@ -73,7 +73,7 @@ meta_analysis <- function(
digits = 2L,
conf.level = 0.95,
...) {
type <- stats_type_switch(type)
type <- extract_stats_type(type)

# nolint start: line_length_linter.
c(.ns, .fn, .f.args) %<-% switch(type,
Expand All @@ -85,7 +85,6 @@ meta_analysis <- function(

check_if_installed(.ns)

# construct a call and then extract a tidy data frame
stats_df <- eval(call2(.fn = .fn, .ns = .ns, data = data, !!!.f.args)) %>%
tidy_model_parameters(include_studies = FALSE, ci = conf.level)

Expand Down
4 changes: 2 additions & 2 deletions R/one-sample-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param effsize.type Type of effect size needed for *parametric* tests. The
#' argument can be `"d"` (for Cohen's *d*) or `"g"` (for Hedge's *g*).
#' @inheritParams long_to_wide_converter
#' @inheritParams stats_type_switch
#' @inheritParams extract_stats_type
#' @inheritParams add_expression_col
#' @inheritParams two_sample_test
#' @inheritParams oneway_anova
Expand Down Expand Up @@ -46,7 +46,7 @@ one_sample_test <- function(
bf.prior = 0.707,
effsize.type = "g",
...) {
type <- stats_type_switch(type)
type <- extract_stats_type(type)
x_vec <- stats::na.omit(pull(data, {{ x }}))

# parametric ---------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions R/oneway-anova.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' ```
#'
#' @inheritParams long_to_wide_converter
#' @inheritParams stats_type_switch
#' @inheritParams extract_stats_type
#' @param conf.level Scalar between `0` and `1` (default: `95%`
#' confidence/credible intervals, `0.95`). If `NULL`, no confidence intervals
#' will be computed.
Expand Down Expand Up @@ -145,7 +145,7 @@ oneway_anova <- function(
...) {
# data -------------------------------------------

type <- stats_type_switch(type)
type <- extract_stats_type(type)
c(x, y) %<-% c(ensym(x), ensym(y))

data %<>% long_to_wide_converter(
Expand Down
4 changes: 2 additions & 2 deletions R/pairwise-comparisons.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' comparisons between group levels with corrections for multiple testing.
#'
#' @inheritParams long_to_wide_converter
#' @inheritParams stats_type_switch
#' @inheritParams extract_stats_type
#' @inheritParams oneway_anova
#' @param p.adjust.method Adjustment method for *p*-values for multiple
#' comparisons. Possible methods are: `"holm"` (default), `"hochberg"`,
Expand Down Expand Up @@ -153,7 +153,7 @@ pairwise_comparisons <- function(
...) {
# data -------------------------------------------

type <- stats_type_switch(type)
type <- extract_stats_type(type)
c(x, y) %<-% c(ensym(x), ensym(y))

data %<>% long_to_wide_converter(
Expand Down
25 changes: 14 additions & 11 deletions R/switch-functions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @name stats_type_switch
#' @name extract_stats_type
#' @title Switch the type of statistics.
#'
#' @description
Expand All @@ -19,22 +19,25 @@
#' @autoglobal
#'
#' @examples
#' stats_type_switch("p")
#' stats_type_switch("bf")
#' extract_stats_type("p")
#' extract_stats_type("bf")
#' @export
# styler: off
stats_type_switch <- function(type) {
extract_stats_type <- function(type) {
case_when(
grepl("^n|^s", type) ~ "nonparametric", # s is for Spearman's rho
grepl("^r", type) ~ "robust",
grepl("^b", type) ~ "bayes",
TRUE ~ "parametric"
grepl("^n", type) ~ "nonparametric",
grepl("^r", type) ~ "robust",
grepl("^b", type) ~ "bayes",
TRUE ~ "parametric"
)
}

#' @rdname extract_stats_type
#' @export
stats_type_switch <- extract_stats_type

#' @noRd
estimate_type_switch <- function(x) {
extract_estimate_type <- function(x) {
case_when(
.grepl("pearson's c", x) ~ list(quote(widehat(italic("C"))["Pearson"])),
.grepl("cohen's d", x) ~ list(quote(widehat(italic("d"))["Cohen"])),
Expand Down Expand Up @@ -62,7 +65,7 @@ estimate_type_switch <- function(x) {
}

#' @noRd
stat_text_switch <- function(x) {
extract_statistic_text <- function(x) {
case_when(
grepl("^welch", x) ~ list(quote(italic("t")["Welch"])),
grepl("afex| of means$", x) ~ list(quote(italic("F")["Fisher"])),
Expand Down Expand Up @@ -93,7 +96,7 @@ prior_switch <- function(x) {
)
}

#' @note Don't curry using `purrr::partial()` because it causes `Found a .Internal call` warning.
#' @note Don't curry using `purrr::partial()` because it causes `"Found a .Internal call"` warning.
#' @noRd
.grepl <- function(pattern, x) grepl(pattern, x, fixed = TRUE)

Expand Down
4 changes: 2 additions & 2 deletions R/two-sample-test.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' Parametric, non-parametric, robust, and Bayesian two-sample tests.
#'
#' @inheritParams long_to_wide_converter
#' @inheritParams stats_type_switch
#' @inheritParams extract_stats_type
#' @inheritParams one_sample_test
#' @inheritParams oneway_anova
#' @inheritParams stats::t.test
Expand Down Expand Up @@ -49,7 +49,7 @@ two_sample_test <- function(
...) {
# data -------------------------------------------

type <- stats_type_switch(type)
type <- extract_stats_type(type)
c(x, y) %<-% c(ensym(x), ensym(y))

data %<>% long_to_wide_converter(
Expand Down
9 changes: 6 additions & 3 deletions man/stats_type_switch.Rd → man/extract_stats_type.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/meta_analysis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-meta-random-bayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ test_that(
skip_if_not_installed("metaBMA")

set.seed(123)
df <- suppressWarnings(meta_analysis(
df <- meta_analysis(
type = "bayes",
data = data_meta,
digits = 3L,
iter = 1000L,
summarize = "integrate"
))
)

expect_type(df, "list")

Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-stats-type-switch.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
test_that(
desc = "switch for stats type works",
code = {
expect_identical(stats_type_switch("p"), "parametric")
expect_identical(stats_type_switch("pearson"), "parametric")
expect_identical(stats_type_switch("non-parametric"), "nonparametric")
expect_identical(stats_type_switch("np"), "nonparametric")
expect_identical(stats_type_switch("r"), "robust")
expect_identical(stats_type_switch("bf"), "bayes")
expect_identical(stats_type_switch("xxx"), "parametric")
expect_identical(extract_stats_type("p"), "parametric")
expect_identical(extract_stats_type("pearson"), "parametric")
expect_identical(extract_stats_type("non-parametric"), "nonparametric")
expect_identical(extract_stats_type("np"), "nonparametric")
expect_identical(extract_stats_type("r"), "robust")
expect_identical(extract_stats_type("bf"), "bayes")
expect_identical(extract_stats_type("xxx"), "parametric")
}
)

0 comments on commit 18d5e83

Please sign in to comment.