Version: 0.1.5
Date: 2025-05-15
Author: Samuel Pawel ORCID iD [aut, cre]
Maintainer: Samuel Pawel <samuel.pawel@uzh.ch>
Title: Power and Sample Size Calculations for Bayes Factor Analysis
Description: Implements z-test, t-test, and normal moment prior Bayes factors based on summary statistics, along with functionality to perform corresponding power and sample size calculations as described in Pawel and Held (2025) <doi:10.1080/00031305.2025.2467919>.
License: GPL-3
Encoding: UTF-8
Imports: lamW
Suggests: roxygen2, tinytest, knitr
VignetteBuilder: knitr
NeedsCompilation: no
RoxygenNote: 7.3.1
URL: https://github.com/SamCH93/bfpwr
BugReports: https://github.com/SamCH93/bfpwr/issues
Packaged: 2025-05-15 07:37:20 UTC; sam
Repository: CRAN
Date/Publication: 2025-05-15 08:20:06 UTC

z-test Bayes factor

Description

This function computes the Bayes factor that quantifies the evidence that the data (in the form of an asymptotically normally distributed parameter estimate with standard error) provide for a point null hypothesis with a normal prior assigned to the parameter under the alternative. The standard error is assumed to be known.

Usage

bf01(estimate, se, null = 0, pm, psd, log = FALSE)

Arguments

estimate

Parameter estimate

se

Standard error of the parameter estimate

null

Parameter value under the point null hypothesis. Defaults to 0

pm

Mean of the normal prior assigned to the parameter under the alternative

psd

Standard deviation of the normal prior assigned to the parameter under the alternative. Set to 0 to obtain a point prior at the prior mean

log

Logical indicating whether the natural logarithm of the Bayes factor should be returned. Defaults to FALSE

Value

Bayes factor in favor of the null hypothesis over the alternative (\text{BF}_{01} > 1 indicates evidence for the null hypothesis, whereas \text{BF}_{01} < 1 indicates evidence for the alternative)

Author(s)

Samuel Pawel

Examples

bf01(estimate = 0.2, se = 0.05, null = 0, pm = 0, psd = 2)


Binomial Bayes factor

Description

This function computes the Bayes factor for testing a binomial proportion p based on x observed successes out of n trials. Two types of tests are available:

Usage

binbf01(
  x,
  n,
  p0 = 0.5,
  type = c("point", "direction"),
  a = 1,
  b = 1,
  log = FALSE
)

Arguments

x

Number of successes

n

Number of trials

p0

Tested binomial proportion. Defaults to 0.5

type

Type of test. Can be "point" or "directional". Defaults to "point"

a

Number of successes parameter of the beta prior distribution. Defaults to 1

b

Number of failures parameter of the beta prior distribution. Defaults to 1

log

Logical indicating whether the natural logarithm of the Bayes factor should be returned. Defaults to FALSE

Value

Bayes factor in favor of the null hypothesis over the alternative (\text{BF}_{01} > 1 indicates evidence for the null hypothesis, whereas \text{BF}_{01} < 1 indicates evidence for the alternative)

Author(s)

Samuel Pawel

See Also

pbinbf01, nbinbf01

Examples

## example on Mendelian inheritance from ?stats::binom.test
binbf01(x = 682, n = 925, p0 = 3/4, a = 1, b = 1, type = "point")
## 18.6 => strong evidence for the hypothesized p = 3/4 compared to other p

## with directional hypothesis
binbf01(x = 682, n = 925, p0 = 3/4, a = 1, b = 1, type = "direction")
## 1.5 => only anecdotal evidence for p <= 3/4 over p > 3/4

## Particle-counting experiment from Stone (1997) with point null
binbf01(x = 106298, n = 527135, p0 = 0.2, a = 1, b = 1, type = "point")
## 8.1 => moderate evidence for the alternative over the null

## Coin flip experiment from Bartos et al. (2023) with point null
binbf01(x = 178079, n = 350757 , p0 = 0.5, a = 5100, b = 4900, type = "point")
## => 1/1.72e+17 extreme evidence in favor of the alternative over the null


Sample size determination for z-test Bayes factor

Description

This function computes the required sample size to obtain a Bayes factor (bf01) more extreme than a threshold k with a specified target power.

Usage

nbf01(
  k,
  power,
  usd,
  null = 0,
  pm,
  psd,
  dpm = pm,
  dpsd = psd,
  nrange = c(1, 10^5),
  lower.tail = TRUE,
  integer = TRUE,
  analytical = TRUE,
  ...
)

Arguments

k

Bayes factor threshold

power

Target power

usd

Unit standard deviation, the (approximate) standard error of the parameter estimate based on \code{n}=1, see details

null

Parameter value under the point null hypothesis. Defaults to 0

pm

Mean of the normal prior assigned to the parameter under the alternative in the analysis

psd

Standard deviation of the normal prior assigned to the parameter under the alternative in the analysis. Set to 0 to obtain a point prior at the prior mean

dpm

Mean of the normal design prior assigned to the parameter. Defaults to the same value as the analysis prior pm

dpsd

Standard deviation of the normal design prior assigned to the parameter. Defaults to the same value as the analysis prior psd

nrange

Sample size search range over which numerical search is performed. Defaults to c(1, 10^5)

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

integer

Logical indicating whether only integer valued sample sizes should be returned. If TRUE the required sample size is rounded to the next larger integer. Defaults to TRUE

analytical

Logical indicating whether analytical (if available) or numerical method should be used. Defaults to TRUE

...

Other arguments passed to stats::uniroot

Details

