-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathECDM.r
More file actions
148 lines (134 loc) · 4.27 KB
/
Copy pathECDM.r
File metadata and controls
148 lines (134 loc) · 4.27 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
############################################################################################
## Estimation of tr(\Sigma^2) by the ECDM methodology.
##
## INPUT:
## W(X); p by n matrix X as X=(x_{1},...,x_{n}),
## where n (≧ 4) is the sample size and p (≧ 1) is the dimension.
##
## OUTPUT:
## Wn: The estimator of \tr(\Sigma^2) by the ECDM methodology.
############################################################################################
W <- function(X){
p <- list(dim(X)[1])
n <- dim(X)[2]
n1 <- as.integer(ceiling(n/2))
n2 <- n - n1
K <- c(3:(2*n-1))
L <- length(K)
Y <- array(0, dim=c(2, L, p[[1]]))
for (l in 1:L)
{
V <- list()
dv <- as.integer(floor(K[l]/2))
if (dv >= n1){
id <- c((dv-n1+1): dv)
V <- append(V, list(id))
} else{
id <- append(c(1: dv), c((dv+n2+1): n))
V <- append(V, list(id))
}
if (dv <= n1){
id <- c((dv+1): (dv+n2))
V <- append(V, list(id))
} else{
id <- append(c(1: (dv-n1)), c((dv+1): n))
V <- append(V, list(id))
}
for (i in 1:2)
{
Y[i, l, ] <- apply(X[, V[[i]]], 1, mean)
}
}
u <- n1 * n2 / ((n1-1) * (n2-1))
w <- 0
for (j in 1:n){
for (i in 1:j){
if (i != j){
w <- w + (as.numeric((X[, i] - Y[1, (i+j-2), ]) %*% (X[, j] - Y[2, (i+j-2), ])))^2
}
}
}
W <- 2 * u / (n * (n - 1)) * w
return(list(Wn=W))
}
#####################################################################################################################
## The test of high-dimensional correlations by (5) in Yata and Aoshima (2016).
##
## INPUT:
## Tecdm(X1,X2); p_1 by n matrix X1 and p_2 by n matrix X2 as X1=(x_{11},...,x_{1n}) and X2=(x_{21},...,x_{2n}),
## where n (≧ 4) is the sample size and p_i (≧ 1) is the dimension of Xi.
##
##
## OUTPUT:
## TestStatistics: The test statistic value for (5) (\hat{T}_n/\hat{\delta}).
## pvalue: The asymptotic p-value for testing (1) by (5)
## (1-\Phi(\hat{T}_n/\hat{\delta}), where \Phi(x) is the c.d.f. of N(0,1)).
## Tn: The estimator of \Delta (\hat{T}_n).
## delta: The estimator of the standard deviation for \hat{T}_n.
#####################################################################################################################
Tecdm <- function(X1, X2){
if (is.null(dim(X1))){
X1 <- t(as.matrix(X1))
}
if (is.null(dim(X2))){
X2 <- t(as.matrix(X2))
}
p <- list(dim(X1)[1], dim(X2)[1])
n <- dim(X1)[2]
n1 <- as.integer(ceiling(n/2))
n2 <- n - n1
K <- c(3:(2*n-1))
L <- length(K)
Y1 <- array(0, dim=c(2, L, p[[1]]))
Y2 <- array(0, dim=c(2, L, p[[2]]))
for (l in 1:L){
V <- list()
dv <- as.integer(floor(K[l]/2))
if (dv >= n1){
id <- c((dv-n1+1): dv)
V <- append(V, list(id))
} else{
id <- append(c(1: dv), c((dv+n2+1): n))
V <- append(V, list(id))
}
if (dv <= n1){
id <- c((dv+1): (dv+n2))
V <- append(V, list(id))
} else{
id <- append(c(1: (dv-n1)), c((dv+1): n))
V <- append(V, list(id))
}
for (i in 1:2){
if (is.null(dim(X1[, V[[i]]]))){
Y1[i, l, ] <- apply(t(as.matrix(X1[, V[[i]]])), 1, mean)
} else {
Y1[i, l, ] <- apply(X1[, V[[i]]], 1, mean)
}
if (is.null(dim(X2[, V[[i]]]))){
Y2[i, l, ] <- apply(t(as.matrix(X2[, V[[i]]])), 1, mean)
} else {
Y2[i, l, ] <- apply(X2[, V[[i]]], 1, mean)
}
}
}
u <- n1 * n2 / ((n1-1) * (n2-1))
w1 <- 0
w2 <- 0
t <- 0
for (j in 1:n){
for (i in 1:j){
if (i != j){
t <- t + as.numeric((X1[, i] - Y1[1, (i+j-2), ]) %*% (X1[, j] - Y1[2, (i+j-2), ])) * as.numeric((X2[, i] - Y2[1, (i+j-2), ]) %*% (X2[, j] - Y2[2, (i+j-2), ]))
w1 <- w1 + (as.numeric((X1[, i] - Y1[1, (i+j-2), ]) %*% (X1[, j] - Y1[2, (i+j-2), ])))^2
w2 <- w2 + (as.numeric((X2[, i] - Y2[1, (i+j-2), ]) %*% (X2[, j] - Y2[2, (i+j-2), ])))^2
}
}
}
W1 <- 2 * u / (n * (n - 1)) * w1
W2 <- 2 * u / (n * (n - 1)) * w2
T_hat <- 2 * u / (n * (n - 1)) * t
delta_hat <- sqrt(2 * W1 * W2) / n
test <- T_hat / delta_hat
p_value <- pnorm(test, lower.tail = FALSE)
return(list(TestStatistics=test, pvalue=p_value, Tn=T_hat, delta=delta_hat))
}