Code
library(ivdtools)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.
library(ivdtools)| 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 |
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
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:
muis the mean difference,sdis the difference standard deviation (>0), anddeltais the clinically acceptable agreement limit (>0).conf.levelis the confidence level of the agreement-limit confidence interval, whileagree.levelis 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.
Given an existing sample size, look up the achievable power:
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.
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.
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:
pis the expected proportion (must be given, 0<p<1) anddis the desired half-width (0<d≤0.5).methodaccepts 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.
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:
alternativecan be"greater"(in which case p1 > p0 is required),"less", or"two.sided". This method is based on confidence-interval inversion: given any two ofn/alpha/power, find the third; when all three are given, it serves as a consistency check.
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
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).
alternative and the relative magnitude of p0/p1.