It is assumed that the standard error of the future parameter estimate is of the form \code{se} =\code{usd}/\sqrt{\code{n}}. For example, for normally distributed data with known standard deviation sd and two equally sized groups of size n, the standard error of an estimated standardized mean difference is \code{se} = \code{sd}\sqrt{2/n}, so the corresponding unit standard deviation is \code{usd} = \code{sd}\sqrt{2}. See the vignette for more information.

Value

The required sample size to achieve the specified power

Note

A warning message will be displayed in case that the specified target power is not achievable under the specified analysis and design priors.

Author(s)

Samuel Pawel

See Also

pbf01, powerbf01, bf01

Examples

## point alternative (analytical and numerical solution available)
nbf01(k = 1/10, power = 0.9, usd = 1, null = 0, pm = 0.5, psd = 0,
      analytical = c(TRUE, FALSE), integer = FALSE)

## standardized mean difference (usd = sqrt(2), effective sample size = per group size)
nbf01(k = 1/10, power = 0.9, usd = sqrt(2), null = 0, pm = 0, psd = 1)
## this is the sample size per group (assuming equally sized groups)

## z-transformed correlation (usd = 1, effective sample size = n - 3)
nbf01(k = 1/10, power = 0.9, usd = 1, null = 0, pm = 0.2, psd = 0.5)
## have to add 3 to obtain the actual sample size

## log hazard/odds ratio (usd = 2, effective sample size = total number of events)
nbf01(k = 1/10, power = 0.9, usd = 2, null = 0, pm = 0, psd = sqrt(0.5))
## have to convert the number of events to a sample size


Sample size determination for binomial Bayes factor

Description

This function computes the required sample size to obtain a binomial Bayes factor (binbf01) more extreme than a threshold k with a specified target power.

Usage

nbinbf01(
  k,
  power,
  p0 = 0.5,
  type = c("point", "direction"),
  a = 1,
  b = 1,
  dp = NA,
  da = a,
  db = b,
  dl = 0,
  du = 1,
  lower.tail = TRUE,
  nrange = c(1, 10^4),
  ...
)

Arguments

k

Bayes factor threshold

power

Target power

p0

Tested binomial proportion. Defaults to 0.5

type

Type of test. Can be "point" or "directional". Defaults to "point"

a

Number of successes parameter of the beta analysis prior distribution. Defaults to 1

b

Number of failures parameter of the beta analysis prior distribution. Defaults to 1

dp

Fixed binomial proportion assumed for the power calculation. Set to NA to use a truncated beta design prior instead (specified via the da, db, dl, and du arguments). Defaults to NA

da

Number of successes parameter of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to the same value a as specified for the analysis prior

db

Number of failures parameter of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to the same value b as specified for the analysis prior

dl

Lower truncation limit of of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to 0

du

Upper truncation limit of of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to 1

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

nrange

Sample size search range over which numerical search is performed. Defaults to c(1, 10^4)

...

Other arguments passed to stats::uniroot

Value

The required sample size to achieve the specified power

Author(s)

Samuel Pawel

See Also

pbinbf01, binbf01

Examples

## sample size parameters
pow <- 0.9
p0 <- 3/4
a <- 1
b <- 1
k <- 1/10

## Not run: 
## sample sizes for directional testing
(nH1 <- nbinbf01(k = k, power = pow, p0 = p0, type = "direction", a = a,
                 b = b, da = a, db = b, dl = p0, du = 1))
(nH0 <- nbinbf01(k = 1/k, power = pow, p0 = p0, type = "direction", a = a,
                 b = b, da = a, db = b, dl = 0, du = p0, lower.tail = FALSE))
nseq <- seq(1, 1.1*max(c(nH1, nH0)), length.out = 100)
powH1 <- pbinbf01(k = k, n = nseq, p0 = p0, type = "direction", a = a,
                  b = b, da = a, db = b, dl = p0, du = 1)
powH0 <- pbinbf01(k = 1/k, n = nseq, p0 = p0, type = "direction", a = a,
                  b = b, da = a, db = b, dl = 0, du = p0, lower.tail = FALSE)
matplot(nseq, cbind(powH1, powH0), type = "s", xlab = "n", ylab = "Power", lty = 1,
        ylim = c(0, 1), col = c(2, 4), las = 1)
abline(h = pow, lty = 2)
abline(v = c(nH1, nH0), col = c(2, 4), lty = 2)
legend("topleft", legend = c("H1", "H0"), lty = 1, col = c(2, 4))

## sample sizes for point null testing
(nH1 <- nbinbf01(k = k, power = pow, p0 = p0, type = "point", a = a,
                 b = b, da = a, db = b))
(nH0 <- nbinbf01(k = 1/k, power = pow, p0 = p0, type = "point", a = a,
                 b = b, dp = p0, lower.tail = FALSE, nrange = c(1, 10^5)))
nseq <- seq(1, max(c(nH1, nH0)), length.out = 100)
powH1 <- pbinbf01(k = k, n = nseq, p0 = p0, type = "point", a = a,
                  b = b, da = a, db = b, dl = 0, du = 1)
powH0 <- pbinbf01(k = 1/k, n = nseq, p0 = p0, type = "point", a = a,
                  b = b, dp = p0, lower.tail = FALSE)
matplot(nseq, cbind(powH1, powH0), type = "s", xlab = "n", ylab = "Power", lty = 1,
        ylim = c(0, 1), col = c(2, 4), las = 1)
abline(h = pow, lty = 2)
abline(v = c(nH1, nH0), col = c(2, 4), lty = 2)
legend("topleft", legend = c("H1", "H0"), lty = 1, col = c(2, 4))

## End(Not run)

Normal moment prior Bayes factor

Description

