From 8d74554eb6f95592a33691eb93a2174193765ead Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Fri, 6 Sep 2024 23:05:29 +0300 Subject: [PATCH 1/5] p_direction --- DESCRIPTION | 1 + R/p_direction.R | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index b592a6a4f..8800af169 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -82,6 +82,7 @@ Suggests: bridgesampling, brms, curl, + distributional, effectsize, emmeans, gamm4, diff --git a/R/p_direction.R b/R/p_direction.R index c5b1f84e0..2e8584302 100644 --- a/R/p_direction.R +++ b/R/p_direction.R @@ -286,6 +286,46 @@ p_direction.draws <- function(x, #' @export p_direction.rvar <- p_direction.draws +#' @export +p_direction.distribution <- function(x, + null = 0, + as_p = FALSE, + remove_na = TRUE, + ...) { + obj_name <- insight::safe_deparse_symbol(substitute(x)) + x <- .clean_distributional(x) + pd <- numeric(length = length(x)) + + for (i in seq_along(pd)) { + low <- distributional::cdf(x[[i]], q = null) + if (.is_discrete_dist(x[[i]])) { + high <- 1 - (low + stats::density(x[[i]], at = null)) + } else { + high <- 1 - low + } + pd[i] <- max(low, high) + } + + out <- data.frame( + Parameter = names(x), + pd = pd, + row.names = NULL, + stringsAsFactors = FALSE + ) + + # rename column + if (as_p) { + out$pd <- pd_to_p(out$pd) + colnames(out)[2] <- "p" + } + + attr(out, "object_name") <- obj_name + attr(out, "as_p") <- as_p + class(out) <- unique(c("p_direction", "see_p_direction", class(out))) + + out +} + #' @rdname p_direction #' @export From dd620c8439c1c3cbe5d92e0c697d8d593ec5a7e0 Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Fri, 6 Sep 2024 23:09:55 +0300 Subject: [PATCH 2/5] Update utils.R --- R/utils.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/R/utils.R b/R/utils.R index 876e7debd..e17b5f3f0 100644 --- a/R/utils.R +++ b/R/utils.R @@ -271,3 +271,19 @@ insight::format_error("The `rvar_col` argument must be a single, valid column name.") } + +#' @keywords internal +.clean_distributional <- function (d) { + nm <- format(d) + attributes(d) <- NULL + names(d) <- nm + d +} + +#' @keywords internal +.is_discrete_dist <- function (d) { + inherits(d, c("dist_bernoulli", "dist_binomial", "dist_categorical", + "dist_geometric", "dist_logarithmic", "dist_multinomial", + "dist_negative_binomial", "dist_poisson", + "dist_poisson_inverse_gaussian")) +} \ No newline at end of file From df83a943bb528c3368bb8cd30e4085723f224f5c Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Fri, 6 Sep 2024 23:15:03 +0300 Subject: [PATCH 3/5] Update NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index 76db6d047..9c5b73b48 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -320,6 +320,7 @@ S3method(p_direction,brmsfit) S3method(p_direction,comparisons) S3method(p_direction,data.frame) S3method(p_direction,default) +S3method(p_direction,distribution) S3method(p_direction,draws) S3method(p_direction,emmGrid) S3method(p_direction,emm_list) From 87e84efd4ec13e97872dd579305901a7ba541f46 Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Fri, 6 Sep 2024 23:16:52 +0300 Subject: [PATCH 4/5] Update utils.R --- R/utils.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/utils.R b/R/utils.R index e17b5f3f0..9cee1ae52 100644 --- a/R/utils.R +++ b/R/utils.R @@ -274,6 +274,7 @@ #' @keywords internal .clean_distributional <- function (d) { + insight::check_if_installed("distributional") nm <- format(d) attributes(d) <- NULL names(d) <- nm From 436315d814f2574cd586f1990f068fee6344c91b Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Fri, 6 Sep 2024 23:29:19 +0300 Subject: [PATCH 5/5] Update p_direction.R --- R/p_direction.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/p_direction.R b/R/p_direction.R index 2e8584302..7110cee8d 100644 --- a/R/p_direction.R +++ b/R/p_direction.R @@ -298,10 +298,9 @@ p_direction.distribution <- function(x, for (i in seq_along(pd)) { low <- distributional::cdf(x[[i]], q = null) + high <- 1 - low if (.is_discrete_dist(x[[i]])) { - high <- 1 - (low + stats::density(x[[i]], at = null)) - } else { - high <- 1 - low + low <- low - stats::density(x[[i]], at = null) } pd[i] <- max(low, high) }