| 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 |
aafbf
calc_aafbf(type, estimates, sigma, b, n_eff)calc_aafbf(type, estimates, sigma, b, n_eff)
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. |
calc_aafbf(type="Inequalities", estimates=.2, sigma=list(.1), b=1, n_eff=100)calc_aafbf(type="Inequalities", estimates=.2, sigma=list(.1), b=1, n_eff=100)
Check if hypothesis is true according to 'eff.sizes'
check_hyp(hypothesis, eff.sizes)check_hyp(hypothesis, eff.sizes)
hypothesis |
The research hypothesis of interest, which must be true |
eff.sizes |
The vector of true effect sizes |
TRUE if the hypothesis is true or FALSE if it is not true
check_hyp("a<b<c", c(0, .5, .8)) # returns TRUEcheck_hyp("a<b<c", c(0, .5, .8)) # returns TRUE
data generation
gen_CRT_data( ndatasets = ndatasets, n1 = n1, n2 = n2, var_u0 = var_u0, var_e = var_e, mean_interv, batch_size )gen_CRT_data( ndatasets = ndatasets, n1 = n1, n2 = n2, var_u0 = var_u0, var_e = var_e, mean_interv, batch_size )
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 |
gen_CRT_data(ndatasets=10, n1=30, n2=10, var_u0=.01, var_e=.1, mean_interv=.5, batch_size=5)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
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 )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 )
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? |
Returns the Bayes Factor or Posterior Model Probabilities for the hypothesis.
get_bf(N=100, attrition="weibull", params=list(.8,1), hypothesis=list("a<b<c","a=b=c"), t.points=c(0,1,2))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
get_neff(model, surviv)get_neff(model, surviv)
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 |
Returns the effective sample size for each treatment condition's effects
# 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)))# 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
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 )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 )
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 |
Returns the power when using a Bayes Factor and the power when using Posterior Model Probabilities.
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)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)
print results
print_results(object_result)print_results(object_result)
object_result |
object containing the result |
Print results for CRT
print results
print_results_SSD_longit(result)print_results_SSD_longit(result)
result |
object containing the result of SSD_longit |
Print results for the function SSD_longit
SSD for CRT
SSD_crt_inform( eff_size, n1 = 15, n2 = 30, ndatasets = 1000, rho, BF_thresh, eta = 0.8, fixed = "n2", max = 1000, batch_size = 100 )SSD_crt_inform( eff_size, n1 = 15, n2 = 30, ndatasets = 1000, rho, BF_thresh, eta = 0.8, fixed = "n2", max = 1000, batch_size = 100 )
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. |
Returns the result of the SSD procedure for CRT
SSD_crt_inform(eff_size, n1 = 15, n2 = 30, ndatasets = 1000, rho, BF_thresh, eta = 0.8, fixed = 'n2', max = 1000, batch_size = 100)SSD_crt_inform(eff_size, n1 = 15, n2 = 30, ndatasets = 1000, rho, BF_thresh, eta = 0.8, fixed = 'n2', max = 1000, batch_size = 100)
SSD for CRT
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 )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 )
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. |
Returns the result of the SSD procedure for CRT
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)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
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 )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 )
eta |
The desired power level. |
hypothesis |
List of the hypotheses to be evaluated. Treatment groups are coded as |
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 |
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 |
attrition |
The attrition pattern ( |
params |
The parameters passed to the survival function specified in |
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 |
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. |
Returns the sample size (number of subjects) necessary to achieve the desired power level eta.
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)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
survival(distributions, params, t.points)survival(distributions, params, t.points)
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. |
Returns the proportion of individuals still remaining in the study at each timepoint.
survival(distributions="weibull", params=list(c(.5, 1)), t.points=c(0,1,2,3,4))survival(distributions="weibull", params=list(c(.5, 1)), t.points=c(0,1,2,3,4))