-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation_S3.R
364 lines (333 loc) · 14.1 KB
/
simulation_S3.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
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
# devtools::install_github('CrossD/RFPCA')
# devtools::install_url("https://cran.r-project.org/src/contrib/Archive/Funclustering/Funclustering_1.0.2.tar.gz")
library(RFPCA) # RFPCA and MFPCA
library(mclust) # clustering measure
library(Funclustering) # funclust (Currently, it is not supported by cran.)
library(funHDDC) # funHDDC
library(gmfd) # gmfd
library(tidyverse)
source("functions.R")
n <- 100 # number of curves
m <- 20 # number of different time points
K <- 20 # number of components
k <- 2 # number of clusters
n_k <- c(rep(round(n/k), k-1),
n - (round(n/k) * (k-1))) # number of curves for each cluster
num.sim <- 100 # number of simulations
### Option for the number of PCs
num.pc.method <- "FVE" # using FVE thresholds
# num.pc.method <- 2 # fixed number
if (num.pc.method == "FVE") {
FVEthresholdSW <- 0.90
FVEthresholdCS <- 0.70
maxK <- Inf
} else if (as.integer(num.pc.method)) {
FVEthresholdSW <- 1
FVEthresholdCS <- 1
maxK <- num.pc.method
}
### Simulation for 3 types of data
clust_list <- list() # clustering objects
for (sim.type in 1:3) {
# sim.type <- 3 # type of generated data
method_list <- c("kCFC(R)","kCFC(M)","K-means(R)","K-means(M)",
"funclust","funHDDC","gmfd")
CCR <- matrix(0, num.sim, length(method_list))
aRand <- matrix(0, num.sim, length(method_list))
colnames(CCR) <- method_list
colnames(aRand) <- method_list
clust_obj_list <- list() # clustering objects per seed
for (seed in 1:num.sim) {
print(paste0("Seed: ", seed))
set.seed(seed)
### Generate curves for each cluster
Lt <- list()
Ly <- list()
mu_list <- list() # meanfunction for each cluster
xi_list <- list() # true FPC scores
phi_list <- list() # true eigenfunctions
cluster <- rep(1:k, n_k) # cluster index
for (i in 1:k) { # generate for each cluster
lambda <- 0.07^(seq_len(K) / 2)
basisType <- 'legendre01'
xiFun <- rnorm
sigma2 <- 0.01
muList <- list(
function(x) x * 2,
function(x) sin(x * 1 * pi) * pi / 2 * 0.6,
function(x) cos(x * 1/2 * pi) * pi / 2 * 0.6,
function(x) rep(0, length(x))
)
if (i == 2) {
# basisType <- "fourier"
if (sim.type == 1) {
lambda <- (i*0.07)^(seq_len(K) / 2)
muList[[2]] <- function(x) (-sin(x * 1 * pi)) * pi / 2 * 0.6
} else if (sim.type == 2) {
lambda <- (i*0.07)^(seq_len(K) / 2)
muList[[2]] <- function(x) (cos(x * 4 * pi)) * pi / 2 * 0.6
} else if (sim.type == 3) {
# lambda <- (i*0.07)^(seq_len(K) / 2)
# muList[[2]] <- function(x) (sin(x * 3 * pi)) * pi / 2 * 0.6
lambda <- ((i+1)*0.07)^(seq_len(K) / 2)
muList[[2]] <- function(x) (-sin(x * 2 * pi)) * pi / 2 * 0.6
muList[[3]] <- function(x) cos(x * pi) * pi / 2 * 0.6
}
}
pts <- seq(0, 1, length.out = m)
mfd <- structure(1, class = 'Sphere')
mu <- Makemu(mfd, muList, c(0, 0, 0, 1), pts)
# Generate samples
samp <- MakeMfdProcess(mfd = mfd,
n = n_k[i],
mu = mu,
pts = pts,
K = K,
xiFun = xiFun,
lambda = lambda,
basisType = basisType,
sigma2 = sigma2)
spSamp <- array2list(samp$X, samp$T)
Ly <- c(Ly, spSamp$Ly)
Lt <- c(Lt, spSamp$Lt)
mu_list <- c(mu_list, list(mu))
xi_list <- c(xi_list, list(samp$xi))
phi_list <- c(phi_list, list(samp$phi))
}
### kCFC with Riemannian metric
fit.kCFC.Riemann <- kCRFC(y = Ly,
t = Lt,
k = k,
kSeed = seed,
maxIter = 125,
optnsSW = list(mfdName = "Sphere",
FVEthreshold = FVEthresholdSW,
maxK = maxK,
# error = T,
userBwMu = "GCV",
userBwCov = "GCV"),
optnsCS = list(mfdName = "Sphere",
FVEthreshold = FVEthresholdCS,
maxK = maxK,
# error = T,
userBwMu = 'GCV',
userBwCov = 'GCV'))
# fit.kCFC.Riemann$cluster # clustering index
# fit.kCFC.Riemann$clustConf0 # initial clustering index from k-means
### kCFC with Euclidean metric (multivariate FPCA)
fit.kCFC.L2 <- kCRFC(y = Ly,
t = Lt,
k = k,
kSeed = seed,
maxIter = 125,
optnsSW = list(mfdName = "Euclidean",
FVEthreshold = FVEthresholdSW,
maxK = maxK,
# error = T,
userBwMu = "GCV",
userBwCov = "GCV"),
optnsCS = list(mfdName = "Euclidean",
FVEthreshold = FVEthresholdCS,
maxK = maxK,
# error = T,
userBwMu = 'GCV',
userBwCov = 'GCV'))
# ### K-means with RFPCA
# fit.rfpca <- RFPCA(Ly = Ly,
# Lt = Lt,
# optns = list(mfdName = "Sphere",
# userBwMu = "GCV",
# userBwCov = "GCV",
# # kernel = kern,
# FVEthreshold = FVEthresholdSW,
# maxK = maxK,
# error = FALSE))
# set.seed(seed)
# fit.kmeans.Riemann <- kmeans(fit.rfpca$xi, centers = k,
# iter.max = 30, nstart = 50)
#
# ### K-means with MFPCA
# fit.mfpca <- RFPCA(Ly = Ly,
# Lt = Lt,
# optns = list(mfdName = "Euclidean",
# userBwMu = "GCV",
# userBwCov = "GCV",
# # kernel = kern,
# FVEthreshold = FVEthresholdSW,
# maxK = maxK,
# error = FALSE))
# set.seed(seed)
# fit.kmeans.L2 <- kmeans(fit.mfpca$xi, centers = k,
# iter.max = 30, nstart = 50)
### funclust - set.seed does not working!!
set.seed(seed)
CWtime <- Lt[[1]]
CWfd <- lapply(1:3, function(mdim){
data <- sapply(Ly, function(y){ y[mdim, ] })
fda::smooth.basisPar(CWtime, data, lambda = 1e-2)$fd # B-spline basis
})
# set.seed(seed)
fit.funclust <- funclust(CWfd, K = k, increaseDimension = T)
# 1 - classError(cluster, fit.funclust$cls)$errorRate
# fit.funclust$cls
### funHDDC
set.seed(seed)
fit.funHDDC <- funHDDC(CWfd,
K = k,
model = "AkjBQkDk",
init = "kmeans",
threshold = 0.2)
# fit.funHDDC$class
### gmfd
# A k-means procedure based on a Mahalanobis type distance for clustering multivariate functional data
# https://link.springer.com/content/pdf/10.1007/s10260-018-00446-6.pdf
set.seed(seed)
FD <- funData(Lt[[1]], list(
t( sapply(Ly, function(y){ y[1, ] }) ),
t( sapply(Ly, function(y){ y[2, ] }) ),
t( sapply(Ly, function(y){ y[3, ] }) )
))
fit.gmfd <- gmfd_kmeans(FD, n.cl = k, metric = "mahalanobis", p = 10^5)
graphics.off() # remove plot panel
# fit.gmfd$cluster
# CCR (correct classification rate) and aRand (adjusted Rand index)
CCR[seed, ] <- c(
1 - classError(cluster, fit.kCFC.Riemann$cluster)$errorRate,
1 - classError(cluster, fit.kCFC.L2$cluster)$errorRate,
## initial clustering (k-means)
1 - classError(cluster, fit.kCFC.Riemann$clustConf0)$errorRate,
1 - classError(cluster, fit.kCFC.L2$clustConf0)$errorRate,
1 - classError(cluster, fit.funclust$cls)$errorRate,
1 - classError(cluster, fit.funHDDC$class)$errorRate,
1 - classError(cluster, fit.gmfd$cluster)$errorRate
)
aRand[seed, ] <- c(
adjustedRandIndex(cluster, fit.kCFC.Riemann$cluster),
adjustedRandIndex(cluster, fit.kCFC.L2$cluster),
## initial clustering (k-means)
adjustedRandIndex(cluster, fit.kCFC.Riemann$clustConf0),
adjustedRandIndex(cluster, fit.kCFC.L2$clustConf0),
adjustedRandIndex(cluster, fit.funclust$cls),
adjustedRandIndex(cluster, fit.funHDDC$class),
adjustedRandIndex(cluster, fit.gmfd$cluster)
)
print(CCR[seed, ])
### Save clustering objects per seed
clust_obj_list[[seed]] <- list(
fit.kCFC.Riemann = fit.kCFC.Riemann,
fit.kCFC.L2 = fit.kCFC.L2,
fit.funclust = fit.funclust,
fit.funHDDC = fit.funHDDC,
fit.gmfd = fit.gmfd
)
}
### Save clustering objects per sim.type
clust_list[[sim.type]] <- clust_obj_list
# colMeans(CCR)
# colMeans(aRand)
# apply(CCR, 2, sd)
# apply(aRand, 2, sd)
### Combine results
if (sim.type == 1) {
res <- data.frame(Method = method_list) %>%
# CCR
left_join(data.frame(
Method = colnames(CCR),
"CCR" = paste0(
format(round(colMeans(CCR), 3), 3),
" (",
format(round(apply(CCR, 2, sd), 3), 3),
")"
)
), by = "Method") %>%
# aRand
left_join(data.frame(
Method = colnames(aRand),
"aRand" = paste0(
format(round(colMeans(aRand), 3), 3),
" (",
format(round(apply(aRand, 2, sd), 3), 3),
")"
)
), by = "Method")
} else if (sim.type > 1) {
res2 <- data.frame(Method = method_list) %>%
# CCR
left_join(data.frame(
Method = colnames(CCR),
"CCR" = paste0(
format(round(colMeans(CCR), 3), 3),
" (",
format(round(apply(CCR, 2, sd), 3), 3),
")"
)
), by = "Method") %>%
# aRand
left_join(data.frame(
Method = colnames(aRand),
"aRand" = paste0(
format(round(colMeans(aRand), 3), 3),
" (",
format(round(apply(aRand, 2, sd), 3), 3),
")"
)
), by = "Method")
res <- cbind(res, res2[, -1])
}
}
res
save(clust_list, res, file = "RData/2022_0930_sim_S3.RData")
length(clust_list)
length(clust_list[[1]])
length(clust_list[[1]][[1]])
### res 잘못 저장됐다...
for (sim.type in 1:3) {
### Combine results
if (sim.type == 1) {
res <- data.frame(Method = method_list) %>%
# CCR
left_join(data.frame(
Method = colnames(CCR),
"CCR" = paste0(
format(round(colMeans(CCR), 3), 3),
" (",
format(round(apply(CCR, 2, sd), 3), 3),
")"
)
), by = "Method") %>%
# aRand
left_join(data.frame(
Method = colnames(aRand),
"aRand" = paste0(
format(round(colMeans(aRand), 3), 3),
" (",
format(round(apply(aRand, 2, sd), 3), 3),
")"
)
), by = "Method")
} else if (sim.type > 1) {
res2 <- data.frame(Method = method_list) %>%
# CCR
left_join(data.frame(
Method = colnames(CCR),
"CCR" = paste0(
format(round(colMeans(CCR), 3), 3),
" (",
format(round(apply(CCR, 2, sd), 3), 3),
")"
)
), by = "Method") %>%
# aRand
left_join(data.frame(
Method = colnames(aRand),
"aRand" = paste0(
format(round(colMeans(aRand), 3), 3),
" (",
format(round(apply(aRand, 2, sd), 3), 3),
")"
)
), by = "Method")
res <- cbind(res, res2[, -1])
}
}
res