Skip to content

Commit 205e645

Browse files
committed
Usability improvements for pd() and pd_to_p()
Fixes #665
1 parent 73d77e3 commit 205e645

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: bayestestR
33
Title: Understand and Describe Bayesian Models and Posterior Distributions
4-
Version: 0.14.0.2
4+
Version: 0.14.0.3
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ S3method(p_significance,stanfit)
368368
S3method(p_significance,stanreg)
369369
S3method(p_to_bf,default)
370370
S3method(p_to_bf,numeric)
371+
S3method(pd_to_p,data.frame)
372+
S3method(pd_to_p,numeric)
371373
S3method(plot,bayesfactor_models)
372374
S3method(plot,bayesfactor_parameters)
373375
S3method(plot,bayestestR_eti)

R/bayesfactor_models.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,10 @@ as.matrix.bayesfactor_models <- function(x, ...) {
562562
is_wrapped <- grepl("(", m_txt, fixed = TRUE)
563563
m_txt[!is_wrapped] <- paste0("(", m_txt[!is_wrapped], ")")
564564

565-
return(m_txt)
565+
m_txt
566566
},
567567
error = function(e) {
568-
return(m_names)
568+
m_names
569569
}
570570
)
571571
}

R/convert_pd_to_p.R

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@
3434
#' pd_to_p(pd = 0.95, direction = "one-sided")
3535
#'
3636
#' @export
37-
pd_to_p <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
37+
pd_to_p <- function(pd, ...) {
38+
UseMethod("pd_to_p")
39+
}
40+
41+
42+
#' @export
43+
#' @rdname pd_to_p
44+
pd_to_p.numeric <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
3845
p <- 1 - pd
3946
if (.get_direction(direction) == 0) {
4047
p <- 2 * p
@@ -56,6 +63,22 @@ pd_to_p <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
5663
}
5764

5865

66+
#' @export
67+
pd_to_p.data.frame <- function(pd, direction = "two-sided", verbose = TRUE, ...) {
68+
# check if data frame has an appropriate column
69+
pd_column <- intersect(c("pd", "p_direction", "PD"), colnames(pd))[1]
70+
if (is.na(pd_column) || length(pd_column) == 0) {
71+
insight::format_error("No column named `pd`, `p_direction`, or `PD` found.")
72+
}
73+
74+
# add p-value column
75+
pd$p <- pd_to_p(as.numeric(pd[[pd_column]]))
76+
# remove pd-column
77+
pd[[pd_column]] <- NULL
78+
pd
79+
}
80+
81+
5982
#' @rdname pd_to_p
6083
#' @export
6184
p_to_pd <- function(p, direction = "two-sided", ...) {

man/bayestestR-package.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pd_to_p.Rd

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)