-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.R
82 lines (77 loc) · 2.18 KB
/
util.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
library(mvtnorm)
# Function to check if a symmetric matrix is positive definite
## Input
### matrix: a symmetric matrix
### tol: tolerance
## Output
### A true or false value
check_positive_definite <- function(matrix, tol = 1e-8) {
eigenvalues <- eigen(matrix, symmetric = TRUE, only.values = TRUE)$values
if (any(eigenvalues <= tol)) {
return(FALSE)
} else {
return(TRUE)
}
}
# Function to generate the correlation matrix
## Input
### t: information time
## Output
### Correlation matrix
cr_function <- function(t){
K <- length(t)
cr <- diag(K)
for (i in 1:K){
for (j in i:K){
cr[i, j] <- sqrt(t[i] / t[j])
}
}
cr <- cr + t(cr) - diag(K)
return(cr)
}
# Error spending function for the OBrien-Fleming Lan-DeMets design
## Input
### alpha: one-sided significance level
### t: information time
## Output
### Vector of cumulative error spent
esf_OBF_function <- function(alpha, t) {
y <- 2 - 2 * pnorm(qnorm(1 - alpha / 2) / sqrt(t))
return(y)
}
# Error spending function for the Pocock Lan-DeMets design
## Input
### alpha: one-sided significance level
### t: information time
## Output
### Vector of cumulative error spent
esf_POC_function <- function(alpha, t) {
y <- alpha * log(1 + (exp(1) - 1) * t)
return(y)
}
# Function to derive the stopping boundary using the cumulative error spent
## Input
### t: information time
### cumulative: cumulative error spent with the same length as t
## Output
### Vector of boundary values
solver_boundary_esf_function <- function(t, cumulative, steps = 1024){
K <- length(t)
cr <- cr_function(t)
solver <- function(x, cumu, cr = cr, c_past){
z <- c(c_past, x)
return(1 - cumu - pmvnorm(upper = z, corr = cr[1:length(z), 1:length(z)],
algorithm = Miwa(steps = steps, checkCorr = FALSE))
)
}
c_boundary <- rep(0,K)
c_boundary[1] <- min(qnorm(1 - cumulative[1]), 10)
if (K > 1){
for (k in 2:K){
a <- uniroot(solver, interval=c(0.001, 10), cumu = cumulative[k],
cr = cr, c_past = c_boundary[1:(k-1)])$root
c_boundary[k] <- min(a, 10)
}
}
return(c_boundary)
}