• Research
  • Publications
  • Talks
  • Books
    • BMHE
    • BCEA book
    • BMPR
  • Software
    • BCEA
    • survHE
    • blendR
    • bmhe
    • SWSamp
    • bmeta
    • INLA
  • Blog
  • Teaching
    • Summer school
  1. SWSamp
  • BCEA
    • CEAC plots
    • Set bcea()
  • survHE
    • Tutorial
  • blendR
  • SWSamp
    • Tutorial
  • bmeta
  • BCEAweb
  • INLA

SWSamp: Simulation-based sample size calculations for a Stepped Wedge Trial (and more)

Bayesian statistics
Miscellanea
Author

Gianluca Baio

Published

March 11, 2018

Code Go to project site


Introduction

SWSamp is an R package designed to allow a wide range of simulation-based sample size calculations, specifically (but not exclusively!) for a Stepped Wedge Trial (SWT) and is based on the general framework described in Baio et al (2015). In its current version, SWSamp consists of 5 main functions: the first one (which is currently in fact specified by three different commands) performs the analytic sample size calculations using the method of Hussey and Hughes (2007). This can be used as a quick alternative for more standard designs or as a first-order approximation in the case of complicated designs (eg including multiple layers of correlation). The second function replicates the sample size calculations based on the (correct form of) the “design effect” specified by Woertman et al (2013). This too can be used very effectively in relatively standard cases, but is less efficient in cases of more complex designs. The core functions of SWSamp are make.swt, which can be used to simulate data as obtained by a reasonably wide range of possible SWTs and sim.power, which actually performs the simulation-based computation of the required sample size. More importantly, sim.power can be used to compute the sample size for other data generating processes (DGPs) or designs (ie not specifically for a SWT), which increases the applicability of SWSamp.

In order to speed up the computation process (which can be intensive, under particularly complex designs), SWSamp is designed to take full advantage of R multi-core processing. For this reason, SWSamp “imports” the NAMESPACE of the R packages foreach, doParallel, iterators and parallel (technically, this means that these packages need to be installed on the user’s local machine, for SWSamp to work). In addition to these, SWSamp formally “depends” on the R package lme4, which can be used to fit linear and generalized linear mixed-effects models. This is relevant when analysing a SWT (which invariably includes some form of clustering in the DGP). Other modelling strategies could be used instead, for example Generalised Estimating Equations (GEE, Zeger and Liang 1986) or a full Bayesian model, eg based on Integrated Nested Laplace Approximation (INLA, Rue et al, 2009). In the current release (version 0.3), the Bayesian approach based on INLA is implemented and can be used by setting the optional parameter method="inla".

Installation

SWSamp can be installed using r-universe by typing the following code in your R terminal.

install.packages(
   "SWSamp", 
   repos = c(
      "https://giabaio.r-universe.dev", 
      "https://cloud.r-project.org",
      "https://inla.r-inla-download.org/R/stable"
   )
)

A full documentation guide available here, including a description of the methods specifically used for a SWT and some for more general data-generating processes.

Last updated: 14 April 2025

Relevant publications

Baio, G, A Copas, G Ambler, J Hargreaves, E Beard, and RZ Omar. 2015. “Sample Size Calculation for A Stepped Wedge Trial.” Trials 16 (August): 354. https://doi.org/10.1186/s13063-015-0840-9.
Beard, E, J Lewis, A Copas, C Davey, D Osrin, G Baio, J Thompson, et al. 2015. “Stepped Wedge Randomised Controlled Trials: Systematic Review of Studies Published Between 2010 and 2014.” Trials 16 (August): 353. https://doi.org/10.1186/s13063-015-0839-2.
Copas, A, J Lewis, J Thompson, C Davey, G Baio, and J Hargreaves. 2015. “Designing A Stepped Wedge Trial: Three Main Designs, Carry-Over Effects and Randomisation Approaches.” Trials 16 (August): 352. https://doi.org/10.1186/s13063-015-0842-7.
Hargreaves, J, A Copas, E Beard, D Osrin, J Lewis, C Davey, J Thompson, G Baio, K Fielding, and A Prost. 2015. “Five Questions to Consider Before Conducting A Stepped Wedge Trial.” Trials 16 (August): 350. https://doi.org/10.1186/s13063-015-0841-8.
blendR
Tutorial

© Gianluca Baio 2022-2024