This function computes the Bayes factor that quantifies the evidence that the data (in the form of an asymptotically normally distributed parameter estimate with standard error) provide for a point null hypothesis with a normal moment prior assigned to the parameter under the alternative.

Usage

nmbf01(estimate, se, null = 0, psd, log = FALSE)

Arguments

estimate

Parameter estimate

se

Standard error of the parameter estimate

null

Parameter value under the point null hypothesis. Defaults to 0

psd

Spread of the normal moment prior assigned to the parameter under the alternative. The modes of the prior are located at \pm\sqrt{2}\,\code{psd}

log

Logical indicating whether the natural logarithm of the Bayes factor should be returned. Defaults to FALSE

Details

A normal moment prior has density f(x \mid \code{null}, \code{psd}) = N(x \mid \code{null}, \code{psd}^2) \times (x - \code{null})/ \code{psd}^2 with N(x \mid m, v) the normal density with mean m and variance v evaluated at x.

Value

Bayes factor in favor of the null hypothesis over the alternative (\text{BF}_{01} > 1 indicates evidence for the null hypothesis, whereas \text{BF}_{01} < 1 indicates evidence for the alternative)

Author(s)

Samuel Pawel

References

Johnson, V. E. and Rossell, D. (2010). On the use of non-local prior densities in Bayesian hypothesis tests. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 72(2):143–170. doi:10.1111/j.1467-9868.2009.00730.x

Pramanik, S. and Johnson, V. E. (2024). Efficient alternatives for Bayesian hypothesis tests in psychology. Psychological Methods, 29(2):243–261. doi:10.1037/met0000482

See Also

nmbf01, pnmbf01, nnmbf01, powernmbf01

Examples

nmbf01(estimate = 0.25, se = 0.05, null = 0, psd = 0.5/sqrt(2)) # mode at 0.5


Sample size determination for normal moment prior Bayes factor

Description

This function computes the required sample size to obtain a normal moment prior Bayes factor (nbf01) more extreme than a threshold k with a specified target power.

Usage

nnmbf01(
  k,
  power,
  usd,
  null = 0,
  psd,
  dpm,
  dpsd,
  nrange = c(1, 10^5),
  lower.tail = TRUE,
  integer = TRUE,
  ...
)

Arguments

k

Bayes factor threshold

power

Target power

usd

Unit standard deviation, the (approximate) standard error of the parameter estimate based on \code{n}=1, see details

null

Parameter value under the point null hypothesis. Defaults to 0

psd

Spread of the normal moment prior assigned to the parameter under the alternative in the analysis. The modes of the prior are located at \pm\sqrt{2}\,\code{psd}

dpm

Mean of the normal design prior assigned to the parameter

dpsd

Standard deviation of the normal design prior assigned to the parameter. Set to 0 to obtain a point prior at the design prior mean

nrange

Sample size search range over which numerical search is performed. Defaults to c(1, 10^5)

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

integer

Logical indicating whether only integer valued sample sizes should be returned. If TRUE the required sample size is rounded to the next larger integer. Defaults to TRUE

...

Other arguments passed to stats::uniroot

Details

It is assumed that the standard error of the future parameter estimate is of the form \code{se} =\code{usd}/\sqrt{\code{n}}. For example, for normally distributed data with known standard deviation sd and two equally sized groups of size n, the standard error of an estimated standardized mean difference is \code{se} = \code{sd}\sqrt{2/n}, so the corresponding unit standard deviation is \code{usd} = \code{sd}\sqrt{2}. See the vignette for more information.

Value

The required sample size to achieve the specified power

Author(s)

Samuel Pawel

See Also

nmbf01, pnmbf01, powernmbf01

Examples

nnmbf01(k = 1/10, power = 0.9, usd = 1, null = 0, psd = 0.5/sqrt(2), dpm = 0.5, dpsd = 0)

Sample size calculations for t-test Bayes factor

Description

This function computes the required sample size to obtain a t-test Bayes factor (tbf01) more extreme than a threshold k with a specified target power.

Usage

ntbf01(
  k,
  power,
  null = 0,
  plocation = 0,
  pscale = 1/sqrt(2),
  pdf = 1,
  type = c("two.sample", "one.sample", "paired"),
  alternative = c("two.sided", "less", "greater"),
  dpm = plocation,
  dpsd = pscale,
  lower.tail = TRUE,
  integer = TRUE,
  nrange = c(2, 10^4),
  ...
)

Arguments

k

Bayes factor threshold

power

Target power

null

Standardized mean difference under the point null hypothesis. Defaults to 0

plocation

t prior location. Defaults to 0

pscale

t prior scale. Defaults to 1/sqrt(2)

pdf

t prior degrees of freedom. Defaults to 1 (a Cauchy prior)

type

Type of t-test. Can be "two.sample" (default), "one.sample", or "paired"

alternative

Direction of the test. Can be either "two.sided" (default), "less", or "greater". The latter two truncate the analysis prior to negative and positive effects, respectively. If set to "less" or "greater", the power is only computed based on data with effect estimates in the direction of the alternative

dpm

Mean of the normal design prior assigned to the standardized mean difference. Defaults to the analysis prior location

dpsd

Standard deviation of the normal design prior assigned to the standardized mean difference. Set to 0 to obtain a point prior at the design prior mean. Defaults to the analysis prior scale

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

integer

Logical indicating whether only integer valued sample sizes should be returned. If TRUE the required sample size is rounded to the next larger integer. Defaults to TRUE

nrange

Sample size search range over which numerical search is performed. Defaults to c(2, 10^4)

...

Other arguments passed to stats::uniroot

Value

The required sample size to achieve the specified power

Author(s)

Samuel Pawel

See Also

