Package 'BayesSSD'

Title: Bayesian sample size determination for multilevel models
Description: This package performs simulation-based Bayesian sample size determination (SSD) for multilevel models. For longitudinal SSD, use the function `SSD_longit`. In the longitudinal case, anticipated participant attrition can be taken into account.
Authors: Ulrich Lösener [aut, cre]
Maintainer: Ulrich Lösener <[email protected]>
License: MIT + file LICENSE
Version: 0.2.0
Built: 2026-11-26 14:01:28 UTC
Source: https://github.com/ulrichlosener/BayesSSD

Help Index


CALCULATE AAFBF

Description

aafbf

Usage

calc_aafbf(type, estimates, sigma, b, n_eff)

Arguments

type

String that indicates the type of comparison of hypotheses. "equality"

estimates

R object with estimates.

sigma

list with covariances.

b

Numerical. Fraction of information to specify the prior.

n_eff

Effective sample size.

n

numeric. Effective sample size.

Examples

calc_aafbf(type="Inequalities", estimates=.2, sigma=list(.1), b=1, n_eff=100)

Check if hypothesis is true according to 'eff.sizes'

Description

Check if hypothesis is true according to 'eff.sizes'

Usage

check_hyp(hypothesis, eff.sizes)

Arguments

hypothesis

The research hypothesis of interest, which must be true

eff.sizes

The vector of true effect sizes

Value

TRUE if the hypothesis is true or FALSE if it is not true

Examples

check_hyp("a<b<c", c(0, .5, .8)) # returns TRUE

Generation of data sets for two treatment-condition cluster randomized trial

Description

data generation

Usage

gen_CRT_data(
  ndatasets = ndatasets,
  n1 = n1,
  n2 = n2,
  var_u0 = var_u0,
  var_e = var_e,
  mean_interv,
  batch_size
)

Arguments

ndatasets

Numeric. Number of data sets that the user wants to generate to determine the sample size

n1

Numeric. Cluster size

n2

Numeric. Total number of clusters.

var_u0

Numeric. Between-cluster variance. Variance at the cluster level.

var_e

Numeric. Within-cluster variance.

mean_interv

Numeric. Equivalent to the effect size.

batch_size

This parameter determines the size of batches used during the fitting of the multilevel model.

rho

Numeric. Intraclass correlation

Examples

gen_CRT_data(ndatasets=10, n1=30, n2=10, var_u0=.01, var_e=.1, mean_interv=.5, batch_size=5)

Calculate the Bayes Factor and posterior model probabilities

Description

Calculate the Bayes Factor and posterior model probabilities

Usage

get_bf(
  N = 100,
  hypothesis = "a<b<c",
  eff.sizes = c(0, 0.5, 0.8),
  t.points = c(0, 1, 2, 3, 4),
  attrition = "weibull",
  params = c(0.8, 1),
  var.u0 = 0.033,
  var.u1 = 0.002,
  var.e = 0.026,
  cov = 0,
  fraction = 1,
  log.grow = F
)

Arguments

N

The total number of subjects or a vector with group sizes for each condition

hypothesis

The hypothesis to be evaluated. Treatment groups are coded as "a", "b", "c", etc.

eff.sizes

The effect sizes defined as the differences between the regression coefficients of interaction between time and condition.

t.points

The points in time of measurement. Can be non-equidistant.

attrition

The attrition pattern (FALSE for no attrition, otherwise "weibull", "modified_weibull", "linear_exponential", "log_logistic", "gompertz" or "non-parametric")

params

The parameters passed to the survival function specified in "attrition". First parameter is omega, second is gamma.

var.u0

The intercept variance.

var.u1

The slope variance.

var.e

The residual variance.

cov

The covariance between intercept variance and slope variance.

fraction

The fraction of information used to construct the prior for the Bayes Factor.

log.grow

Use log-linear growth?

Value

Returns the Bayes Factor or Posterior Model Probabilities for the hypothesis.

Examples

get_bf(N=100, attrition="weibull", params=list(.8,1),
hypothesis=list("a<b<c","a=b=c"), t.points=c(0,1,2))

Calculate the effective sample size in a multilevel model with attrition for multiple treatment groups

Description

Calculate the effective sample size in a multilevel model with attrition for multiple treatment groups

Usage

get_neff(model, surviv)

Arguments

model

A multilevel model of class 'mermod' (specifically 'lmerMod') with ≥2 treatment conditions

surviv

Vector (or list of vectors) containing the proportion of subjects remaining at each timepoint

