Skip to content

Commit 9d54c53

Browse files
Merge remote-tracking branch 'upstream'
2 parents 96e87ab + 782290e commit 9d54c53

File tree

11 files changed

+900
-123
lines changed

11 files changed

+900
-123
lines changed

selectiveInference-currentCRAN/DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Package: selectiveInference
22
Type: Package
33
Title: Tools for Post-Selection Inference
4-
Version: 1.2.3
4+
Version: 1.2.4
55
Date: 2017-09-18
66
Author: Ryan Tibshirani, Rob Tibshirani, Jonathan Taylor,
77
Joshua Loftus, Stephen Reid
88
Maintainer: Rob Tibshirani <[email protected]>
9-
Depends: glmnet, intervals, survival
9+
Depends: glmnet, intervals, survival, R (>= 3.4.0)
1010
Suggests: Rmpfr
1111
Description: New tools for post-selection inference, for use
1212
with forward stepwise regression, least angle regression, the

selectiveInference/R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ solve_QP <- function(Sigma, bound, maxiter, theta, linear_func, gradient, ever_a
55
.Call('_selectiveInference_solve_QP', PACKAGE = 'selectiveInference', Sigma, bound, maxiter, theta, linear_func, gradient, ever_active, nactive, kkt_tol, objective_tol, max_active)
66
}
77

8+
solve_QP_wide <- function(X, bound, maxiter, theta, linear_func, gradient, X_theta, ever_active, nactive, kkt_tol, objective_tol, max_active) {
9+
.Call('_selectiveInference_solve_QP_wide', PACKAGE = 'selectiveInference', X, bound, maxiter, theta, linear_func, gradient, X_theta, ever_active, nactive, kkt_tol, objective_tol, max_active)
10+
}
11+
812
update1_ <- function(Q2, w, m, k) {
913
.Call('_selectiveInference_update1_', PACKAGE = 'selectiveInference', Q2, w, m, k)
1014
}

selectiveInference/R/funs.fixed.R

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ fixedLassoInf <- function(x, y, beta,
8282

8383
tol.coef = tol.beta * sqrt(n^2 / colSums(x^2))
8484
# print(tol.coef)
85-
vars = which(abs(beta) > tol.coef)
85+
vars = which(abs(beta) > tol.coef)
86+
# vars = abs(beta) > tol.coef
8687
# print(beta)
8788
# print(vars)
88-
if(length(vars)==0){
89+
if(sum(vars)==0){
8990
cat("Empty model",fill=T)
9091
return()
9192
}
@@ -96,8 +97,10 @@ fixedLassoInf <- function(x, y, beta,
9697
"'thresh' parameter, for a more accurate convergence."))
9798

9899
# 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)
101104
A = out$A
102105
b = out$b
103106

@@ -127,7 +130,7 @@ fixedLassoInf <- function(x, y, beta,
127130
# add additional targets for inference if provided
128131
if (!is.null(add.targets)) vars = sort(unique(c(vars,add.targets,recursive=T)))
129132

130-
k = length(vars)
133+
k = length(vars)
131134
pv = vlo = vup = numeric(k)
132135
vmat = matrix(0,k,n)
133136
ci = tailarea = matrix(0,k,2)
@@ -154,20 +157,24 @@ fixedLassoInf <- function(x, y, beta,
154157

155158
# Reorder so that active set S is first
156159
Xordered = Xint[,c(S,notS,recursive=T)]
160+
hsigmaS = 1/n*(t(XS)%*%XS) # hsigma[S,S]
161+
hsigmaSinv = solve(hsigmaS) # pinv(hsigmaS)
157162

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)))
161165

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
163167

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+
}
165177

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-
171178
M <- (((htheta%*%t(Xordered))+ithetasigma%*%FS%*%hsigmaSinv%*%t(XS))/n)
172179
# vector which is offset for testing debiased beta's
173180
null_value <- (((ithetasigma%*%FS%*%hsigmaSinv)%*%sign(hbetaS))*lambda/n)
@@ -264,10 +271,11 @@ fixedLassoPoly =
264271
## Approximates inverse covariance matrix theta
265272
## using coordinate descent
266273