ptbf01, powertbf01, tbf01

Examples

 ## example from Schönbrodt and Wagenmakers (2018, p.135)
 ntbf01(k = 1/6, power = 0.95, dpm = 0.5, dpsd = 0, alternative = "greater")
 ntbf01(k = 1/6, power = 0.95, dpm = 0.5, dpsd = 0.1, alternative = "greater")
 ntbf01(k = 6, power = 0.95, dpm = 0, dpsd = 0, alternative = "greater",
        lower.tail = FALSE, nrange = c(2, 10000))


Cumulative distribution function of the z-test Bayes factor

Description

This function computes the probability of obtaining a Bayes factor (bf01) more extreme than a threshold k with a specified sample size.

Usage

pbf01(k, n, usd, null = 0, pm, psd, dpm = pm, dpsd = psd, lower.tail = TRUE)

Arguments

k

Bayes factor threshold

n

Sample size

usd

Unit standard deviation, the (approximate) standard error of the parameter estimate based on \code{n}=1, see details

null

Parameter value under the point null hypothesis. Defaults to 0

pm

Mean of the normal prior assigned to the parameter under the alternative in the analysis

psd

Standard deviation of the normal prior assigned to the parameter under the alternative in the analysis. Set to 0 to obtain a point prior at the prior mean

dpm

Mean of the normal design prior assigned to the parameter. Defaults to the same value as the analysis prior pm

dpsd

Standard deviation of the normal design prior assigned to the parameter. Defaults to the same value as the analysis prior psd

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

Details

It is assumed that the standard error of the future parameter estimate is of the form \code{se} =\code{usd}/\sqrt{\code{n}}. For example, for normally distributed data with known standard deviation sd and two equally sized groups of size n, the standard error of an estimated standardized mean difference is \code{se} = \code{sd}\sqrt{2/n}, so the corresponding unit standard deviation is \code{usd} = \code{sd}\sqrt{2}. See the vignette for more information.

Value

The probability that the Bayes factor is less or greater (depending on the specified lower.tail) than the specified threshold k

Author(s)

Samuel Pawel

See Also

nbf01, powerbf01, bf01

Examples

## point alternative (psd = 0)
pbf01(k = 1/10, n = 200, usd = 2, null = 0, pm = 0.5, psd = 0)

## normal alternative (psd > 0)
pbf01(k = 1/10, n = 100, usd = 2, null = 0, pm = 0.5, psd = 2)

## design prior is the null hypothesis (dpm = 0, dpsd = 0)
pbf01(k = 10, n = 1000, usd = 2, null = 0, pm = 0.3, psd = 2, dpm = 0, dpsd = 0, lower.tail = FALSE)

## draw a power curve
nseq <- round(exp(seq(log(10), log(10000), length.out = 100)))
plot(nseq, pbf01(k = 1/10, n = nseq, usd = 2, null = 0, pm = 0.3, psd = 0), type = "l",
     xlab = "n", ylab = bquote("Pr(BF"["01"] <= 1/10 * ")"), ylim = c(0, 1),
     log = "x", las = 1)

## standardized mean difference (usd = sqrt(2), effective sample size = per group size)
n <- 30
pbf01(k = 1/10, n = n, usd = sqrt(2), null = 0, pm = 0, psd = 1)

## z-transformed correlation (usd = 1, effective sample size = n - 3)
n <- 100
pbf01(k = 1/10, n = n - 3, usd = 1, null = 0, pm = 0.2, psd = 0.5)

## log hazard/odds ratio (usd = 2, effective sample size = total number of events)
nevents <- 100
pbf01(k = 1/10, n = nevents, usd = 2, null = 0, pm = 0, psd = sqrt(0.5))


Cumulative distribution function of the binomial Bayes factor

Description

This function computes the probability of obtaining a binomial Bayes factor (binbf01) more extreme than a threshold k with a specified sample size.

Usage

pbinbf01(
  k,
  n,
  p0 = 0.5,
  type = c("point", "direction"),
  a = 1,
  b = 1,
  dp = NA,
  da = a,
  db = b,
  dl = 0,
  du = 1,
  lower.tail = TRUE
)

Arguments

k

Bayes factor threshold

n

Number of trials

p0

Tested binomial proportion. Defaults to 0.5

type

Type of test. Can be "point" or "directional". Defaults to "point"

a

Number of successes parameter of the beta analysis prior distribution. Defaults to 1

b

Number of failures parameter of the beta analysis prior distribution. Defaults to 1

dp

Fixed binomial proportion assumed for the power calculation. Set to NA to use a truncated beta design prior instead (specified via the da, db, dl, and du arguments). Defaults to NA

da

Number of successes parameter of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to the same value a as specified for the analysis prior

db

Number of failures parameter of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to the same value b as specified for the analysis prior

dl

Lower truncation limit of of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to 0

du

Upper truncation limit of of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to 1

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

Value

The probability that the Bayes factor is less or greater (depending on the specified lower.tail) than the specified threshold k

Author(s)

Samuel Pawel

See Also

binbf01, nbinbf01

Examples

## compute probability that BF > 10 under the point null
a <- 1
b <- 1
p0 <- 3/4
k <- 10
nseq <- seq(1, 1000, length.out = 100)
powH0 <- pbinbf01(k = k, n = nseq, p0 = p0, type = "point", a = a, b = b,
                  dp = p0, lower.tail = FALSE)
plot(nseq, powH0, type = "s", xlab = "n", ylab = "Power")

