-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathlasso.R
More file actions
148 lines (120 loc) · 2.93 KB
/
lasso.R
File metadata and controls
148 lines (120 loc) · 2.93 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
#' ---
#' title: " L1 (lasso) regularization"
#' author: "Michael Clark"
#' css: '../other.css'
#' highlight: pygments
#' date: ""
#' ---
#'
#' See Tibshirani (1996) for the source, or Murphy PML (2012) for a nice
#' overview (watch for typos in depictions). A more conceptual depiction of the
#' lasso can be found in penalized_ML.R.
#'
#' # Coordinate descent
#'
#'
lasso <- function(
X, # model matrix
y, # target
lambda = .1, # penalty parameter
soft = TRUE, # soft vs. hard thresholding
tol = 1e-6, # tolerance
iter = 100, # number of max iterations
verbose = TRUE # print out iteration number
) {
# soft thresholding function
soft_thresh <- function(a, b) {
out = rep(0, length(a))
out[a > b] = a[a > b] - b
out[a < -b] = a[a < -b] + b
out
}
w = solve(crossprod(X) + diag(lambda, ncol(X))) %*% crossprod(X,y)
tol_curr = 1
J = ncol(X)
a = rep(0, J)
c_ = rep(0, J)
i = 1
while (tol < tol_curr && i < iter) {
w_old = w
a = colSums(X^2)
l = length(y)*lambda # for consistency with glmnet approach
c_ = sapply(1:J, function(j) sum( X[,j] * (y - X[,-j] %*% w_old[-j]) ))
if (soft) {
for (j in 1:J) {
w[j] = soft_thresh(c_[j]/a[j], l/a[j])
}
}
else {
w = w_old
w[c_< l & c_ > -l] = 0
}
tol_curr = crossprod(w - w_old)
i = i + 1
if (verbose && i%%10 == 0) message(i)
}
w
}
#' # Data setup
#'
#'
set.seed(8675309)
N = 500
p = 10
X = scale(matrix(rnorm(N*p), ncol=p))
b = c(.5, -.5, .25, -.25, .125, -.125, rep(0, p-6))
y = scale(X %*% b + rnorm(N, sd=.5))
lambda = .1
# debugonce(lasso)
#' Note, if `lambda=0`, result is the same as `lm.fit`.
#'
#'
result_soft = lasso(
X,
y,
lambda = lambda,
tol = 1e-12,
soft = TRUE
)
result_hard = lasso(
X,
y,
lambda = lambda,
tol = 1e-12,
soft = FALSE
)
#' `glmnet` is by default a mixture of ridge and lasso penalties, setting alpha
#' = 1 reduces to lasso (alpha=0 would be ridge). We set the lambda to a couple
#' values while only wanting the one set to the same lambda value as above (s).
library(glmnet)
glmnet_res = coef(
glmnet(
X,
y,
alpha = 1,
lambda = c(10, 1, lambda),
thresh = 1e-12,
intercept = FALSE
),
s = lambda
)
library(lassoshooting)
ls_res = lassoshooting(
X = X,
y = y,
lambda = length(y) * lambda,
thr = 1e-12
)
#' # Comparison
data.frame(
lm = coef(lm(y ~ . - 1, data.frame(X))),
lasso_soft = result_soft,
lasso_hard = result_hard,
lspack = ls_res$coef,
glmnet = glmnet_res[-1, 1],
truth = b
)
#' # Source
#' Base R source code found at https://github.com/m-clark/Miscellaneous-R-Code/blob/master/ModelFitting/lasso.R
#' for some more detailed R code, check out
#' http://jocelynchi.com/a-coordinate-descent-algorithm-for-the-lasso-problem (now defunct, but might find relevant article at the website)