masize: Sample Size Calculation for Mediation Analysis

View source: R/masize.R

masizeR Documentation

Sample Size Calculation for Mediation Analysis

Description

Computes sample size requirements for regression models in which the effect of a primary predictor (X_1) is evaluated in the presence of a mediator (X_2). The calculation is based on the expected information matrix of the full model and a Wald test under the alternative hypothesis.

Usage

masize(model, opts, alpha = 0.025, gamma = 0.2)

Arguments

model

A character string specifying the model type:

  • Linear models: "linear1", "linear2", "linear3", "linear4"

  • Logistic models: "logistic1"–"logistic5"

  • Poisson models: "poisson1"–"poisson9"

  • Cox models: "cox1"–"cox9"

opts

Named list of model-specific parameters, which may include:

  • b1 regression coefficient for X1.

  • b1star coefficient for X1 in the reduced model.

  • b2 regression coefficient for X2.

  • PTE proportion of total effect explained.

  • rho correlation between X1 and X2.

  • sdx1, sdx2 standard deviations of X1 and X2.

  • f1, f2 prevalences for binary X1 and X2.

  • sdy residual SD (linear models).

  • p marginal outcome prevalence (logistic models).

  • m marginal outcome mean (Poisson models).

  • f proportion of uncensored observations (Cox models).

  • fc early censoring proportion (Cox extensions).

  • ns simulation size for simulation-based methods.

  • seed random seed.

alpha

One-sided type I error rate.

gamma

Type-II error rate.

Details

The framework applies to linear, logistic, Poisson, and Cox regression models and supports four predictor–mediator configurations:

  • CpCm: continuous X1 and continuous X2.

  • BpCm: binary X1 and continuous X2.

  • CpBm: continuous X1 and binary X2.

  • BpBm: binary X1 and binary X2.

The naming convention is:

  • "cc" continuous-continuous.

  • "bc" binary-continuous.

  • "cb" continuous-binary.

  • "bb" binary-binary.

  • ".approx" use analytic approximations.

  • ".s" use simulation.

The same formulation can also be used to evaluate the effect of a predictor in the presence of a confounder by swapping the roles of X1 and X2.

Throughout, X1 denotes the primary predictor and X2 denotes the mediator. Coefficients b1 and b2 correspond to X1 and X2 respectively, and f1 and f2 denote prevalences when variables are binary. rho denotes the correlation between X1 and X2.

The function is also applicable to the analogous problem of detecting the effect of a primary predictor in the presence of a confounder by treating X2 as the primary predictor and X1 as the confounder.

Let b1* (b1star) denote the coefficient of X1 in the reduced model excluding X2, and b1 the coefficient in the full model. Mediation can be expressed as attenuation b1* - b1 or proportion of total effect explained (PTE):

PTE = \frac{b_1^*-b_1}{b_1^*}

Testing mediation is equivalent (linear models) or approximately equivalent (GLMs and Cox models) to testing the coefficient of X2 in the full model. Sample size is therefore derived from a Wald test on b2 using the expected information matrix.

For binary variables, use \sqrt{f(1-f)} for the corresponding standard deviation.

Linear models parameters are based on \insertCitehsieh98;textualgap and use a variance inflation factor 1/(1-\rho^2), where \rho is the correlation between X1 and X2. Logistic and Cox models generally require simulation to estimate information, while Poisson models admit analytic solutions in most cases.

The quantity PTE and b1star parameter for linear model are provided as follows,

 getPTE <- function(b1, b2, rho, sdx1=1, sdx2=1)
                    b2*sdx2*rho/((b1+b2*sdx2*rho/sdx1)*sdx1)
 getb1star <- function(b1, b2, rho, sdx1=1, sdx2=1) b1+b2*sdx2*rho/sdx1
 

For logistic model, the approximate solution due to Hsieh is implemented in the function logistic.approx, and can be used for all four cases. Arguments are p, b2, rho, sdx2, alpha, and gamma. Simulating the information matrix of the logistic model provides somewhat more accurate sample size estimates than the Hsieh approximation. The functions for cases CpCm, BpCm, CpBm, and BpBm are respectively logistic.ccs, logistic.bcs, logistic.cbs, and logistic.bbs, as for the Poisson and Cox models. Arguments for these functions include p, b1, sdx1 or f1, b2, sdx2 or f2, rho, alpha, gamma, and ns. As in other functions, sdx1, sdx2, alpha, and gamma are set to the defaults listed above. These four functions call the utility functions and getb0 (to calculate the intercept parameter from the others), which are supplied.

getb0 <- function(p, X, b1, b2)
{
  stopifnot(is.numeric(p), length(p) == 1, p > 0, p < 1)
  eta0 <- X[,2] * b1 + X[,3] * b2
  f <- function(b0) mean(plogis(b0+eta0)) - p
  uniroot(f, interval = c(-50,50), tol = 1e-10)$root
}

