-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimStudies.R
165 lines (138 loc) · 7.25 KB
/
SimStudies.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
#==========================================================================================
#==========================================================================================
# This is additional code for simulation studies 6.1.3, 6.2, and 6.3
#==========================================================================================
#==========================================================================================
library(reshape2);library(tidyverse);library(ggpubr)
#==========================================================================================
# dataset generation
#==========================================================================================
I=100;J=200 # total number of rows and columns in dataset
R=5;C=5 # "true" number of row and column blocks
set.seed(1)
truthCs = rep(1:R, each=(I/R))
truthDs = rep(1:C, each=(J/C))
mus = runif(R*C,-3,3)
mus = matrix(c(mus),nrow=R,ncol=C,byrow=FALSE)
X = matrix(rnorm(I*J,mean=0,sd=5),nrow=I,ncol=J)
musmatrix = matrix(NA,nrow=I,ncol=J)
for(i in 1:max(truthCs)){
for(j in 1:max(truthDs)){
X[truthCs==i,truthDs==j] = X[truthCs==i,truthDs==j]+mus[i,j]
musmatrix[truthCs==i,truthDs==j]<-mus[i,j]
}
}
ids=matrix(c(1:(R*C)),nrow=R,ncol=C,byrow=TRUE);idsmatrix=matrix(nrow=I,ncol=J)
for(i in 1:max(truthCs)){for(j in 1:max(truthDs)){idsmatrix[truthCs==i,truthDs==j]<-ids[i,j]}}
Es=matrix(nrow = I*J,ncol=2);Es[,1]=rep(1:I,J);Es[,2]=rep(1:J,each=I)
truthEs=rep(NA,I*J);for(i in 1:(I*J)){truthEs[i]=idsmatrix[Es[i,1],Es[i,2]]}
saveRDS(X,"SimStudies_X.rds")
#==========================================================================================
# Performing Bayesian biclustering (with real dataset SimStudies_X.rds) using either the first or
# second version (see master codes) of the MCMC sampler.
# Use 'tCs = truthCs', 'tDs = truthDs', and 'tEs=truthEs' in functions 'row_ri', 'col_ri', and 'element_ri'.
# Obtain (save) the results and plots.
# The following lines of code can be used to replicate the results and plots from the simulation studies.
#==========================================================================================
setwd(".........") # set working directory
load("....RData") # load saved results
#==========================================================================================
# Simulation study 6.1.3
#==========================================================================================
library(coda)
# Geweke's convergence diagnostic
gdsse = as.vector(geweke.diag(as.mcmc(sse_dat$SSE))) # for total SSE values
gdsse
gdlp = as.vector(geweke.diag(as.mcmc(lp_dat$log_posterior))) # for log posterior values
gdlp
# Use 'gelman.diag' function for obtaining Gelman and Rubin's convergence diagnostic for multiple chains.
#==========================================================================================
# Simulation study 6.2
#==========================================================================================
# perform k-means clustering
km.Cs = kmeans(train,5,nstart=20)$cluster
km.Ds = kmeans(t(train),5,nstart=20)$cluster
km.mus = matrix(NA,nrow=I,ncol=J)
for(i in 1:I){
for(j in 1:J){
km.mus[i,j] = mean(train[km.Cs==km.Cs[i],km.Ds==km.Ds[j]])
}
}
# plot true underlying means matrix
musmatrix = data.frame(musmatrix)
plotmus = expand.grid(Rows = rownames(musmatrix), Columns = colnames(musmatrix))
vec = as.vector(as.matrix(musmatrix))
plotmus$Z = vec
color = c(rainbow(14,start=0.45,end=0.6)[14:1],rainbow(16,start=0.05,end=0.2)[16:1])
g.mus = ggplot(plotmus, aes(x = Columns, y = Rows)) + geom_tile(aes(fill = Z)) +
theme_bw() +
scale_fill_gradientn(colours = color, na.value = "white") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_discrete(limits = rev(levels(as.factor(plotmus$Rows)))) +
ggtitle("True underlying means") +
theme(plot.title = element_text(hjust = 0.5))
g.mus + rremove("xy.text")
# plot means matrix from k-means implementation
km.mus = data.frame(km.mus)
plotkm.mus = expand.grid(Rows = rownames(km.mus), Columns = colnames(km.mus))
vec = as.vector(as.matrix(km.mus))
plotkm.mus$Z = vec
color = c(rainbow(14,start=0.45,end=0.6)[14:1],rainbow(16,start=0.05,end=0.2)[16:1])
g.km.mus = ggplot(plotkm.mus, aes(x = Columns, y = Rows)) + geom_tile(aes(fill = Z)) +
theme_bw() +
scale_fill_gradientn(colours = color, na.value = "white") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_discrete(limits = rev(levels(as.factor(plotkm.mus$Rows)))) +
ggtitle("5-means clustering") +
theme(plot.title = element_text(hjust = 0.5))
g.km.mus + rremove("xy.text")
# plot means matrix from Bayesian biclustering implementation
bbc.mus = matrix(NA,nrow=I,ncol=J)
for(i in 1:I){
for(j in 1:J){
# change criterion (see paper and master codes)
bbc.mus[i,j] = mean(train[id_row_mat[,which.max(avgRowRI)]==id_row_mat[i,which.max(avgRowRI)],
id_col_mat[which.max(avgColRI),]==id_col_mat[which.max(avgColRI),j]])
}
}
bbc.mus = data.frame(bbc.mus)
plotbbc.mus = expand.grid(Rows = rownames(bbc.mus), Columns = colnames(bbc.mus))
vec = as.vector(as.matrix(bbc.mus))
plotbbc.mus$Z = vec
color = c(rainbow(14,start=0.45,end=0.6)[14:1],rainbow(16,start=0.05,end=0.2)[16:1])
g.bbc.mus = ggplot(plotbbc.mus, aes(x = Columns, y = Rows)) + geom_tile(aes(fill = Z)) +
theme_bw() +
scale_fill_gradientn(colours = color, na.value = "white") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_discrete(limits = rev(levels(as.factor(plotbbc.mus$Rows)))) +
ggtitle("Bayesian biclustering") +
theme(plot.title = element_text(hjust = 0.5))
g.bbc.mus + rremove("xy.text")
# plot means matrices on the same plot
ggarrange(g.mus+rremove("xy.text"), g.km.mus+rremove("xy.text"), g.bbc.mus+rremove("xy.text"),
labels=c("(a)","(b)","(c)"),ncol=3,common.legend = TRUE,font.label = list(size=5))
#==========================================================================================
# Simulation study 6.3
#==========================================================================================
#sparse biclustering
library(sparseBC)
bicluster = sparseBC(train,5,5,0)
bc.mus = bicluster$mus
# plot means matrix from sparse biclustering implementation
bc.mus=data.frame(bc.mus)
plotbc.mus <- expand.grid(Rows = rownames(bc.mus), Columns = colnames(bc.mus))
vec = as.vector(as.matrix(bc.mus))
plotbc.mus$Z <- vec
color = c(rainbow(14,start=0.45,end=0.6)[14:1],rainbow(16,start=0.05,end=0.2)[16:1])
g.bc.mus=ggplot(plotbc.mus, aes(x = Columns, y = Rows)) + geom_tile(aes(fill = Z)) +
theme_bw() +
scale_fill_gradientn(colours = color, na.value = "white") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_discrete(limits = rev(levels(as.factor(plotbc.mus$Rows)))) +
ggtitle("Sparse biclustering") +
theme(plot.title = element_text(hjust = 0.5))
g.bc.mus + rremove("xy.text")
# plot means matrices on the same plot
ggarrange(g.bc.mus+rremove("xy.text"),g.bbc.mus+rremove("xy.text"),ncol=2,
labels=c("(a)","(b)"),common.legend = TRUE,font.label = list(size=8))
#==========================================================================================