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 2 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
40 changes: 40 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,46 @@
#' @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
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
Expand Down Expand Up @@ -482,7 +522,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 525 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=525,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
16 changes: 16 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,19 @@

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

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

Check warning on line 276 in R/utils.R

View workflow job for this annotation

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

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

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

Check warning on line 284 in R/utils.R

View workflow job for this annotation

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

file=R/utils.R,line=284,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 289 in R/utils.R

View workflow job for this annotation

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

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