-
Notifications
You must be signed in to change notification settings - Fork 9
/
.Rhistory
512 lines (512 loc) · 17.6 KB
/
.Rhistory
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
Results3[[i]] = list(Beta = betas[i], Communities = temp$Communities)
Mean.Score[i] = temp$Mean.Score
Number.Communities[i] = length(temp$Communities)
}
if(length(Results2) > 0){
betas = seq(0.01, 1, by = 0.01)
Results3 = list()
Number.Communities = rep(0,length(betas))
Mean.Score = rep(0,length(betas))
for(i in 1:length(betas)){
temp = cleanup(Results2, betas[i])
Results3[[i]] = list(Beta = betas[i], Communities = temp$Communities)
Mean.Score[i] = temp$Mean.Score
Number.Communities[i] = length(temp$Communities)
}
}
if(length(Results2) > 0){
betas = seq(0.01, 1, by = 0.01)
Results3 = list()
Number.Communities = rep(0,length(betas))
Mean.Score = rep(0,length(betas))
for(i in 1:length(betas)){
temp = cleanup(Results2, betas[i])
Results3[[i]] = list(Beta = betas[i], Communities = temp$Communities)
Mean.Score[i] = temp$Mean.Score
Number.Communities[i] = length(temp$Communities)
}
}
temp
length(Results2)
betas = seq(0.01, 1, by = 0.01)
Results3 = list()
Number.Communities = rep(0,length(betas))
Number.Communities = rep(0, length(betas))
Number.Communities
Mean.Score = rep(0, length(betas))
temp
for(i in 1:length(betas)){
temp = cleanup(Results2, betas[i])
Results3[[i]] = list(Beta = betas[i], Communities = temp$Communities)
Mean.Score[i] = temp$Score
Number.Communities[i] = length(temp$Communities)
}
i
temp$Score
temp
length(betas)
temp = cleanup(Results2, betas[i])
temp
cleanup = function(Results, beta){
k = length(Results)
if(k < 2){
Results <- Results[[1]]
Results$Mean.Score <- Results$Score
return(Results)
}
indx.rm = numeric()
if(k > 1){
for(i in 1:(k-1)){
for(j in (i+1):k){
match = length(intersect(Results[[i]]$B, Results[[j]]$B))*length(intersect(Results[[i]]$I, Results[[j]]$I))/(min(length(Results[[i]]$B)*length(Results[[i]]$I),length(Results[[j]]$B)*length(Results[[j]]$I)))
pot = c(i,j)
indx = pot[which.min(c(Results[[i]]$Score, Results[[j]]$Score))]
if(match > beta-0.00001){indx.rm = c(indx.rm,indx)}
}
}
}
cleanup = function(Results, beta){
k = length(Results)
if(k < 2){
Results <- Results[[1]]
Results$Mean.Score <- Results$Score
return(Results)
}
indx.rm = numeric()
if(k > 1){
for(i in 1:(k-1)){
for(j in (i+1):k){
match = length(intersect(Results[[i]]$B, Results[[j]]$B))*length(intersect(Results[[i]]$I, Results[[j]]$I))/(min(length(Results[[i]]$B)*length(Results[[i]]$I),length(Results[[j]]$B)*length(Results[[j]]$I)))
pot = c(i,j)
indx = pot[which.min(c(Results[[i]]$Score, Results[[j]]$Score))]
if(match > beta-0.00001){indx.rm = c(indx.rm,indx)}
}
}
}
Results[indx.rm] = NULL
temp = 0
for(i in 1:length(Results)){
temp = temp + Results[[i]]$Score
}
Mean.score = temp/length(Results)
return(list(Communities = Results, Mean.Score = Mean.score))
}
if(length(Results.temp) < 1){return("No Community Found")}
Scores = rep(0, length(Results.temp))
for(i in 1:length(Results.temp)){
if(length(Results.temp[[i]]$B) == 0){Scores[i] = -1000}
if(length(Results.temp[[i]]$B) > 0){
Scores[i] = Results.temp[[i]]$Score
}
}
Scores = round(Scores, 2)
#keep only unique communities with score greater than threshold
indx = which(!duplicated(Scores) == TRUE)
indx.2 = which(Scores > min.score)
Results2 = Results.temp[intersect(indx, indx.2)]
if(length(Results2) == 0){
Results = NULL
return(Object = NULL)
}
if(length(Results2) > 0){
betas = seq(0.01, 1, by = 0.01)
Results3 = list()
Number.Communities = rep(0, length(betas))
Mean.Score = rep(0, length(betas))
for(i in 1:length(betas)){
temp = cleanup(Results2, betas[i])
Results3[[i]] = list(Beta = betas[i], Communities = temp$Communities)
Mean.Score[i] = temp$Mean.Score
Number.Communities[i] = length(temp$Communities)
}
}
Z = data.frame(Beta = betas, Mean.Score = Mean.Score, Number.Communities = Number.Communities)
Object = list(Community.List = Results3, Diagnostics = Z)
Z
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
plot(community.object, main = "Diagnostic Plot AU_CS")
cleanup = function(Results, beta){
k = length(Results)
if(k < 2){
Results <- Results[[1]]
Results$Mean.Score <- Results$Score
Results$Number.Communities <- 1
return(Results)
}
indx.rm = numeric()
if(k > 1){
for(i in 1:(k-1)){
for(j in (i+1):k){
match = length(intersect(Results[[i]]$B, Results[[j]]$B))*length(intersect(Results[[i]]$I, Results[[j]]$I))/(min(length(Results[[i]]$B)*length(Results[[i]]$I),length(Results[[j]]$B)*length(Results[[j]]$I)))
pot = c(i,j)
indx = pot[which.min(c(Results[[i]]$Score, Results[[j]]$Score))]
if(match > beta-0.00001){indx.rm = c(indx.rm,indx)}
}
}
}
Results[indx.rm] = NULL
temp = 0
for(i in 1:length(Results)){
temp = temp + Results[[i]]$Score
}
Mean.score = temp/length(Results)
return(list(Communities = Results, Mean.Score = Mean.score))
}
object <- refine(community.object, k = 6, m = m, n = n)
object <- refine(community.object, k = 1, m = m, n = n)
vertex.set <- 1:10
layer.set <- 1:2
n = 61
if(length(layer.set) < 1 || length(vertex.set) < 1){
return(obs.score = 0)
}
if(length(layer.set) == 1){
super.mod <- mod.matrix[[layer.set]] #just a single igraph object
}
if(length(layer.set) > 1){
#merge the modularity graphs
super.mod <- graph.empty(n = n, directed = FALSE)
for(j in layer.set){
super.mod <- union(super.mod, mod.matrix[[j]]) #take union of all networks in the layer.set
}
}
super.mod
super.mod.subgraph <- induced_subgraph(super.mod, v = vertex.set)
#
edge.weights <- as.data.frame(get.edge.attribute(super.mod.subgraph))
edge.weights[is.na(edge.weights)] <- 0
edge.weights
modularity.score <- colSums(edge.weights)
modularity.score
modularity.score <- rowSums(edge.weights)
modularity.score
modularity.score[which(modularity.score < 0)] <- 0
modularity.score
tot.mod <- sum(modularity.score)
obs.score <- (tot.mod)^2 / (n^2*choose(length(vertex.set), 2)*(length(layer.set)))
obs.score
choose(length(vertex.set), 2)
source("score_temp.R")
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
score = function(mod.matrix, vertex.set, layer.set, n){
if(length(layer.set) < 1 || length(vertex.set) < 1){
return(obs.score = 0)
}
if(length(layer.set) == 1){
super.mod <- mod.matrix[[layer.set]] #just a single igraph object
}
if(length(layer.set) > 1){
#merge the modularity graphs
super.mod <- graph.empty(n = n, directed = FALSE)
for(j in layer.set){
super.mod <- union(super.mod, mod.matrix[[j]]) #take union of all networks in the layer.set
}
}
#take sub-graph of the modularity matrix
super.mod.subgraph <- induced_subgraph(super.mod, v = vertex.set)
#get the edge weights all together
edge.weights <- as.data.frame(get.edge.attribute(super.mod.subgraph))
edge.weights[is.na(edge.weights)] <- 0
modularity.score <- rowSums(edge.weights) #sum across vertices first
modularity.score[which(modularity.score < 0)] <- 0 #only keep positive values
tot.mod <- sum(modularity.score)
obs.score <- (tot.mod)^2 / (n^2*choose(length(vertex.set), 2)*(length(layer.set)))
return(obs.score)
}
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
plot(community.object)
dev.new()
plot(community.object)
mod.matrix <- modularity.matrix(adjacency)
mod.matrix
for(i in 1:m){
if(i == 1){
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- initialization(graph, prop.sample, m, n)
}else{
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- Map(c, initial.set, initialization(graph, prop.sample, m, n))
}
}
print(paste("Search Stage"))
cat(paste("Searching over", length(initial.set[[1]]), "seed sets \n"))
Results.temp <- list()
K <- length(initial.set[[1]])
#detectCores detects the number of cores available on your instance
registerDoParallel(detectCores())
Results.temp <- foreach(i=1:K,.packages="MultilayerExtraction") %dopar% {
starter <- list()
starter$vertex.set <- as.numeric(initial.set$vertex.set[[i]])
starter$layer.set <- as.numeric(initial.set$layer.set[[i]])
single.swap(starter, adjacency, mod.matrix, m, n)
}
Results.temp
if(length(Results.temp) < 1){return("No Community Found")}
Scores = rep(0, length(Results.temp))
Scores
for(i in 1:length(Results.temp)){
if(length(Results.temp[[i]]$B) == 0){Scores[i] = -1000}
if(length(Results.temp[[i]]$B) > 0){
Scores[i] = Results.temp[[i]]$Score
}
}
Scores
Scores = round(Scores, 2)
Scores
indx = which(!duplicated(Scores) == TRUE)
indx
for(i in 1:length(Results.temp)){
if(length(Results.temp[[i]]$B) == 0){Scores[i] = -1000}
if(length(Results.temp[[i]]$B) > 0){
Scores[i] = Results.temp[[i]]$Score
}
}
Scores = round(Scores, 5)
Scores
indx = which(!duplicated(Scores) == TRUE)
indx
indx.2 = which(Scores > min.score)
indx.2
Results2 = Results.temp[intersect(indx, indx.2)]
Results2
if(length(Results2) == 0){
Results = NULL
return(Object = NULL)
}
if(length(Results2) > 0){
betas = seq(0.01, 1, by = 0.01)
Results3 = list()
Number.Communities = rep(0, length(betas))
Mean.Score = rep(0, length(betas))
for(i in 1:length(betas)){
temp = cleanup(Results2, betas[i])
Results3[[i]] = list(Beta = betas[i], Communities = temp$Communities)
Mean.Score[i] = temp$Mean.Score
Number.Communities[i] = length(temp$Communities)
}
}
Mean.Score
Z = data.frame(Beta = betas, Mean.Score = Mean.Score, Number.Communities = Number.Communities)
Object = list(Community.List = Results3, Diagnostics = Z)
Z
multilayer.extraction = function(adjacency, seed = 123, min.score = 0, prop.sample = 0.05, directed = c(FALSE, TRUE)){
#adjacency should be an edgelist with three columns - node1, node2, layer
#layer should be numbered with integers
#each network has the same number of nodes
#nodes are indexed by the integers starting from 1
m <- max(adjacency[, 3]) #max of the layer index
n <- length(unique(c(adjacency[, 1], adjacency[, 2])))
directed <- directed[1]
#Calculate the modularity matrix
print(paste("Estimation Stage"))
mod.matrix <- modularity.matrix(adjacency)
#Initialize the communities #TODO
print(paste("Initialization Stage"))
for(i in 1:m){
if(i == 1){
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- initialization(graph, prop.sample, m, n)
}else{
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- Map(c, initial.set, initialization(graph, prop.sample, m, n))
}
}
#Search Across Initial sets
print(paste("Search Stage"))
cat(paste("Searching over", length(initial.set[[1]]), "seed sets \n"))
Results.temp <- list()
K <- length(initial.set[[1]])
#detectCores detects the number of cores available on your instance
registerDoParallel(detectCores())
Results.temp <- foreach(i=1:K,.packages="MultilayerExtraction") %dopar% {
starter <- list()
starter$vertex.set <- as.numeric(initial.set$vertex.set[[i]])
starter$layer.set <- as.numeric(initial.set$layer.set[[i]])
single.swap(starter, adjacency, mod.matrix, m, n)
}
#Cleanup the results: Keep the unique communities
print(paste("Cleaning Stage"))
if(length(Results.temp) < 1){return("No Community Found")}
Scores = rep(0, length(Results.temp))
for(i in 1:length(Results.temp)){
if(length(Results.temp[[i]]$B) == 0){Scores[i] = -1000}
if(length(Results.temp[[i]]$B) > 0){
Scores[i] = Results.temp[[i]]$Score
}
}
Scores = round(Scores, 5)
#keep only unique communities with score greater than threshold
indx = which(!duplicated(Scores) == TRUE)
indx.2 = which(Scores > min.score)
Results2 = Results.temp[intersect(indx, indx.2)]
if(length(Results2) == 0){
Results = NULL
return(Object = NULL)
}
if(length(Results2) > 0){
betas = seq(0.01, 1, by = 0.01)
Results3 = list()
Number.Communities = rep(0, length(betas))
Mean.Score = rep(0, length(betas))
for(i in 1:length(betas)){
temp = cleanup(Results2, betas[i])
Results3[[i]] = list(Beta = betas[i], Communities = temp$Communities)
Mean.Score[i] = temp$Mean.Score
Number.Communities[i] = length(temp$Communities)
}
}
Z = data.frame(Beta = betas, Mean.Score = Mean.Score, Number.Communities = Number.Communities)
Object = list(Community.List = Results3, Diagnostics = Z)
class(Object) = "MultilayerCommunity"
return(Object)
}
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
dev.new()
plot(community.object)
object <- refine(community.object, k = 6, m = m, n = n)
num.layers <- colSums(object$Layers > 0)
num.vertices <- colSums(object$Vertices)
print(data.frame(Layers = num.layers, Vertices = num.vertices))
indx1 <- which(object$Vertices[,1] == 1)
indx2 <- setdiff(which(object$Vertices[,2] == 1), indx1)
indx3 <- setdiff(setdiff(which(object$Vertices[,3] == 1), indx1), indx2)
indx4 <- setdiff(setdiff(setdiff(which(object$Vertices[,4] == 1), indx3), indx2),indx1)
indx5 <- setdiff(setdiff(setdiff(setdiff(which(object$Vertices[,5] == 1), indx4), indx3),indx2), indx1)
indx6 <- setdiff(setdiff(setdiff(setdiff(setdiff(which(object$Vertices[,6] == 1),
indx1), indx2), indx3), indx4), indx5)
none2 <- setdiff(1:61, union(union(union(indx1, indx2), union(indx3, indx4)), union(indx5, indx6)))
image(object$Layers, main = "Layers in Communities")
image(object$Layers, main = "Layers in Communities")
re.order <- c(indx1, indx2, indx3, indx4, indx5, indx6, none2)
image(AU_CS$coauthor[re.order,re.order], main = "Co-Author")
image(AU_CS$leisure[re.order,re.order], main = "Leisure")
image(AU_CS$work[re.order,re.order], main = "Work")
image(AU_CS$lunch[re.order,re.order], main = "Lunch")
image(AU_CS$facebook[re.order,re.order], main = "Facebook")
dev.new()
image(AU_CS$coauthor[re.order,re.order], main = "Co-Author")
image(AU_CS$leisure[re.order,re.order], main = "Leisure")
image(AU_CS$work[re.order,re.order], main = "Work")
image(AU_CS$lunch[re.order,re.order], main = "Lunch")
image(AU_CS$facebook[re.order,re.order], main = "Facebook")
rm(list = ls())
install.packages("devtools")
library(devtools, quietly = TRUE)
#install and load MultilayerExtraction
devtools::install_github('jdwilson4/multilayer_extraction')
library(MultilayerExtraction, quietly = TRUE)
library(Matrix, quietly = TRUE)
library(igraph, quietly = TRUE)
library(foreach, quietly = TRUE)
library(doParallel, quietly = TRUE)
library(parallel, quietly = TRUE)
data("AU_CS")
#number of nodes and layers
n <- dim(AU_CS[[1]])[1]
m <- length(AU_CS)
#types of relationships among attendees
relationship.names <- names(AU_CS)
print(relationship.names)
image(AU_CS$coauthor, main = "Co-author")
image(AU_CS$facebook, main = "Leisure")
image(AU_CS$leisure, main = "Facebook")
Mean.Degree <- sapply(AU_CS, mean) * n
print(Mean.Degree)
network <- adjacency.to.edgelist(AU_CS)
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
#plot the number of communities across overlap parameter beta
plot(community.object, main = "Diagnostic Plot AU_CS")
rm(list = ls())
devtools::install_github('jdwilson4/multilayer_extraction')
library(MultilayerExtraction, quietly = TRUE)
?modularity_matrix
?adjacency_to_edgelist
data("AU_CS")
#number of nodes and layers
n <- dim(AU_CS[[1]])[1]
m <- length(AU_CS)
network <- adjacency.to.edgelist(AU_CS)
setwd("/Users/jdwilson4/Dropbox/Github/MultilayerExtraction")
document()
devtools::install_github('jdwilson4/multilayer_extraction')
library(MultilayerExtraction, quietly = TRUE)
devtools::install_github('jdwilson4/multilayer_extraction')
devtools::install_github('jdwilson4/multilayer_extraction')
document()
document()
devtools::install_github('jdwilson4/multilayer_extraction')
devtools::install_github('jdwilson4/multilayer_extraction')
document
document()
document()
devtools::install_github('jdwilson4/multilayer_extraction')
library(MultilayerExtraction, quietly = TRUE)
network <- adjacency.to.edgelist(AU_CS)
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
network
multilayer.modularity.matrix()
?multilayer.modularity.matrix
rm(list = ls())
devtools::install_github('jdwilson4/multilayer_extraction')
library(MultilayerExtraction, quietly = TRUE)
library(Matrix, quietly = TRUE)
library(igraph, quietly = TRUE)
library(foreach, quietly = TRUE)
library(doParallel, quietly = TRUE)
library(parallel, quietly = TRUE)
data("AU_CS")
#number of nodes and layers
n <- dim(AU_CS[[1]])[1]
m <- length(AU_CS)
network <- adjacency.to.edgelist(AU_CS)
?multilayer.extraction
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
class(network)
adjacency = network
m
n
for(i in 1:m){
if(i == 1){
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- initialization(graph, prop.sample, m, n)
}else{
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- Map(c, initial.set, initialization(graph, prop.sample, m, n))
}
}
directed = FALSE
for(i in 1:m){
if(i == 1){
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- initialization(graph, prop.sample, m, n)
}else{
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- Map(c, initial.set, initialization(graph, prop.sample, m, n))
}
}
prop.sample = 0.01
for(i in 1:m){
if(i == 1){
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- initialization(graph, prop.sample, m, n)
}else{
graph <- graph_from_edgelist(as.matrix(subset(as.data.frame(adjacency), layer == i))[, 1:2],
directed = directed)
initial.set <- Map(c, initial.set, initialization(graph, prop.sample, m, n))
}
}
mod.matrix <- multilayer.modularity.matrix(adjacency)
class(mod.matrix)
mod.matrix
community.object <- multilayer.extraction(adjacency = network, seed = 123, min.score = 0, prop.sample = .10)
library(MultilayerExtraction)
?multilayer.extraction