For Poisson model, The function implementing the approximate solution based on the variance inflation factor is poisson.approx, and can be used for all four cases. Arguments are EY (the marginal mean of the Poisson outcome), b2, sdx2, rho, alpha and gamma, with sdx2, alpha and gamma set to the usual defaults; For cases CpCm and BpCm (continuous mediators), the approximate formula is also the analytic solution. For these cases, we supply redundant functions poisson.cc and poisson.bc, with the same arguments and defaults as for poisson.approx (it's the same function). For the two cases with binary mediators, the functions are poisson.cb and poisson.bb. In addition to m, b2, f2, rho, alpha, and gamma, b1 and sdx1 or f1 must be specified. Defaults are as usual. Functions using simulation for the Poisson model are available: poisson.ccs, poisson.bcs, poisson.cbs, and poisson.bbs. As in the logistic case, these require arguments b1 and sdx1 or f1. For this case, however, the analytic functions are faster, avoid simulation error, and should be used. We include these functions as templates that could be adapted to other joint predictor distributions.

For Cox model, the function implementing the approximate solution, using the variance inflation factor and derived by \insertCiteschmoor00;textualgap is cox.approx, and can be used for all four cases. Arguments are b2, sdx2, rho, alpha, gamma, and f. The approximation works very well for cases CpCm and BpCm (continuous mediators), but is a bit less accurate for cases CpBm and BpBm (binary mediators). We get some improvement for those cases using the simulation approach. This approach is implemented for all four, as functions cox.ccs, cox.bcs, cox.cbs, and cox.bbs. Arguments are b1, sdx1 or f1, b2, sdx2 or f2, rho, alpha, gamma, f, and ns, with defaults as described above. Slight variants of these functions, cox.ccs2, cox.bcs2, cox.cbs2, and cox.bbs2, make it possible to allow for early censoring of a fraction fc of observations; but in our experience this has virtually no effect, even with values of fc of 0.5. The default for fc is 0.

A summary of the arguments is as follows, noting that additional parameter seed can be supplied for simulation-based method.

Model Arguments Description
Linear models:
linear1 b2, rho, sdx2, sdy linear
linear2 b1star, PTE, rho, sdx1, sdy lineara
linear3 b1star, b2, PTE, sdx1, sdx2, sdy linearb
linear4 b1star, b1, b2, sdx1, sdx2, sdy linearc
Logistic models:
logistic1 p, b2, rho, sdx2 logistic.approx
logistic2 p, b1, b2, rho, sdx1, sdx2, ns logistic.ccs
logistic3 p, b1, f1, b2, rho, sdx2, ns logistic.bcs
logistic4 p, b1, b2, f2, rho, sdx1, ns logistic.cbs
logistic5 p, b1, f1, b2, f2, rho, ns logistic.bbs
Poission models:
poisson1 m, b2, rho, sdx2 poisson.approx
poisson2 m, b2, rho, sdx2 poisson.cc
poisson3 m, b2, rho, sdx2 poisson.bc
poisson4 m, b1, b2, f2, rho, sdx1 poisson.cb
poisson5 m, b1, f1, b2, f2, rho poisson.bb
poisson6 m, b1, b2, rho, sdx1, sdx2, ns poisson.ccs
poisson7 m, b1, f1, b2, rho, sdx2, ns poisson.bcs
poisson8 m, b1, b2, f2, rho, sdx1, ns poisson.cbs
poisson9 m, b1, f1, b2, f2, rho, ns poisson.bbs
Cox models:
cox1 b2, rho, f, sdx2 cox.approx
cox2 b1, b2, rho, f, sdx1, sdx2, ns cox.ccs
cox3 b1, f1, b2, rho, f, sdx2, ns cox.bcs
cox4 b1, b2, f2, rho, f, sdx1, ns cox.cbs
cox5 b1, f1, b2, f2, rho, f, ns cox.bbs
cox6 b1, b2, rho, f, fc, sdx1, sdx2, ns cox.ccs2
cox7 b1, f1, b2, rho, f, fc, sdx2, ns cox.bcs2
cox8 b1, b2, f2, rho, f, fc, sdx1, ns cox.cbs2
cox9 b1, f1, b2, f2, rho, f, fc, ns cox.bbs2

Value

A list containing:

  • desc Description of the selected method.

  • n Required sample size.

  • d Required number of events (Cox models only).

References

\insertRef

hsieh98gap

\insertRef

schmoor00gap

\insertRef

vittinghoff09gap

See Also

ab

Examples

## Not run: 
## linear model
# CpCm
opts <- list(b2=0.5, rho=0.3, sdx2=1, sdy=1)
masize("linear1",opts)
# BpBm
opts <- list(b2=0.75, rho=0.3, f2=0.25, sdx2=sqrt(0.25*0.75), sdy=3)
masize("linear1",opts,gamma=0.1)

## logistic model
# CpBm
opts <- list(p=0.25, b2=log(0.5), rho=0.5, sdx2=0.5)
masize("logistic1",opts)
opts <- list(p=0.25, b1=log(1.5), sdx1=1, b2=log(0.5), f2=0.5, rho=0.5, ns=10000,
             seed=1234)
masize("logistic4",opts)
opts <- list(p=0.25, b1=log(1.5), sdx1=1, b2=log(0.5), f2=0.5, rho=0.5, ns=10000,
             seed=1234)
opts <- list(p=0.25, b1=log(1.5), sdx1=4.5, b2=log(0.5), f2=0.5, rho=0.5, ns=50000,
             seed=1234)
masize("logistic4",opts)

## Poisson model
# BpBm
opts <- list(m=0.5, b2=log(1.25), rho=0.3, sdx2=sqrt(0.25*0.75))
masize("poisson1",opts)
opts <- list(m=0.5, b1=log(1.4), f1=0.25, b2=log(1.25), f2=0.25, rho=0.3)
masize("poisson5",opts)
opts <- c(opts,ns=10000, seed=1234)
masize("poisson9",opts)

## Cox model
# BpBm
opts <- list(b2=log(1.5), rho=0.45, f=0.2, sdx2=sqrt(0.25*0.75))
masize("cox1",opts)
opts <- list(b1=log(2), f1=0.5, b2=log(1.5), f2=0.25, rho=0.45, f=0.2, seed=1234)
masize("cox5",c(opts, ns=10000))
masize("cox5",c(opts, ns=50000))

## End(Not run)


gap documentation built on Oct. 27, 2026, 1:07 a.m.