## compare to normal approximation
pm <- a/(a + b) # prior mean under H1
psd <- sqrt(a*b/(a + b)^2/(a + b + 1)) # prior standard deviation under H1
pownormH0 <- pbf01(k = k, n = nseq, usd = sqrt(p0*(1 - p0)), null = p0,
                   pm = pm, psd = psd, dpm = p0, dpsd = 0, lower.tail = FALSE)
lines(nseq, pownormH0, type = "s", col = 2)
legend("right", legend = c("Exact", "Normal approximation"), lty = 1,
       col = c(1, 2))

## compute probability that BF < 1/10 under the p|H1 ~ Beta(a, b) alternative
a <- 10
b <- 5
p0 <- 3/4
k <- 1/10
powH1 <- pbinbf01(k = k, n = nseq, p0 = p0, type = "point", a = a, b = b,
                  da = a, db = b, dl = 0, du = 1)
plot(nseq, powH1, type = "s", xlab = "n", ylab = "Power")

## compare to normal approximation
pm <- a/(a + b) # prior mean under H1
psd <- sqrt(a*b/(a + b)^2/(a + b + 1)) # prior standard deviation under H1
pownormH1 <- pbf01(k = k, n = nseq, usd = sqrt(pm*(1 - pm)), null = p0,
                   pm = pm, psd = psd, dpm = pm, dpsd = psd)
lines(nseq, pownormH1, type = "s", col = 2)
legend("right", legend = c("Exact", "Normal approximation"), lty = 1,
       col = c(1, 2))

## probability that directional BF <= 1/10 under uniform [3/4, 1] design prior
pow <- pbinbf01(k = 1/10, n = nseq, p0 = 3/4, type = "direction", a = 1, b = 1,
                da = 1, db = 1, dl = 3/4, du = 1)
plot(nseq, pow, type = "s", xlab = "n", ylab = "Power")

Plot method for class "power.bftest"

Description

Plot method for class "power.bftest"

Usage

## S3 method for class 'power.bftest'
plot(
  x,
  nlim = c(2, 500),
  ngrid = 100,
  type = "l",
  plot = TRUE,
  nullplot = TRUE,
  ...
)

Arguments

x

Object of class "power.bftest"

nlim

Range of sample sizes over which the power should be computed. Defaults to c(2, 500)

ngrid

Number of grid point for which power should be computed. Defaults to 100

type

Type of plot. Defaults to "l" (line-plot)

plot

Logical indicating whether data should be plotted. If FALSE only the data used for plotting are returned.

nullplot

Logcal indicating whether a second plot with the power in favor of the null (using a Bayes factor threshold of 1/k) should be created. Defaults to TRUE

...

Other arguments (for consistency with the generic)

Value

Plots power curves (if specified) and invisibly returns a list of data frames containing the data underlying the power curves

Author(s)

Samuel Pawel

See Also

powerbf01, powertbf01, powernmbf01

Examples

ssd1 <- powerbf01(k = 1/6, power = 0.95, pm = 0, psd = 1/sqrt(2), dpm = 0.5, dpsd = 0)
plot(ssd1, nlim = c(1, 8000))

power1 <- powerbf01(k = 1/2, n = 120, pm = 0, psd = 1/sqrt(2), dpm = 0.5, dpsd = 0)
plot(power1, nlim = c(1, 1000))


Cumulative distribution function of the normal moment prior Bayes factor

Description

This function computes the probability of obtaining a normal moment prior Bayes factor (nmbf01) more extreme than a threshold k with a specified sample size.

Usage

pnmbf01(k, n, usd, null = 0, psd, dpm, dpsd, lower.tail = TRUE)

Arguments

k

Bayes factor threshold

n

Sample size

usd

Unit standard deviation, the (approximate) standard error of the parameter estimate based on \code{n}=1, see details

null

Parameter value under the point null hypothesis. Defaults to 0

psd

Spread of the normal moment prior assigned to the parameter under the alternative in the analysis. The modes of the prior are located at \pm\sqrt{2}\,\code{psd}

dpm

Mean of the normal design prior assigned to the parameter

dpsd

Standard deviation of the normal design prior assigned to the parameter. Set to 0 to obtain a point prior at the design prior mean

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

Details

It is assumed that the standard error of the future parameter estimate is of the form \code{se} =\code{usd}/\sqrt{\code{n}}. For example, for normally distributed data with known standard deviation sd and two equally sized groups of size n, the standard error of an estimated standardized mean difference is \code{se} = \code{sd}\sqrt{2/n}, so the corresponding unit standard deviation is \code{usd} = \code{sd}\sqrt{2}. See the vignette for more information.

Value

The probability that the Bayes factor is less or greater (depending on the specified lower.tail) than the specified threshold k

Author(s)

Samuel Pawel

See Also

nmbf01, nnmbf01, powernmbf01

Examples

## point desing prior (psd = 0)
pnmbf01(k = 1/10, n = 200, usd = 2, null = 0, psd = 0.5/sqrt(2), dpm = 0.5, dpsd = 0)

## normal design prior to incorporate parameter uncertainty (psd > 0)
pnmbf01(k = 1/10, n = 200, usd = 2, null = 0, psd = 0.5/sqrt(2), dpm = 0.5, dpsd = 0.25)

## design prior is the null hypothesis (dpm = 0, dpsd = 0)
pnmbf01(k = 10, n = 200, usd = 2, null = 0, psd = 0.5/sqrt(2), dpm = 0, dpsd = 0,
        lower.tail = FALSE)


Power and sample size calculations for z-test Bayes factor

Description

Compute probability that z-test Bayes factor is smaller than a specified threshold (the power), or determine sample size to obtain a target power.

Usage

powerbf01(
  n = NULL,
  power = NULL,
  k = 1/10,
  sd = 1,
  null = 0,
  pm,
  psd,
  type = c("two.sample", "one.sample", "paired"),
  dpm = pm,
  dpsd = psd,
  nrange = c(1, 10^5)
)