Value

Returns the effective sample size for each treatment condition's effects

Examples

# Example with 3 treatment conditions
library(lme4)
library(Matrix)
library(MASS)
get_neff(model = lmermodel, N=100, t.points=c(0,1,2,3,4),
        surviv=list(c(1,.9,.8,.6,.5), c(1,.85,.7,.5,.3),
        c(1,.95,.9,.8,.7)))

Bayesian power calculation with attrition for three treatment conditions

Description

Bayesian power calculation with attrition for three treatment conditions

Usage

get_power(
  N = 100,
  hypothesis = "a<b<c",
  eff.sizes = c(0, 0.5, 0.8),
  t.points = c(0, 1, 2, 3, 4),
  m = 1000,
  BFthres = 5,
  PMPthres = 0.9,
  attrition = "weibull",
  params = c(0.5, 1),
  var.u0 = 0.033,
  var.u1 = 0.1,
  var.e = 0.026,
  cov = 0,
  fraction = 1,
  log.grow = F,
  seed = NULL
)

Arguments

N

The total number of subjects or a vector with group sizes for each condition

hypothesis

The hypothesis to be evaluated. Treatment groups are coded as "a", "b", "c", etc.

eff.sizes

The effect sizes defined as the differences between the regression coefficients of interaction between time and condition.

t.points

The points in time of measurement. Can be non-equidistant.

m

The number of datasets simulated. The higher m, the more accurate the power level but the higher the computation cost

BFthres

The Threshold a Bayes Factor needs to exceed in order to be considered convincing evidence.

PMPthres

The Threshold a Posterior Model Probability needs to exceed in order to be considered convincing evidence.

attrition

The attrition pattern (FALSE for no attrition, otherwise "weibull", "modified_weibull", "linear_exponential", "log_logistic", "gompertz" or "non-parametric")

params

The parameters passed to the survival function specified in "attrition". First parameter is omega, second is gamma.

var.u0

The intercept variance.

var.u1

The slope variance.

var.e

The residual variance.

cov

The covariance between intercept variance and slope variance.

fraction

The fraction of information used to construct the prior for the Bayes Factor.

log.grow

Use log-linear growth?

seed

Set a seed for reproducibility

Value

Returns the power when using a Bayes Factor and the power when using Posterior Model Probabilities.

Examples

get_power(attrition="weibull", params=c(.5,1),
m=1000, N=100, t.points=c(0,1,2,3,4), var.u0=0.03,
var.u1=.1, var.e=.02, cov=0, eff.sizes=c(0, .5, .5),
BFthres=5, fraction=1, log.grow=F, seed=NULL,
hypothesis="a<b<c", PMPthres=.9)

Sample Size Determination for Cluster Randomized Trials

Description

SSD for CRT

Usage

SSD_crt_inform(
  eff_size,
  n1 = 15,
  n2 = 30,
  ndatasets = 1000,
  rho,
  BF_thresh,
  eta = 0.8,
  fixed = "n2",
  max = 1000,
  batch_size = 100
)

Arguments

n1

Numeric. Cluster size

n2

Numeric. Total number of clusters.

rho

Numeric. Intraclass correlation

eta

Numeric. Probability of exceeding Bayes Factor threshold.

fixed

Character. Indicating which sample is fixed (n1 or n2)

max

Maximum sample size.

batch_size

This parameter determines the size of batches used during the fitting of the multilevel model.

eff.size

Numeric. Effect size

n.datasets

Numeric. Number of data sets that the user wants to generate to determine the sample size.

BF.thresh

Numeric. Value of the Bayes factor that is going to be the threshold.

Value

Returns the result of the SSD procedure for CRT

Examples

SSD_crt_inform(eff_size, n1 = 15, n2 = 30,
ndatasets = 1000, rho, BF_thresh, eta = 0.8,
fixed = 'n2', max = 1000, batch_size = 100)

Sample Size Determination for Cluster Randomized Trials

Description

SSD for CRT

Usage

SSD_crt_null(
  eff_size,
  n1 = 15,
  n2 = 30,
  ndatasets = 1000,
  rho,
  BF_thresh1,
  BF_thresh0,
  eta1 = 0.8,
  eta0 = 0.8,
  fixed = "n2",
  b_fract = 3,
  max = 1000,
  batch_size = 100
)

Arguments

eff_size

Numeric. Effect size

n1

Numeric. Cluster size

n2

Numeric. Total number of clusters

ndatasets

Numeric. Number of data sets that the user wants to generate to determine the sample size.

