-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStepsOfAlgorithm.R
113 lines (77 loc) · 2.44 KB
/
StepsOfAlgorithm.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
# Wed Oct 21 16:29:37 2020
# Author: Jeffrey Durieux, MSc
# What: steps of algorithm
# 1. generate P
# while no improvement in loss occur
# 2. extract ica on sorted data
# 3. compute Ahat, Xhat and Lir
source('dummydatascripts.R')
source('sortX.R')
source('ICAonList.R')
source('computeAhats.R')
source('computeXhats.R')
source('Avoid_nc_N.R')
ClusterwiseJICA <- function(X, k = 2, nc = 2, starts = 10){
ResultsStarts <- list()
for(start in 1:starts){
totalSS <- sum(X^2)
lossiter <- totalSS + 1
iter <- 0
repeat{
iter <- iter + 1
if(iter >= 2){
cat('Iteration ', iter, ' loss value: ', lossiter[iter],'VAF:' ,Lir$vaf ,'\n')
}else{
cat('Iteration ', iter, ' loss value: ', lossiter[iter],'\n')
}
# algo step 1
if(iter == 1){
p <- CICA:::clusf(ncol(X), nClus = k)
}else{
p <- Lir$newp
}
List <- sortX(X, p)
# algo step 2
#### add stop warning over here ##### about nc <= k_n
icaparam <- ICAonList(List, nc = nc)
# algo step 3
Ahh <- Ahats(X = X, icapara = icaparam)
Lir <- XhatsAndLir(X = X, Sr = icaparam$Sr, Ahats = Ahh)
# avoid clus size lower than nc
Lir$newp <- Avoid_nc_N(Lir$newp, Lir$lossvec, nc = nc)
lossiter <- c(lossiter, Lir$loss)
#abs(lossiter[iter + 1] - lossiter[iter]) < .00001
if( lossiter[iter] - lossiter[iter + 1] < .00001){
break()
}
}
out <- list()
out$p <- Lir$newp
out$ica <- icaparam
out$lossiter <- lossiter
ResultsStarts[[start]] <- out
# make some sort of bubble sort algorithm so that only 2 starts are stored
# output only the lowest start
}
return(ResultsStarts)
}
rm(res)
##### data contains 20 subjects with 2 underlying source components (two modalities each. since 2 underlying source components are used the number of clusters equals 2)
res <- ClusterwiseJICA(X = X, k = 4, nc = 3, starts = 10)
minloss <- sapply(seq_along(res), function(anom) tail(res[[anom]]$lossiter,1))
plot(minloss)
id <- minloss == min(minloss)
resmin <- res[id]
sols <- length(resmin)
id <- sample(sols,1)
rr <- resmin[[id]]
rr$p
k4 <- tail(rr$lossiter,n = 1)
table(rr$p)
library(CICA)
Tucker(SR1, rr$ica$Sr[[1]])
Tucker(SR2, rr$ica$Sr[[2]])
Tucker(SR1, rr$ica$Sr[[3]])
Tucker(SR2, rr$ica$Sr[[3]])
Tucker(A1, rr$ica$Mr[[2]])
Tucker(A2, rr$ica$Mr[[1]])