Title: Sample Size and Power Calculation for Bayesian Testing with Bayes Factor
Version: 1.0.2
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_v2>.
BugReports: https://github.com/tkWong3004/BayesPower/issues
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 7.3.3
Imports: rlang, shiny, gsl, Rcpp, ExtDist, ggplot2, patchwork, rmarkdown, glue, hypergeo, rootSolve, shinyWidgets, grDevices, tidyr
LinkingTo: Rcpp, BH
NeedsCompilation: yes
Packaged: 2026-02-12 10:00:58 UTC; u971096
Author: Tsz Keung Wong [aut, cre], Samuel Pawel [aut], Jorge Tendeiro [aut]
Maintainer: Tsz Keung Wong <t.k.wong3004@gmail.com>
Repository: CRAN
Date/Publication: 2026-02-12 10:20:02 UTC

Bayes Factor for a Bayesian One-Proportion Test

Description

Calculate the Bayes factor (BF10) for a single-proportion test, either against a point null or an interval null hypothesis.

Usage

BF10.bin.test(
  x,
  n,
  alpha,
  beta,
  h0,
  scale,
  prior_analysis,
  alternative,
  ROPE = NULL
)

Arguments

x

Numeric integer. Observed number of successes (non-negative integer scalar, must be \le n).

n

Numeric integer. Sample size (positive integer scalar).

alpha

Numeric scalar. Shape parameter of the analysis beta prior under the alternative hypothesis (required if prior_analysis = "beta").

beta

Numeric scalar. Shape parameter of the analysis beta prior under the alternative hypothesis (required if prior_analysis = "beta").

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 = "Moment").

prior_analysis

character. the analysis prior under the alternative hypothesis: "beta" (stretched beta) or "Moment" (normal-moment prior).

alternative

character. Hypothesis being tested: two-sided ("two.sided"), right-sided ("greater"), or left-sided ("less").

ROPE

Numeric vector. Optional numeric vector specifying bounds for an interval null; used if interval BF is calculated.

Value

An object of class "BFvalue_bin" containing:

Examples

BF10.bin.test(
 x = 42,
 n = 52,
 h0 = 0.5,
 prior_analysis = "beta",
 alternative = "greater",
 alpha = 1,
 beta = 1)


Bayes factor for a Bayesian correlation test

Description

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.

Usage

BF10.cor(
  r,
  n,
  k,
  alpha,
  beta,
  h0,
  alternative,
  scale,
  prior_analysis,
  ROPE = NULL
)

Arguments

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 ("d_beta") under the alternative hypothesis.

alpha

Numeric scalar. Parameter for the analysis beta prior ("beta") under the alternative hypothesis.

beta

Numeric scalar. Parameter for the analysis beta prior ("beta") under the alternative hypothesis.

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 ("two.sided"), right-sided ("greater"), or left-sided ("less").

scale

Numeric scalar. Scale parameter for the analysis normal-moment prior ("Moment"). Must be > 0.

prior_analysis

Character. Analysis prior: default beta ("d_beta"), beta ("beta"), or normal-moment ("Moment").

ROPE

Numeric vector. Optional numeric vector specifying bounds for an interval null hypothesis. For "two.sided", must be two distinct finite values between -0.5 and 0.5. For "greater" or "less", must satisfy additional bounds relative to h0.

Value

A list with class "BFvalue_r" containing:

Examples

BF10.cor(
  r = 0.3930924,
  n = 46,
  prior_analysis = "d_beta",
  k = 1,
  h0 = 0,
  alternative = "two.sided")

Bayes Factor for a Bayesian F-Test

Description

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.

Usage

BF10.f.test(fval, df1, df2, dff, rscale, f_m, prior_analysis, ROPE = NULL)

Arguments

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 \ge 3.

rscale

Numeric scalar. Scale parameter for the effect-size prior (only used when prior_analysis = "effectsize").

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 "effectsize" or "Moment".

ROPE

Numeric scaler. Optional numeric scalar specifying an upper bound for an interval null hypothesis. If provided, must be > 0.

Value

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 f_m, and degrees of freedom dff.

bf10

The computed Bayes factor.

p.value

p-value.

Examples

BF10.f.test(
  fval = 4.5,
  df1 = 2,
  df2 = 12,
  dff = 12,
  rscale = 0.707,
  f_m = 0.1,
  prior_analysis = "effectsize"
)


