| Title: | Sample Size and Power Calculation for Bayesian Testing with Bayes Factor |
|---|---|
| Description: | The goal of 'BayesPower' is to provide tools for Bayesian sample size determination and power analysis across a range of common hypothesis testing scenarios using Bayes factors. The main function, BayesPower_BayesFactor(), launches an interactive 'shiny' application for performing these analyses. The application also provides command-line code for reproducibility. Details of the methods are described in the tutorial by Wong, Pawel, and Tendeiro (2025) <doi:10.31234/osf.io/pgdac_v3>. |
| Authors: | Tsz Keung Wong [aut, cre], Samuel Pawel [aut], Jorge Tendeiro [aut] |
| Maintainer: | Tsz Keung Wong <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.4 |
| Built: | 2026-05-13 09:31:53 UTC |
| Source: | https://github.com/cran/BayesPower |
This function starts the interactive Shiny application for Bayesian power analysis using Bayes factors. The app provides a graphical user interface built with shiny.
BayesPower_BayesFactor()BayesPower_BayesFactor()
The application includes both the UI and server components, which are defined internally in the package. When run, a browser window or RStudio viewer pane will open to display the interface.
No return value, called for its side effects.
if (interactive()) { # Launch the Shiny application BayesPower_BayesFactor() }if (interactive()) { # Launch the Shiny application BayesPower_BayesFactor() }
Calculate the Bayes factor (BF10) for a single-proportion test, either against a point null or an interval null hypothesis.
BF10.bin.test( x, n, alpha, beta, h0, scale, prior_analysis, alternative, ROPE = NULL )BF10.bin.test( x, n, alpha, beta, h0, scale, prior_analysis, alternative, ROPE = NULL )
x |
Numeric integer. Observed number of successes (non-negative integer scalar, must be |
n |
Numeric integer. Sample size (positive integer scalar). |
alpha |
Numeric scalar. Shape parameter of the analysis beta prior under the alternative hypothesis
(required if |
beta |
Numeric scalar. Shape parameter of the analysis beta prior under the alternative hypothesis
(required if |
h0 |
Numeric scalar. Null proportion value (numeric scalar between 0.1 and 0.9). |
scale |
Numeric scalar. Scale parameter for the analysis prior (only used if |
prior_analysis |
Character. the analysis prior under the alternative hypothesis:
|
alternative |
Character. Hypothesis being tested: two-sided ( |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
An object of class "BFvalue_bin" containing:
bf10: Bayes factor in favor of the alternative hypothesis.
type: Test type ("One-proportion").
x: Number of successes.
n: Sample size.
h0: Null proportion value.
analysis_h1: List describing the analysis prior, containing
prior (prior distribution), alpha (alpha parameter),
beta (beta parameter), and scale (scale parameter).
alternative: the direction of the alternative hypothesis.
ROPE: interval null bounds (if specified).
p.value: Numeric, p-value.
BF10.bin.test( x = 42, n = 52, h0 = 0.5, prior_analysis = "beta", alternative = "greater", alpha = 1, beta = 1)BF10.bin.test( x = 42, n = 52, h0 = 0.5, prior_analysis = "beta", alternative = "greater", alpha = 1, beta = 1)
Calculate the Bayes factor (BF10) for a correlation coefficient, either against a point null
or an interval null hypothesis. Supports default beta ("d_beta"), stretched beta ("beta"),
and normal-moment ("Moment") priors for the alternative hypothesis.
BF10.cor( r, n, k, alpha, beta, h0, alternative, scale, prior_analysis, ROPE = NULL )BF10.cor( r, n, k, alpha, beta, h0, alternative, scale, prior_analysis, ROPE = NULL )
r |
Numeric scalar. Observed correlation coefficient. Must be a numeric scalar between -1 and 1. |
n |
Numeric integer. Sample size. Must be a numeric scalar greater than 3. |
k |
Numeric scalar. Parameter for the analysis default beta prior ( |
alpha |
Numeric scalar. Parameter for the analysis beta prior ( |
beta |
Numeric scalar. Parameter for the analysis beta prior ( |
h0 |
Numeric scalar. Null value of the correlation. Must be a numeric scalar between -0.8 and 0.8. |
alternative |
Character. The direction of the alternative hypothesis being tested: two-sided ( |
scale |
Numeric scalar. Scale parameter for the analysis normal-moment prior ( |
prior_analysis |
Character. Analysis prior: default beta ( |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
A list with class "BFvalue_r" containing:
type: "Correlation"
bf10: Calculated Bayes factor BF10
h0: Null value of the correlation
r: Observed correlation coefficient
n: Sample size
analysis_h1: List with the analysis prior parameters: prior_analysis, k, alpha, beta, and scale.
alternative: the direction of the alternative hypothesis
ROPE: Interval bounds if specified
p.value: Numeric, p-value.
BF10.cor( r = 0.3930924, n = 46, prior_analysis = "d_beta", k = 1, h0 = 0, alternative = "two.sided")BF10.cor( r = 0.3930924, n = 46, prior_analysis = "d_beta", k = 1, h0 = 0, alternative = "two.sided")
Computes the Bayes factor (BF10) for an F-test, comparing a full model to a reduced model under either an effect-size prior or a moment prior. Optionally, an interval null hypothesis can be specified.
BF10.f.test(fval, df1, df2, dff, rscale, f_m, prior_analysis, ROPE = NULL)BF10.f.test(fval, df1, df2, dff, rscale, f_m, prior_analysis, ROPE = NULL)
fval |
Numeric scalar. Observed F statistic (must be at least 0). |
df1 |
Numeric scalar. Numerator degrees of freedom (must be > 0). |
df2 |
Numeric scalar. Denominator degrees of freedom (must be > 0). |
dff |
Numeric scalar. Degrees of freedom for the analysis prior under
the alternative hypothesis. For the Moment prior, this must be |
rscale |
Numeric scalar. Scale parameter for the effect-size prior
(only used when |
f_m |
Numeric scalar. Cohen's f effect-size parameter for the analysis prior. |
prior_analysis |
Character. Analysis prior under the
alternative hypothesis. Must be either |
ROPE |
Numeric scaler. Optional numeric scalar specifying an upper bound for an interval null hypothesis. If provided, must be > 0. |
A list of class "BFvalue_f" containing:
fval: Input F-value.
df1, df2: Degrees of freedom.
ROPE: Interval bound (if specified).
analysis_h1: List containing the analysis prior specification, including
the prior distribution, the scale rscale, f_m, and degrees of freedom dff.
bf10: The computed Bayes factor.
p.value: Numeric, p-value.
BF10.f.test( fval = 4.5, df1 = 2, df2 = 12, dff = 12, rscale = 0.707, f_m = 0.1, prior_analysis = "effectsize" )BF10.f.test( fval = 4.5, df1 = 2, df2 = 12, dff = 12, rscale = 0.707, f_m = 0.1, prior_analysis = "effectsize" )
Compute the Bayes factor (BF10) for a Bayesian test of two proportions.
BF10.props(a0, b0, a1, b1, a2, b2, N1, N2, x1, x2)BF10.props(a0, b0, a1, b1, a2, b2, N1, N2, x1, x2)
a0 |
Numeric scalar. Alpha parameter of the Beta prior under the null hypothesis. |
b0 |
Numeric scalar. Beta parameter of the Beta prior under the null hypothesis. |
a1 |
Numeric scalar. Alpha parameter of the Beta prior for group 1 under the alternative hypothesis. |
b1 |
Numeric scalar. Beta parameter of the Beta prior for group 1 under the alternative hypothesis. |
a2 |
Numeric scalar. Alpha parameter of the Beta prior for group 2 under the alternative hypothesis. |
b2 |
Numeric scalar. Beta parameter of the Beta prior for group 2 under the alternative hypothesis. |
N1 |
Numeric integer. Sample size for group 1. |
N2 |
Numeric integer. Sample size for group 2. |
x1 |
Numeric integer. Number of successes observed in group 1. |
x2 |
Numeric integer. Number of successes observed in group 2. |
A list of class BFvalue_2p containing:
type: the string "Two-proportions".
analysis_h0: list with a and b for the null prior.
analysis_h1_theta_1: list with a and b for group 1 prior under H1.
analysis_h1_theta_2: list with a and b for group 2 prior under H1.
bf10: the computed Bayes factor (BF10).
N1, x1, N2, x2: the input sample sizes and observed successes.
OddRatio: observed odd ratio.
p.value: Numeric, p-value.
BF10.props( a0 = 1, b0 = 1, a1 = 1, b1 = 1, a2 = 1, b2 = 1, N1 = 493, N2 = 488, x1 = 155, x2 = 150)BF10.props( a0 = 1, b0 = 1, a1 = 1, b1 = 1, a2 = 1, b2 = 1, N1 = 493, N2 = 488, x1 = 155, x2 = 150)
Computes the Bayes factor (BF10) for a one-sample t-test, comparing an observed t-value against either a point null hypothesis or an interval null hypothesis.
BF10.ttest.OneSample( tval, df, prior_analysis, location, scale, dff, alternative, ROPE = NULL )BF10.ttest.OneSample( tval, df, prior_analysis, location, scale, dff, alternative, ROPE = NULL )
tval |
Numeric scalar. Observed t-value from the one-sample t-test. |
df |
Numeric scalar. Degrees of freedom of the t-test (must be >= 1). |
prior_analysis |
Character. Analysis prior under the
alternative hypothesis. Must be either |
location |
Numeric scalar. Location parameter for the analysis prior under the alternative hypothesis. |
scale |
Numeric scalar. Scale parameter for the analysis prior under the alternative hypothesis (must be > 0). |
dff |
Numeric scalar. Degrees of freedom for the t-distribution prior (only required if |
alternative |
Character. The direction of the alternative hypothesis two-sided ( |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
An object of class "BFvalue_t" containing:
type: Character indicating "One-sample t-test".
bf10: Numeric, the Bayes factor (BF10).
tval: Observed t-value.
df: Degrees of freedom.
analysis_h1: List with the analysis prior parameters:
prior_analysis, location, scale, and optionally dff.
alternative: Character, the direction of the alternative hypothesis.
ROPE: Optional numeric vector of interval null bounds.
d: Numeric, observed Cohen's d.
p.value: Numeric, p-value.
BF10.ttest.OneSample( tval = 2, df = 50, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1, alternative = "two.sided")BF10.ttest.OneSample( tval = 2, df = 50, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1, alternative = "two.sided")
Compute the Bayes factor (BF10) for a two-sample independent-samples t-test. Supports both point-null and interval-null hypotheses.
BF10.ttest.TwoSample( tval, N1, N2, prior_analysis, location, scale, dff, alternative, ROPE = NULL )BF10.ttest.TwoSample( tval, N1, N2, prior_analysis, location, scale, dff, alternative, ROPE = NULL )
tval |
Numeric scalar. Observed t-value from the two-sample t-test. |
N1 |
Numeric integer. Sample size of group 1 (must be > 2, will be rounded to nearest integer). |
N2 |
Numeric integer. Sample size of group 2 (must be > 2, will be rounded to nearest integer). |
prior_analysis |
Character. Analysis prior under the alternative hypothesis:
|
location |
Numeric scalar. Location parameter of the analysis prior. |
scale |
Numeric scalar > 0. Scale parameter of the analysis prior. |
dff |
Numeric scalar. Degrees of freedom for the analysis prior (required if prior_analysis = |
alternative |
Character. The direction of the alternative hypothesis two-sided ( |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
A list of class BFvalue_t containing:
type: Character string describing the test type.
bf10: Computed Bayes factor BF10.
tval: Observed t-value.
df: Degrees of freedom.
analysis_h1: List with the analysis prior parameters:
prior_analysis, location, scale, and optionally dff.
alternative: Hypothesis tested ("two.sided", "greater", or "less").
ROPE: Interval bounds used, if any.
N1: Sample size of group 1.
N2: Sample size of group 2.
d: Numeric, observed Cohen's d.
p.value: Numeric, p-value.
BF10.ttest.TwoSample( tval = -1.148, N1 = 53, N2 = 48, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1, alternative = "two.sided", ROPE = c(-0.36,0.36))BF10.ttest.TwoSample( tval = -1.148, N1 = 53, N2 = 48, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1, alternative = "two.sided", ROPE = c(-0.36,0.36))
Perform sample size determination or power calculation of compelling and misleading evidence for a Bayesian test of a single proportion. Can handle both point-null and interval-null hypothesis, and allows specifying analysis and design priors.
BFpower.bin( alternative, threshold, h0, true_rate, false_rate, prior_analysis, alpha, beta, scale, prior_design = NULL, alpha_d, beta_d, location_d, scale_d, N = NULL, ROPE = NULL, type_rate = "positive" )BFpower.bin( alternative, threshold, h0, true_rate, false_rate, prior_analysis, alpha, beta, scale, prior_design = NULL, alpha_d, beta_d, location_d, scale_d, N = NULL, ROPE = NULL, type_rate = "positive" )
alternative |
Character. The direction of the alternative hypothesis : two-sided ( |
threshold |
Numeric scalar. Threshold for compelling evidence (must be at least 1). |
h0 |
Numeric scalar. Null proportion value for the test (numeric scalar between 0.1 and 0.9). |
true_rate |
Numeric scalar. Targeted true positive rate or true negative rate . |
false_rate |
Numeric scalar. Targeted false positive rate or false negative rate . |
prior_analysis |
Character. Analysis prior under the alternative hypothesis: |
alpha |
Numeric scalar. Parameter for the analysis beta prior (used when |
beta |
Numeric scalar. Parameter for the analysis beta prior (used when |
scale |
Numeric scalar. Scale parameter for the analysis moment prior (used when |
prior_design |
Character. Design prior under the alternative hypothesis: |
alpha_d |
Numeric scalar. Parameter for the design beta prior (used when |
beta_d |
Numeric scalar. Parameter for the design beta prior (used when |
location_d |
Numeric scalar. Proportion value for the design point prior ( |
scale_d |
Numeric scalar. Scale parameter for the design moment prior (used when |
N |
Numeric integer. Sample size. If |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
type_rate |
Character. Either |
1. Sample Size Determination Mode (when N = NULL):
If no sample size is provided, the function calculates the minimum sample size needed to achieve the desired configuration below. The user must provide:
type_rate - either "positive" to control true/false positive rates or "negative" to control true/false negative rates.
true_rate - the targeted true positive or true negative rate (between 0.6 and 0.999).
false_rate - the acceptable false positive or false negative rate (between 0.001 and 0.1).
threshold - the Bayes factor threshold for compelling evidence (must be at least 1).
The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence (i.e., true positive/negative rate) meets or exceeds true_rate, while the probability of misleading evidence (i.e., false positive/negative rate) does not exceed false_rate.
2. Fixed-sample Analysis Mode (when N is supplied):
If a positive numeric sample size N is provided, the function computes the probabilities of obtaining compelling or misleading evidence for that fixed sample size. In this mode, type_rate, true_rate, and false_rate are ignored; only the Bayes factor threshold threshold is used.
Direction of the Alternative Hypothesis:
The argument alternative specifies the direction of the test and can be set to "two.sided", "greater", or "less".
Interval Null Hypothesis:
The interval null hypothesis can be specified using the argument ROPE,
which defines an interval around the null value of h0.
The required form of ROPE depends on the direction of alternative:
"greater" or "less": ROPE must be a scalar.
It should be positive for "greater" and negative for "less".
"two.sided": ROPE must be a numeric vector of length 2,
where the lower bound is negative and the upper bound is positive.
If ROPE = NULL, a point-null hypothesis is assumed.
Analysis Priors:
The user must specify the analysis prior under the alternative hypothesis using prior_analysis:
beta (beta prior): alpha and beta > 0.
Moment (normal-moment prior) : scale > 0.
Design Priors (optional):
The design prior under the alternative hypothesis can optionally be specified using prior_design:
beta (beta prior): alpha_d and beta_d > 0.
Moment (normal-moment prior): scale_d > 0.
Point (point prior): location_d numeric scalar.
If prior_design is NULL, no design prior is used.
A list of class "BFpower" containing:
type: Test type ("One proportion").
alternative: alternative hypothesis.
h0: The proportion under the null hypothesis.
analysis_h1: List describing the analysis prior, containing
prior (prior distribution), alpha (alpha parameter),
beta (beta parameter), and scale (scale parameter).
design_h1: List describing the design prior (if provided), containing
prior (prior distribution), alpha (alpha parameter),
beta (beta parameter), and scale (scale parameter).
results: Data frame of probabilities of compelling/misleading evidence and the required or supplied sample size.
threshold: Compelling-evidence threshold.
If sample size determination fails, the function returns NaN and prints a message.
BFpower.bin( alternative = "greater", threshold = 3, true_rate = 0.8, false_rate = 0.05, h0 = 0.5, prior_analysis = "beta", alpha = 1, beta = 1)BFpower.bin( alternative = "greater", threshold = 3, true_rate = 0.8, false_rate = 0.05, h0 = 0.5, prior_analysis = "beta", alpha = 1, beta = 1)
Perform sample size determination or power calculation of compelling and misleading evidence for a Bayesian correlation test. Can handle both point-null and interval-null hypothesis, and allows specifying analysis and design priors.
BFpower.cor( alternative, h0, ROPE = NULL, threshold, true_rate, false_rate, prior_analysis, k, alpha, beta, scale, prior_design = NULL, alpha_d, beta_d, location_d, k_d, scale_d, N = NULL, type_rate = "positive" )BFpower.cor( alternative, h0, ROPE = NULL, threshold, true_rate, false_rate, prior_analysis, k, alpha, beta, scale, prior_design = NULL, alpha_d, beta_d, location_d, k_d, scale_d, N = NULL, type_rate = "positive" )
alternative |
Character. The direction of the alternative hypothesis being tested: two-sided ( |
h0 |
Numeric scalar. Null rho correlation value. Must be between -0.8 and 0.8. |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
threshold |
Numeric scalar. Threshold for compelling evidence (must be at least 1). |
true_rate |
Numeric scalar. Targeted true positive rate (if |
false_rate |
Numeric scalar. Targeted false positive rate (if |
prior_analysis |
Character. Analysis prior under the alternative hypothesis:
default beta ( |
k |
Numeric scalar. Parameter for the default beta prior ( |
alpha |
Numeric scalar. Parameter for the beta prior ( |
beta |
Numeric scalar. Parameter for the beta prior ( |
scale |
Numeric scalar. Scale parameter for the normal-moment prior ( |
prior_design |
Character. Design prior under the alternative hypothesis: default beta ( |
alpha_d |
Numeric scalar. Parameter for the design beta prior ( |
beta_d |
Numeric scalar. Parameter for the design beta prior ( |
location_d |
Numeric scalar. Location parameter for the design point prior ( |
k_d |
Numeric scalar. Parameter for the design default beta prior ( |
scale_d |
Numeric scalar. Scale parameter for the design normal-moment prior ( |
N |
Numeric integer. Sample size. Only required if the goal is not sample size determination, but rather to calculate the probability of obtaining compelling or misleading evidence for a given sample size. |
type_rate |
Character. Character. Either |
1. Sample Size Determination Mode (when N = NULL):
If no sample size is provided, the function calculates the minimum sample size needed to achieve the desired configuration below. The user must provide:
type_rate - either "positive" to control true/false positive rates, or "negative" to control true/false negative rates.
true_rate - the targeted true positive or true negative rate (between 0.6 and 0.999).
false_rate - the acceptable false positive or false negative rate (between 0.001 and 0.1).
threshold - the Bayes factor threshold for compelling evidence (must be at least 1).
The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence (i.e., true positive/negative rate) meets or exceeds true_rate, while the probability of misleading evidence (i.e., false positive/negative rate) does not exceed false_rate.
2. Fixed-sample Analysis Mode (when N is supplied):
If a positive numeric sample size N is provided, the function computes the probabilities of obtaining compelling or misleading evidence for that fixed sample size. In this mode, the arguments type_rate, true_rate, and false_rate are ignored; only the Bayes factor threshold threshold is used.
Direction of the Alternative Hypothesis:
The argument alternative specifies the direction of the test and can be set to "two.sided", "greater", or "less".
Interval Null Hypothesis:
The interval null hypothesis can be specified using the argument ROPE,
which defines an interval around the null value of h0.
The required form of ROPE depends on the direction of alternative:
"greater" or "less": ROPE must be a scalar.
It should be positive for "greater" and negative for "less".
"two.sided": ROPE must be a numeric vector of length 2,
where the lower bound is negative and the upper bound is positive.
If ROPE = NULL, a point-null hypothesis is assumed.
Analysis Priors:
The user must specify the analysis prior under the alternative hypothesis using prior_analysis:
d_beta (default beta): k > 0.
beta (stretched beta): alpha and beta > 0.
Moment (normal-moment prior): scale > 0.
Design Priors (optional):
The design prior under the alternative hypothesis can optionally be specified using prior_design:
d_beta (default beta): k_d > 0.
beta (stretched beta): alpha_d and beta_d > 0.
Moment (normal-moment prior): scale_d > 0.
Point (point prior): location_d.
If prior_design is NULL, no design prior is used.
A list of class BFpower_r containing:
type: Test type (always "Correlation").
alternative: the direction of the alternative hypothesis.
h0: the value of correlation under the null hypothesis.
ROPE: Bounds for interval null (if used).
analysis_h1: List with the analysis prior parameters: prior_analysis, k, alpha, beta, and scale.
design_h1: List with the design prior parameters: prior_design, k, alpha, beta, scale, and location.
results: Data frame with the probabilities of compelling/misleading evidence, and with the required sample size.
threshold: Threshold of compelling evidence.
BFpower.cor( alternative = "greater", h0 = 0, threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "d_beta", k = 1, prior_design = "Point", location_d = 0.3 )BFpower.cor( alternative = "greater", h0 = 0, threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "d_beta", k = 1, prior_design = "Point", location_d = 0.3 )
Perform sample size determination or power calculation of compelling and misleading evidence for a Bayesian F-test comparing a full model to a nested reduced model. Can handle both point-null and interval-null hypothesis, and allows specifying analysis and design priors.
BFpower.f.test( threshold, true_rate, false_rate, p, k, prior_analysis, dff, rscale, f_m, prior_design = NULL, dff_d, rscale_d, f_m_d, N = NULL, type_rate = "positive", ROPE = NULL )BFpower.f.test( threshold, true_rate, false_rate, p, k, prior_analysis, dff, rscale, f_m, prior_design = NULL, dff_d, rscale_d, f_m_d, N = NULL, type_rate = "positive", ROPE = NULL )
threshold |
Numeric scalar. Threshold for compelling evidence (must be at least 1). |
true_rate |
Numeric scalar. Targeted true positive or true negative rate (used only when
sample size determination is requested; |
false_rate |
Numeric scalar. Targeted false positive or false negative rate (used only when
sample size determination is requested; |
p |
Numeric integer. Number of predictors in the reduced model. |
k |
Numeric integer. Number of predictors in the full model (must satisfy |
prior_analysis |
Character. Analysis prior model under the alternative hypothesis:
|
dff |
Numeric scalar. Degrees of freedom for the analysis prior under the alternative
hypothesis. Must be a positive scalar, and must be at least 3 if
|
rscale |
Numeric scalar. Scale parameter for the analysis effect-size prior (only used when
|
f_m |
Numeric scalar. Cohen's |
prior_design |
Character. Design prior model under the alternative hypothesis:
|
dff_d |
Numeric scalar. Degrees of freedom for the design prior. Must be a positive scalar,
and at least 3 if |
rscale_d |
Numeric scalar. Scale parameter for the design effect-size prior
(only used when |
f_m_d |
Numeric scalar. Cohen's |
N |
Numeric integer. Sample size. If |
type_rate |
Character. Either |
ROPE |
Numeric vector. Numeric bounds for the interval null (only used when interval Bayes factors are required). |
1. Sample Size Determination Mode (when N = NULL):
If no sample size is provided, the function calculates the minimum sample size needed to achieve the desired configuration below. The user must provide:
type_rate - either "positive" to control true/false positive rates, or "negative" to control true/false negative rates.
true_rate - the targeted true positive or true negative rate (between 0.6 and 0.999).
false_rate - the acceptable false positive or false negative rate (between 0.001 and 0.1).
threshold - the Bayes factor threshold for compelling evidence (must be > 1).
The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence (i.e., true positive/negative rate) meets or exceeds true_rate, while the probability of misleading evidence (i.e., false positive/negative rate) does not exceed false_rate.
2. Fixed-sample Analysis Mode (when N is supplied):
If a positive numeric sample size N is provided, the function computes the probabilities of obtaining compelling or misleading evidence for that fixed sample size. In this mode, type_rate, true_rate, and false_rate are ignored; only the Bayes factor threshold threshold is used.
Interval Null Hypothesis:
The interval null hypothesis can be specified using the argument ROPE,
which defines an interval around the null value of 0. The specified value of ROPE should be a positive numeric scaler.
If ROPE = NULL, a point-null hypothesis is assumed.
Analysis Priors:
The user must specify the analysis prior under the alternative hypothesis using prior_analysis:
effectsize (effect size prior): rscale > 0, f_m , and dff.
Moment (normal-moment prior): f_m and dff .
Design Priors (optional):
The design prior under the alternative hypothesis can optionally be specified using prior_design:
effectsize (effect size prior): rscale_d > 0, f_m_d, and dff_d .
Moment (normal-moment prior): f_m_d and dff_d .
Point (point prior): f_m_d.
If prior_design is NULL, no design prior is used.
A list of class BFpower containing:
type: Test type (always "Regression/ANOVA").
k, p: Number of predictors in the full and reduced models.
ROPE: Bounds for interval null (if used).
analysis_h1: List containing the analysis prior specification, including
the prior distribution, the scale rscale, f_m, and degrees of freedom dff.
design_h1: List containing the design prior specification, including
the prior distribution, the scale rscale, f_m, and degrees of freedom dff
(or NULL if not specified).
results: Data frame of probabilities of compelling/misleading evidence and
the required or supplied sample size.
threshold: Threshold of compelling evidence.
If sample size determination fails, the function returns NaN and prints a message.
BFpower.f.test( threshold = 3, true_rate = 0.8, false_rate = 0.05, p = 3, k = 4, prior_analysis = "effectsize", dff = 3, rscale = 0.18, f_m = 0.1, prior_design = "Point", f_m_d = 0.1)BFpower.f.test( threshold = 3, true_rate = 0.8, false_rate = 0.05, p = 3, k = 4, prior_analysis = "effectsize", dff = 3, rscale = 0.18, f_m = 0.1, prior_design = "Point", f_m_d = 0.1)
Perform sample size determination or power calculation of compelling and misleading evidence for a Bayesian test of two proportions.
Under the null hypothesis, and it is
assigned a shared analysis beta prior. Under the alternative hypothesis, and
are treated as distinct parameters and are assigned independent beta analysis priors.
The function supports the specification of point design prior.
BFpower.props( threshold, true_rate, a0, b0, a1, b1, a2, b2, prior_design_1 = "same", a1d, b1d, dp1, prior_design_2 = "same", a2d, b2d, dp2, N1 = NULL, N2 = NULL, type_rate = "positive" )BFpower.props( threshold, true_rate, a0, b0, a1, b1, a2, b2, prior_design_1 = "same", a1d, b1d, dp1, prior_design_2 = "same", a2d, b2d, dp2, N1 = NULL, N2 = NULL, type_rate = "positive" )
threshold |
Numeric scalar. Threshold of compelling evidence. |
true_rate |
Numeric scalar. Targeted true positive rate (if |
a0 |
Numeric scalar. Alpha parameter of the Beta prior under the null hypothesis. |
b0 |
Numeric scalar. Beta parameter of the Beta prior under the null hypothesis. |
a1 |
Numeric scalar. Alpha parameter of the Beta analysis prior for group 1 under the alternative hypothesis. |
b1 |
Numeric scalar. Beta parameter of the Beta analysis prior for group 1 under the alternative hypothesis. |
a2 |
Numeric scalar. Alpha parameter of the Beta analysis prior for group 2 under the alternative hypothesis. |
b2 |
Numeric scalar. Beta parameter of the Beta analysis prior for group 2 under the alternative hypothesis. |
prior_design_1 |
Character. The design prior of group 1: |
a1d |
Numeric scalar. Alpha parameter of the design prior for group 1 (used if |
b1d |
Numeric scalar. Beta parameter of the design prior for group 1 (used if |
dp1 |
Numeric scalar. True proportion for group 1 in the design prior (used if |
prior_design_2 |
Character. The design prior of group 2: |
a2d |
Numeric scalar. Alpha parameter of the design prior for group 2 (used if |
b2d |
Numeric scalar. Beta parameter of the design prior for group 2 (used if |
dp2 |
Numeric scalar. True proportion for group 2 in the design prior (used if |
N1 |
Numeric integer. Sample size for group 1. |
N2 |
Numeric integer. Sample size for group 2. |
type_rate |
Character. Choose |
1. Sample Size Determination Mode (when N1 = NULL and N2 = NULL):
If no sample sizes are provided for the two groups, the function calculates the minimum sample sizes needed to achieve the desired configuration. The user must provide:
type_rate - either "positive" to control true/false positive rates or "negative" to control true/false negative rates.
true_rate - the targeted true positive or true negative rate (between 0.6 and 0.999).
threshold - the Bayes factor threshold for compelling evidence (must be > 1).
The function iteratively finds the smallest sample sizes for which the probability of obtaining compelling evidence (i.e., true positive/negative rate) meets or exceeds true_rate.
2. Fixed-sample Analysis Mode (when N1 and N2 are supplied):
If positive numeric sample sizes N1 and N2 are provided, the function computes the probabilities of obtaining compelling or misleading evidence for these fixed sample sizes. In this mode, type_rate and true_rate are ignored; only the Bayes factor threshold threshold is used.
Analysis Priors:
The user must specify the analysis priors under the null and alternative hypotheses:
Null hypothesis: Beta prior with parameters a0 and b0.
Alternative hypothesis:
Group 1: Beta prior with hyperparameters a1 and b1.
Group 2: Beta prior with hyperparameters a2 and b2.
Design Priors (optional):
Design priors for the alternative hypothesis can optionally be specified:
Group 1 design prior (prior_design_1):
"same": uses the corresponding analysis prior (a1, b1).
"beta" (beta prior): requires hyperparameters a1d and b1d.
"Point" (point prior): requires fixed proportion dp1.
Group 2 design prior (prior_design_2):
"same": uses the corresponding analysis prior (a2, b2).
"beta" (beta prior): requires hyperparameters a2d and b2d.
"Point" (point prior): requires fixed proportion dp2.
An object of class BFpower (a list) containing:
type: Character, always "Two-proportions".
analysis_h0: List of analysis prior parameters under the null, containing a and b.
analysis_h1_theta_1: List of analysis prior parameters for group 1 under the alternative, containing a and b.
analysis_h1_theta_2: List of analysis prior parameters for group 2 under the alternative, containing a and b.
design_h1_theta_1: List of design prior parameters for group 1 under the alternative, containing prior, a, b, and p.
design_h1_theta_2: List of design prior parameters for group 2 under the alternative, containing prior, a, b, and p.
results: Data frame of probabilities of compelling and misleading evidence.
grid: Grid used for computation.
threshold: Threshold of compelling evidence.
mode_bf: Character string specifying the mode (sample size determination or power calculation).
BFpower.props( threshold = 3, true_rate = 0.8, a0 = 1, b0 = 1, a1 = 156, b1 = 339, a2 = 151, b2 = 339)BFpower.props( threshold = 3, true_rate = 0.8, a0 = 1, b0 = 1, a1 = 156, b1 = 339, a2 = 151, b2 = 339)
Perform sample size determination or power calculation of compelling and misleading evidence for a one-sample Bayesian t-test. Can handle both point-null and interval-null hypothesis, and allows specifying analysis and design priors.
BFpower.ttest.OneSample( alternative, ROPE = NULL, prior_analysis, location, scale, dff, prior_design = NULL, location_d, scale_d, dff_d, N = NULL, type_rate = "positive", true_rate, false_rate, threshold )BFpower.ttest.OneSample( alternative, ROPE = NULL, prior_analysis, location, scale, dff, prior_design = NULL, location_d, scale_d, dff_d, N = NULL, type_rate = "positive", true_rate, false_rate, threshold )
alternative |
Character. The direction of the alternative hypothesis : two-sided ( |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
prior_analysis |
Character. The analysis prior under the alternative hypothesis:
|
location |
Numeric scaler. Location parameter for the analysis prior under the alternative hypothesis. |
scale |
Numeric scaler. Scale parameter for the analysis prior under the alternative hypothesis (must be > 0). |
dff |
Numeric scaler. Degrees of freedom for the analysis prior under the alternative hypothesis (required if |
prior_design |
Optional Character. The design prior under the alternative hypothesis:
|
location_d |
Numeric scaler. Location parameter for the design prior under the alternative hypothesis. |
scale_d |
Numeric scaler. Scale parameter for the design prior under the alternative hypothesis. |
dff_d |
Numeric scaler. Degrees of freedom for the design prior under the alternative hypothesis (required if |
N |
Numeric integer. Sample size. |
type_rate |
Character. Either |
true_rate |
Numeric scaler. Target true positive or negative rate (between 0.6 and 0.999). |
false_rate |
Numeric scaler. Target false positive or false negative rate (between 0.001 and 0.1). |
threshold |
Numeric scaler. Threshold of compelling evidence (must be at least 1). |
1. Sample Size Determination Mode (when N = NULL):
If no sample size is provided, the function calculates the minimum sample size needed to achieve the desired configuration below. The user must provide:
type_rate - either "positive" to control true/false positive rates,
or "negative" to control true/false negative rates.
true_rate - the targeted true positive or true negative rate (between 0.6 and 0.999).
false_rate - the acceptable false positive or false negative rate (between 0.001 and 0.1).
threshold - the Bayes factor threshold for compelling evidence (must be at least 1).
The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence (i.e., true positive/negative rate) meets or exceeds true_rate, while the probability of misleading evidence (i.e., false positive/negative rate) does not exceed false_rate.
2. Fixed-sample Analysis Mode (when N is supplied):
If a positive numeric sample size N is provided, the function computes the probabilities of obtaining compelling or misleading evidence for that fixed sample size. In this mode, the arguments type_rate, true_rate, and false_rate are ignored; only the Bayes factor threshold threshold is used.
Direction of the Alternative Hypothesis:
The argument alternative specifies the direction of the test and can be set to "two.sided", "greater", or "less".
Interval Null Hypothesis:
The interval null hypothesis can be specified using the argument ROPE,
which defines an interval around the null value of 0.
The required form of ROPE depends on the direction of alternative:
"greater" or "less": ROPE must be a scalar.
It should be positive for "greater" and negative for "less".
"two.sided": ROPE must be a numeric vector of length 2,
where the lower bound is negative and the upper bound is positive.
If ROPE = NULL, a point-null hypothesis is assumed.
Analysis Priors:
The user must specify the analysis prior under the alternative hypothesis using prior_analysis:
Normal (normal prior): location and scale > 0.
Moment (normal-moment prior): scale > 0.
t-distribution (scaled t prior): location, scale > 0, and dff > 0.
Design Priors (optional):
The design prior under the alternative hypothesis can optionally be specified using prior_design:
Normal (normal prior): location_d and scale_d > 0.
Moment (normal-moment prior): scale_d > 0.
t-distribution (scaled t prior): location_d, scale_d > 0, and dff_d > 0.
Point (point prior): location_d.
If prior_design is NULL, no design prior is used.
An object of class BFpower_t containing:
type: Character, always "One-sample t-test".
alternative: Character, the direction of the alternative hypothesis.
ROPE: Optional numeric vector for interval null bounds.
analysis_h1: List with the analysis prior parameters: prior_analysis, location, scale, and optionally dff.
design_h1: List with the design prior parameters: prior_design, location, scale, and optionally dff (or NULL if not provided).
results: Data frame of probabilities: compelling/misleading evidence, or NaN if calculation fails.
threshold: Numeric, threshold of compelling evidence.
BFpower.ttest.OneSample( alternative = "two.sided", threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1 )BFpower.ttest.OneSample( alternative = "two.sided", threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1 )
Perform sample size determination or power calculation of compelling and misleading evidence for a two-sample Bayesian t-test. Can handle both point-null and interval-null hypothesis, and allows specifying analysis and design priors.
BFpower.ttest.TwoSample( alternative, ROPE = NULL, threshold, true_rate, false_rate, prior_analysis, location, scale, dff, prior_design = NULL, location_d, scale_d, dff_d, N1 = NULL, N2 = NULL, r = NULL, type_rate = "positive" )BFpower.ttest.TwoSample( alternative, ROPE = NULL, threshold, true_rate, false_rate, prior_analysis, location, scale, dff, prior_design = NULL, location_d, scale_d, dff_d, N1 = NULL, N2 = NULL, r = NULL, type_rate = "positive" )
alternative |
Character. The direction of the alternative hypothesis: two-sided ( |
ROPE |
Optional numeric vector. Specifies bounds for an interval
null hypothesis. For |
threshold |
Numeric scalar. Threshold for compelling evidence (must be at least 1). |
true_rate |
Numeric scalar. Target true positive or negative rate . |
false_rate |
Numeric scalar. Target false positive or negative rate . |
prior_analysis |
Character. Analysis prior under the alternative hypothesis:
|
location |
Numeric scalar. Location parameter for the analysis prior. |
scale |
Numeric scalar > 0. Scale parameter for the analysis prior. |
dff |
Numeric scalar. Degrees of freedom for the analysis prior (required if prior_analysis = |
prior_design |
Optional Character. Design prior under the alternative:
|
location_d |
Numeric scalar. Location parameter for the design prior. |
scale_d |
Numeric scalar > 0. Scale parameter for the design prior. |
dff_d |
Numeric scalar. Degrees of freedom for the design prior (required if |
N1 |
Numeric integer. Sample size for group 1 (used if |
N2 |
Numeric integer. Sample size for group 2 (used if |
r |
Optional numeric scalar. Ratio of sample size |
type_rate |
Character, either |
1. Sample size determination mode (when N1 = NULL and N2 = NULL, but r is provided):
If no sample size is provided, the function calculates the minimum sample size needed to achieve the desired configuration below. The user must provide:
type_rate - either "positive" to control true/false positive rates,
or "negative" to control true/false negative rates.
true_rate - the targeted true positive or true negative rate (between 0.6 and 0.999).
false_rate - the acceptable false positive or false negative rate (between 0.001 and 0.1).
threshold - the Bayes factor threshold for compelling evidence (must be > 1).
r - the allocation ratio of group 2 to group 1 sample sizes (N2/N1).
The function iteratively finds the smallest sample size N1 and N2 = r * N1 for which the probability of obtaining compelling evidence (i.e., true positive/negative rate) meets or exceeds true_rate, while the probability of misleading evidence (i.e., false positive/negative rate) does not exceed false_rate.
2. Fixed-sample analysis mode (when N1 and N2 are supplied):
If a positive numeric sample size N1 and N2 are provided, the function computes the probabilities of obtaining compelling or misleading evidence for that fixed sample size. In this mode, the arguments type_rate, r, true_rate, and false_rate are ignored; only the Bayes factor threshold threshold is used.
Direction of the Alternative Hypothesis:
The argument alternative specifies the direction of the test and can be set to "two.sided", "greater", or "less".
Interval Null Hypothesis:
The interval null hypothesis can be specified using the argument ROPE,
which defines an interval around the null value of 0.
The required form of ROPE depends on the direction of alternative:
"greater" or "less": ROPE must be a scalar.
It should be positive for "greater" and negative for "less".
"two.sided": ROPE must be a numeric vector of length 2,
where the lower bound is negative and the upper bound is positive.
If ROPE = NULL, a point-null hypothesis is assumed.
Analysis Priors:
The user must specify the analysis prior under the alternative hypothesis using prior_analysis:
Normal (normal prior): location and scale > 0.
Moment (normal-moment prior): scale > 0.
t-distribution (scaled t prior): location, scale > 0, and dff > 0.
Design Priors (optional):
The design prior under the alternative hypothesis can optionally be specified using prior_design:
Normal (normal prior): location_d and scale_d > 0.
Moment (normal-moment prior): scale_d > 0.
t-distribution (scaled t prior): location_d, scale_d > 0, and dff_d > 0.
Point (point prior): location_d.
If prior_design is NULL, no design prior is used.
An object of class BFpower_t containing:
type: Character string describing the test type.
alternative: Alternative hypothesis ("two.sided", "greater", or "less").
ROPE: Interval bounds under the null used, if any.
analysis_h1: List with the analysis prior parameters: prior_analysis, location, scale, and optionally dff.
design_h1: List with the design prior parameters: prior_design, location, scale, and optionally dff (or NULL if not provided).
results: Data frame with probabilities of compelling/misleading evidence.
threshold: Threshold of compelling evidence.
BFpower.ttest.TwoSample( alternative = "two.sided", ROPE = c(-0.36, 0.36), threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "Normal", location = -0.23, scale = 0.2, dff = 1, type_rate = "negative", r = 1)BFpower.ttest.TwoSample( alternative = "two.sided", ROPE = c(-0.36, 0.36), threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "Normal", location = -0.23, scale = 0.2, dff = 1, type_rate = "negative", r = 1)
Visualizes a "BFpower" object.
## S3 method for class 'BFpower' plot(x, plot_power = FALSE, plot_rel = FALSE, ...)## S3 method for class 'BFpower' plot(x, plot_power = FALSE, plot_rel = FALSE, ...)
x |
A |
plot_power |
Logical. If |
plot_rel |
Logical. If |
... |
Additional arguments (currently unused; included for method consistency). |
This plot method can return up to three plots (or five plots for testing two-proportions) based on the information
from the "BFpower" object:
The first plot displays the analysis prior and the design prior. However, for BFpower.props, three plots are returned, corresponding to the three thetas.
The second plot contains two panels where the left panel shows the true and false positive rates as a function of sample size, and the right panel shows the true and false negative rates.
The third plot illustrates the relationship between the data and the Bayes factors.
The object can be generated by any of the following functions:
BF10.ttest.OneSample, BF10.ttest.TwoSample, BF10.cor, BFpower.f.test BF10.bin.test, or BF10.props.
A list of up to three ggplot objects.
results <- BFpower.cor( alternative = "greater", h0 = 0, threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "beta", alpha = 1, beta = 1, prior_design = "Point", location_d = 0.3 ) print(results) plot(results, plot_power = TRUE, plot_rel = TRUE)results <- BFpower.cor( alternative = "greater", h0 = 0, threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "beta", alpha = 1, beta = 1, prior_design = "Point", location_d = 0.3 ) print(results) plot(results, plot_power = TRUE, plot_rel = TRUE)
Displays the results of a "BFpower" object.
## S3 method for class 'BFpower' print(x, ...)## S3 method for class 'BFpower' print(x, ...)
x |
A |
... |
Additional arguments (currently unused; included for method consistency). |
This method prints key information from the "BFpower" object,
including the type of hypothesis specification, priors, true and false rates,
and required sample. The object can be generated by any of the following functions:
BFpower.ttest.OneSample, BFpower.ttest.TwoSample, BFpower.cor, BFpower.f.test, BFpower.bin, or BFpower.props.
Invisibly returns the input "BFpower" object.
results <- BFpower.ttest.OneSample( alternative = "two.sided", threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1 ) print(results)results <- BFpower.ttest.OneSample( alternative = "two.sided", threshold = 3, true_rate = 0.8, false_rate = 0.05, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1 ) print(results)
Displays the results of a "BFvalue" object.
## S3 method for class 'BFvalue' print(x, ...)## S3 method for class 'BFvalue' print(x, ...)
x |
A |
... |
Additional arguments (currently unused; included for method consistency). |
This method prints key results from a Bayesian test, including the
Bayes factor and relevant test statistics with frequentist test result. The object can be generated
by any of the following functions:
BF10.ttest.OneSample, BF10.ttest.TwoSample, BF10.cor, BFpower.f.test BF10.bin.test, or BF10.props.
Invisibly returns the input "BFvalue" object.
result <- BF10.ttest.OneSample( tval = 2, df = 50, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1, alternative = "two.sided") print(result)result <- BF10.ttest.OneSample( tval = 2, df = 50, prior_analysis = "t-distribution", location = 0, scale = 0.707, dff = 1, alternative = "two.sided") print(result)