rho

Numeric. Intraclass correlation

fixed

Character. Indicating which sample is fixed (n1 or n2)

b_fract

Numeric. Fraction of information used to specify the prior distribution.

max

Maximum sample size.

batch_size

This parameter determines the size of batches used during the fitting of the multilevel model.

BF_thresh

Numeric. Value of the Bayes factor that is going to be the threshold.

eta

Numeric. Probability of exceeding Bayes Factor threshold.

Value

Returns the result of the SSD procedure for CRT

Examples

SSD_crt_null(eff_size, n1 = 15, n2 = 30,
ndatasets = 1000, rho, BF_thresh1, BF_thresh0,
eta1 = 0.8, eta0 = 0.8, fixed = "n2", b_fract = 3,
max = 1000, batch_size = 100)

Perform simulation-based Bayesian sample size determination

Description

Perform simulation-based Bayesian sample size determination

Usage

SSD_longit(
  eta = 0.8,
  hypothesis = "a<b<c",
  eff.sizes = c(0, 0.5, 0.8),
  t.points = c(0, 1, 2, 3, 4),
  m = 100,
  BFthres = 5,
  PMPthres = 0.9,
  method = "bfc",
  attrition = "weibull",
  params = c(0.5, 1),
  var.u0 = 0.033,
  var.u1 = 0.1,
  var.e = 0.026,
  cov = 0,
  log.grow = F,
  seed = NULL,
  sensitivity = F,
  tol = 0.01,
  N.max = 1000,
  N.min = 30,
  group.sizes = NULL
)

Arguments

eta

The desired power level.

hypothesis

List of the hypotheses to be evaluated. Treatment groups are coded as a, b, c, etc.

eff.sizes

The effect sizes defined as the differences between the regression coefficients of interaction between time and condition.

t.points

The points in time of measurement. Can be non-equidistant.

m

The number of datasets simulated in each iteration. The higher m, the more accurate the power level but the higher the computation time

BFthres

The Threshold a Bayes Factor needs to exceed in order to be considered convincing evidence.

PMPthres

The Threshold a Posterior Model Probability needs to exceed in order to be considered convincing evidence.

method

The method used for hypothesis evaluation. If bfc/BFc, then the hypothesis is compared against its complement via the Bayes Factor. If bf/BF, then the first hypothesis is compared to the second one via the Bayes Factor. If pmp/PMP, then the first hypothesis is compared to the whole set of hypotheses including the complement via posterior model probabilities.

attrition

The attrition pattern (FALSE for no attrition, otherwise weibull, modified_weibull, linear_exponential, log_logistic, gompertz or non-parametric)

params

The parameters passed to the survival function specified in attrition. First parameter is omega, second gamma and third (if applicable) is kappa.

var.u0

The intercept variance.

var.u1

The slope variance.

var.e

The residual variance.

cov

The covariance between intercept variance and slope variance.

log.grow

Logical. Use log-linear growth?

seed

Set a seed for reproducibility?

sensitivity

Logical. Conduct a sensitivity analysis for the b fraction?

tol

Tolerance for the deviation of the final result from eta. Higher values may speed up performance.

N.max

The maximum sample size to be considered. Lower values may speed up performance.

N.min

The minimum sample size to be considered. Higher values may speed up performance.

group.sizes

If group sizes are unequal, vector of proportions of group sizes for each condition. NULL if balanced design is to be used.

Value

Returns the sample size (number of subjects) necessary to achieve the desired power level eta.

Examples

SSD_longit(eta=.8, attrition="weibull", params=c(.5,1),
m=100, t.points=c(0,1,2,3,4), var.u0=0.01,
var.u1=.1, var.e=.01, cov=0, eff.sizes=c(0, .5, .8),
BFthres=5, log.grow=F, seed=NULL,
hypothesis="a<b<c", PMPthres=.9, sensitivity=F, tol=.001,
N.max=1000)

Calculate survival curves to model attrition

Description

Calculate survival curves to model attrition

Usage

survival(distributions, params, t.points)

Arguments

distributions

Can be either a vector of length 1 or of length g, where g is the number of treatment conditions. Specifies the distribution(s) used to model attrition.

params

List with either 1 element or g elements. Each element contains the parameters for the respective survival function.

t.points

Vector containing the measurement occasions.

Value

Returns the proportion of individuals still remaining in the study at each timepoint.

Examples

survival(distributions="weibull", params=list(c(.5, 1)), t.points=c(0,1,2,3,4))