Bayes factor for comparing two proportions

Description

Compute the Bayes factor (BF10) for a Bayesian test of two proportions.

Usage

BF10.props(a0, b0, a1, b1, a2, b2, n1, n2, x1, x2)

Arguments

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.

Value

A list of class BFvalue_2p containing:

Examples

BF10.props(
a0 = 1,
b0 = 1,
a1 = 1,
b1 = 1,
a2 = 1,
b2 = 1,
n1 = 493,
n2 = 488,
x1 = 155,
x2 = 150)

Bayes Factor for a One-Sample Bayesian t-Test

Description

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.

Usage

BF10.ttest.OneSample(
  tval,
  df,
  prior_analysis,
  location,
  scale,
  dff,
  alternative,
  ROPE = NULL
)

Arguments

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 string. Statistical model for the analysis prior under the alternative hypothesis. Choices are:

"Normal"

Normal distribution.

"Moment"

Normal moment prior.

"t-distribution"

Scaled t-distribution.

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 prior_analysis = "t-distribution"; must be > 0). Ignored otherwise.

alternative

Character string. The direction of the alternative hypothesis. One of:

"!="

Two-sided (difference from 0).

">"

Right-sided (greater than 0).

"<"

Left-sided (less than 0).

ROPE

Optional numeric vector. Specifies bounds for an interval null hypothesis. For:

Two-sided ("two.sided")

Must be a numeric vector of length 2 with two distinct finite values.

Right-sided ("greater")

Must be a numeric scalar > 0.

Left-sided ("less")

Must be a numeric scalar < 0.

Value

A list 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.

Examples

BF10.ttest.OneSample(
tval = 2,
df = 50,
prior_analysis = "t-distribution",
location = 0,
scale = 0.707,
dff = 1,
alternative = "two.sided")



Bayes Factor for Two-Sample Bayesian t-Test

Description

Compute the Bayes factor (BF10) for a two-sample independent-samples t-test. Supports both point-null and interval-null hypotheses.

Usage

BF10.ttest.TwoSample(
  tval,
  N1,
  N2,
  prior_analysis,
  location,
  scale,
  dff,
  alternative,
  ROPE = NULL
)

Arguments

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: "Normal", "Moment" (normal-moment prior), or "t-distribution".

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 = "t-distribution"; ignored otherwise).

alternative

Character. The direction of the alternative hypothesis two-sided ("two.sided"), right-sided ("greater"), or left-sided ("less").

ROPE

Optional numeric. Bounds for an interval null:

  • For "two.sided", must be a numeric vector of length 2 with distinct finite values.

  • For "greater", must be a single numeric scalar > 0.

  • For "less", must be a single numeric scalar < 0.

Value

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 (currently NA / not computed).

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.

Examples

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))


Sample Size Determination for the Bayesian one-proportion test

Description

Perform sample size determination or the calculation of compelling and misleading evidence for a Bayesian test of a single proportion.

Usage

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",
  plot_power = FALSE,
  plot_rel = FALSE
)

Arguments

alternative

Character. The direction of the alternative hypothesis : two-sided ("two.sided"), right-sided ("greater"), or left-sided ("less").

threshold

