forked from szcf-weiya/ESL-CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim-3-18.R
178 lines (169 loc) · 6.14 KB
/
sim-3-18.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
## ###############################################################################################################################
## R Code for the simulation of Fig. 3.18
##
## Refer to
## 1. https://esl.hohoweiya.xyz/03-Linear-Methods-for-Regression/3.6-A-Comparison-of-the-Selection-and-Shrinkage-Methods/index.html
## 2. https://esl.hohoweiya.xyz//notes/linear-reg/sim-3-18/index.html
## for more details
##
## ###############################################################################################################################
## ###############################################################################################################
## generate simulated data
## ###############################################################################################################
genXY <- function(rho = 0.5, # correlation
N = 100, # number of sample
beta = c(4, 2)) # true coefficient
{
# covariance matrix
Sigma = matrix(c(1, rho,
rho, 1), 2, 2)
library(MASS)
X = mvrnorm(N, c(0, 0), Sigma)
Y = X[, 1] * beta[1] + X[, 2] * beta[2]
return(list(X=X, Y=Y))
}
## ###############################################################################################################
## main function
## return the beta calculated by 6 methods (ols, ridge, lasso, pcr (plus mypcr which from scratch), pls, subset)
##
## ###############################################################################################################
select.vs.shrink <- function(X, Y)
{
## ############################################
## least square regressions
## ############################################
ols.fit = lm(Y ~ 0 + X)
ols.beta = coef(ols.fit)
ols.beta = as.matrix(t(ols.beta))
## ###########################################
## setting
## ###########################################
## create grid to fit lasso/ridge path
grid = 10^seq(10, -2, length = 100)
## ############################################
## lasso
## ############################################
library(glmnet)
## use cross-validation to choose the best model
## lasso.fit = cv.glmnet(X, Y, alpha = 1)
lasso.fit = glmnet(X, Y, alpha = 1, lambda = grid)
#plot(lasso.fit)
## extract beta
lasso.beta = as.matrix(lasso.fit$beta) # convert dsCMatrix to regular matrix
#plot(lasso.beta[1,], lasso.beta[2,])
lasso.beta = t(lasso.beta)
attr(lasso.beta, "dimnames") = list(NULL,
c("X1","X2"))
## ############################################
## ridge regression
## ############################################
ridge.fit = glmnet(X, Y, alpha = 0, lambda = grid)
ridge.beta = as.matrix(ridge.fit$beta) # convert dsCMatrix to regular matrix
ridge.beta = t(ridge.beta)
attr(ridge.beta, "dimnames") = list(NULL,
c("X1", "X2"))
## ############################################
## principal component regression (PCR)
## ############################################
library(pls)
pcr.fit = pcr(Y ~ X, scale = FALSE)
pcr.beta = pcr.fit$coefficients
pcr.beta = rbind(c(0, 0), pcr.beta[,,1], pcr.beta[,,2]) # c(0, 0) for zero PC
## for plot
## or write from scratch
## get PCs
pc = prcomp(X, scale = FALSE)
pc.m = pc$rotation
## scores
pc.z = pc$x
## use one pc
mypcr.fit.1 = lm(Y ~ 0+pc.z[,1])
## use two pc
mypcr.fit.2 = lm(Y ~ 0+pc.z)
## original beta
mypcr.beta.1 = coef(mypcr.fit.1) * pc.m[, 1]
mypcr.beta.2 = t(pc.m %*% coef(mypcr.fit.2))
mypcr.beta = rbind(c(0, 0), mypcr.beta.1, mypcr.beta.2)
attr(mypcr.beta, "dimnames") = list(NULL,
c("X1", "X2"))
## ############################################
## Partial Least Squares (PLS)
## ############################################
pls.fit = plsr(Y ~ X, scale = FALSE)
pls.beta = pls.fit$coefficients
pls.beta = rbind(c(0, 0), pls.beta[,,1], pls.beta[,,2])
## ############################################
## Best Subset
## ############################################
library(leaps)
bs.fit = regsubsets(x = X, y = Y, intercept = FALSE)
if (summary(bs.fit)$which[1, 1])
{
bs.beta = c(coef(bs.fit, 1), 0)
} else {
bs.beta = c(0, coef(bs.fit, 1))
}
bs.beta = rbind(c(0, 0), bs.beta, coef(bs.fit, 2))
attr(bs.beta, "dimnames") = list(NULL,
c("X1","X2"))
res = list(ols = ols.beta,
ridge = ridge.beta,
lasso = lasso.beta,
pcr = pcr.beta,
mypcr = mypcr.beta,
pls = pls.beta,
subset = bs.beta)
class(res) = "selectORshrink"
return(res)
}
## #######################################################################
## plot function
## #######################################################################
plot.selectORshrink <- function(obj, rho = 0.5)
{
plot(0, 0,
type = "n",
xlab = expression(beta[1]),
ylab = expression(beta[2]),
main = substitute(paste(rho,"=",r), list(r=rho)),
xlim = c(0, 6),
ylim = c(-1, 3))
par(lwd = 3, cex = 1)
lines(obj$ridge, col = "red")
lines(obj$lasso, col = "green")
lines(obj$pcr, col = "purple")
lines(obj$pls, col = "orange")
lines(obj$subset, col = "blue")
points(obj$ols, col = "black", pch = 16)
abline(h=0, lty = 2)
abline(v=0, lty = 2)
legend(4.8, 3,
c("Ridge", "Lasso", "PCR", "PLS", "Best Subset", "Least Squares"),
col = c("red", "green", "purple", "orange", "blue", "black"),
lty = c(1,1,1,1,1,NA),
pch =c(NA,NA,NA,NA,NA, 16),
box.col = "white",
box.lwd = 0,
bg = "transparent")
}
## ###################################################################################
## results
## ###################################################################################
## case 1
set.seed(1234)
data = genXY()
X = data$X
Y = data$Y
res1 = select.vs.shrink(X, Y)
png("res_rho_05.png", width = 640, height = 480)
plot(res1, rho = 0.5)
dev.off()
## case 2
set.seed(1234)
data2 = genXY(rho = -0.5)
X2 = data2$X
Y2 = data2$Y
res2 = select.vs.shrink(X2, Y2)
png("res_rho_-05.png", width = 640, height = 480)
plot(res2, rho = -0.5)
dev.off()