From 82417ca9afe91b07ba95afd4a6c4375fd54f45e1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 10 May 2024 10:42:51 +0200 Subject: [PATCH] fix --- DESCRIPTION | 1 - R/bootstrap.R | 15 +++++-------- R/xtab_statistics.R | 53 ++++++++++++++++++++++----------------------- man/bootstrap.Rd | 15 ++++--------- 4 files changed, 35 insertions(+), 49 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 51af31c..7cb8cb0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,7 +30,6 @@ Suggests: broom, car, coin, - dplyr, ggplot2, graphics, MASS, diff --git a/R/bootstrap.R b/R/bootstrap.R index 1f8bdbd..444896a 100644 --- a/R/bootstrap.R +++ b/R/bootstrap.R @@ -40,7 +40,7 @@ #' @seealso \code{\link{boot_ci}} to calculate confidence intervals from #' bootstrap samples. #' -#' @examplesIf getRversion() >= "4.2.0" && requireNamespace("dplyr", quietly = TRUE) && requireNamespace("purrr", quietly = TRUE) +#' @examples #' data(efc) #' bs <- bootstrap(efc, 5) #' @@ -61,16 +61,11 @@ #' mean(as.data.frame(x)$c12hour, na.rm = TRUE) #' })) #' -#' # or as tidyverse-approach -#' library(dplyr) -#' library(purrr) -#' bs <- efc |> -#' bootstrap(100) |> -#' mutate( -#' c12hour = map_dbl(strap, ~mean(as.data.frame(.x)$c12hour, na.rm = TRUE)) -#' ) #' # bootstrapped standard error -#' boot_se(bs, c12hour) +#' boot_se(bs, "c12hour") +#' +#' # bootstrapped CI +#' boot_ci(bs, "c12hour") #' @export bootstrap <- function(data, n, size) { if (!missing(size) && !is.null(size)) { diff --git a/R/xtab_statistics.R b/R/xtab_statistics.R index 4e40732..19fc5ab 100644 --- a/R/xtab_statistics.R +++ b/R/xtab_statistics.R @@ -106,7 +106,6 @@ #' ) #' @export crosstable_statistics <- function(data, x1 = NULL, x2 = NULL, statistics = c("auto", "cramer", "phi", "spearman", "kendall", "pearson", "fisher"), weights = NULL, ...) { - insight::check_if_installed("dplyr") # match arguments statistics <- match.arg(statistics) @@ -114,7 +113,20 @@ crosstable_statistics <- function(data, x1 = NULL, x2 = NULL, statistics = c("au stat.html <- NULL # check if data is a table - if (!is.table(data)) { + if (is.table(data)) { + # 'data' is a table - copy to table object + tab <- data + # check if statistics are possible to compute + if (statistics %in% c("spearman", "kendall", "pearson")) { + stop( + sprintf( + "Need arguments `data`, `x1` and `x2` to compute %s-statistics.", + statistics + ), + call. = FALSE + ) + } + } else { # evaluate unquoted names x1 <- deparse(substitute(x1)) x2 <- deparse(substitute(x2)) @@ -146,19 +158,6 @@ crosstable_statistics <- function(data, x1 = NULL, x2 = NULL, statistics = c("au } else { tab <- table(data) } - } else { - # 'data' is a table - copy to table object - tab <- data - # check if statistics are possible to compute - if (statistics %in% c("spearman", "kendall", "pearson")) { - stop( - sprintf( - "Need arguments `data`, `x1` and `x2` to compute %s-statistics.", - statistics - ), - call. = FALSE - ) - } } # get expected values @@ -218,21 +217,21 @@ crosstable_statistics <- function(data, x1 = NULL, x2 = NULL, statistics = c("au } # compute method string - method <- dplyr::case_when( - statistics == "kendall" ~ "Kendall's tau", - statistics == "spearman" ~ "Spearman's rho", - statistics == "pearson" ~ "Pearson's r", - statistics == "cramer" ~ "Cramer's V", - statistics == "phi" ~ "Phi" + method <- ifelse(statistics == "kendall", "Kendall's tau", + ifelse(statistics == "spearman", "Spearman's rho", # nolint + ifelse(statistics == "pearson", "Pearson's r", # nolint + ifelse(statistics == "cramer", "Cramer's V", "Phi") # nolint + ) + ) ) # compute method string - method.html <- dplyr::case_when( - statistics == "kendall" ~ "Kendall's τ", - statistics == "spearman" ~ "Spearman's ρ", - statistics == "pearson" ~ "Pearson's r", - statistics == "cramer" ~ "Cramer's V", - statistics == "phi" ~ "φ" + method.html <- ifelse(statistics == "kendall", "Kendall's τ", + ifelse(statistics == "spearman", "Spearman's ρ", # nolint + ifelse(statistics == "pearson", "Pearson's r", # nolint + ifelse(statistics == "cramer", "Cramer's V", "&phi") # nolint + ) + ) ) # return result diff --git a/man/bootstrap.Rd b/man/bootstrap.Rd index b54c639..e40b784 100644 --- a/man/bootstrap.Rd +++ b/man/bootstrap.Rd @@ -49,7 +49,6 @@ method automatically applies whenever coercion is done because a data frame is required as input. See 'Examples' in \code{\link{boot_ci}}. } \examples{ -\dontshow{if (getRversion() >= "4.2.0" && requireNamespace("dplyr", quietly = TRUE) && requireNamespace("purrr", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} data(efc) bs <- bootstrap(efc, 5) @@ -70,17 +69,11 @@ bs$c12hour <- unlist(lapply(bs$strap, function(x) { mean(as.data.frame(x)$c12hour, na.rm = TRUE) })) -# or as tidyverse-approach -library(dplyr) -library(purrr) -bs <- efc |> - bootstrap(100) |> - mutate( - c12hour = map_dbl(strap, ~mean(as.data.frame(.x)$c12hour, na.rm = TRUE)) - ) # bootstrapped standard error -boot_se(bs, c12hour) -\dontshow{\}) # examplesIf} +boot_se(bs, "c12hour") + +# bootstrapped CI +boot_ci(bs, "c12hour") } \seealso{ \code{\link{boot_ci}} to calculate confidence intervals from