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


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".


There are two ways of installing SWSamp. A “stable” version is packaged and binary files are available for Windows and as source. To install the stable version on a Windows machine, run the following commands


Note that you need to specify a vector of repositories - the first one hosts SWSamp, while the second one should be an official CRAN mirror. You can select whichever one you like, but a CRAN mirror must be provided, so that install.packages() can also install the “dependencies” (e.g. other packages that are required for SWSamp to work). The third one is used to install the package INLA, which can be used to perform simulation-based sample size calculations using a Bayesian approach. This process can be quite lengthy, if you miss many of the relevant packages.

To install from source (e.g. on a Linux machine), run


The second way involves using the “development” version of SWSamp - this will usually be updated more frequently and may be continuously tested. On Windows machines, you need to install a few dependencies, including Rtools first, e.g. by running

pkgs <- c("foreach", "doParallel", "iterators", "parallel", "Matrix","lme4",
repos <- c("https://cran.rstudio.com", "https://www.math.ntnu.no/inla/R/stable") 
install.packages(pkgs,repos=repos,dependencies = "Depends")

before installing the package using devtools:


Under Linux or MacOS, it is sufficient to install the package via devtools:


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.

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.