Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support {distributional} package #674

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Suggests:
bridgesampling,
brms,
curl,
distributional,
effectsize,
emmeans,
gamm4,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions R/p_direction.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#' @section Methods of computation:
#'
#' The *pd* is defined as:
#' \deqn{p_d = max({Pr(\hat{\theta} < \theta_{null}), Pr(\hat{\theta} > \theta_{null})})}{pd = max(mean(x < null), mean(x > null))}

Check warning on line 79 in R/p_direction.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/p_direction.R,line=79,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 131 characters.
#'
#' The most simple and direct way to compute the *pd* is to compute the
#' proportion of positive (or larger than `null`) posterior samples, the
Expand All @@ -98,7 +98,7 @@
#'
#' @seealso [pd_to_p()] to convert between Probability of Direction (pd) and p-value.
#'
#' @note There is also a [`plot()`-method](https://easystats.github.io/see/articles/bayestestR.html) implemented in the \href{https://easystats.github.io/see/}{\pkg{see}-package}.

Check warning on line 101 in R/p_direction.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/p_direction.R,line=101,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 179 characters.
#'
#' @references
#' - Makowski, D., Ben-Shachar, M. S., Chen, S. A., & Lüdecke, D. (2019).
Expand All @@ -108,7 +108,7 @@
#' (2021). A cautionary note on estimating effect size. Advances in Methods
#' and Practices in Psychological Science, 4(1). \doi{10.1177/2515245921992035}
#'
#' @examplesIf requireNamespace("rstanarm", quietly = TRUE) && requireNamespace("emmeans", quietly = TRUE) && requireNamespace("brms", quietly = TRUE) && requireNamespace("BayesFactor", quietly = TRUE)

Check warning on line 111 in R/p_direction.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/p_direction.R,line=111,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 201 characters.
#' library(bayestestR)
#'
#' # Simulate a posterior distribution of mean 1 and SD 1
Expand Down Expand Up @@ -286,6 +286,45 @@
#' @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)
mattansb marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down Expand Up @@ -482,7 +521,7 @@
#' @export
p_direction.stanreg <- function(x,
effects = c("fixed", "random", "all"),
component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"),

Check warning on line 524 in R/p_direction.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/p_direction.R,line=524,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 136 characters.
parameters = NULL,
method = "direct",
null = 0,
Expand Down
17 changes: 17 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,20 @@

insight::format_error("The `rvar_col` argument must be a single, valid column name.")
}

#' @keywords internal
.clean_distributional <- function (d) {

Check warning on line 317 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils.R,line=317,col=34,[function_left_parentheses_linter] Remove spaces before the left parenthesis in a function definition.
insight::check_if_installed("distributional")
nm <- format(d)
attributes(d) <- NULL
names(d) <- nm
d
}

#' @keywords internal
.is_discrete_dist <- function (d) {

Check warning on line 326 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils.R,line=326,col=30,[function_left_parentheses_linter] Remove spaces before the left parenthesis in a function definition.
inherits(d, c("dist_bernoulli", "dist_binomial", "dist_categorical",
"dist_geometric", "dist_logarithmic", "dist_multinomial",
"dist_negative_binomial", "dist_poisson",
"dist_poisson_inverse_gaussian"))
}

Check warning on line 331 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils.R,line=331,col=2,[trailing_blank_lines_linter] Add a terminal newline.
Loading