267-
debiasingMatrix = function(Sigma,
274+
debiasingMatrix = function(Xinfo, # could be X or t(X) %*% X / n depending on is_wide
275+
is_wide,
268276
nsample,
269277
rows,
270-
verbose=FALSE,
278+
verbose=FALSE,
271279
mu=NULL, # starting value of mu
272280
linesearch=TRUE, # do a linesearch?
273281
scaling_factor=1.5, # multiplicative factor for linesearch
@@ -284,7 +292,7 @@ debiasingMatrix = function(Sigma,
284292
max_active = max(50, 0.3 * nsample)
285293
}
286294

287-
p = nrow(Sigma);
295+
p = ncol(Xinfo);
288296
M = matrix(0, length(rows), p);
289297

290298
if (is.null(mu)) {
@@ -295,19 +303,19 @@ debiasingMatrix = function(Sigma,
295303
xp = round(p/10);
296304
idx = 1;
297305
for (row in rows) {
298-
299306
if ((idx %% xp)==0){
300307
xperc = xperc+10;
301308
if (verbose) {
302309
print(paste(xperc,"% done",sep="")); }
303310
}
304311

305-
output = debiasingRow(Sigma,
312+
output = debiasingRow(Xinfo, # could be X or t(X) %*% X / n depending on is_wide
313+
is_wide,
306314
row,
307315
mu,
308316
linesearch=linesearch,
309317
scaling_factor=scaling_factor,
310-
max_active=max_active,
318+
max_active=max_active,
311319
max_try=max_try,
312320
warn_kkt=FALSE,
313321
max_iter=max_iter,
@@ -329,31 +337,32 @@ debiasingMatrix = function(Sigma,
329337
return(M)
330338
}
331339

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
333341

334-
debiasingRow = function (Sigma,
342+
debiasingRow = function (Xinfo, # could be X or t(X) %*% X / n depending on is_wide
343+
is_wide,
335344
row,
336345
mu,
337-
linesearch=TRUE, # do a linesearch?
346+
linesearch=TRUE, # do a linesearch?
338347
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?
340349
max_try=10, # how many steps in linesearch?
341350
warn_kkt=FALSE, # warn if KKT does not seem to be satisfied?
342351
max_iter=100, # how many iterations for each optimization problem
343352
kkt_tol=1.e-4, # tolerance for the KKT conditions
344353
objective_tol=1.e-8 # tolerance for relative decrease in objective
345354
) {
346355

347-
p = nrow(Sigma)
356+
p = ncol(Xinfo)
348357

349358
if (is.null(max_active)) {
350-
max_active = nrow(Sigma)
359+
max_active = min(nrow(Xinfo), ncol(Xinfo))
351360
}
352361

353362
# Initialize variables
354363

355364
soln = rep(0, p)
356-
365+
Xsoln = rep(0, n)
357366
ever_active = rep(0, p)
358367
ever_active[1] = row # 1-based
359368
ever_active = as.integer(ever_active)
@@ -371,17 +380,33 @@ debiasingRow = function (Sigma,
371380

372381
while (counter_idx < max_try) {
373382

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+
}
385410

386411
iter = result$iter
387412

@@ -439,6 +464,7 @@ debiasingRow = function (Sigma,
439464

440465
}
441466

467+
442468
##############################
443469

444470
print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
@@ -478,4 +504,3 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
478504
# lambda = 2*mean(apply(t(x)%*%eps,2,max))
479505
# return(lambda)
480506
#}
481-

selectiveInference/R/funs.inf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ TG.limits = function(Z, A, b, eta, Sigma=NULL) {
209209
target_estimate = sum(as.numeric(eta) * as.numeric(Z))
210210

211211
if (max(A %*% as.numeric(Z) - b) > 0) {
212-
warning('Contsraint not satisfied. A %*% Z should be elementwise less than or equal to b')
212+
warning('Constraint not satisfied. A %*% Z should be elementwise less than or equal to b')
213213
}
214214

215215
if (is.null(Sigma)) {

selectiveInference/man/debiasingMatrix.Rd

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Newton step from some consistent estimator (such as the LASSO)
1111
to find a debiased solution.
1212
}
1313
\usage{
14-
debiasingMatrix(Sigma,
14+
debiasingMatrix(Xinfo,
15+
is_wide,
1516
nsample,
1617
rows,
1718
verbose=FALSE,
@@ -26,8 +27,14 @@ debiasingMatrix(Sigma,
2627
objective_tol=1.e-8)
2728
}
2829
\arguments{
29-
\item{Sigma}{
30-
A symmetric non-negative definite matrix, often a cross-covariance matrix.
30+
\item{Xinfo}{
31+
Either a non-negative definite matrix S=t(X) %*% X / n or X itself. If
32+
is_wide is TRUE, then Xinfo should be X, otherwise it should be S.
33+
}
34+
\item{is_wide}{
35+
Are we solving for rows of the debiasing matrix assuming it is
36+
a wide matrix so that Xinfo=X and the non-negative definite
37+
matrix of interest is t(X) %*% X / nrow(X).
3138
}
3239
\item{nsample}{
3340
Number of samples used in forming the cross-covariance matrix.
@@ -101,8 +108,9 @@ set.seed(10)
101108
n = 50
102109
p = 100
103110
X = matrix(rnorm(n * p), n, p)
104-
S = t(X) \%*\% X / n
105-
M = debiasingMatrix(S, n, c(1,3,5))
106-
111+
S = t(X) %*% X / n
112+
M = debiasingMatrix(S, FALSE, n, c(1,3,5))
113+
M2 = debiasingMatrix(X, TRUE, n, c(1,3,5))
114+
max(M - M2)
107115
}
108116

selectiveInference/src/Makevars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PKG_CFLAGS= -I.
22
PKG_CPPFLAGS= -I.
33
PKG_LIBS=-L.
44

5-
$(SHLIB): Rcpp Rcpp-matrixcomps.o Rcpp-debias.o RcppExports.o quadratic_program.o
5+
$(SHLIB): Rcpp Rcpp-matrixcomps.o Rcpp-debias.o RcppExports.o quadratic_program.o quadratic_program_wide.o
66

77
clean:
88
rm -f *o

0 commit comments

Comments
 (0)