-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.R
82 lines (35 loc) · 1.29 KB
/
plot.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
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
n <- 100
p <- 4
p0 <- 2
beta <- c(rep(1,p0),rep(0,p-p0))
X <- (matrix(rnorm(n*p),nrow=n))
Y <- (X %*% beta + rnorm(n))
library(lars)
lars.out <- lars(X,Y,type="lasso",intercept=FALSE)
beta.hat <- coef(lars.out)
lambda.hat <- lars.out$lambda
# remove one variable at a time and see what happens to the lasso solution path:
beta.J.hat <- array(0,dim=c(p,p,p))
lambda.J.hat <- matrix(0,p,p)
for(j in 1:p)
{
lars.out <- lars(X[,-j],Y,type="lasso")
beta.J.hat[,-j,j] <- coef(lars.out)
lambda.J.hat[-p,j] <- lars.out$lambda
}
# Make plots of each leave-one-variable-out solution path with all-variables soluation path overlaid.
# At the same time compute the "distance" between each leave-one-variable-out solution path and the all-variables solution path (still needs to be coded).
par(mfrow=c(2,2),mar=c(0,0,0,0))
for(j in 1:p)
{
plot(NA,ylim=range(beta.J.hat,beta.hat),xlim=range(lambda.J.hat,lambda.hat),xaxt="n",yaxt="n")
for(k in 1:p)
{
#j=1;k=1
lines(beta.J.hat[,k,j]~lambda.J.hat[,j], lwd = 2, lty=3, col = "red")
lines(beta.hat[,k]~c(lambda.hat,0),lwd = 2)
xx = c(lambda.J.hat[,j], rev(c(lambda.hat,0)))
yy = c(beta.J.hat[,k,j], rev(beta.hat[,k]))
polygon(xx, yy, col = "gray", border = NULL)
}
}