@@ -6,11 +6,11 @@ fixedLassoInf <- function(x, y, beta, lambda, family=c("gaussian","binomial","co
6
6
sigma = NULL , alpha = 0.1 ,
7
7
type = c(" partial" ," full" ), tol.beta = 1e-5 , tol.kkt = 0.1 ,
8
8
gridrange = c(- 100 ,100 ), bits = NULL , verbose = FALSE ) {
9
-
9
+
10
10
family = match.arg(family )
11
11
this.call = match.call()
12
12
type = match.arg(type )
13
-
13
+
14
14
if (family == " binomial" ) {
15
15
if (type != " partial" ) stop(" Only type= partial allowed with binomial family" )
16
16
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,
20
20
else if (family == " cox" ) {
21
21
if (type != " partial" ) stop(" Only type= partial allowed with Cox family" )
22
22
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 )
24
24
return (out )
25
25
}
26
-
26
+
27
27
else {
28
28
29
-
30
-
29
+
30
+
31
31
checkargs.xy(x ,y )
32
32
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" )
34
34
checkargs.misc(beta = beta ,lambda = lambda ,sigma = sigma ,alpha = alpha ,
35
35
gridrange = gridrange ,tol.beta = tol.beta ,tol.kkt = tol.kkt )
36
36
if (! is.null(bits ) && ! requireNamespace(" Rmpfr" ,quietly = TRUE )) {
37
37
warning(" Package Rmpfr is not installed, reverting to standard precision" )
38
38
bits = NULL
39
39
}
40
-
40
+
41
41
n = nrow(x )
42
42
p = ncol(x )
43
43
beta = as.numeric(beta )
@@ -66,14 +66,14 @@ else{
66
66
" (to within specified tolerances). You might try rerunning" ,
67
67
" glmnet with a lower setting of the" ,
68
68
" 'thresh' parameter, for a more accurate convergence." ))
69
-
69
+
70
70
# Get lasso polyhedral region, of form Gy >= u
71
71
out = fixedLasso.poly(x ,y ,beta ,lambda ,vars )
72
72
G = out $ G
73
73
u = out $ u
74
74
75
75
# Check polyhedral region
76
- tol.poly = 0.01
76
+ tol.poly = 0.01
77
77
if (min(G %*% y - u ) < - tol.poly * sqrt(sum(y ^ 2 )))
78
78
stop(paste(" Polyhedral constraints not satisfied; you must recompute beta" ,
79
79
" more accurately. With glmnet, make sure to use exact=TRUE in coef()," ,
@@ -94,17 +94,17 @@ else{
94
94
" you may want to use the estimateSigma function" ))
95
95
}
96
96
}
97
-
97
+
98
98
k = length(vars )
99
- pv = vlo = vup = numeric (k )
99
+ pv = vlo = vup = numeric (k )
100
100
vmat = matrix (0 ,k ,n )
101
101
ci = tailarea = matrix (0 ,k ,2 )
102
102
sign = numeric (k )
103
103
104
104
if (type == " full" & p > n )
105
105
warning(paste(" type='full' does not make sense when p > n;" ,
106
106
" switching to type='partial'" ))
107
-
107
+
108
108
if (type == " partial" || p > n ) {
109
109
xa = x [,vars ,drop = F ]
110
110
M = pinv(crossprod(xa )) %*% t(xa )
@@ -113,17 +113,17 @@ else{
113
113
M = pinv(crossprod(x )) %*% t(x )
114
114
M = M [vars ,,drop = F ]
115
115
}
116
-
116
+
117
117
for (j in 1 : k ) {
118
118
if (verbose ) cat(sprintf(" Inference for variable %i ...\n " ,vars [j ]))
119
-
119
+
120
120
vj = M [j ,]
121
121
mj = sqrt(sum(vj ^ 2 ))
122
122
vj = vj / mj # Standardize (divide by norm of vj)
123
123
sign [j ] = sign(sum(vj * y ))
124
124
vj = sign [j ] * vj
125
125
a = poly.pval(y ,G ,u ,vj ,sigma ,bits )
126
- pv [j ] = a $ pv
126
+ pv [j ] = a $ pv
127
127
vlo [j ] = a $ vlo * mj # Unstandardize (mult by norm of vj)
128
128
vup [j ] = a $ vup * mj # Unstandardize (mult by norm of vj)
129
129
vmat [j ,] = vj * mj * sign [j ] # Unstandardize (mult by norm of vj)
@@ -133,12 +133,12 @@ else{
133
133
ci [j ,] = a $ int * mj # Unstandardize (mult by norm of vj)
134
134
tailarea [j ,] = a $ tailarea
135
135
}
136
-
136
+
137
137
out = list (type = type ,lambda = lambda ,pv = pv ,ci = ci ,
138
138
tailarea = tailarea ,vlo = vlo ,vup = vup ,vmat = vmat ,y = y ,
139
139
vars = vars ,sign = sign ,sigma = sigma ,alpha = alpha ,
140
140
sd = sigma * sqrt(rowSums(vmat ^ 2 )),
141
- coef0 = vmat %*% y ,
141
+ coef0 = vmat %*% y ,
142
142
call = this.call )
143
143
class(out ) = " fixedLassoInf"
144
144
return (out )
@@ -160,7 +160,7 @@ function(x, y, beta, lambda, a) {
160
160
161
161
P = diag(1 ,nrow(xa )) - xa %*% xap
162
162
# NOTE: inactive constraints not needed below!
163
-
163
+
164
164
G = - rbind(
165
165
# 1/lambda * t(xac) %*% P,
166
166
# -1/lambda * t(xac) %*% P,
@@ -175,17 +175,6 @@ function(x, y, beta, lambda, a) {
175
175
176
176
return (list (G = G ,u = u ))
177
177
}
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
- }
189
178
190
179
# #############################
191
180
@@ -195,7 +184,7 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
195
184
196
185
cat(sprintf(" \n Standard deviation of noise (specified or estimated) sigma = %0.3f\n " ,
197
186
x $ sigma ))
198
-
187
+
199
188
cat(sprintf(" \n Testing results at lambda = %0.3f, with alpha = %0.3f\n " ,x $ lambda ,x $ alpha ))
200
189
cat(" " ,fill = T )
201
190
tab = cbind(x $ vars ,
@@ -209,7 +198,7 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
209
198
}
210
199
rownames(tab ) = rep(" " ,nrow(tab ))
211
200
print(tab )
212
-
201
+
213
202
cat(sprintf(" \n Note: coefficients shown are %s regression coefficients\n " ,
214
203
ifelse(x $ type == " partial" ," partial" ," full" )))
215
204
invisible ()
@@ -220,10 +209,10 @@ print.fixedLassoInf <- function(x, tailarea=TRUE, ...) {
220
209
# if(nsamp < 10) stop("More Monte Carlo samples required for estimation")
221
210
# if (length(sigma)!=1) stop("sigma should be a number > 0")
222
211
# if (sigma<=0) stop("sigma should be a number > 0")
223
-
212
+
224
213
# n = nrow(x)
225
214
# eps = sigma*matrix(rnorm(nsamp*n),n,nsamp)
226
215
# lambda = 2*mean(apply(t(x)%*%eps,2,max))
227
216
# return(lambda)
228
217
# }
229
-
218
+
0 commit comments