Skip to content

Commit

Permalink
Usability improvements for pd() and pd_to_p()
Browse files Browse the repository at this point in the history
Fixes #665
  • Loading branch information
strengejacke committed Aug 31, 2024
1 parent 73d77e3 commit 205e645
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: bayestestR
Title: Understand and Describe Bayesian Models and Posterior Distributions
Version: 0.14.0.2
Version: 0.14.0.3
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ S3method(p_significance,stanfit)
S3method(p_significance,stanreg)
S3method(p_to_bf,default)
S3method(p_to_bf,numeric)
S3method(pd_to_p,data.frame)
S3method(pd_to_p,numeric)
S3method(plot,bayesfactor_models)
S3method(plot,bayesfactor_parameters)
S3method(plot,bayestestR_eti)
Expand Down
4 changes: 2 additions & 2 deletions R/bayesfactor_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,10 @@ as.matrix.bayesfactor_models <- function(x, ...) {
is_wrapped <- grepl("(", m_txt, fixed = TRUE)
m_txt[!is_wrapped] <- paste0("(", m_txt[!is_wrapped], ")")

return(m_txt)
m_txt
},
error = function(e) {
return(m_names)
m_names
}
)
}
Expand Down
25 changes: 24 additions & 1 deletion R/convert_pd_to_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@
#' pd_to_p(pd = 0.95, direction = "one-sided")
#'
#' @export
pd_to_p <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
pd_to_p <- function(pd, ...) {
UseMethod("pd_to_p")
}


#' @export
#' @rdname pd_to_p
pd_to_p.numeric <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
p <- 1 - pd
if (.get_direction(direction) == 0) {
p <- 2 * p
Expand All @@ -56,6 +63,22 @@ pd_to_p <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
}


#' @export
pd_to_p.data.frame <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
# check if data frame has an appropriate column
pd_column <- intersect(c("pd", "p_direction", "PD"), colnames(pd))[1]
if (is.na(pd_column) || length(pd_column) == 0) {
insight::format_error("No column named `pd`, `p_direction`, or `PD` found.")
}

# add p-value column
pd$p <- pd_to_p(as.numeric(pd[[pd_column]]))
# remove pd-column
pd[[pd_column]] <- NULL
pd
}


#' @rdname pd_to_p
#' @export
p_to_pd <- function(p, direction = "two-sided", ...) {
Expand Down
2 changes: 1 addition & 1 deletion man/bayestestR-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions man/pd_to_p.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 205e645

Please sign in to comment.