Skip to content

Commit cb3ef4a

Browse files
author
Joshua Loftus
committed
Moving some common functions
1 parent 74e3c48 commit cb3ef4a

File tree

3 files changed

+115
-115
lines changed

3 files changed

+115
-115
lines changed

selectiveInference/R/funs.common.R

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ standardize <- function(x, y, intercept, normalize) {
3232
y = as.numeric(y)
3333
n = nrow(x)
3434
p = ncol(x)
35-
35+
3636
if (intercept) {
3737
bx = colMeans(x)
3838
by = mean(y)
@@ -57,10 +57,10 @@ standardize <- function(x, y, intercept, normalize) {
5757
# Interpolation function to get coefficients
5858

5959
coef.interpolate <- function(betas, s, knots, dec=TRUE) {
60-
# Sort the s values
60+
# Sort the s values
6161
o = order(s,dec=dec)
6262
s = s[o]
63-
63+
6464
k = length(s)
6565
mat = matrix(rep(knots,each=k),nrow=k)
6666
if (dec) b = s >= mat
@@ -72,7 +72,7 @@ coef.interpolate <- function(betas, s, knots, dec=TRUE) {
7272
p = numeric(k)
7373
p[i] = 0
7474
p[!i] = ((s-knots[blo])/(knots[bhi]-knots[blo]))[!i]
75-
75+
7676
beta = t((1-p)*t(betas[,blo,drop=FALSE]) + p*t(betas[,bhi,drop=FALSE]))
7777
colnames(beta) = as.character(round(s,3))
7878
rownames(beta) = NULL
@@ -100,7 +100,7 @@ checkargs.misc <- function(sigma=NULL, alpha=NULL, k=NULL,
100100
mult=NULL, ntimes=NULL,
101101
beta=NULL, lambda=NULL, tol.beta=NULL, tol.kkt=NULL,
102102
bh.q=NULL) {
103-
103+
104104
if (!is.null(sigma) && sigma <= 0) stop("sigma must be > 0")
105105
if (!is.null(lambda) && lambda < 0) stop("lambda must be >= 0")
106106
if (!is.null(alpha) && (alpha <= 0 || alpha >= 1)) stop("alpha must be between 0 and 1")
@@ -144,3 +144,43 @@ estimateSigma <- function(x, y, intercept=TRUE, standardize=TRUE) {
144144
return(list(sigmahat=sigma, df=nz))
145145
}
146146

147+
# Update the QR factorization, after a column has been
148+
# added. Here Q1 is m x n, Q2 is m x k, and R is n x n.
149+
150+
updateQR <- function(Q1,Q2,R,col) {
151+
m = nrow(Q1)
152+
n = ncol(Q1)
153+
k = ncol(Q2)
154+
155+
a = .C("update1",
156+
Q2=as.double(Q2),
157+
w=as.double(t(Q2)%*%col),
158+
m=as.integer(m),
159+
k=as.integer(k),
160+
dup=FALSE,
161+
package="selectiveInference")
162+
163+
Q2 = matrix(a$Q2,nrow=m)
164+
w = c(t(Q1)%*%col,a$w)
165+
166+
# Re-structure: delete a column from Q2, add one to
167+
# Q1, and expand R
168+
Q1 = cbind(Q1,Q2[,1])
169+
Q2 = Q2[,-1,drop=FALSE]
170+
R = rbind(R,rep(0,n))
171+
R = cbind(R,w[Seq(1,n+1)])
172+
173+
return(list(Q1=Q1,Q2=Q2,R=R))
174+
}
175+
176+
# Moore-Penrose pseudo inverse for symmetric matrices
177+
178+
pinv <- function(A, tol=.Machine$double.eps) {
179+
e = eigen(A)
180+
v = Re(e$vec)
181+
d = Re(e$val)
182+
d[d > tol] = 1/d[d > tol]
183+
d[d < tol] = 0
184+
if (length(d)==1) return(v*d*v)
185+
else return(v %*% diag(d) %*% t(v))
186+
}

selectiveInference/R/funs.fixed.R

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ fixedLassoInf <- function(x, y, beta, lambda, family=c("gaussian","binomial","co
66
sigma=NULL, alpha=0.1,
77
type=c("partial","full"), tol.beta=1e-5, tol.kkt=0.1,
88
gridrange=c(-100,100), bits=NULL, verbose=FALSE) {
9-
9+
1010
family = match.arg(family)
1111
this.call = match.call()
1212
type = match.arg(type)
13-
13+
1414
if(family=="binomial") {
1515
if(type!="partial") stop("Only type= partial allowed with binomial family")
1616
out=fixedLogitLassoInf(x,y,beta,lambda,alpha=alpha, type="partial", tol.beta=tol.beta, tol.kkt=tol.kkt,
@@ -20,24 +20,24 @@ sigma=NULL, alpha=0.1,
2020
else if(family=="cox") {
2121
if(type!="partial") stop("Only type= partial allowed with Cox family")
2222
out=fixedCoxLassoInf(x,y,status,beta,lambda,alpha=alpha, type="partial",tol.beta=tol.beta,
23-
tol.kkt=tol.kkt, gridrange=gridrange, bits=bits, verbose=verbose,this.call=this.call)
23+
tol.kkt=tol.kkt, gridrange=gridrange, bits=bits, verbose=verbose,this.call=this.call)
2424
return(out)
2525
}
26-
26+
2727
else{
2828

29-
30-
29+
30+
3131
checkargs.xy(x,y)
3232
if (missing(beta) || is.null(beta)) stop("Must supply the solution beta")
33-
if (missing(lambda) || is.null(lambda)) stop("Must supply the tuning parameter value lambda")
33+
if (missing(lambda) || is.null(lambda)) stop("Must supply the tuning parameter value lambda")
3434
checkargs.misc(beta=beta,lambda=lambda,sigma=sigma,alpha=alpha,
3535
gridrange=gridrange,tol.beta=tol.beta,tol.kkt=tol.kkt)
3636
if (!is.null(bits) && !requireNamespace("Rmpfr",quietly=TRUE)) {
3737
warning("Package Rmpfr is not installed, reverting to standard precision")
3838
bits = NULL
3939
}
40-
40+
4141
n = nrow(x)
4242
p = ncol(x)
4343
beta = as.numeric(beta)
@@ -66,14 +66,14 @@ else{
6666
"(to within specified tolerances). You might try rerunning",
6767
"glmnet with a lower setting of the",
6868
"'thresh' parameter, for a more accurate convergence."))
69-
69+
7070
# Get lasso polyhedral region, of form Gy >= u
7171
out = fixedLasso.poly(x,y,beta,lambda,vars)
7272
G = out$G
7373
u = out$u
7474

7575
# Check polyhedral region
76-
tol.poly = 0.01
76+
tol.poly = 0.01
7777
if (min(G %*% y - u) < -tol.poly * sqrt(sum(y^2)))
7878
stop(paste("Polyhedral constraints not satisfied; you must recompute beta",
7979
"more accurately. With glmnet, make sure to use exact=TRUE in coef(),",
@@ -94,17 +94,17 @@ else{
9494
"you may want to use the estimateSigma function"))
9595
}
9696
}
97-
97+
9898
k = length(vars)
99-
pv = vlo = vup = numeric(k)
99+
pv = vlo = vup = numeric(k)
100100
vmat = matrix(0,k,n)
101101
ci = tailarea = matrix(0,k,2)
102102
sign = numeric(k)
103103

104104
if (type=="full" & p > n)
105105
warning(paste("type='full' does not make sense when p > n;",
106106
"switching to type='partial'"))
107-
107+
108108
if (type=="partial" || p > n) {
109109
xa = x[,vars,drop=F]
110110
M = pinv(crossprod(xa)) %*% t(xa)
@@ -113,17 +113,17 @@ else{
113113
M = pinv(crossprod(x)) %*% t(x)
114114
M = M[vars,,drop=F]
115115
}
116-
116+
117117
for (j in 1:k) {
118118
if (verbose) cat(sprintf("Inference for variable %i ...\n",vars[j]))
119-
119+
120120
vj = M[j,]
121121
mj = sqrt(sum(vj^2))
122122
vj = vj / mj # Standardize (divide by norm of vj)
123123
sign[j] = sign(sum(vj*y))
124124
vj = sign[j] * vj
125125
a = poly.pval(y,G,u,vj,sigma,bits)
126-
pv[j] = a$pv
126+
pv[j] = a$pv
127127
vlo[j] = a$vlo * mj # Unstandardize (mult by norm of vj)
128128
vup[j] = a$vup * mj # Unstandardize (mult by norm of vj)
129129
vmat[j,] = vj * mj * sign[j] # Unstandardize (mult by norm of vj)
@@ -133,12 +133,12 @@ else{
133133
ci[j,] = a$int * mj # Unstandardize (mult by norm of vj)
134134
tailarea[j,] = a$tailarea
135135
}
136-
136+
137137
out = list(type=type,lambda=lambda,pv=pv,ci=ci,
138138
tailarea=tailarea,vlo=vlo,vup=vup,vmat=vmat,y=y,
139139
vars=vars,sign=sign,sigma=sigma,alpha=alpha,
140140
sd=sigma*sqrt(rowSums(vmat^2)),
141-
coef0=vmat%*%y,
141+
coef0=vmat%*%y,
142142
call=this.call)
143143
class(out) = "fixedLassoInf"
144144
return(out)
@@ -160,7 +160,7 @@ function(x, y, beta, lambda, a) {
160160

161161
P = diag(1,nrow(xa)) - xa %*% xap
162162
#NOTE: inactive constraints not needed below!
163-
163+
164164
G = -rbind(
165165
# 1/lambda * t(xac) %*% P,
166166
# -1/lambda * t(xac) %*% P,
@@ -175,17 +175,6 @@ function(x, y, beta, lambda, a) {
175175

176176
return(list(G=G,u=u))
177177
}
178-
# Moore-Penrose pseudo inverse for symmetric matrices
179-
180-
pinv <- function(A, tol=.Machine$double.eps) {
181-
e = eigen(A)
182-
v = Re(e$vec)
183-
d = Re(e$val)
184-
d[d > tol] = 1/d[d > tol]
185-
d[d < tol] = 0
186-
if (length(d)==1) return(v*d*v)
187-
else return(v %*% diag(d) %*% t(v))
188-
}
189178

190179
##############################
191180

@@ -195,7 +184,7 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
195184

196185
cat(sprintf("\nStandard deviation of noise (specified or estimated) sigma = %0.3f\n",
197186
x$sigma))
198-
187+
199188
cat(sprintf("\nTesting results at lambda = %0.3f, with alpha = %0.3f\n",x$lambda,x$alpha))
200189
cat("",fill=T)
201190
tab = cbind(x$vars,
@@ -209,7 +198,7 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
209198
}
210199
rownames(tab) = rep("",nrow(tab))
211200
print(tab)
212-
201+
213202
cat(sprintf("\nNote: coefficients shown are %s regression coefficients\n",
214203
ifelse(x$type=="partial","partial","full")))
215204
invisible()
@@ -220,10 +209,10 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
220209
# if(nsamp < 10) stop("More Monte Carlo samples required for estimation")
221210
# if (length(sigma)!=1) stop("sigma should be a number > 0")
222211
# if (sigma<=0) stop("sigma should be a number > 0")
223-
212+
224213
# n = nrow(x)
225214
# eps = sigma*matrix(rnorm(nsamp*n),n,nsamp)
226215
# lambda = 2*mean(apply(t(x)%*%eps,2,max))
227216
# return(lambda)
228217
#}
229-
218+

0 commit comments

Comments
 (0)