From 02429bc7968b57f1f66ef88c09ae6141ad60dcaf Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 10 May 2024 00:00:28 +0200 Subject: [PATCH] fix --- NAMESPACE | 2 -- R/boot_ci.R | 23 ++++++++++++++--------- R/gmd.R | 23 +++++++++++------------ man/boot_ci.Rd | 29 ++++++++++++++++------------- man/gmd.Rd | 4 ++-- 5 files changed, 43 insertions(+), 38 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 3de2e0c..3fe93bc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -123,9 +123,7 @@ importFrom(performance,mse) importFrom(performance,rmse) importFrom(purrr,map) importFrom(purrr,map_dbl) -importFrom(purrr,map_df) importFrom(purrr,map_lgl) -importFrom(sjmisc,is_empty) importFrom(sjmisc,is_float) importFrom(sjmisc,str_contains) importFrom(sjmisc,typical_value) diff --git a/R/boot_ci.R b/R/boot_ci.R index ba3cf81..dd8254e 100644 --- a/R/boot_ci.R +++ b/R/boot_ci.R @@ -61,11 +61,14 @@ #' bs <- bootstrap(efc, 100) #' #' # now run models for each bootstrapped sample -#' bs$models <- map(bs$strap, ~lm(neg_c_7 ~ e42dep + c161sex, data = .x)) +#' bs$models <- lapply( +#' bs$strap, +#' function(.x) lm(neg_c_7 ~ e42dep + c161sex, data = .x) +#' ) #' #' # extract coefficient "dependency" and "gender" from each model -#' bs$dependency <- map_dbl(bs$models, ~coef(.x)[2]) -#' bs$gender <- map_dbl(bs$models, ~coef(.x)[3]) +#' bs$dependency <- vapply(bs$models, function(x) coef(x)[2], numeric(1)) +#' bs$gender <- vapply(bs$models, function(x) coef(x)[3], numeric(1)) #' #' # get bootstrapped confidence intervals #' boot_ci(bs$dependency) @@ -76,9 +79,9 @@ #' #' # alternative function calls. #' boot_ci(bs$dependency) -#' boot_ci(bs, dependency) -#' boot_ci(bs, dependency, gender) -#' boot_ci(bs, dependency, gender, method = "q") +#' boot_ci(bs, "dependency") +#' boot_ci(bs, c("dependency", "gender")) +#' boot_ci(bs, c("dependency", "gender"), method = "q") #' #' #' # compare coefficients @@ -130,13 +133,15 @@ #' # compute the CI for all bootstrapped model coefficients #' boot_ci()} #' @export -boot_ci <- function(data, ..., method = c("dist", "quantile"), ci.lvl = 0.95) { - insight::check_if_installed("dplyr") +boot_ci <- function(data, select = NULL, method = c("dist", "quantile"), ci.lvl = 0.95) { # match arguments method <- match.arg(method) # evaluate arguments, generate data - .dat <- get_dot_data(data, dplyr::quos(...)) + if (is.null(select)) + .dat <- as.data.frame(data) + else + .dat <- data[select] # compute confidence intervals for all values transform_boot_result(lapply(.dat, function(x) { diff --git a/R/gmd.R b/R/gmd.R index 2fed09a..98ad9c5 100644 --- a/R/gmd.R +++ b/R/gmd.R @@ -21,21 +21,20 @@ #' @examples #' data(efc) #' gmd(efc$e17age) -#' gmd(efc, e17age, c160age, c12hour) +#' gmd(efc, c("e17age", "c160age", "c12hour")) #' -#' @importFrom purrr map_df -#' @importFrom sjmisc is_empty #' @export -gmd <- function(x, ...) { - insight::check_if_installed("dplyr") - # evaluate dots - qs <- dplyr::quos(...) - if (!sjmisc::is_empty(qs)) x <- suppressMessages(dplyr::select(x, !!!qs)) - - if (is.data.frame(x)) - purrr::map_df(x, gmd_helper) - else +gmd <- function(x, select = NULL) { + if (is.data.frame(x)) { + do.call(rbind, lapply(select, function(i) { + data.frame( + variable = i, + gmd = gmd_helper(x[[i]]) + ) + })) + } else { gmd_helper(x) + } } diff --git a/man/boot_ci.Rd b/man/boot_ci.Rd index 7f4c252..4d57d31 100644 --- a/man/boot_ci.Rd +++ b/man/boot_ci.Rd @@ -7,7 +7,7 @@ \alias{boot_est} \title{Standard error and confidence intervals for bootstrapped estimates} \usage{ -boot_ci(data, ..., method = c("dist", "quantile"), ci.lvl = 0.95) +boot_ci(data, select = NULL, method = c("dist", "quantile"), ci.lvl = 0.95) boot_se(data, ...) @@ -19,12 +19,6 @@ boot_est(data, ...) \item{data}{A data frame that containts the vector with bootstrapped estimates, or directly the vector (see 'Examples').} -\item{...}{Optional, unquoted names of variables with bootstrapped estimates. -Required, if either \code{data} is a data frame (and no vector), -and only selected variables from \code{data} should be processed. -You may also use functions like \code{:} or tidyselect's -\code{select_helpers()}.} - \item{method}{Character vector, indicating if confidence intervals should be based on bootstrap standard error, multiplied by the value of the quantile function of the t-distribution (default), or on sample @@ -32,6 +26,12 @@ quantiles of the bootstrapped values. See 'Details' in \code{boot_ci()}. May be abbreviated.} \item{ci.lvl}{Numeric, the level of the confidence intervals.} + +\item{...}{Optional, unquoted names of variables with bootstrapped estimates. +Required, if either \code{data} is a data frame (and no vector), +and only selected variables from \code{data} should be processed. +You may also use functions like \code{:} or tidyselect's +\code{select_helpers()}.} } \value{ A data frame with either bootstrap estimate, @@ -79,11 +79,14 @@ data(efc) bs <- bootstrap(efc, 100) # now run models for each bootstrapped sample -bs$models <- map(bs$strap, ~lm(neg_c_7 ~ e42dep + c161sex, data = .x)) +bs$models <- lapply( + bs$strap, + function(.x) lm(neg_c_7 ~ e42dep + c161sex, data = .x) +) # extract coefficient "dependency" and "gender" from each model -bs$dependency <- map_dbl(bs$models, ~coef(.x)[2]) -bs$gender <- map_dbl(bs$models, ~coef(.x)[3]) +bs$dependency <- vapply(bs$models, function(x) coef(x)[2], numeric(1)) +bs$gender <- vapply(bs$models, function(x) coef(x)[3], numeric(1)) # get bootstrapped confidence intervals boot_ci(bs$dependency) @@ -94,9 +97,9 @@ confint(fit)[2, ] # alternative function calls. boot_ci(bs$dependency) -boot_ci(bs, dependency) -boot_ci(bs, dependency, gender) -boot_ci(bs, dependency, gender, method = "q") +boot_ci(bs, "dependency") +boot_ci(bs, c("dependency", "gender")) +boot_ci(bs, c("dependency", "gender"), method = "q") # compare coefficients diff --git a/man/gmd.Rd b/man/gmd.Rd index 03823df..f8ca77f 100644 --- a/man/gmd.Rd +++ b/man/gmd.Rd @@ -4,7 +4,7 @@ \alias{gmd} \title{Gini's Mean Difference} \usage{ -gmd(x, ...) +gmd(x, select = NULL) } \arguments{ \item{x}{A vector or data frame.} @@ -30,7 +30,7 @@ silently removed. \examples{ data(efc) gmd(efc$e17age) -gmd(efc, e17age, c160age, c12hour) +gmd(efc, c("e17age", "c160age", "c12hour")) } \references{