11  Sample Size Calculation

11.1 Analysis Objectives

Sample size calculation determines the required sample size or the achievable test power during the research-design and performance-evaluation planning phase. ivdtools provides three standalone functions corresponding to three common problem types:

  • sample_size_bland_altman(): sample size/power for Bland–Altman agreement evaluation (Lu et al. 2016);
  • sample_size_proportion_ci(): sample size required for a single-proportion confidence interval to reach a specified half-width;
  • sample_size_proportion(): sample size/power/significance for a one-arm target-value test (a single-sample proportion against a known target).

All three functions are parameter-driven and do not require raw data files. They return scalars with attributes; print() gives a friendly explanation, and as.numeric() extracts the numeric result. The parameters in the examples (power, confidence level, allowable half-width, etc.) are teaching settings; formal studies must use the parameters specified in the protocol.

Code
library(ivdtools)

11.2 Overview of Functions

Function Main purpose Solving direction
sample_size_bland_altman() BA agreement evaluation from power find n, or from n find power
sample_size_proportion_ci() Single-proportion confidence interval from d find n, or from n find d
sample_size_proportion() One-arm target-value test given any two of n/alpha/power, find the third

11.3 Example 1: Sample Size for Bland–Altman Agreement

11.3.1 Finding Sample Size from Target Power

Code
n_ba <- sample_size_bland_altman(
  power = 0.80,
  mu = 0.2,
  sd = 1,
  delta = 2.5
)
print(n_ba)
Sample size for Bland-Altman agreement assessment
  Mode             : Compute n from power
  Mean difference  : 0.2000
  SD of differences: 1.0000
  Clinical limit   : 2.5000
  Confidence level : 0.9500
  Agreement level  : 0.9500
  Sample size (n)  : 201
  Power            : 0.800300
Code
as.numeric(n_ba)
[1] 201

Under the settings of mean difference mu = 0.2, difference SD sd = 1, and clinical agreement limit delta = 2.5, about 201 pairs are needed to achieve 80% power.

Parameter notes: mu is the mean difference, sd is the difference standard deviation (>0), and delta is the clinically acceptable agreement limit (>0). conf.level is the confidence level of the agreement-limit confidence interval, while agree.level is the coverage of the LoA; the two are at different levels — the former affects the reliability of the estimate, the latter determines the width of the LoA itself.

11.3.2 Finding Power from Sample Size

Given an existing sample size, look up the achievable power:

Code
sample_size_bland_altman(n = 201, mu = 0.2, sd = 1, delta = 2.5)
Sample size for Bland-Altman agreement assessment
  Mode             : Compute power from n
  Mean difference  : 0.2000
  SD of differences: 1.0000
  Clinical limit   : 2.5000
  Confidence level : 0.9500
  Agreement level  : 0.9500
  Sample size (n)  : 201
  Power            : 0.800300

This mutually verifies the above; n=201 exactly corresponds to power 0.80.

11.4 Example 2: Sample Size for a Single-Proportion Confidence Interval

11.4.1 Finding Sample Size from Target Half-Width

Code
n_pci <- sample_size_proportion_ci(p = 0.3, d = 0.05)
print(n_pci)
Single proportion confidence interval
  Mode             : Compute n from p and d
  Method           : wilson
  Target proportion: 0.3000
  Confidence level : 0.9500
  Sample size (n)  : 320
  Half-width (d)   : 0.050000

For an expected proportion p = 0.3, to keep the half-width of the 95% confidence interval no larger than 0.05, about 320 cases are needed.

11.4.2 Finding Half-Width from Sample Size

Code
sample_size_proportion_ci(p = 0.3, n = 320)
Single proportion confidence interval
  Mode             : Compute d from p and n
  Method           : wilson
  Target proportion: 0.3000
  Confidence level : 0.9500
  Sample size (n)  : 320
  Half-width (d)   : 0.049967

At n=320 the half-width is about 0.05, mutually verifying the above.

Parameter notes: p is the expected proportion (must be given, 0<p<1) and d is the desired half-width (0<d≤0.5). method accepts 7 confidence-interval methods (default "wilson", also "wald", "wald-cc", "agresti-coull", "jeffreys", "wilson-cc", "clopper-pearson"); the method choice affects the required sample size and should be stated in the protocol.

11.5 Example 3: Sample Size for a One-Arm Target-Value Test

11.5.1 Finding Sample Size

Code
n_sp <- sample_size_proportion(
  p0 = 0.2,
  p1 = 0.35,
  alpha = 0.05,
  power = 0.8,
  alternative = "greater",
  method = "wilson"
)
print(n_sp)
Sample size for a single-arm target value test
  Mode             : Compute n from alpha and power
  Method           : wilson
  H0: p <= 0.2000
  H1: p > 0.2000
  Expected p       : 0.3500
  Sample size (n)  : 47
  Alpha            : 0.050000
  Power            : 0.815687
  Critical x       : 14

Under the settings of p0 = 0.2 (target/null-hypothesis proportion), p1 = 0.35 (expected proportion), one-sided α = 0.05, and power 0.80, about 47 cases are needed.

Parameter notes: alternative can be "greater" (in which case p1 > p0 is required), "less", or "two.sided". This method is based on confidence-interval inversion: given any two of n/alpha/power, find the third; when all three are given, it serves as a consistency check.

11.5.2 Finding Power

Code
sample_size_proportion(
  p0 = 0.2,
  p1 = 0.35,
  n = 47,
  alpha = 0.05,
  alternative = "greater"
)
Sample size for a single-arm target value test
  Mode             : Compute power from n and alpha
  Method           : wilson
  H0: p <= 0.2000
  H1: p > 0.2000
  Expected p       : 0.3500
  Sample size (n)  : 47
  Alpha            : 0.050000
  Power            : 0.815687
  Critical x       : 14

11.5.3 Finding the Significance Level (alpha)

Code
sample_size_proportion(
  p0 = 0.2,
  p1 = 0.35,
  n = 47,
  power = 0.8,
  alternative = "greater"
)
Sample size for a single-arm target value test
  Mode             : Compute alpha from n and power
  Method           : wilson
  H0: p <= 0.2000
  H1: p > 0.2000
  Expected p       : 0.3500
  Sample size (n)  : 47
  Alpha            : 0.046728
  Power            : 0.815687
  Critical x       : 14

The three solving directions verify one another, and the results are consistent (n=47, power=0.8, alpha≈0.05).

11.6 Key Points for Interpreting Results

  1. Parameters first: Before computing sample size, determine power, confidence level, allowable error/half-width, expected proportion, and the one- or two-sided direction.
  2. Results are conditional: The sample size is valid only under the set parameters; inaccurate parameter estimates lead to inaccurate sample sizes.
  3. Consistent methods: For proportion-type problems, determine the CI method (Wilson, etc.) in advance; do not choose after the fact.
  4. Direction constraint: For the one-arm test, note the consistency between alternative and the relative magnitude of p0/p1.
  5. Record in protocol: Write the parameters, function calls, and outputs together into the study protocol for easy review and update.