Numeric scalar. Threshold for compelling evidence (must be > 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: "beta" or "Moment".

alpha

Numeric scalar. Parameter for the analysis beta prior (used when prior_analysis = "beta").

beta

Numeric scalar. Parameter for the analysis beta prior (used when prior_analysis = "beta").

scale

Numeric scalar. Scale parameter for the analysis moment prior (used when prior_analysis = "Moment").

prior_design

Character. Design prior under the alternative hypothesis: "beta", "Moment", or "Point".

alpha_d

Numeric scalar. Parameter for the design beta prior (used when prior_design = "beta").

beta_d

Numeric scalar. Parameter for the design beta prior (used when prior_design = "beta").

location_d

Numeric scalar. Proportion value for the design point prior (prior_design = "Point"). Represents the true proportion under the alternative hypothesis.

scale_d

Numeric scalar. Scale parameter for the design moment prior (used when prior_design = "Moment").

N

Numeric integer. Sample size. If NULL, sample size determination is performed.

ROPE

Numeric vector. Numeric bounds for the interval null (used when computing interval Bayes factors).

  • For hypothesis = "two.sided", must be a numeric vector of length 2 with distinct finite values.

  • For "greater", must be a single numeric scalar > 0.

  • For "less", must be a single numeric scalar < 0.

type_rate

Character. Either "positive" (controls true/false positive rates) or "negative" (controls true/false negative rates).

plot_power

Logical. Whether to plot power curves when sample size determination is requested.

plot_rel

Logical. Whether to plot probability of misleading evidence.

Details

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:

The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence meets or exceeds true_rate, while the probability of misleading evidence 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.

Model specification:

The user must specify the analysis prior under the alternative hypothesis using prior_analysis:

The design prior under the alternative hypothesis can optionally be specified using prior_design:

If prior_design is NULL, no design prior is used.

interval null Hypothesis:

If ROPE is provided, the function evaluates the Bayes factor for an interval null. Otherwise, a point-null hypothesis is assumed.

Hypothesis:

The function supports one-sided ("greater" or "less") and two-sided ("two.sided") tests. Design prior and interval null bounds must be consistent with the directionality of the hypothesis.

Plotting:

If plot_power = TRUE, the function plots the probability of compelling evidence as a function of sample size. If plot_rel = TRUE, the relationship between the Bayes factor and the number of successes (proportion) is plotted.

Value

A list of class "BFpower_bin" containing:

If sample size determination fails, the function returns NaN and prints a message.

Examples

BFpower.bin(
  alternative = "greater",
  threshold = 3,
  true_rate = 0.8,
  false_rate = 0.05,
  h0 = 0.5,
  prior_analysis = "beta",
  alpha = 1,
  beta = 1,
  plot_rel = TRUE,
  plot_power = TRUE)



Sample Size Determination for the Bayesian correlation test

Description

Perform sample size determination or the probability of obtaining compelling or misleading evidence for a Bayesian correlation test. Can handle both point-null and interval-null hypothesis, and allows specifying analysis and design priors.

Usage

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",
  plot_power = FALSE,
  plot_rel = FALSE
)

Arguments

alternative

character. The direction of the alternative hypothesis being tested: two-sided ("two.sided"), right-sided ("greater"), or left-sided ("less").

h0

Numeric scalar. Null rho correlation value. Must be between -0.8 and 0.8.

ROPE

Optional numeric vector or scalar specifying bounds for an interval null; used if interval Bayes factor is calculated.

  • For alternative = "two.sided", must be a numeric vector of length 2 with distinct finite values.

  • For alternative = "greater", must be a single numeric scalar > 0.

  • For alternative = "less", must be a single numeric scalar < 0.

threshold

Numeric scalar. Threshold of compelling evidence (numeric scalar > 1).

true_rate

Numeric scalar. Targeted true positive rate (if positive = "positive") or true negative rate (if positive = "negative").

false_rate

Numeric scalar. Targeted false positive rate (if positive = "positive") or false negative rate (if positive = "negative").

prior_analysis

character. Analysis prior under the alternative hypothesis: default beta ("d_beta"), beta ("beta"), or normal moment ("Moment").

k

Numeric scalar. Parameter for the default beta prior ("d_beta").

alpha

Numeric scalar. Parameter for the beta prior ("beta").

beta

Numeric scalar. Parameter for the beta prior ("beta").

scale

Numeric scalar. Scale parameter for the normal moment prior ("Moment").

prior_design

character. Design prior under the alternative hypothesis: default beta ("d_beta"), beta ("beta"), normal moment ("Moment"), or point ("Point").

alpha_d

Numeric scalar. Parameter for the design beta prior ("beta").

beta_d

Numeric scalar. Parameter for the design beta prior ("beta").

location_d

Numeric scalar. Location parameter for the design point prior ("Point").

k_d

Numeric scalar. Parameter for the design default beta prior ("d_beta").

scale_d

Numeric scalar. Scale parameter for the design normal moment prior ("Moment").

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 indicating which rate to control: "positive" (true/false positive rates) or "negative" (true/false negative rates).

plot_power

Logical; if TRUE, plots power curves.

plot_rel

Logical; if TRUE, plots the relationship between the BF and data.

Details

1. Sample size determination mode (when N = NULL):

If no sample size is provided, the function determines the minimum sample size required to meet the desired requirements. In this mode, the user must supply the following arguments:

