-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTBIgenJW_light.R
76 lines (65 loc) · 2.16 KB
/
TBIgenJW_light.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
TBIgenJW_test <- function (mat1, mat2, method = "chord",
nperm = 99, replace = FALSE, test.t.perm = FALSE, seed. = NULL, clock = FALSE) {
##### dissim function to compute dissimilarities
dissim <- function(mat1, mat2, n, method) {
vecD = vector(mode = "numeric", length = n)
if (any(method == c("chord"))) {
for (i in 1:n) vecD[i] <- dist(rbind(mat1[i, ], mat2[i,]))
list(vecD = vecD)
}
}
##### initialization
if (!is.null(seed.))
set.seed(seed.)
epsilon <- sqrt(.Machine$double.eps)
method <- match.arg(method, c("chord"))
n = nrow(mat1)
p = ncol(mat1)
if ((nrow(mat2) != n) | (ncol(mat2) != p))
stop("The matrices are not of the same size.")
if (method == "chord") {
tr <- TRUE
}
else {
tr <- FALSE
}
test.B.C <- NA
BCD <- FALSE
BCD.mat <- NA
BCD.summ <- NA
if (tr)
tmp <- dissim(decostand(mat1, "norm"), decostand(mat2,
"norm"), n, method)
else tmp <- dissim(mat1, mat2, n, method)
vecD.ref <- tmp$vecD
BC <- NA
##### permutations
if (nperm > 0) {
my.vec <- sample(1:(10 * nperm), size = nperm)
nGE.D = rep(1, n)
for (iperm in 1:nperm) {
set.seed(my.vec[iperm])
mat1.perm <- apply(mat1, 2, sample, replace = replace)
set.seed(my.vec[iperm])
mat2.perm <- apply(mat2, 2, sample, replace = replace)
if (tr) {
tmp <- dissim(decostand(mat1.perm, "norm"),
decostand(mat2.perm, "norm"), n, method)
}
else {
tmp <- dissim(mat1.perm, mat2.perm, n, method)
}
vecD.perm <- tmp$vecD
ge <- which(vecD.perm + epsilon >= vecD.ref)
if (length(ge) > 0)
nGE.D[ge] <- nGE.D[ge] + 1
}
p.dist <- nGE.D/(nperm + 1)
}
p.adj <- p.adjust(p.dist, "holm")
out <- list(TBI = vecD.ref, p.TBI = p.dist, p.adj = p.adj)
class(out) <- "TBI"
return(out)
}
test <- TBIgenJW_light(CD2TBI(i, earliest, scenario = scenario),
CD2TBI(i, latest, scenario = scenario), method = "chord", n = 1000 )