forked from theresascharl/RNAseq_clustering
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_study.R
More file actions
389 lines (311 loc) · 12 KB
/
Copy pathsimulation_study.R
File metadata and controls
389 lines (311 loc) · 12 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
## load packages ----
library("matrixNormal")
library("MatTransMix") # model-based three-way clustering
library("mclust") # model-based two-way clustering
library("cluster") # clustering algorithms
library("factoextra") # clustering algorithms & visualization
library("e1071") # adjusted Rand index
library("abind")
source("functions_cluster_postprocessing.R")
source("Mat.EM.R")
## Step 1 - generate data ----
## Generate 100 datasets using cluster mean, covariance and size
## from the fission three-way cluster solution with AR1 column-wise
## covariance matrix
## load parameters for simulated data ---
load("fission_ALR_G-VVI-VAR1_1to20.RData")
M1 <- fit_ar1[[10]]
mu <- M1$Mu # means
sigma <- M1$Sigma # covariance between experiments U
psi <- M1$Psi # covariance between time points V
size <- table(M1$id) # cluster size
## generate 100 artificial data sets
set.seed(123)
for (d in 1:100) {
FILE <- paste0("data", d, ".RData")
if (!file.exists(FILE)) {
X1 <- lapply(seq_along(size), function(i) {
replicate(size[i], matrixNormal::rmatnorm(M = mu[,,i],
U = sigma[,,i],
V = psi[,,i]))
})
X1 <- do.call("abind", X1)
save(X1, file = FILE)
}
}
## Step 2 - three-way clustering ----
## by model-based clustering using MatTransMix
## initialize result ---
BIC_ID <- matrix(ncol = 100, nrow = sum(size))
ICL_ID <- matrix(ncol = 100, nrow = sum(size))
BIC_AR1_ID <- matrix(ncol = 100, nrow = sum(size))
ICL_AR1_ID <- matrix(ncol = 100, nrow = sum(size))
FILE_BIC <- "BIC_ID_threeway.RData"
FILE_ICL <- "ICL_ID_threeway.RData"
FILE_AR1_BIC <- "BIC_AR1_ID_threeway.RData"
FILE_AR1_ICL <- "ICL_AR1_ID_threeway.RData"
runs <- 1:100
if (file.exists(FILE_BIC) & file.exists(FILE_ICL) &
file.exists(FILE_AR1_BIC) & file.exists(FILE_AR1_ICL) ) {
load(FILE_BIC)
load(FILE_ICL)
load(FILE_AR1_BIC)
load(FILE_AR1_ICL)
already_done_bic <- which(colSums(is.na(BIC_ID)) == 0)
already_done_icl <- which(colSums(is.na(ICL_ID)) == 0)
already_done <- intersect(already_done_bic, already_done_icl)
runs <- setdiff(runs, already_done)
}
for (d in runs) {
cat(paste("Dataset", d, "\n"))
load(paste0("data", d, ".RData"))
M <- vector("list", 20)
for (i in 1:20) {
cat("i =", i, "\n")
set.seed(123)
init <- MatTrans.init(X1, K = i, n.start = 10)
M[[i]] <- MatTrans.EM(X1, initial = init, model = "G-VVI-VV",
row.skew = TRUE, col.skew = TRUE,
trans = "None", silent = TRUE, size.control = 10)
}
# select k based on BIC ---
Mbic <- vapply(M, function(x) x$best.bic, numeric(1))
mbic <- which.min(Mbic)
cat("For dataset", d, "the minimum BIC is", mbic, ".")
# select k based on ILC ---
my_icl <- vapply(M, function(x) tryCatch(icl_M(x), error = function(e) NA_real_),
numeric(1))
micl <- which.min(as.numeric(my_icl))
cat("For dataset", d, "the minimum ICL is", micl, ".")
BIC_ID[, d] <- M[[mbic]]$best.result[[1]]$id
save(BIC_ID, file = FILE_BIC)
ICL_ID[, d] <- M[[micl]]$best.result[[1]]$id
save(ICL_ID, file = FILE_ICL)
fit_ar1 <- vector("list", 20)
control <- list(verbose = 1000, iter.max = 1000)
for (g in 1:20) {
cat("g = ", g, "\n")
if (length(M[[g]]$result) > 0) {
initial <- M[[g]]$result[[1]]
fit_ar1[[g]] <- Mat.EM(X1, initial,
row_model = "diag",
col_model = "ar1",
control = control)
}
}
BIC <- data.frame(model = "AR1", G = 1:20, BIC = vapply(fit_ar1, function(x)
ifelse(is.null(x$bic), NA_real_, x$bic), numeric(1)))
# no results for more than 14 components
BIC_AR1_ID[, d] <- fit_ar1[[which.min(BIC[1:14,3])]]$id
save(BIC_AR1_ID, file = FILE_AR1_BIC)
# select k based on ILC ----
icl_M <- function(object, ...)
{
z <- object$best.result[[1]]$gamma
n <- nrow(z)
if (is.null(z)) {
z <- matrix(1, nrow = n, ncol = 1)
}
C <- matrix(0, n, ncol(z))
for (i in 1:n) {
C[i, which.max(z[i,])] <- 1
}
object$best.bic + 2 * sum(C * ifelse(z > 0, log(z), 0))
}
getICL <- function(x) {
if (is.null(x$bic)) {
return(NA_real_)
}
z <- x$gamma
n <- nrow(z)
if (is.null(z)) {
z <- matrix(1, nrow = n, ncol = 1)
}
C <- matrix(0, n, ncol(z))
for (i in 1:n) {
C[i, which.max(z[i,])] <- 1
}
x$bic + 2 * sum(C * ifelse(z > 0, log(z), 0))
}
ICL <- data.frame(model = "AR1", G = 1:20,
ICL = vapply(fit_ar1, getICL, numeric(1)))
ICL_AR1_ID[, d] <- fit_ar1[[which.min(ICL[1:14,3])]]$id
save(ICL_AR1_ID, file = FILE_AR1_ICL)
}
## Step 3 - two-way clustering ----
## by model-based clustering using mclust
## initialize result ---
BIC_ID <- matrix(ncol = 100, nrow = 769)
ICL_ID <- matrix(ncol = 100, nrow = 769)
for (d in 1:100) {
cat(paste("Dataset", d, "\n"))
load(paste0("data", d, ".RData"))
data1 <- t(rbind(X1[1,,], X1[2,,]))
## only allow VVV ---
set.seed(123)
m_VVV <- Mclust(data = data1, G = 1:20, modelNames = "VVV")
kbic <- m_VVV$G
cat("For dataset", d, "the minimum BIC is", kbic, ".")
## select the number of clusters by ICL
Mm <- vector("list", 20)
set.seed(123)
for (i in 1:20) {
Mm[[i]] <- Mclust(data = data1, G = i, modelNames = "VVV")
}
# no results for more than 14 clusters
Micl <- vapply(Mm, function(x) tryCatch(icl(x), error = function(e) NA_real_),
numeric(1))
kicl <- which.max(Micl)
cat("For dataset", d, "the minimum ICL is", kicl, ".")
set.seed(123)
m_VVV <- Mclust(data = data1, G = kbic, modelNames = "VVV")
BIC_ID[, d] <- m_VVV$classification
save(BIC_ID, file = "BIC_ID_twoway.RData")
set.seed(123)
m_VVV <- Mclust(data = data1, G = kicl, modelNames = "VVV")
ICL_ID[, d] <- m_VVV$classification
save(ICL_ID, file = "ICL_ID_twoway.RData")
}
## Step 4 - kmeans ----
## select k using average silhouette method
## and the true number of clusters 10
## initialize result ---
SIL_ID <- matrix(ncol = 100, nrow = 769)
TEN_ID <- matrix(ncol = 100, nrow = 769)
for (z in 1:100) {
cat(paste("Dataset", z, "\n"))
load(paste0("data", z, ".RData"))
data1 <- t(rbind(X1[1,,], X1[2,,]))
## find k using silhouette method
set.seed(123)
res <- fviz_nbclust(data1, kmeans, k.max = 20,
method = "silhouette")
silk <- which.max(res$data$y)
set.seed(123)
km_sil <- kmeans(data1, centers=silk, iter.max = 100, nstart = 10)
print(table(km_sil$cluster))
## use 10 clusters
set.seed(123)
km_10 <- kmeans(data1, centers = 10, iter.max = 100, nstart = 10)
SIL_ID[, z] <- km_sil$cluster
save(SIL_ID, file = "kmeans_silhouette.RData")
TEN_ID[, z] <- km_10$cluster
save(TEN_ID, file = "kmeans_ten.RData")
}
## Step 5 - Summary ----
## true cluster membership by design
true <- rep(1:length(size), size)
## initialize
result <- matrix(NA_real_, nrow = 800, ncol = 5)
result <- as.data.frame(result)
colnames(result) <- c("dataset", "cluster.method", "nclus.method", "crand", "K")
result$dataset <- rep(1:100, 8)
result$cluster.method <- rep(c("3-way", "3-way AR1", "2-way", "kmeans"),
each = 200)
result$cluster.method <- ordered(result$cluster.method,
levels = c("3-way", "3-way AR1", "2-way",
"kmeans"))
result$nclus.method <- rep(c("BIC", "ICL", "BIC", "ICL", "BIC", "ICL",
"Silhouette", "Fixed.10"), each = 100)
result$nclus.method <- ordered(result$nclus.method,
levels = c("BIC", "ICL", "Silhouette", "Fixed.10"))
## load three-way BIC results MatTransMix
load("BIC_ID_threeway.RData")
res <- vapply(seq_len(ncol(BIC_ID)), function(i)
compareMatchedClasses(true, BIC_ID[,i])$crand,
numeric(1))
result$crand[1:100] <- res
result$K[1:100] <- apply(BIC_ID, 2, max)
## load three-way ICL results MatTransMix
load("ICL_ID_threeway.RData")
res <- vapply(seq_len(ncol(ICL_ID)), function(i)
compareMatchedClasses(true, ICL_ID[,i])$crand,
numeric(1))
result$crand[101:200] <- res
result$K[101:200] <- apply(ICL_ID, 2, max)
## load three-way BIC results Mat.EM AR1
load("BIC_AR1_ID_threeway.RData")
res <- vapply(seq_len(ncol(BIC_AR1_ID)), function(i)
compareMatchedClasses(true, BIC_AR1_ID[,i])$crand,
numeric(1))
result$crand[201:300] <- res
result$K[201:300] <- apply(BIC_AR1_ID, 2, max)
## load three-way ICL results Mat.EM AR1
load("ICL_AR1_ID_threeway.RData")
res <- vapply(seq_len(ncol(ICL_AR1_ID)), function(i)
compareMatchedClasses(true, ICL_AR1_ID[,i])$crand,
numeric(1))
result$crand[301:400] <- res
result$K[301:400] <- apply(ICL_AR1_ID, 2, max)
## load two-way BIC results
load("BIC_ID_twoway.RData")
res <- vapply(seq_len(ncol(BIC_ID)), function(i)
compareMatchedClasses(true, BIC_ID[,i])$crand,
numeric(1))
result$crand[401:500] <- res
result$K[401:500] <- apply(BIC_ID, 2, max)
## load two-way ICL results
load("ICL_ID_twoway.RData")
res <- vapply(seq_len(ncol(ICL_ID)), function(i)
compareMatchedClasses(true, ICL_ID[,i])$crand,
numeric(1))
result$crand[501:600] <- res
result$K[501:600] <- apply(ICL_ID, 2, max)
## load kmeans Silhouette results
load("kmeans_silhouette.RData")
res <- vapply(seq_len(ncol(SIL_ID)), function(i)
compareMatchedClasses(true, SIL_ID[,i])$crand,
numeric(1))
result$crand[601:700] <- res
result$K[601:700] <- apply(SIL_ID, 2, max)
## load kmeans with 10 clusters
load("kmeans_ten.RData")
res <- vapply(seq_len(ncol(TEN_ID)), function(i)
compareMatchedClasses(true, TEN_ID[,i])$crand,
numeric(1))
result$crand[701:800] <- res
result$K[701:800] <- apply(TEN_ID, 2, max)
result$method <- rep(c("A", "B"), each = 100)
save(result, file = "simulation_result.RData")
## Step 6 - visualization ----
## Visualization
library("ggplot2")
library("wesanderson")
load("simulation_result.RData")
pdf("Figure8a.pdf")
ggplot(result, aes(x = cluster.method, y = crand, fill = nclus.method)) +
theme_bw() + ylim(0:1) +
scale_fill_manual(values = wes_palette(n = 4, name = "GrandBudapest1")) +
xlab("cluster method") +
ylab("adjusted Rand index") +
labs(fill = "Select K by") +
geom_boxplot()
dev.off()
mycols <- wes_palette(n = 4, name = "GrandBudapest1")
mcols <- mycols[c(1, 1, 1, 4, 2, 2, 2, 3)]
clusMeth <- data.frame(cluster.method = c("3-way", "3-way AR1", "2-way", "kmeans",
"3-way", "3-way AR1", "2-way", "kmeans"),
clmethod = c("3-way", "3-way AR1", "2-way", "kmeans",
"3-way", "3-way AR1", "2-way", "kmeans"),
nclus.method = c("BIC","ICL", "BIC", "ICL", "BIC", "ICL",
"Silhouette", "Fixed.10"),
mcols = mcols)
clusMeth$cluster.method <- ordered(clusMeth$cluster.method,
levels = c("3-way", "3-way AR1", "2-way",
"kmeans"))
clusMeth$nclus.method <- ordered(clusMeth$nclus.method,
levels = c("BIC","ICL", "Silhouette",
"Fixed.10"))
pdf("Figure8b.pdf")
ggplot(result, aes(x = K, fill = nclus.method)) +
geom_bar() +
scale_fill_manual(values = wes_palette(n = 4, name = "GrandBudapest1")) +
theme_bw() +
labs(fill = "Select K by") +
xlab("Number of clusters K") +
ylab("count") +
facet_wrap(method ~ cluster.method, nrow = 2) +
theme( strip.text.x = element_blank() ) +
theme(legend.position = "none") +
geom_text(data = clusMeth, aes(x = 6, y = 92, label = clmethod))
dev.off()