-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_related.R
More file actions
325 lines (187 loc) · 8.83 KB
/
path_related.R
File metadata and controls
325 lines (187 loc) · 8.83 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
require(lars)
require(glmnet)
#sourceCpp('LOCO_TS.cpp')
ExtractLars.Path <- function(x, y, whichCov, betaNULL = 0, normalize = TRUE, intercept = FALSE){
# Extract Path info
#
# Args:
# X,Y: design matrix and response vector
# which.covariate: if is a vector, indicating which covariate we will be computing; if is a list: then do multiple testing.
#
# normalize: argguments of lars
# betaNULL: same size and same data type with which.covariate, specify the null hypothesis H0: beta = betaNULL.
# Returns:
# A list of lambda vector, original path, and the LOCO path
#
#
n = nrow(x)
p = ncol(x)
################# Here we go ! ! ! #######################################################
##### extract the path #######
if (length(whichCov) != length(betaNULL)){
stop("Length of variables being tested must equal to # of hypothesis")
}else{
multiTest <- length(whichCov) > 1
}
# simultanoues testing
if(multiTest){
#### ajust Y by using Y - X_j * beta_j_NULL - X_k * beta_k_NULL - ...
adjust.X <- rowSums( t( t(x) * betaNULL ) )
adjust.Y <- y - adjust.X
X_scaled <- scale(x)
lars.out <- lars(X_scaled, adjust.Y, type = "lasso", intercept = intercept, use.Gram = FALSE, normalize=normalize)
lambda.hat <- sort(c(lars.out$lambda,0),decreasing=FALSE)
lars.j.out <- lars(X_scaled[, -whichCov], adjust.Y, type = "lasso", intercept = intercept, use.Gram = FALSE,normalize=normalize)
lambda.j.hat <- sort(c(lars.j.out$lambda,0),decreasing=FALSE)
# indivdual testing
}else if(!multiTest){ #indivdual test
adjust.Y <- y - betaNULL * x[,whichCov]
X_scaled <- scale(x)
lars.out <- lars(X_scaled, adjust.Y , type = "lasso", intercept = intercept, use.Gram = FALSE, normalize=normalize)
lambda.hat <- sort(c(lars.out$lambda,0),decreasing = FALSE)
lars.j.out <- lars(X_scaled[, -whichCov], adjust.Y, type = "lasso", intercept = intercept, use.Gram = FALSE, normalize=normalize)
lambda.j.hat <- sort(c(lars.j.out$lambda,0), decreasing = FALSE)
}else{
stop("wrong input of multiTest, must be boolean")
}
#### remove the unoverlapping part (test)
if(lambda.hat[1] != lambda.j.hat[1]){
leftmost = c(lambda.hat[1], lambda.j.hat[1])
whichone = which.max(leftmost)
if(whichone ==1){
lambda.j.hat = lambda.j.hat[lambda.j.hat >= lambda.hat[1]]
lambda.j.hat = c(lambda.hat[1], lambda.j.hat)
}else{
lambda.hat = lambda.hat[lambda.hat >= lambda.j.hat[1]]
lambda.hat = c(lambda.j.hat[1], lambda.hat)
}
}
union.lambda <- sort(unique(c(lambda.hat,lambda.j.hat)), decreasing = FALSE)
beta.j.hat <- matrix(0, length(union.lambda), p)
beta.hat <- predict(lars.out, s=union.lambda, type="coef", mode="lambda")$coefficients
beta.j.hat[, -whichCov] <- predict(lars.j.out, s=union.lambda, type="coef", mode="lambda")$coefficients
#beta.hat <- coef(lars.out, lam = union.lambda)
#beta_val <- coef(lars.j.out, lam = union.lambda)
return(list(union.lambda = union.lambda, beta.hat = beta.hat, beta.j.hat = beta.j.hat))
}
LOCOLars.TS <- function(obj){
# Calculate PATH statistic exactly
#
# Args:
# X,Y: design matrix and response vector
# which.covariate: if is a vector, indicating which covariate we will be computing; if is a list: then do multiple testing.
#
# normalize: argguments of lars
# betaNULL: same size and same data type with which.covariate, specify the null hypothesis H0: beta = betaNULL.
# Returns:
# A list of lambda vector, original path, the LOCO path, and Test Statistic
#
#
M <- length(obj$union.lambda)
Delta <- obj$beta.j.hat - obj$beta.hat
Delta_1 <- Delta[-M, ]
Delta_2 <- Delta[-1, ]
Lambda <- diff(obj$union.lambda)
Epsilon <- 1/3 * Lambda * (Delta_1 * Delta_1 + Delta_1 * Delta_2 + Delta_2 * Delta_2)
return(sum(Epsilon))
}
LOCOLars.TS.new <- function(obj, s = 2, t = 2){
# Calculate PATH statistic exactly
#
# Args:
# X,Y: design matrix and response vector
# which.covariate: if is a vector, indicating which covariate we will be computing; if is a list: then do multiple testing.
#
# normalize: argguments of lars
# betaNULL: same size and same data type with which.covariate, specify the null hypothesis H0: beta = betaNULL.
# Returns:
# A list of lambda vector, original path, the LOCO path, and Test Statistic
#
#
M <- length(obj$union.lambda)
Delta <- obj$beta.j.hat - obj$beta.hat
Delta_1 <- Delta[-M, ]
Delta_2 <- Delta[-1, ]
Lambda <- diff(obj$union.lambda)
equal_sign_indicator <- Delta_2 * Delta_1 < 0
Epsilon <- 1/(s+1) * Lambda * abs( (Delta_2 ^ (s+1) - ((-1) ^ equal_sign_indicator) * Delta_1 ^ (s+1)) / (Delta_2 - Delta_1) )
if(s == t){
return( (sum(Epsilon, na.rm = TRUE)) ^ (1/t) )
}else{
return( sum(rowSums(Epsilon) ^ (t/s), na.rm = TRUE) ^ (1/t) )
}
}
## NOW, GLMNET ! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#######################################################
#######################################################
################## GLMNET #############################
################## GLMNET #############################
################## GLMNET #############################
#######################################################
#######################################################
ExtractNet.Path <- function(x, y, whichCov, betaNULL = 0, nlambda = 1000, alpha = 1, family = "gaussian", normalize = TRUE, intercept = FALSE){
# Extract Path info
#
# Args:
# X,Y: design matrix and response vector
# which.covariate: if is a vector, indicating which covariate we will be computing; if is a list: then do multiple testing.
#
# normalize: argguments of lars
# betaNULL: same size and same data type with which.covariate, specify the null hypothesis H0: beta = betaNULL.
# Returns:
# A list of lambda vector, original path, and the LOCO path
#
#
n <- nrow(x)
p <- ncol(x)
################# Here we go ! ! ! #######################################################
##### extract the path #######
if (length(whichCov) != length(betaNULL)){
stop("Length of variables being tested must equal the length of their Null hypothesis")
}else{
multiTest <- length(whichCov) > 1
}
# simultanoues testing
if(multiTest){
#### ajust Y by using Y - X_j * beta_j_NULL - X_k * beta_k_NULL - ...
adjust.X <- rowSums( t( t(x) * betaNULL ) )
adjust.Y <- as.vector( y - adjust.X )
X_scaled = scale(x)
## TODO
max_lam = NA
net.out <- glmnet(X_scaled, adjust.Y, nlambda = nlambda, lambda.min.ratio = 0.001, alpha = alpha, family = family, intercept = intercept, standardize = normalize)
lambda.hat <- sort(net.out$lambda, decreasing = FALSE)
net.j.out <- glmnet(X_scaled[, -whichCov], adjust.Y, nlambda = nlambda, lambda.min.ratio = 0.001, alpha = alpha, family = family, intercept = intercept, standardize = normalize)
lambda.j.hat <- sort(net.j.out$lambda, decreasing = FALSE)
# indivdual testing
}else if(!multiTest){ #indivdual test
adjust.Y <- as.vector( y - betaNULL * x[,whichCov] )
X_scaled <- scale(x)
## TODO
net.out <- glmnet(X_scaled, adjust.Y, nlambda = nlambda, lambda.min.ratio = 0.001, alpha = alpha, family = family, intercept = intercept, standardize = normalize)
lambda.hat <- sort(net.out$lambda, decreasing = FALSE)
net.j.out <- glmnet(X_scaled[, -whichCov], adjust.Y, nlambda = nlambda, lambda.min.ratio = 0.001, alpha = alpha, family = family, intercept = intercept, standardize = normalize)
lambda.j.hat <- sort(net.j.out$lambda, decreasing = FALSE)
}else{
stop("wrong input of multiTest, must be boolean")
}
minLam <- min(lambda.hat, lambda.j.hat)
lambda.hat = lambda.hat[lambda.hat > minLam]
lambda.j.hat = lambda.hat[lambda.hat > minLam]
union.lambda <- sort(unique(c(lambda.hat,lambda.j.hat)), decreasing = FALSE)
beta.j.hat <- Matrix(0, length(union.lambda), p) # sparse Matrix
beta.hat <- predict(net.out, s=union.lambda, type="coef")
beta.hat <- t( beta.hat[-1, ] ) # we don't need intercept
beta.j.tmp <- predict(net.j.out, s=union.lambda, type="coef")
beta.j.hat[, -whichCov] <- t( beta.j.tmp[-1, ] ) # we don't need intercep
return(list(union.lambda = union.lambda, beta.hat = beta.hat, beta.j.hat = beta.j.hat))
}
LOCONet.TS <- function(obj){
M <- length(obj$union.lambda)
Delta <- obj$beta.j.hat - obj$beta.hat
Delta_1 <- Delta[-M, ]
Delta_2 <- Delta[-1, ]
Lambda <- diff(obj$union.lambda)
Epsilon <- 1/3 * Lambda * (Delta_1 * Delta_1 + Delta_1 * Delta_2 + Delta_2 * Delta_2)
return(sum(Epsilon))
}