The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence meets or exceeds true_rate, while the probability of misleading evidence 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.

Hypothesis specification:

The alternative argument defines the direction of the alternative hypothesis : "two.sided" for two-sided, "greater" for right-sided, or "less" for left-sided tests. The optional ROPE argument specifies bounds for an interval null hypothesis. If ROPE = NULL, a point-null test is assumed.

Analysis Priors:

The analysis prior specifies the prior distribution of the correlation under the alternative hypothesis. Depending on prior_analysis, the user must supply:

Design Priors (optional):

A design prior can be supplied to reflect uncertainty about the correlation during study planning. If provided, prior_design must be one of "d_beta", "beta", "Moment", or "Point", and the corresponding parameters must be supplied:

interval null Hypothesis:

If ROPE is provided, the function evaluates the Bayes factor for an interval null. Otherwise, a point-null hypothesis is assumed.

Plotting:

If plot_power = TRUE, the function plots the probability of compelling evidence as a function of sample size. If plot_rel = TRUE, the relationship between the BF and correlation is plotted.

Value

A list of class BFpower_r containing:

Examples

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,
   plot_power = TRUE,
   plot_rel = TRUE
 )


Sample Size Determination for the Bayesian F-test

Description

This function performs sample size determination (when N = NULL) or calculates the probability of compelling/misleading evidence for a fixed sample size.

Usage

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,
  plot_power = FALSE,
  plot_rel = FALSE
)

Arguments

threshold

Numeric scalar. Threshold for compelling evidence (must be > 1).

true_rate

Numeric scalar. Targeted true positive or true negative rate (used only when sample size determination is requested; N = NULL).

false_rate

Numeric scalar. Targeted false positive or false negative rate (used only when sample size determination is requested; N = NULL).

p

Numeric integer. Number of predictors in the reduced model.

k

Numeric integer. Number of predictors in the full model (must satisfy k > p).

prior_analysis

Character. Analysis prior model under the alternative hypothesis: "effectsize" or "Moment".

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 prior_analysis = "Moment".

rscale

Numeric scalar. Scale parameter for the analysis effect-size prior (only used when prior_analysis = "effectsize").

f_m

Numeric scalar. Cohen's f effect-size parameter for the analysis prior (must be > 0).

prior_design

Character. Design prior model under the alternative hypothesis: "effectsize", "Moment", or "Point".

dff_d

Numeric scalar. Degrees of freedom for the design prior. Must be a positive scalar, and at least 3 if prior_design = "Moment".

rscale_d

Numeric scalar. Scale parameter for the design effect-size prior (only used when prior_design = "effectsize").

f_m_d

Numeric scalar. Cohen's f value for the design prior or the effect-size of the point design prior.

N

Numeric integer. Sample size. If NULL, sample size determination is performed.

type_rate

Character. Either "positive" (control true/false positive rates) or "negative" (control true/false negative rates).

ROPE

Numeric vector. Numeric bounds for the interval null (only used when interval Bayes factors are required).

plot_power

Logical. Whether to plot power curves when sample size determination is requested.

plot_rel

Logical. Whether to plot the relationship between the BF and data.

Details

Computes required sample size or probabilities of compelling or misleading evidence for a fixed sample size.

1. Sample size determination mode (when N = NULL):

If no sample size is provided, the function calculates the minimum sample size to achieve the desired configuration below. The user must provide:

The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence meets or exceeds true_rate, while the probability of misleading evidence 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.

Model specification:

The function requires the user to specify the full model (k predictors) and the reduced model (p predictors, k > p), and the analysis prior under the alternative hypothesis. Depending on the chosen prior_analysis, different arguments are required:

The design prior under the alternative hypothesis can optionally be specified using prior_design, which can be:

interval null Hypothesis:

If ROPE is provided, the function evaluates the Bayes factor for an interval null. Otherwise, a point-null hypothesis is assumed.

Plotting:

If plot_power = TRUE, the function plots the probability of compelling evidence as a function of sample size. If plot_rel = TRUE, the relationship between the Bayes factor and Cohen's f is plotted.

Value

A list of class BFpower_f 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 f_m, and degrees of freedom dff.

design_h1

List containing the design prior specification, including the prior distribution, the scale rscale, f 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.

plot_power

