@@ -82,10 +82,11 @@ fixedLassoInf <- function(x, y, beta,
82
82
83
83
tol.coef = tol.beta * sqrt(n ^ 2 / colSums(x ^ 2 ))
84
84
# print(tol.coef)
85
- vars = which(abs(beta ) > tol.coef )
85
+ vars = which(abs(beta ) > tol.coef )
86
+ # vars = abs(beta) > tol.coef
86
87
# print(beta)
87
88
# print(vars)
88
- if (length (vars )== 0 ){
89
+ if (sum (vars )== 0 ){
89
90
cat(" Empty model" ,fill = T )
90
91
return ()
91
92
}
@@ -96,8 +97,10 @@ fixedLassoInf <- function(x, y, beta,
96
97
" 'thresh' parameter, for a more accurate convergence." ))
97
98
98
99
# Get lasso polyhedral region, of form Gy >= u
99
- if (type == ' full' & p > n ) out = fixedLassoPoly(x ,y ,lambda ,beta ,vars ,inactive = TRUE )
100
- else out = fixedLassoPoly(x ,y ,lambda ,beta ,vars )
100
+ logical.vars = rep(FALSE ,p )
101
+ logical.vars [vars ]= TRUE
102
+ if (type == ' full' ) out = fixedLassoPoly(x ,y ,lambda ,beta ,logical.vars ,inactive = TRUE )
103
+ else out = fixedLassoPoly(x ,y ,lambda ,beta ,logical.vars )
101
104
A = out $ A
102
105
b = out $ b
103
106
@@ -127,7 +130,7 @@ fixedLassoInf <- function(x, y, beta,
127
130
# add additional targets for inference if provided
128
131
if (! is.null(add.targets )) vars = sort(unique(c(vars ,add.targets ,recursive = T )))
129
132
130
- k = length(vars )
133
+ k = length(vars )
131
134
pv = vlo = vup = numeric (k )
132
135
vmat = matrix (0 ,k ,n )
133
136
ci = tailarea = matrix (0 ,k ,2 )
@@ -154,20 +157,24 @@ fixedLassoInf <- function(x, y, beta,
154
157
155
158
# Reorder so that active set S is first
156
159
Xordered = Xint [,c(S ,notS ,recursive = T )]
160
+ hsigmaS = 1 / n * (t(XS )%*% XS ) # hsigma[S,S]
161
+ hsigmaSinv = solve(hsigmaS ) # pinv(hsigmaS)
157
162
158
- hsigma <- 1 / n * (t(Xordered )%*% Xordered )
159
- hsigmaS <- 1 / n * (t(XS )%*% XS ) # hsigma[S,S]
160
- hsigmaSinv <- solve(hsigmaS ) # pinv(hsigmaS)
163
+ FS = rbind(diag(length(S )),matrix (0 ,pp - length(S ),length(S )))
164
+ GS = cbind(diag(length(S )),matrix (0 ,length(S ),pp - length(S )))
161
165
162
- # Approximate inverse covariance matrix for when (n < p) from lasso_Inference.R
166
+ is_wide = n < ( 2 * p ) # somewhat arbitrary decision -- it is really for when we don't want to form with pxp matrices
163
167
164
- htheta = debiasingMatrix(hsigma , n , 1 : length(S ), verbose = FALSE , max_try = linesearch.try , warn_kkt = TRUE )
168
+ # Approximate inverse covariance matrix for when (n < p) from lasso_Inference.R
169
+ if (! is_wide ) {
170
+ hsigma = 1 / n * (t(Xordered )%*% Xordered )
171
+ htheta = debiasingMatrix(hsigma , is_wide , n , 1 : length(S ), verbose = FALSE , max_try = linesearch.try , warn_kkt = TRUE )
172
+ ithetasigma = (GS - (htheta %*% hsigma ))
173
+ } else {
174
+ htheta = debiasingMatrix(Xordered , is_wide , n , 1 : length(S ), verbose = FALSE , max_try = linesearch.try , warn_kkt = TRUE )
175
+ ithetasigma = (GS - ((htheta %*% t(Xordered )) %*% Xordered )/ n )
176
+ }
165
177
166
- FS = rbind(diag(length(S )),matrix (0 ,pp - length(S ),length(S )))
167
- GS = cbind(diag(length(S )),matrix (0 ,length(S ),pp - length(S )))
168
- ithetasigma = (GS - (htheta %*% hsigma ))
169
- # ithetasigma = (diag(pp) - (htheta%*%hsigma))
170
-
171
178
M <- (((htheta %*% t(Xordered ))+ ithetasigma %*% FS %*% hsigmaSinv %*% t(XS ))/ n )
172
179
# vector which is offset for testing debiased beta's
173
180
null_value <- (((ithetasigma %*% FS %*% hsigmaSinv )%*% sign(hbetaS ))* lambda / n )
@@ -264,10 +271,11 @@ fixedLassoPoly =
264
271
# # Approximates inverse covariance matrix theta
265
272
# # using coordinate descent
266
273
267
- debiasingMatrix = function (Sigma ,
274
+ debiasingMatrix = function (Xinfo , # could be X or t(X) %*% X / n depending on is_wide
275
+ is_wide ,
268
276
nsample ,
269
277
rows ,
270
- verbose = FALSE ,
278
+ verbose = FALSE ,
271
279
mu = NULL , # starting value of mu
272
280
linesearch = TRUE , # do a linesearch?
273
281
scaling_factor = 1.5 , # multiplicative factor for linesearch
@@ -284,7 +292,7 @@ debiasingMatrix = function(Sigma,
284
292
max_active = max(50 , 0.3 * nsample )
285
293
}
286
294
287
- p = nrow( Sigma );
295
+ p = ncol( Xinfo );
288
296
M = matrix (0 , length(rows ), p );
289
297
290
298
if (is.null(mu )) {
@@ -295,19 +303,19 @@ debiasingMatrix = function(Sigma,
295
303
xp = round(p / 10 );
296
304
idx = 1 ;
297
305
for (row in rows ) {
298
-
299
306
if ((idx %% xp )== 0 ){
300
307
xperc = xperc + 10 ;
301
308
if (verbose ) {
302
309
print(paste(xperc ," % done" ,sep = " " )); }
303
310
}
304
311
305
- output = debiasingRow(Sigma ,
312
+ output = debiasingRow(Xinfo , # could be X or t(X) %*% X / n depending on is_wide
313
+ is_wide ,
306
314
row ,
307
315
mu ,
308
316
linesearch = linesearch ,
309
317
scaling_factor = scaling_factor ,
310
- max_active = max_active ,
318
+ max_active = max_active ,
311
319
max_try = max_try ,
312
320
warn_kkt = FALSE ,
313
321
max_iter = max_iter ,
@@ -329,31 +337,32 @@ debiasingMatrix = function(Sigma,
329
337
return (M )
330
338
}
331
339
332
- # Find one row of the debiasing matrix
340
+ # Find one row of the debiasing matrix -- assuming X^TX/n is not too large -- i.e. X is tall
333
341
334
- debiasingRow = function (Sigma ,
342
+ debiasingRow = function (Xinfo , # could be X or t(X) %*% X / n depending on is_wide
343
+ is_wide ,
335
344
row ,
336
345
mu ,
337
- linesearch = TRUE , # do a linesearch?
346
+ linesearch = TRUE , # do a linesearch?
338
347
scaling_factor = 1.2 , # multiplicative factor for linesearch
339
- max_active = NULL , # how big can active set get?
348
+ max_active = NULL , # how big can active set get?
340
349
max_try = 10 , # how many steps in linesearch?
341
350
warn_kkt = FALSE , # warn if KKT does not seem to be satisfied?
342
351
max_iter = 100 , # how many iterations for each optimization problem
343
352
kkt_tol = 1.e-4 , # tolerance for the KKT conditions
344
353
objective_tol = 1.e-8 # tolerance for relative decrease in objective
345
354
) {
346
355
347
- p = nrow( Sigma )
356
+ p = ncol( Xinfo )
348
357
349
358
if (is.null(max_active )) {
350
- max_active = nrow(Sigma )
359
+ max_active = min( nrow(Xinfo ), ncol( Xinfo ) )
351
360
}
352
361
353
362
# Initialize variables
354
363
355
364
soln = rep(0 , p )
356
-
365
+ Xsoln = rep( 0 , n )
357
366
ever_active = rep(0 , p )
358
367
ever_active [1 ] = row # 1-based
359
368
ever_active = as.integer(ever_active )
@@ -371,17 +380,33 @@ debiasingRow = function (Sigma,
371
380
372
381
while (counter_idx < max_try ) {
373
382
374
- result = solve_QP(Sigma ,
375
- mu ,
376
- max_iter ,
377
- soln ,
378
- linear_func ,
379
- gradient ,
380
- ever_active ,
381
- nactive ,
382
- kkt_tol ,
383
- objective_tol ,
384
- max_active )
383
+ if (! is_wide ) {
384
+ result = solve_QP(Xinfo , # this is non-neg-def matrix
385
+ mu ,
386
+ max_iter ,
387
+ soln ,
388
+ linear_func ,
389
+ gradient ,
390
+ ever_active ,
391
+ nactive ,
392
+ kkt_tol ,
393
+ objective_tol ,
394
+ max_active )
395
+ } else {
396
+ result = solve_QP_wide(Xinfo , # this is a design matrix
397
+ mu ,
398
+ max_iter ,
399
+ soln ,
400
+ linear_func ,
401
+ gradient ,
402
+ Xsoln ,
403
+ ever_active ,
404
+ nactive ,
405
+ kkt_tol ,
406
+ objective_tol ,
407
+ max_active )
408
+
409
+ }
385
410
386
411
iter = result $ iter
387
412
@@ -439,6 +464,7 @@ debiasingRow = function (Sigma,
439
464
440
465
}
441
466
467
+
442
468
# #############################
443
469
444
470
print.fixedLassoInf <- function (x , tailarea = TRUE , ... ) {
@@ -478,4 +504,3 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
478
504
# lambda = 2*mean(apply(t(x)%*%eps,2,max))
479
505
# return(lambda)
480
506
# }
481
-
0 commit comments