Arguments

n

Sample size (per group for two-sample tests). Has to be NULL if power is specified. Defaults to NULL

power

Target power. Has to be NULL if n is specified. Defaults to NULL

k

Bayes factor threshold. Defaults to 1/10, Jeffreys' threshold for 'strong evidence' against the null hypothesis

sd

Standard deviation of one observation (for type = "two.sample" or type = "one.sample") or of one difference within a pair of observations (type = "paired"). Is assumed to be known. Defaults to 1

null

Mean difference under the point null hypothesis. Defaults to 0

pm

Mean of the normal prior assigned to the mean difference under the alternative in the analysis

psd

Standard deviation of the normal prior assigned to the mean difference under the alternative in the analysis. Set to 0 to obtain a point prior at the prior mean

type

The type of test. One of "two.sample", "one.sample", "paired". Defaults to "two.sample"

dpm

Mean of the normal design prior assigned to the mean difference. Defaults to the same value as the analysis prior pm

dpsd

Standard deviation of the normal design prior assigned to the mean difference. Defaults to the same value as the analysis prior psd

nrange

Sample size search range over which numerical search is performed (only taken into account when n is NULL). Defaults to c(1, 10^5)

Details

This function provides a similar interface as stats::power.t.test. It also assumes that the data are continuous and that the parameter of interest is either a mean or a (standardized) mean difference. For some users, the low-level functions nbf01 (to directly compute the sample size for a fixed power) and pbf01 (to directly compute the power for a fixed sample size) may also be useful because they can be used for other data and parameter types.

Value

Object of class "power.bftest", a list of the arguments (including the computed one) augmented with method and note elements

Note

A warning message will be displayed in case that the specified target power is not achievable under the specified analysis and design priors.

Author(s)

Samuel Pawel

See Also

plot.power.bftest, nbf01, pbf01, bf01

Examples

## determine power
powerbf01(n = 100, pm = 0, psd = 1, dpm = 0.5, dpsd = 0)

## determine sample size
powerbf01(power = 0.99, pm = 0, psd = 1, dpm = 0.5, dpsd = 0)


Power and sample size calculations for binomial Bayes factor

Description

Compute probability that binomial Bayes factor (binbf01) is smaller than a specified threshold (the power), or determine sample size to obtain a target power.

Usage

powerbinbf01(
  n = NULL,
  power = NULL,
  k = 1/10,
  p0 = 0.5,
  type = c("point", "direction"),
  a = 1,
  b = 1,
  dp = NA,
  da = a,
  db = b,
  dl = 0,
  du = 1,
  nrange = c(1, 10^4)
)

Arguments

n

Sample size. Has to be NULL if power is specified. Defaults to NULL

power

Target power. Has to be NULL if n is specified. Defaults to NULL

k

Bayes factor threshold. Defaults to 1/10, Jeffreys' threshold for 'strong evidence' against the null hypothesis

p0

Tested binomial proportion. Defaults to 0.5

type

Type of test. Can be "point" or "directional". Defaults to "point"

a

Number of successes parameter of the beta analysis prior distribution. Defaults to 1

b

Number of failures parameter of the beta analysis prior distribution. Defaults to 1

dp

Fixed binomial proportion assumed for the power calculation. Set to NA to use a truncated beta design prior instead (specified via the da, db, dl, and du arguments). Defaults to NA

da

Number of successes parameter of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to the same value a as specified for the analysis prior

db

Number of failures parameter of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to the same value b as specified for the analysis prior

dl

Lower truncation limit of of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to 0

du

Upper truncation limit of of the truncated beta design prior distribution. Is only taken into account if dp = NA. Defaults to 1

nrange

Sample size search range over which numerical search is performed (only taken into account when n is NULL). Defaults to c(1, 10^3)

Details

This function provides a similar interface as stats::power.prop.test. For some users, the low-level functions nbinbf01 (to directly compute the sample size for a fixed power) and pbinbf01 (to directly compute the power for a fixed sample size) may also be useful.

Value

Object of class "power.bftest", a list of the arguments (including the computed one) augmented with method and note elements

Author(s)

Samuel Pawel

See Also

plot.power.bftest, pbinbf01, nbinbf01, binbf01

Examples

## determine sample size
(nres <- powerbinbf01(power = 0.8, p0 = 0.2, type = "direction", dl = 0.2))
## Not run: 
plot(nres, nlim = c(1, 250), ngrid = 250, type = "s")

## End(Not run)

## determine power
(powres <- powerbinbf01(n = 100, type = "point"))
## Not run: 
plot(powres)

## End(Not run)


Power and sample size calculations for normal moment prior Bayes factor

Description

Compute probability that normal moment prior Bayes factor is smaller than a specified threshold (the power), or determine sample size to obtain a target power.

Usage

powernmbf01(
  n = NULL,
  power = NULL,
  k = 1/10,
  sd = 1,
  null = 0,
  psd,
  type = c("two.sample", "one.sample", "paired"),
  dpm,
  dpsd,
  nrange = c(1, 10^5)
)

Arguments

n

Sample size (per group for two-sample tests). Has to be NULL if power is specified. Defaults to NULL

power

Target power. Has to be NULL if n is specified. Defaults to NULL

k

Bayes factor threshold. Defaults to 1/10, Jeffreys' threshold for 'strong evidence' against the null hypothesis

sd

Standard deviation of one observation (for type = "two.sample" or type = "one.sample") or of one difference within a pair of observations (type = "paired"). Is assumed to be known. Defaults to 1

null

