-
Notifications
You must be signed in to change notification settings - Fork 2
/
Example1.R
56 lines (42 loc) · 1.64 KB
/
Example1.R
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
#############################################################
# Generation of data 1 for Example 1 (beta0 = 0, p = 2, X is scaled)
#############################################################
n = 48 # sample size
beta = c(1, 0.5) # true coefficients vector
p = length(beta)
sigma = 0.4 # noise standard deviation
library(mnormt)
set.seed(234865) # set seed
# Generate matrix of covariates
X1 = c(rep(1, n/2), rep(-1, n/2))
X2 = c(rep(2, 6), rep(-2, 6), rep(0, n - 12))
# Verify has the right scaling
crossprod(X1)/n # should be 1
crossprod(X2)/n # should be 1
X = cbind(X1, X2)
# Generate response Y
Y = X %*% beta + sigma * rnorm(n)
# Coordinate descent implementation for the case p = 2
#############################################################
# Helper functions source
source("LassoFunctions.R")
niter = 50 # fixed number of iterations (for simplicity)
lambda = 0 # tuning parameter
beta_start = rep(0, 2) # starting point
# Apply coordinate descent
out = coordinateLasso(X, Y, beta_start, lambda, niter = niter)
plot(0:niter, out$fobj_vec)
# [ToDo] Feel free to see how the results change with different set of parameters below
###################################################################################
# Second set of parameters
lambda = 0.5 # tuning parameter
beta_start = rep(2, 2) # starting point
# Apply coordinate descent
out = coordinateLasso(X, Y, beta_start, lambda, niter = niter)
plot(0:niter, out$fobj_vec)
# Third set of parameters
lambda = 1 # tuning parameter
beta_start = rep(0, 2) # starting point 1
# Apply coordinate descent
out = coordinateLasso(X, Y, beta_start, lambda, niter = niter)
plot(0:niter, out$fobj_vec)