From 0722e361d93aad1bd9b1bf7948be97ff698f12a2 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sat, 14 Jan 2023 20:15:10 +0100 Subject: [PATCH] Don't remove all-NA columns; retain NAs in labels (#226) * Don't remove all-NA columns Closes https://github.com/IndrajeetPatil/ggstatsplot/issues/833 * don't remove NAs from labels Closes #176 * Apply automatic changes * Update print.R * suppress messages in docs Co-authored-by: IndrajeetPatil --- NEWS.md | 7 +++++ R/add_expression_col.R | 9 ++++-- R/helpers_easystats.R | 2 +- R/oneway_anova.R | 2 +- R/print.R | 12 ++------ R/tidy_model_expressions.R | 2 +- R/two_sample_test.R | 2 +- man/contingency_table.Rd | 4 +-- man/corr_test.Rd | 4 +-- man/meta_analysis.Rd | 4 +-- man/one_sample_test.Rd | 4 +-- man/oneway_anova.Rd | 6 ++-- man/pairwise_comparisons.Rd | 4 +-- man/rmd-fragments/return.Rmd | 4 +-- man/two_sample_test.Rd | 7 ++--- tests/testthat/_snaps/meta_random_bayes.md | 26 ++++++++-------- .../testthat/_snaps/meta_random_parametric.md | 11 ++++--- tests/testthat/_snaps/meta_random_robust.md | 8 ++--- tests/testthat/_snaps/oneway_anova_robust.md | 30 +++++++++++++++++++ tests/testthat/test-oneway_anova_robust.R | 22 ++++++++++++++ vignettes/web_only/dataframe_outputs.Rmd | 2 +- 21 files changed, 107 insertions(+), 65 deletions(-) diff --git a/NEWS.md b/NEWS.md index f46ca4c7..f32b731d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,13 @@ NEW FEATURES - All returned data frames from `{statsExpressions}` get a new `statsExpressions` class and a print method for this class. +MINOR CHANGES + +- `tidy_model_parameters()` no longer removes columns which contain only missing + values. + +- Wilcox tests no longer provide exact *p*-values. + # statsExpressions 1.3.6 - Maintenance release. diff --git a/R/add_expression_col.R b/R/add_expression_col.R index 6298a3d6..060e33a7 100644 --- a/R/add_expression_col.R +++ b/R/add_expression_col.R @@ -166,12 +166,15 @@ add_expression_col <- function(data, .data_to_char <- function(data, k = 2L, k.df = 0L, k.df.error = 0L) { data %>% mutate( - across(.fns = ~ format_value(.x, k), .cols = matches("^est|^sta|p.value|.scale$|.low$|.high$|^log")), - across(.fns = ~ format_value(.x, k.df), .cols = matches("^df$")), - across(.fns = ~ format_value(.x, k.df.error), .cols = matches("^df.error$")), + across(.fns = ~ .to_char(.x, k), .cols = matches("^est|^sta|p.value|.scale$|.low$|.high$|^log")), + across(.fns = ~ .to_char(.x, k.df), .cols = matches("^df$")), + across(.fns = ~ .to_char(.x, k.df.error), .cols = matches("^df.error$")), across(.fns = ~ paste0(.x * 100, "%"), .cols = matches("^conf.level$")) ) } #' @noRd .prettyNum <- function(x) prettyNum(x, big.mark = ",", scientific = FALSE) + +#' @noRd +.to_char <- function(x, digits = 2L) format_value(x, digits, missing = "NA") diff --git a/R/helpers_easystats.R b/R/helpers_easystats.R index 8b336f9f..987c904a 100644 --- a/R/helpers_easystats.R +++ b/R/helpers_easystats.R @@ -10,7 +10,7 @@ tidy_model_parameters <- function(model, ...) { stats_df <- model_parameters(model, verbose = FALSE, ...) %>% mutate(conf.method = . %@% "ci_method") %>% - select(where(~ !all(is.na(.x))), -matches("Difference")) %>% # remove columns where all rows are NAs + select(-matches("Difference")) %>% standardize_names(style = "broom") %>% rename_all(~ gsub("cramers.", "", .x)) %>% rename_all(.funs = recode, "bayes.factor" = "bf10") %>% diff --git a/R/oneway_anova.R b/R/oneway_anova.R index e89be3ca..2d80406c 100644 --- a/R/oneway_anova.R +++ b/R/oneway_anova.R @@ -45,7 +45,7 @@ #' # for reproducibility #' set.seed(123) #' library(statsExpressions) -#' library(afex) # for within-subjects parametric ANOVA +#' suppressPackageStartupMessages(library(afex)) # for within-subjects parametric ANOVA #' #' # ----------------------- parametric ------------------------------------- #' diff --git a/R/print.R b/R/print.R index 5530b20f..2ba115f4 100644 --- a/R/print.R +++ b/R/print.R @@ -1,16 +1,8 @@ #' @export print.statsExpressions <- function(x, ...) { withr::with_options( - list( - tibble.width = Inf, - pillar.bold = TRUE, - pillar.neg = TRUE, - pillar.subtle_num = TRUE, - pillar.min_chars = Inf - ), - code = { - NextMethod() - } + list(pillar.width = Inf, pillar.bold = TRUE, pillar.subtle_num = TRUE), + NextMethod() ) invisible(x) diff --git a/R/tidy_model_expressions.R b/R/tidy_model_expressions.R index b9cdf3cc..72dd0e95 100644 --- a/R/tidy_model_expressions.R +++ b/R/tidy_model_expressions.R @@ -59,7 +59,7 @@ tidy_model_expressions <- function(data, if (statistic == "t") { df %<>% mutate( expression = case_when( - df.error %in% c("", "Inf") ~ glue("list({es.text}=='{estimate}', italic(t)=='{statistic}', italic(p)=='{p.value}')"), + df.error %in% c("NA", "Inf") ~ glue("list({es.text}=='{estimate}', italic(t)=='{statistic}', italic(p)=='{p.value}')"), TRUE ~ glue("list({es.text}=='{estimate}', italic(t)('{df.error}')=='{statistic}', italic(p)=='{p.value}')") ) ) diff --git a/R/two_sample_test.R b/R/two_sample_test.R index 5f86c438..a94e3531 100644 --- a/R/two_sample_test.R +++ b/R/two_sample_test.R @@ -24,7 +24,7 @@ #' ```{r child="man/rmd-fragments/return.Rmd"} #' ``` #' -#' @examples identical(Sys.getenv("NOT_CRAN"), "true") +#' @examplesIf identical(Sys.getenv("NOT_CRAN"), "true") #' # for reproducibility #' set.seed(123) #' library(statsExpressions) diff --git a/man/contingency_table.Rd b/man/contingency_table.Rd index 52c947b0..2ca897e4 100644 --- a/man/contingency_table.Rd +++ b/man/contingency_table.Rd @@ -100,9 +100,7 @@ two degrees of freedom (e.g. anova) \item \code{expression}: pre-formatted expression containing statistical details } -For examples of dataframe outputs, see examples and \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{this vignette}. - -Note that all examples are preceded by \code{set.seed()} calls for reproducibility. +For examples, see \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{data frame output vignette}. } \description{ Parametric and Bayesian one-way and two-way contingency table analyses. diff --git a/man/corr_test.Rd b/man/corr_test.Rd index 43b7ec1e..0f89cf65 100644 --- a/man/corr_test.Rd +++ b/man/corr_test.Rd @@ -79,9 +79,7 @@ two degrees of freedom (e.g. anova) \item \code{expression}: pre-formatted expression containing statistical details } -For examples of dataframe outputs, see examples and \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{this vignette}. - -Note that all examples are preceded by \code{set.seed()} calls for reproducibility. +For examples, see \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{data frame output vignette}. } \description{ Parametric, non-parametric, robust, and Bayesian correlation test. diff --git a/man/meta_analysis.Rd b/man/meta_analysis.Rd index 556ea4e2..af449e2d 100644 --- a/man/meta_analysis.Rd +++ b/man/meta_analysis.Rd @@ -69,9 +69,7 @@ two degrees of freedom (e.g. anova) \item \code{expression}: pre-formatted expression containing statistical details } -For examples of dataframe outputs, see examples and \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{this vignette}. - -Note that all examples are preceded by \code{set.seed()} calls for reproducibility. +For examples, see \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{data frame output vignette}. } \description{ Parametric, non-parametric, robust, and Bayesian random-effects meta-analysis. diff --git a/man/one_sample_test.Rd b/man/one_sample_test.Rd index 7d04dbc3..fb0ce9fa 100644 --- a/man/one_sample_test.Rd +++ b/man/one_sample_test.Rd @@ -88,9 +88,7 @@ two degrees of freedom (e.g. anova) \item \code{expression}: pre-formatted expression containing statistical details } -For examples of dataframe outputs, see examples and \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{this vignette}. - -Note that all examples are preceded by \code{set.seed()} calls for reproducibility. +For examples, see \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{data frame output vignette}. } \description{ Parametric, non-parametric, robust, and Bayesian one-sample tests. diff --git a/man/oneway_anova.Rd b/man/oneway_anova.Rd index 99e2ff1d..671b3c61 100644 --- a/man/oneway_anova.Rd +++ b/man/oneway_anova.Rd @@ -114,9 +114,7 @@ two degrees of freedom (e.g. anova) \item \code{expression}: pre-formatted expression containing statistical details } -For examples of dataframe outputs, see examples and \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{this vignette}. - -Note that all examples are preceded by \code{set.seed()} calls for reproducibility. +For examples, see \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{data frame output vignette}. } \description{ Parametric, non-parametric, robust, and Bayesian one-way ANOVA. @@ -178,7 +176,7 @@ The table below provides summary about: # for reproducibility set.seed(123) library(statsExpressions) -library(afex) # for within-subjects parametric ANOVA +suppressPackageStartupMessages(library(afex)) # for within-subjects parametric ANOVA # ----------------------- parametric ------------------------------------- diff --git a/man/pairwise_comparisons.Rd b/man/pairwise_comparisons.Rd index 13ca2d6d..037d49c2 100644 --- a/man/pairwise_comparisons.Rd +++ b/man/pairwise_comparisons.Rd @@ -105,9 +105,7 @@ two degrees of freedom (e.g. anova) \item \code{expression}: pre-formatted expression containing statistical details } -For examples of dataframe outputs, see examples and \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{this vignette}. - -Note that all examples are preceded by \code{set.seed()} calls for reproducibility. +For examples, see \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{data frame output vignette}. } \description{ Calculate parametric, non-parametric, robust, and Bayes Factor pairwise diff --git a/man/rmd-fragments/return.Rmd b/man/rmd-fragments/return.Rmd index 3cdd270c..1411dc54 100644 --- a/man/rmd-fragments/return.Rmd +++ b/man/rmd-fragments/return.Rmd @@ -30,6 +30,4 @@ The returned tibble data frame can contain some or all of the following columns - `expression`: pre-formatted expression containing statistical details -For examples of dataframe outputs, see examples and [this vignette](https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html). - -Note that all examples are preceded by `set.seed()` calls for reproducibility. +For examples, see [data frame output vignette](https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html). diff --git a/man/two_sample_test.Rd b/man/two_sample_test.Rd index 250858de..636b8b81 100644 --- a/man/two_sample_test.Rd +++ b/man/two_sample_test.Rd @@ -119,9 +119,7 @@ two degrees of freedom (e.g. anova) \item \code{expression}: pre-formatted expression containing statistical details } -For examples of dataframe outputs, see examples and \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{this vignette}. - -Note that all examples are preceded by \code{set.seed()} calls for reproducibility. +For examples, see \href{https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html}{data frame output vignette}. } \description{ Parametric, non-parametric, robust, and Bayesian two-sample tests. @@ -179,7 +177,7 @@ The table below provides summary about: } \examples{ -identical(Sys.getenv("NOT_CRAN"), "true") +\dontshow{if (identical(Sys.getenv("NOT_CRAN"), "true")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} # for reproducibility set.seed(123) library(statsExpressions) @@ -263,4 +261,5 @@ two_sample_test( subject.id = subject, type = "bayes" ) +\dontshow{\}) # examplesIf} } diff --git a/tests/testthat/_snaps/meta_random_bayes.md b/tests/testthat/_snaps/meta_random_bayes.md index 1917054b..b69a7f04 100644 --- a/tests/testthat/_snaps/meta_random_bayes.md +++ b/tests/testthat/_snaps/meta_random_bayes.md @@ -3,23 +3,23 @@ Code dplyr::select(df, -expression) Output - # A tibble: 2 x 16 + # A tibble: 2 x 19 term effectsize estimate std.error conf.level 1 Overall meta-analytic posterior estimate -0.650 0.222 0.95 2 tau meta-analytic posterior estimate 0.486 0.184 0.95 - conf.low conf.high bf10 component prior.distribution prior.location - - 1 -1.12 -0.251 53.0 meta Student's t 0 - 2 0.205 0.917 53.0 meta Inverse gamma 1 - prior.scale method conf.method log_e_bf10 - - 1 0.707 Bayesian meta-analysis using 'metaBMA' ETI 3.97 - 2 0.15 Bayesian meta-analysis using 'metaBMA' ETI 3.97 - n.obs - - 1 16 - 2 16 + conf.low conf.high weight bf10 rhat ess component prior.distribution + + 1 -1.12 -0.251 NA 53.0 NA NA meta Student's t + 2 0.205 0.917 NA 53.0 NA NA meta Inverse gamma + prior.location prior.scale method conf.method + + 1 0 0.707 Bayesian meta-analysis using 'metaBMA' ETI + 2 1 0.15 Bayesian meta-analysis using 'metaBMA' ETI + log_e_bf10 n.obs + + 1 3.97 16 + 2 3.97 16 --- diff --git a/tests/testthat/_snaps/meta_random_parametric.md b/tests/testthat/_snaps/meta_random_parametric.md index 7893ebd7..ae883702 100644 --- a/tests/testthat/_snaps/meta_random_parametric.md +++ b/tests/testthat/_snaps/meta_random_parametric.md @@ -3,13 +3,16 @@ Code select(df, -expression) Output - # A tibble: 1 x 12 + # A tibble: 1 x 13 term effectsize estimate std.error conf.level conf.low 1 Overall meta-analytic summary estimate -0.767 0.212 0.95 -1.18 - conf.high statistic p.value method conf.method n.obs - - 1 -0.351 -3.62 0.000295 Meta-analysis using 'metafor' Wald 16 + conf.high statistic p.value weight method conf.method + + 1 -0.351 -3.62 0.000295 NA Meta-analysis using 'metafor' Wald + n.obs + + 1 16 --- diff --git a/tests/testthat/_snaps/meta_random_robust.md b/tests/testthat/_snaps/meta_random_robust.md index 59d77485..d07fde7a 100644 --- a/tests/testthat/_snaps/meta_random_robust.md +++ b/tests/testthat/_snaps/meta_random_robust.md @@ -3,13 +3,13 @@ Code select(df, -expression) Output - # A tibble: 1 x 12 + # A tibble: 1 x 13 term effectsize estimate std.error conf.low conf.high 1 Overall meta-analytic summary estimate -0.746 0.234 -1.26 -0.343 - statistic p.value conf.level method - - 1 -3.20 0.000501 0.95 Robust meta-analysis using 'metaplus' + statistic p.value weight conf.level method + + 1 -3.20 0.000501 NA 0.95 Robust meta-analysis using 'metaplus' conf.method n.obs 1 Wald 16 diff --git a/tests/testthat/_snaps/oneway_anova_robust.md b/tests/testthat/_snaps/oneway_anova_robust.md index 52c25d69..10f23811 100644 --- a/tests/testthat/_snaps/oneway_anova_robust.md +++ b/tests/testthat/_snaps/oneway_anova_robust.md @@ -90,3 +90,33 @@ italic("n")["pairs"] == "88") +--- + + Code + select(df2, -expression) + Output + # A tibble: 1 x 11 + statistic df df.error p.value + + 1 22.1 1 3 0.0182 + method + + 1 A heteroscedastic one-way repeated measures ANOVA for trimmed means + effectsize estimate + + 1 Algina-Keselman-Penfield robust standardized difference average -Inf + conf.level conf.low conf.high n.obs + + 1 0.95 -Inf NaN 4 + +--- + + Code + df2[["expression"]] + Output + [[1]] + list(italic("F")["trimmed-means"](1, 3) == "22.09", italic(p) == + "0.02", widehat(delta)["R-avg"]^"AKP" == "-Inf", CI["95%"] ~ + "[" * "-Inf", "NA" * "]", italic("n")["pairs"] == "4") + + diff --git a/tests/testthat/test-oneway_anova_robust.R b/tests/testthat/test-oneway_anova_robust.R index 15683682..1ca47266 100644 --- a/tests/testthat/test-oneway_anova_robust.R +++ b/tests/testthat/test-oneway_anova_robust.R @@ -55,5 +55,27 @@ test_that( set.seed(123) expect_snapshot(select(df1, -expression)) expect_snapshot(df1[["expression"]]) + + # edge case with few cases + + dat <- tibble( + cases = c(1, 1, 2, 2, 3, 3, 4, 4), + groups = c("b", "a", "b", "a", "b", "a", "b", "a"), + values = c(2, 5, 3, 4, 4, 6, 5, 8) + ) + + set.seed(123) + df2 <- oneway_anova( + data = dat, + x = groups, + y = values, + type = "r", + conf.level = 0.99, + paired = TRUE + ) + + set.seed(123) + expect_snapshot(select(df2, -expression)) + expect_snapshot(df2[["expression"]]) } ) diff --git a/vignettes/web_only/dataframe_outputs.Rmd b/vignettes/web_only/dataframe_outputs.Rmd index 5978063e..765739bc 100644 --- a/vignettes/web_only/dataframe_outputs.Rmd +++ b/vignettes/web_only/dataframe_outputs.Rmd @@ -215,7 +215,7 @@ two_sample_test( ```{r} #| label = "anova_w" -library(afex) +suppressPackageStartupMessages(library(afex)) # ----------------------- parametric ---------------------------------------