Parameter value under the point null hypothesis. Defaults to 0

psd

Spread of the normal moment prior assigned to the parameter under the alternative in the analysis. The modes of the prior are located at \pm\sqrt{2}\,\code{psd}

type

The type of test. One of "two.sample", "one.sample", "paired". Defaults to "two.sample"

dpm

Mean of the normal design prior assigned to the parameter

dpsd

Standard deviation of the normal design prior assigned to the parameter. Set to 0 to obtain a point prior at the design prior mean

nrange

Sample size search range over which numerical search is performed (only taken into account when n is NULL). Defaults to c(1, 10^5)

Details

This function provides a similar interface as stats::power.t.test. It also assumes that the data are continuous and that the parameter of interest is either a mean or a (standardized) mean difference. For some users, the low-level functions nnmbf01 (to directly compute the sample size for a fixed power) and pnmbf01 (to directly compute the power for a fixed sample size) may also be useful because they can be used for other data and parameter types.

Value

Object of class "power.bftest", a list of the arguments (including the computed one) augmented with method and note elements

Author(s)

Samuel Pawel

See Also

plot.power.bftest, nnmbf01, pnmbf01, nmbf01

Examples

## determine power
powernmbf01(n = 100, psd = 1, dpm = 0.5, dpsd = 0)

## determine sample size
powernmbf01(power = 0.99, psd = 1, dpm = 0.5, dpsd = 0)


Power and sample size calculations for t-test Bayes factor

Description

Compute probability that t-test Bayes factor is smaller than a specified threshold (the power), or determine sample size to obtain a target power.

Usage

powertbf01(
  n = NULL,
  power = NULL,
  k = 1/10,
  null = 0,
  plocation = 0,
  pscale = 1/sqrt(2),
  pdf = 1,
  type = c("two.sample", "one.sample", "paired"),
  alternative = c("two.sided", "less", "greater"),
  dpm = plocation,
  dpsd = pscale,
  nrange = c(2, 10^4)
)

Arguments

n

Sample size (per group)

power

Target power. Has to be NULL if n is specified. Defaults to NULL

k

Bayes factor threshold. Defaults to 1/10, Jeffreys' threshold for 'strong evidence' against the null hypothesis

null

Standardized mean difference under the point null hypothesis. Defaults to 0

plocation

t prior location. Defaults to 0

pscale

t prior scale. Defaults to 1/sqrt(2)

pdf

t prior degrees of freedom. Defaults to 1 (a Cauchy prior)

type

Type of t-test. Can be "two.sample" (default), "one.sample", or "paired"

alternative

Direction of the test. Can be either "two.sided" (default), "less", or "greater". The latter two truncate the analysis prior to negative and positive effects, respectively. If set to "less" or "greater", the power is only computed based on data with effect estimates in the direction of the alternative

dpm

Mean of the normal design prior assigned to the standardized mean difference. Defaults to the analysis prior location

dpsd

Standard deviation of the normal design prior assigned to the standardized mean difference. Set to 0 to obtain a point prior at the design prior mean. Defaults to the analysis prior scale

nrange

Sample size search range over which numerical search is performed (only taken into account when n is NULL). Defaults to c(2, 10^4)

Details

This function provides a similar interface as stats::power.t.test. For some users, the low-level functions ntbf01 (to directly compute the sample size for a fixed power) and ptbf01 (to directly compute the power for a fixed sample size) may also be useful.

Value

Object of class "power.bftest", a list of the arguments (including the computed one) augmented with method and note elements

Author(s)

Samuel Pawel

See Also

plot.power.bftest, ptbf01, ntbf01, tbf01

Examples

## determine power
powertbf01(n = 146, k = 1/6, dpm = 0.5, dps = 0, alternative = "greater")

## determine sample size
powertbf01(power = 0.95, k = 1/6, dpm = 0.5, dps = 0, alternative = "greater")


Print method for class "power.bftest"

Description

Print method for class "power.bftest"

Usage

## S3 method for class 'power.bftest'
print(x, digits = getOption("digits"), ...)

Arguments

x

Object of class "power.bftest"

digits

Number of digits for formatting of numbers

...

Other arguments (for consistency with the generic)

Value

Prints text summary in the console and invisibly returns the "power.bftest" object

Note

Function adapted from stats:::print.power.htest written by Peter Dalgaard

Author(s)

Samuel Pawel

See Also

powerbf01

Examples

powerbf01(power = 0.95, pm = 0, psd = 1, dpm = 0.5, dpsd = 0)
powerbf01(power = 0.95, pm = 0, psd = 1, dpm = 0.5, dpsd = 0, type = "one.sample")
powerbf01(power = 0.95, pm = 0, psd = 1, dpm = 0.5, dpsd = 0, type = "paired")
powerbf01(power = 0.95, pm = 1, psd = 0, dpm = 0.8, dpsd = 0, type = "paired")


Cumulative distribution function of the t-test Bayes factor

Description

This function computes the probability of obtaining a t-test Bayes factor (tbf01) more extreme than a threshold k with a specified sample size.

Usage

ptbf01(
  k,
  n,
  n1 = n,
  n2 = n,
  null = 0,
  plocation = 0,
  pscale = 1/sqrt(2),
  pdf = 1,
  dpm = plocation,
  dpsd = pscale,
  type = c("two.sample", "one.sample", "paired"),
  alternative = c("two.sided", "less", "greater"),
  lower.tail = TRUE,
  drange = "adaptive",
  ...
)

Arguments

k

Bayes factor threshold

n

Sample size (per group)

n1

Sample size in group 1 (only required for two-sample t-test with unequal group sizes)

n2

Sample size in group 2 (only required for two-sample t-test with unequal group sizes)

