-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.R
executable file
·71 lines (63 loc) · 1.66 KB
/
simulation.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
source("two_mode_k_means.R")
library(plotly)
Simulation <- function(n, m, K, L, ESD) {
# Function generates P, Q, V, E and X then it passes all the required
# parameters to the funciton Two_mode_k_means(n, m, K, L, P, Q, X).
# As a result it receives VAF, new P and Q for generating new X and
# building a plot.
#
# Args:
# n: Number of rows.
# m: Number of columns.
# K: Number of row clusters.
# L: Number of column clusters.
# ESD: Error standard deviation
#
# Returns:
# Nothing. Function prints a VAF criterion and builds a heat map.
P = RandMembMatrix(n, K)
Q = RandMembMatrix(m, L)
quantiles <- qnorm(seq(1 / (K * L + 1), 1 - 1 / (K * L + 1), 1 / (K * L + 1)))
V <- matrix(sample(quantiles), nrow = K, ncol = L)
E <- matrix(rnorm(n * m), n, m)
X <- P %*% V %*% Conj(t.default(Q)) + E * ESD
return.list <- Two_mode_k_means(n, m, K, L, P, Q, X)
cat("VAF = ", return.list$vaf)
new.X <- GenerateNewX(m, n, K, L, X, return.list$P, return.list$Q)
BuildPlot(n, new.X)
}
BuildPlot <- function(n, X){
p <- plot_ly(
x = seq(1, n, 1), y = seq(1, 1, 1),
z = X, type = "heatmap"
)
p
}
RandMembMatrix <- function(nrow, ncol) {
m <- matrix(0L, nrow = nrow, ncol = ncol)
m[cbind(sequence(nrow), sample(ncol, nrow, TRUE))] <- 1L
return (m)
}
GenerateNewX <- function(m, n, K, L, X, P, Q) {
c = 1
new.X = X
for (i in 1 : K) {
for (j in 1 : n) {
if (P[j, i] == 1) {
new.X[c,] = X[j,]
c = c + 1
}
}
}
c = 1
X = new.X
for (i in 1 : L) {
for (j in 1 : m) {
if (Q[j, i] == 1) {
new.X[, c] = X[, j]
c = c + 1
}
}
}
return (new.X)
}