-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_sparse_pca.R
More file actions
281 lines (246 loc) · 8.01 KB
/
Copy pathexample_sparse_pca.R
File metadata and controls
281 lines (246 loc) · 8.01 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#' Implémentation de l'ACP sparse
#'
#' Algorithme d'après Zou et al. 2006
#'
#' Dépendances de ce script :
#' - utils.R => pré-traitement des données génomiques
#' - functions.R => implémentation de l'ACP sparse
#'
#' Ce script génère les figures du rapport :
#' - simulation de données d'ACP
#' - exemple sur des données génomiques
#'
#' @author R. Bordas A. Hollands
#'
library(elasticnet)
library(glmnet)
library(mvtnorm)
library(tidyverse)
library(FactoMineR)
library(factoextra)
colors <- c(
PCA = "#FDAE61",
SPCA = "#ABDDA4",
spca.crit2 = "#3288BD",
norm = "#ABDDA4",
sparse = "#3288BD"
)
source("utils.R")
source("functions.R")
##### simulations - inspiré de Chavent & Chavent (2020)
eigenval <- c(200, 100, 50, 50, 6, 5, 4, 3, 2, 1)
# True loading matrix
v1 <- c(1, 2, 0.5, 1, 0, 0, 0, 0, 0.9, 0.9)
v2 <- c(0, 0, 0, 0, 1, 4, 0.75, 1, -0.3, 0.3)
v1 <- v1 / norm(v1, type = "2")
v2 <- v2 / norm(v2, type = "2")
sim.PCA.data <- function(eigvects, eigvals, n_, p_)
{
V_star <- matrix(0L, nrow = p_, ncol = p_)
m <- length(eigvects)
for (j in 1:m)
V_star[, j] <- eigvects[[j]]
set.seed(42)
V_star[, (m + 1):p_] <- matrix(runif(p_ * (p_ - m)), nrow = p_, ncol = p_ - m)
# matrice orthogonale selon Gram-Schmidt (i.e. décomposition QR)
V <- qr.Q(qr(V_star))
V <- round(V, digits = 12) # pour éviter erreurs d'arrondis
C <- V %*% diag(eigvals) %*% t(V) / n_
print(dim(C))
set.seed(42)
A <- mvtnorm::rmvnorm(n_,sigma=C)
return(list(
A = A,
Vtrue = V
))
}
sim.data <- sim.PCA.data(list(v1, v2), eigenval, n_ = 100, p_ = 10)
print(dim(sim.data$A))
xtable(sim.data$Vtrue[, 1:2], digits = 3)
sim.pca.res <- PCA(sim.data$A, ncp = 2)
print(sim.pca.res$svd$V[, 1:2])
xtable(sim.pca.res$svd$V[, 1:2], digits = 3)
spca.sim <- standard.spca(
sim.data$A,
k = 2,
lambda = c(0.1, 0.1),
stop.criteria = "norm",
max_iteration = 200,
alpha = 0.5
)
print(sim.data$V[, 1:2])
print(spca.sim$B)
xtable(spca.sim$B, digits = 3)
##### données génomiques (Golub et al. 1999) #####
data.train <- load_gene_data(
filename = "../data/all_aml/all_aml_train.csv",
output_name = "../data/all_aml/all_aml_train_preprocessed.csv"
)
print(dim(data.train$data)) # 38 x 7129
data.test <- load_gene_data(
filename = "../data/all_aml/all_aml_test.csv",
output_name = "../data/all_aml/all_aml_test_preprocessed.csv"
)
print(dim(data.test$data)) # 35 x 7129
##### LARS-EN pour trouver des lambdas_1j optimaux sur les données génomiques
pca.1 <- PCA(X_train, ncp = 37)
V <- pca.1$svd$V
lambdas <- list()
for (i in 1:5) {
# LARS-EN implémenté dans elasticnet::enet
mdl.enet <- enet(V[, -i], V[, i], lambda = 4, trace = FALSE, intercept = TRUE)
candidates <- mdl.enet$L1norm[mdl.enet$L1norm > 0]
lambdas[[i]] <- list(c(min(candidates), max(candidates)))
}
lambdas
###### SPCA avec le critère d'arrêt de la norme de A et B
# using lambda_1,j from LARS-EN optim
start_time <- Sys.time()
testing.spca1 <- standard.spca(
X_train,
k = 3,
# correcting for the way glmnet defines penalties :
# lambda = c(0.07813366, 0.02340993, 0.05277198) / 0.5,
lambda = c(0.44479509, 0.75967155, 0.36226425),
stop.criteria = "norm",
max_iteration = 200,
alpha = 0.5
)
end_time <- Sys.time()
print(end_time - start_time)
pev.1 <- compute.pev(testing.spca1$B, X_train)
print(pev1)
print(colSums(testing.spca1$B != 0))
pca.1 <- PCA(X_train, ncp = 6)
print(pca.1$eig[1:3, "percentage of variance"])
print(sum(pca.1$eig[1:3, "percentage of variance"]))
print(sum(pca.1$eig[1:2, "percentage of variance"]))
res.df <- data.frame(
"method" = c("PCA", "SPCA"),
"total_var" = c(sum(pca.1$eig[1:3, "percentage of variance"]), sum(pev))
)
res.df.per_pc <- data.frame(
"method" = as.factor(c(rep("PCA", 3), rep("SPCA", 3))),
"pc" = as.factor(c(rep(c("PC1", "PC2", "PC3"), 2))),
"var" = c(pca.1$eig[1:3, "percentage of variance"], pev1 * 100)
)
print(res.df.per_pc)
res.df.per_pc %>%
ggplot(aes(x = pc, y = var, fill = method)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_text(aes(label = round(var, 2)),vjust = 1.5,
position = position_dodge(.9), size = 2) +
labs(x = "principal components", y = "explained variance (%)") +
scale_fill_manual(values = colors) +
theme_classic() +
theme(text = element_text(size = 8))
ggsave(
"pev_spca_pca.pdf",
path ="figures",
width=3,
height=1.5
)
data_to_plot <- data.frame(X_train %*% testing.spca1$B, Y_labels = as.factor(y_train))
data_to_plot$Y_labels <- factor(reduced_data$Y, levels = c(0, 1), labels = c("ALL", "AML"))
fig <- plot_ly(data_to_plot, x = ~X1, y = ~X2, z = ~X3, color = ~Y_labels,
type = "scatter3d")
fig <- fig %>% layout(scene = list(xaxis = list(title = 'PC1'),
yaxis = list(title = 'PC2'),
zaxis = list(title = 'PC3')))
fig
###### ACP sparse avec le critère d'arrêt sparse (voir elasticnet package)
# using lambda_1,j from LARS-EN optim
testing.spca1.crit <- standard.spca(
X_train,
k = 3,
# lambda = c(0.5, 0.35, 0.85),
# correcting for the way glmnet defines penalties :
# lambda = c(0.07813366, 0.02340993, 0.05277198) / 0.5,
lambda = c(0.44479509, 0.75967155, 0.36226425),
stop.criteria = "other.stopping",
max_iteration = 200,
alpha = 0.5
)
svdobj <- svd(X_train)
totalvariance <- sum((svdobj$d) ^ 2)
u <- X_train %*% testing.spca1.crit$B
R <- qr.R(qr(u))
pev2 <- diag(R ^ 2) / totalvariance
print(pev2)
print(colSums(testing.spca1.crit$B != 0))
data_to_plot <- data.frame(X_train %*% testing.spca1.crit$B, Y_labels = as.factor(y_train))
fig <- plot_ly(data_to_plot, x = ~X1, y = ~X2, z = ~X3, color = ~Y_labels,
type = "scatter3d")
fig <- fig %>% layout(scene = list(xaxis = list(title = 'PC1'),
yaxis = list(title = 'PC2'),
zaxis = list(title = 'PC3')))
fig
res.sparse.per_pc <- data.frame(
"stopping_criteria" = as.factor(c(rep("norm", 3), rep("sparse", 3))),
"pc" = as.factor(c(rep(c("PC1", "PC2", "PC3"), 2))),
"var" = c(pev1 * 100, pev2 * 100),
"sparsity_lvl" = c(
colSums(testing.spca1$B != 0),
colSums(testing.spca1.crit$B != 0)
)
)
print(res.sparse.per_pc)
res.sparse.per_pc %>%
ggplot(aes(x = pc, y = sparsity_lvl, fill = stopping_criteria)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(x = "principal components", y = "non-zero coordinates", fill = "stopping crit.") +
scale_fill_manual(values = colors) +
theme_classic() +
theme(text = element_text(size = 8))
ggsave(
"sparsity_counts_spca.pdf",
path ="figures",
width=3,
height=1.5
)
res.sparse.per_pc %>%
ggplot(aes(x = pc, y = var, fill = stopping_criteria)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_text(aes(label = round(var, 2)),vjust = 1.5,
position = position_dodge(.9), size = 2) +
labs(x = "principal components", y = "explained variance (%)", fill = "stopping crit.") +
scale_fill_manual(values = colors) +
theme_classic() +
theme(text = element_text(size = 8))
ggsave(
"variance_stopping_crit_spca.pdf",
path ="figures",
width=3,
height=1.5
)
##### ACP sparse vs ACP sparse pour données de puces à ADN
sparsity_lvls <- matrix(0L, nrow = 8, ncol = 3)
alphas <- c(0.5, 0.2, 0.1, 0.01, 0.001, 1e-4, 1e-5, 1e-6)
for (i in 1:8) {
testing.spca1.crit <- standard.spca(
X_train,
k = 3,
# correcting for the way glmnet defines penalties :
lambda = c(0.07813366, 0.02340993, 0.05277198) / 0.5,
stop.criteria = "norm",
max_iteration = 200,
alpha = alphas[i]
)
sparsity_lvls[i, ] <- colSums(testing.spca1.crit$B != 0)
}
pdf(file = "figures/sparsity_lvls_microarray_custom_code.pdf", width = 4, height = 3)
par(mar = c(4, 5, 1, 1))
plot(
1 - alphas,
sparsity_lvls[, 1] / 7129,
log = "x",
lty = 2,
type = "b",
xlab = TeX(r"($\alpha$)"),
ylab = TeX(r"(|$\beta_{1}|_0$ / p)"),
ylim = c(0, 1)
)
abline(v = 0.5, col = "red", lty = 1)
#arrows(0.65, 0.4, 0.75)
# text(x = c(0.7), y = 0.3, TeX(r"($\alpha \rightarrow 1$)"), adj = c(0.5, 0.5))
dev.off()