-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleSize.R
42 lines (38 loc) · 1.13 KB
/
sampleSize.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Correction factor
# r = w (in slide notation)
# p = v
R <- function(r, p, rho) {
l1 <- (1 + (r - 1) * rho) / r
l2 <- (p * rho^2) / (1 + (p - 1) * rho)
return(l1 - l2)
}
# Sample size calculator
n <- function(Delta, r, p, rho, alpha = 0.05, beta = 0.8) {
f <- (qnorm(1 - alpha/2) + qnorm(beta))^2
R.int <- R(r, p, rho)
print(paste("f =", round(f, 2)))
print(paste("R =", round(R.int, 2)))
n <- (2 / Delta^2) * R.int * f
print(paste("Sample size per group required =", ceiling(n)))
invisible(n)
}
# Examples
n(0.4, 1, 1, 0.7)
n(0.4, 2, 1, 0.7)
n(0.4, 3, 1, 0.7)
# Overall plot
rp <- expand.grid(1:8, 1:8)
colnames(rp) <- c("r", "p")
rp$n <- apply(rp, 1, function(x) n(0.4, x[1], x[2], 0.7))
rp$n <- rp$n / rp$n[1]
library(ggplot2)
library(scales)
ggplot(aes(x = factor(r), y = n, colour = factor(p)), data = rp) +
geom_line(aes(group = factor(p))) +
scale_y_continuous(labels = scales::percent) +
labs(
colour = "v (Number of baseline / pre-follow-up measurements)",
x = "w (Number of follow-up measurements)",
y = "Sample size relative to a trial with v = 1 and w = 1"
) +
theme(legend.position = "bottom")