power_diff: Power simulation function for a two-group comparison

View source: R/delay_test.R

power_diffR Documentation

Power simulation function for a two-group comparison

Description

Simulate power for a test of difference between two groups for a given distribution with delay. The effect is specified in terms of the model parameters for both groups. There are two modes of operation:

  1. power=NULL: simulate power based on given sample size n (post-hoc power estimation)

  2. n=NULL: search iteratively for a suitable sample size n for a given power

Usage

power_diff(
  distribution = c("exponential", "weibull"),
  twoPhase = FALSE,
  eff = stop("Provide parameters for both groups that reflect the effect!"),
  param = "delay1",
  test = c("bootstrap", "logrank", "logrank_pp", "LRT"),
  method = c("MPSE", "MLEw", "MLEc", "MLEn"),
  n = NULL,
  r = 1,
  sig.level = 0.05,
  power = NULL,
  nPowerSim = 1600,
  R = 200,
  nRange = c(5, 250),
  verbose = 0
)

Arguments

distribution

character. Which assumed distribution is used for the power calculation. Default is "exponential".

twoPhase

logical(1). Do we model two phases per group? Default is FALSE, i.e. a single delay phase per group.

eff

list of length 2. The two list elements must be numeric vectors that contain the model parameters (as understood by the delay-distribution functions provided by this package) for the two groups.

param

character. Parameter name(s) which are to be tested for difference and for which to simulate the power. Default value is 'delay1'. You can specify multiple parameters, by giving a vector or by concatenating them with a + in a single string. For logrank tests, this argument is ignored.

test

character. Which test to use for this power estimation? Defaults to "bootstrap". Non-parametric logrank test is also possible (either "logrank" or "logrank_pp"). See also test_diff().

method

character. Which fitting method to use in case of a parametric test.

n

integer. Number of observations per group for the power simulation or NULL when n is to be estimated for a given power.

r

numeric. Ratio of both groups sizes, ny / nx. Default value is 1, i.e. balanced group sizes. Must be positive.

sig.level

numeric. Significance level. Default is 0.05.

power

numeric. NULL when power is to be estimated for a given sample size or a desired power is specified (and n is estimated).

nPowerSim

integer. Number of simulation rounds. Default value 1600 yields a standard error of 0.01 for power if the true power is 80%.

R

integer. Number of bootstrap samples for test of difference within each power simulation. It affects the resolution of the P-value for each simulation round. A value of around R=200 gives a resolution of 0.5% which might be enough for power analysis.

nRange

integer. Admissible range for sample size when power is specified and sample size is requested. The routine might not find the optimal sample size when this range is set too wide.

verbose

numeric. How many details are requested? Higher value means more details. 0=off, no details.

Details

The power is estimated by simulating data according to the specified model and testing for differences in the simulated data. The proportion of simulated datasets where the test rejects the null hypothesis at the given significance level is the estimated power. The test can be a parametric bootstrap test or a non-parametric logrank test. For logrank tests, the ⁠param=⁠ argument should not be specified. Specify the effect size (⁠eff=⁠) as a list with two elements, each element holds the parameter vector of the distribution of the response variable of one group. The fitting method (⁠method=⁠) and the kind of significance test (⁠test=⁠) are handed down to test_diff(). The more power simulation rounds (parameter ⁠nPowerSim=⁠) the more densely the space of possible data according to the specified model is sampled.

Note that estimating sample size n is computationally intensive. The iterative search uses heuristics to find a sample size n within the provided range (⁠nRange=⁠), and the estimated sample size might yield a slightly different power level. Hence, check the reported power in the output. The search algorithm comes to better results when the admissible range for sample size (⁠nRange=⁠) is chosen sensibly and not too wide. In case the estimated sample size and the achieved power is too high it might pay off to rerun the function with an adapted admissible range for the sample size by giving a narrower range in ⁠nRange=⁠.

Value

List of results of power simulation. Or NULL in case of errors.

See Also

test_diff()

Examples

# Simulate power for a given sample size:
# test for any difference in a delay-exponential model using logrank test
# the assumed effect is given in terms of model parameters for both groups
power_diff(
  eff = list(ctrl = c(delay1 = 5, rate1 = .09),
             trtm = c(delay1 = 7, rate1 = .12)),
  test = "logrank",
  n = 16, power = NULL, nPowerSim = 300)

## Not run: 
# test for difference in delay in a delay-exponential model via a bootstrap test:
# the power is estimated based on nPowersim = 520 simulated datasets
# for real applications, use a higher nPowerSim (e.g. 1600) for more
# precise power estimation and a higher R (e.g. 400) for more precise
# P-value estimation in each simulation round
set.seed(123) # for reproducibility
power_diff(
  eff = list(grA = c(delay1 = 5.2, rate1 = .1),
             grB = c(delay1 = 7, rate1 = .12)),
  param = "delay1",
  test = "bootstrap", method = "MPSE",
  n = 16, power = NULL,
  nPowerSim = 520, R = 160)

# test for difference in rate in a delay-exponential model via a bootstrap test:
# required sample size is estimated for a given power.
# provide a suitable range for the sample size search via nRange=.
# The search for n takes more time than the previous example, as the function
# iteratively evaluates different sample sizes in order to find the right one.
set.seed(1234) # for reproducibility
power_diff(
  eff = list(grA = c(delay1 = 5, rate1 = .07),
             grB = c(delay1 = 7, rate1 = .18)),
  param = "rate1",
  test = "bootstrap", method = "MPSE",
  n = NULL, power = 0.8,
  nPowerSim = 480, R = 150,
  nRange = c(18, 48))

## End(Not run)

incubate documentation built on Nov. 22, 2026, 1:07 a.m.