Logical; whether power curves are plotted.

plot_rel

Logical; whether the relationship between the Bayes factor and data is plotted.

If sample size determination fails, the function returns NaN and prints a message.

Examples

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,
 plot_power = TRUE,
 plot_rel = TRUE)


Sample Size Determination for the Bayesian test of two proportions

Description

Perform sample size determination or calculate probabilities of compelling and misleading evidence for a Bayesian comparison of two proportions.

Usage

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",
  plot_power = FALSE,
  plot_rel = FALSE
)

Arguments

threshold

Numeric scalar. Threshold of compelling evidence.

true_rate

Numeric scalar. Targeted true positive rate (if positive = "positive") or true negative rate (if positive = "negative").

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: "beta", "Point", or "same" (if "same", the design prior is identical to the analysis prior).

a1d

Numeric scalar. Alpha parameter of the design prior for group 1 (used if model1 = "beta").

b1d

Numeric scalar. Beta parameter of the design prior for group 1 (used if model1 = "beta").

dp1

Numeric scalar. True proportion for group 1 in the design prior (used if model1 = "Point").

prior_design_2

Character. The design prior of group 2: "beta", "Point", or "same" (if "same", the design prior is identical to the analysis prior).

a2d

Numeric scalar. Alpha parameter of the design prior for group 2 (used if model2 = "beta").

b2d

Numeric scalar. Beta parameter of the design prior for group 2 (used if model2 = "beta").

dp2

Numeric scalar. True proportion for group 2 in the design prior (used if model2 = "Point").

n1

Numeric integer. Sample size for group 1.

n2

Numeric integer. Sample size for group 2.

type_rate

Character. Choose "positive" to control true/false positive rates or "negative" to control true/false negative rates.

plot_power

Logical; if TRUE, plot the power curve.

plot_rel

Logical; if TRUE, plot the grid for the values of BF across all possible combination of x1 and x2.

Details

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:

The function iteratively finds the smallest sample sizes for which the probability of obtaining compelling evidence 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.

Model specification:

The user must specify the analysis priors under the null and alternative hypotheses using Beta parameters:

Design priors for the alternative hypothesis can optionally be specified:

Plotting:

If plot_power = TRUE, a power curve is plotted showing the probability of compelling evidence as a function of sample sizes. If plot_rel = TRUE, a grid of Bayes factors across possible outcomes is plotted.

Value

An object of class BFpower_2p (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).

plot_power

Logical; whether the power curve is plotted.

plot_rel

Logical; whether the relationship between the Bayes factor and observed proportion differences is plotted.

Examples

BFpower.props(
threshold = 3,
true_rate = 0.8,
a0 = 1,
b0 = 1,
a1 = 156,
b1 = 339,
a2 = 151,
b2 = 339,
plot_power = TRUE,
plot_rel = TRUE)


Sample Size Determination for the One-Sample Bayesian t-Test

Description

Perform sample size determination or calculate the probability of obtaining compelling or 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.

Usage

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,
  plot_power = FALSE,
  plot_rel = FALSE
)

Arguments

alternative

Character. The direction of the alternative hypothesis : two-sided ("two.sided" ), right-sided ("greater"), or left-sided ("less").

ROPE

Optional numeric vector. Bounds for an interval null hypothesis.

  • For hypothesis = "two.sided", must be a numeric vector of length 2 with distinct finite values.

  • For "greater", must be a single numeric scalar > 0.

  • For "less", must be a single numeric scalar < 0.

prior_analysis

Character. The analysis prior under the alternative hypothesis: "Normal", "Moment" (normal-moment prior), or "t-distribution".

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_analysis = "t-distribution").

prior_design

Optional character. The design prior under the alternative hypothesis: "Normal", "Moment", "t-distribution", or "Point".

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 prior_design = "t-distribution").

N

Numeric integer. Sample size.

type_rate

Character. Either "positive" (controls true/false positive rates) or "negative" (controls true/false negative rates).

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 > 1).

plot_power

Logical. If TRUE, plots power curve.

plot_rel

Logical. If TRUE, plots the relationship between the BF and data.

Details

1. Sample size determination mode (when N = NULL):

If no sample size is provided, the function determines the minimum sample size. In this mode, the user must supply the following arguments:

