diff --git a/DESCRIPTION b/DESCRIPTION index 42d55df64..db7038791 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -82,6 +82,7 @@ Suggests: bridgesampling, brms, curl, + distributional, effectsize, emmeans, gamm4, diff --git a/NAMESPACE b/NAMESPACE index 0151ef477..a5fe4d344 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) diff --git a/R/p_direction.R b/R/p_direction.R index c5b1f84e0..7110cee8d 100644 --- a/R/p_direction.R +++ b/R/p_direction.R @@ -286,6 +286,45 @@ 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) + high <- 1 - low + if (.is_discrete_dist(x[[i]])) { + low <- low - stats::density(x[[i]], at = null) + } + 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 diff --git a/R/utils.R b/R/utils.R index e8aa83116..00c37c959 100644 --- a/R/utils.R +++ b/R/utils.R @@ -312,3 +312,20 @@ insight::format_error("The `rvar_col` argument must be a single, valid column name.") } + +#' @keywords internal +.clean_distributional <- function (d) { + insight::check_if_installed("distributional") + 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