null

Standardized mean difference under the point null hypothesis. Defaults to 0

plocation

t prior location. Defaults to 0

pscale

t prior scale. Defaults to 1/sqrt(2)

pdf

t prior degrees of freedom. Defaults to 1 (a Cauchy prior)

dpm

Mean of the normal design prior assigned to the standardized mean difference. Defaults to the analysis prior location

dpsd

Standard deviation of the normal design prior assigned to the standardized mean difference. Set to 0 to obtain a point prior at the design prior mean. Defaults to the analysis prior scale

type

Type of t-test. Can be "two.sample" (default), "one.sample", or "paired"

alternative

Direction of the test. Can be either "two.sided" (default), "less", or "greater". The latter two truncate the analysis prior to negative and positive effects, respectively. If set to "less" or "greater", the power is only computed based on data with effect estimates in the direction of the alternative

lower.tail

Logical indicating whether Pr(\mathrm{BF}_{01} \leq k) (TRUE) or Pr(\mathrm{BF}_{01} > k) (FALSE) should be computed. Defaults to TRUE

drange

Standardized mean difference search range over which the critical values are searched for. Can be either set to a numerical range or to "adaptive" (default) which determines the range in an adaptive way from the other input parameters

...

Other arguments passed to stats::uniroot

Value

The probability that the Bayes factor is less or greater (depending on the specified lower.tail) than the specified threshold k

Author(s)

Samuel Pawel

See Also

tbf01, ntbf01, powertbf01

Examples

## example from Schönbrodt and Wagenmakers (2018, p. 135)
ptbf01(k = 1/6, n = 146, dpm = 0.5, dpsd = 0, alternative = "greater")
ptbf01(k = 6, n = 146, dpm = 0, dpsd = 0, alternative = "greater",
       lower.tail = FALSE)

## two-sided
ptbf01(k = 1/6, n = 146, dpm = 0.5, dpsd = 0)
ptbf01(k = 6, n = 146, dpm = 0, dpsd = 0, lower.tail = FALSE)

## one-sample test
ptbf01(k = 1/6, n = 146, dpm = 0.5, dpsd = 0, alternative = "greater", type = "one.sample")


t-test Bayes factor

Description

This function computes the Bayes factor that forms the basis of the informed Bayesian t-test from Gronau et al. (2020). The Bayes factor quantifies the evidence that the data provide for the null hypothesis that the standardized mean difference (SMD) is zero against the alternative that the SMD is non-zero. A location-scale t-distribution is assumed for the SMD under the alternative hypothesis. The Jeffreys-Zellner-Siow (JZS) Bayes factor (Rouder et al., 2009) is obtained as a special case by setting the location of the prior to zero and the prior degrees of freedom to one, which is the default.

The data are summarized by t-statistics and sample sizes. The following types of t-statistics are accepted:

Usage

tbf01(
  t,
  n,
  n1 = n,
  n2 = n,
  plocation = 0,
  pscale = 1/sqrt(2),
  pdf = 1,
  type = c("two.sample", "one.sample", "paired"),
  alternative = c("two.sided", "less", "greater"),
  log = FALSE,
  ...
)

Arguments

t

t-statistic

n

Sample size (per group)

n1

Sample size in group 1 (only required for two-sample t-test with unequal group sizes)

n2

Sample size in group 2 (only required for two-sample t-test with unequal group sizes)

plocation

t prior location. Defaults to 0

pscale

t prior scale. Defaults to 1/sqrt(2)

pdf

t prior degrees of freedom. Defaults to 1 (a Cauchy prior)

type

Type of t-test. Can be "two.sample" (default), "one.sample", or "paired"

alternative

Direction of the test. Can be either "two.sided" (default), "less", or "greater". The latter two truncate the analysis prior to negative and positive effects, respectively.

log

Logical indicating whether the natural logarithm of the Bayes factor should be returned. Defaults to FALSE

...

Additional arguments passed to stats::integrate

Details

The Bayes factor is implemented as in equation (5) in Gronau et al. (2020), and using suitable truncation in case of one-sided alternatives. Integration is performed numerically with stats::integrate.

Value

Bayes factor in favor of the null hypothesis over the alternative (\text{BF}_{01} > 1 indicates evidence for the null hypothesis, whereas \text{BF}_{01} < 1 indicates evidence for the alternative)

Author(s)

Samuel Pawel

References

Rouder, J. N., Speckman, P. L., Sun, D., Morey, R. D., Iverson, G. (2009). Bayesian t tests for accepting and rejecting the null hypothesis. Psychonomic Bulletin & Review, 16(2):225-237. doi:10.3758/PBR.16.2.225

Gronau, Q. F., Ly., A., Wagenmakers, E.J. (2020). Informed Bayesian t-Tests. The American Statistician, 74(2):137-143. doi:10.1080/00031305.2018.1562983

See Also

powertbf01, ptbf01, ntbf01

Examples

## analyses from Rouder et al. (2009):
## values from Table 1
tbf01(t = c(0.69, 3.20), n = 100, pscale = 1, type = "one.sample")
## examples from p. 232
tbf01(t = c(2.24, 2.03), n = 80, pscale = 1, type = "one.sample")

## analyses from Gronau et al. (2020) section 3.2:
## informed prior
tbf01(t = -0.90, n1 = 53, n2 = 57, plocation = 0.350, pscale = 0.102, pdf = 3,
      alternative = "greater", type = "two.sample")
## default (one-sided) prior
tbf01(t = -0.90, n1 = 53, n2 = 57, plocation = 0, pscale = 1/sqrt(2), pdf = 1,
      alternative = "greater", type = "two.sample")