File: hrg.R

package info (click to toggle)
r-cran-igraph 1.2.3-1~bpo9%2B1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 14,984 kB
  • sloc: ansic: 117,319; cpp: 22,287; fortran: 4,551; yacc: 1,150; tcl: 931; lex: 478; makefile: 149; sh: 9
file content (105 lines) | stat: -rw-r--r-- 1,868 bytes parent folder | download | duplicates (5)
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

pause <- function() {}

### Download the Zachary Karate Club network from Nexus

karate <- nexus.get("karate")
karate

pause()

### Optimalize modularity

optcom <- cluster_optimal(karate)
V(karate)$comm <- membership(optcom)
plot(optcom, karate)

pause()

### Fit a HRG model to the network

hrg <- fit_hrg(karate)
hrg

pause()

### The fitted model, more details

print(hrg, level=5)

pause()

### Plot the full hierarchy, as an igraph graph

ihrg <- as.igraph(hrg)
ihrg$layout <- layout.reingold.tilford
plot(ihrg, vertex.size=10, edge.arrow.size=0.2)

pause()

### Customize the plot a bit, show probabilities and communities

vn <- sub("Actor ", "", V(ihrg)$name)
colbar <- rainbow(length(optcom))
vc <- ifelse(is.na(V(ihrg)$prob), colbar[V(karate)$comm], "darkblue")
V(ihrg)$label <- ifelse(is.na(V(ihrg)$prob), vn, round(V(ihrg)$prob, 2))
par(mar=c(0,0,3,0))
plot(ihrg, vertex.size=10, edge.arrow.size=0.2,
     vertex.shape="none", vertex.label.color=vc,
     main="Hierarchical network model of the Karate Club")

pause()

### Plot it as a dendrogram, looks better if the 'ape' package is installed

plot_dendrogram(hrg)

pause()

### Make a very hierarchical graph

g1 <- make_full_graph(5)
g2 <- make_ring(5)

g <- g1 + g2
g <- g + edge(1, vcount(g1)+1)

plot(g)

pause()

### Fit HRG

ghrg <- fit_hrg(g)
plot_dendrogram(ghrg)

pause()

### Create a consensus dendrogram from multiple samples, takes longer...

hcons <- consensus_tree(g)
hcons$consensus

pause()

### Predict missing edges

pred <- predict_edges(g)
pred

pause()

### Add some the top 5 predicted edges to the graph, colored red

E(g)$color <- "grey"
lay <- layout_nicely(g)
g2 <- add_edges(g, t(pred$edges[1:5,]), color="red")
plot(g2, layout=lay)

pause()

### Add four more predicted edges, colored orange

g3 <- add_edges(g2, t(pred$edges[6:9,]), color="orange")
plot(g3, layout=lay)