The function iteratively finds the smallest sample size for which the probability of obtaining compelling evidence meets or exceeds true_rate, while the probability of misleading evidence 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.

Analysis Priors:

The analysis prior specifies the prior distribution of the effect under the alternative hypothesis. The user must provide:

Design Priors (optional):

A design prior can be supplied to reflect uncertainty about the effect size during study planning. If provided, the following must be supplied:

interval null Hypothesis:

The argument ROPE specifies the bounds of an interval null hypothesis. If ROPE is provided, the function evaluates the Bayes factor for an interval null hypothesis. For a point-null hypothesis, ROPE should be left as NULL.

Plotting:

If plot_power = TRUE, the function plots the probability of compelling evidence as a function of sample size. If plot_rel = TRUE, the relationship betwwen the BF and data is plotted.

Value

An object of class BFpower_t (a list) 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.

plot_power

Logical, whether to plot the power curve.

plot_rel

Logical, whether the relationship between the BF and t-value is plotted.

Examples

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
)

Sample Size Determination for the Two-Sample Bayesian t-Test

Description

Perform sample size determination or calculate the probabilities of obtaining compelling or misleading evidence for a two-sample Bayesian t-test. Supports point-null and interval-null hypotheses, and allows specifying analysis and design priors.

Usage

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",
  plot_power = FALSE,
  plot_rel = FALSE
)

Arguments

alternative

Character. The direction of the alternative hypothesis: two-sided ("two.sided"), right-sided ("greater"), or left-sided ("less").

ROPE

Optional numeric. Bounds for an interval null:

  • For hypothesis = "two.sided", must be a numeric vector of length 2 with distinct finite values.

  • For "greater", must be a single numeric scalar > 0.

  • For "less", must be a single numeric scalar < 0.

threshold

Numeric scalar. Threshold for compelling evidence (must be > 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: "Normal", "Moment", or "t-distribution".

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 = "t-distribution"; ignored otherwise).

prior_design

Optional character. Design prior under the alternative: "Normal", "Moment", "t-distribution", or "Point".

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 prior_design = "t-distribution"; ignored otherwise).

N1

Numeric integer. Sample size for group 1 (used if r = NULL).

N2

Numeric integer. Sample size for group 2 (used if r = NULL).

r

Optional numeric scalar. Ratio of sample size N2 / N1 (used if N1 and N2 are NULL).

type_rate

Character, either "positive" or "negative"; determines whether to control true/false positive or true/false negative rates .

plot_power

Logical. If TRUE, a plot of the power or probability of compelling evidence is generated.

plot_rel

Logical. Whether the relationship between the BF and data is plotted..

Details

1. Sample size determination mode (when N1 = NULL and N2 = NULL, but r is provided):

If no sample sizes are provided, the function calculates the minimum required sample sizes for both groups. In this mode, the user must supply:

The function iteratively finds the smallest sample sizes N1 and N2 = r * N1 for which the probability of obtaining compelling evidence meets or exceeds true_rate, while the probability of misleading evidence does not exceed false_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 those fixed sample sizes. In this mode, the arguments type_rate, true_rate, and false_rate are ignored; only the Bayes factor threshold threshold is used.

Analysis Priors:

The analysis prior specifies the prior distribution of the effect under the alternative hypothesis. The user must provide:

Design Priors (optional):

A design prior can be supplied to reflect uncertainty about the effect size during study planning. If provided, the following must be supplied:

interval null Hypothesis:

The argument ROPE specifies the bounds of an interval null hypothesis. If ROPE is provided, the function evaluates the Bayes factor for an interval null hypothesis. For a point-null hypothesis, ROPE should be left as NULL.

Plotting:

If plot_power = TRUE, the function plots the probability of compelling evidence as a function of the sample sizes. If plot_rel = TRUE, the relationship between BF and data is plotted.

Value

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.

plot_power

Logical flag for plotting power.

plot_rel

Logical flag for plotting the relationship between BF and t-value.

Examples

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)

Launch the BayesPower Shiny Application

Description

This function starts the interactive Shiny application for Bayesian power analysis using Bayes factors. The app provides a graphical user interface built with shiny.

Usage

BayesPower_BayesFactor()

Details

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.

Value

No return value, called for its side effects.

Examples

if (interactive()) {
  # Launch the Shiny application
  BayesPower_BayesFactor()
}