File: centrality.R

package info (click to toggle)
r-cran-igraph 0.7.1-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 14,280 kB
  • sloc: ansic: 150,105; cpp: 19,404; fortran: 3,777; yacc: 1,164; tcl: 931; lex: 484; makefile: 13; sh: 9
file content (209 lines) | stat: -rw-r--r-- 4,666 bytes parent folder | download
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

pause <- function() {
  cat("Press ENTER/RETURN/NEWLINE to continue.")
  readLines(n=1)
  invisible()
}

### Traditional approaches: degree, closeness, betweenness
g <- graph.formula(Andre----Beverly:Diane:Fernando:Carol,
                   Beverly--Andre:Diane:Garth:Ed,
                   Carol----Andre:Diane:Fernando,
                   Diane----Andre:Carol:Fernando:Garth:Ed:Beverly,
                   Ed-------Beverly:Diane:Garth,
                   Fernando-Carol:Andre:Diane:Garth:Heather,
                   Garth----Ed:Beverly:Diane:Fernando:Heather,
                   Heather--Fernando:Garth:Ike,
                   Ike------Heather:Jane,
                   Jane-----Ike )

pause()

### Hand-drawn coordinates
coords <- c(5,5,119,256,119,256,120,340,478,
            622,116,330,231,116,5,330,451,231,231,231)
coords <- matrix(coords, nc=2)

pause()

### Labels the same as names
V(g)$label <- V(g)$name
g$layout <- coords # $

pause()

### Take a look at it
plotG <- function(g) {
  plot(g, asp=FALSE, vertex.label.color="blue", vertex.label.cex=1.5,
       vertex.label.font=2, vertex.size=25, vertex.color="white",
       vertex.frame.color="white", edge.color="black")
}
plotG(g)

pause()

### Add degree centrality to labels
V(g)$label <- paste(sep="\n", V(g)$name, degree(g))

pause()

### And plot again
plotG(g)

pause()

### Betweenness
V(g)$label <- paste(sep="\n", V(g)$name, round(betweenness(g), 2))
plotG(g)

pause()

### Closeness
V(g)$label <- paste(sep="\n", V(g)$name, round(closeness(g), 2))
plotG(g)

pause()

### Eigenvector centrality
V(g)$label <- paste(sep="\n", V(g)$name, round(evcent(g)$vector, 2))
plotG(g)

pause()

### PageRank
V(g)$label <- paste(sep="\n", V(g)$name, round(page.rank(g)$vector, 2))
plotG(g)

pause()

### Correlation between centrality measures
karate <- graph.famous("Zachary")
cent <- list(`Degree`=degree(g),
             `Closeness`=closeness(g),
             `Betweenness`=betweenness(g),
             `Eigenvector`=evcent(g)$vector,
             `PageRank`=page.rank(g)$vector)

pause()

### Pairs plot
pairs(cent, lower.panel=function(x,y) {
  usr <- par("usr")
  text(mean(usr[1:2]), mean(usr[3:4]), round(cor(x,y), 3), cex=2, col="blue")
} )

pause()

## ### A real network, US supreme court citations
## ##  You will need internet connection for this to work
## vertices <- read.csv("http://jhfowler.ucsd.edu/data/judicial.csv")
## edges <- read.table("http://jhfowler.ucsd.edu/data/allcites.txt")
## jg <- graph.data.frame(edges, vertices=vertices, dir=TRUE)

## pause()

## ### Basic data
## summary(jg)

## pause()

## ### Is it a simple graph?
## is.simple(jg)

## pause()

## ### Is it connected?
## is.connected(jg)

## pause()

## ### How many components?
## no.clusters(jg)

## pause()

## ### How big are these?
## table(clusters(jg)$csize)

## pause()

## ### In-degree distribution
## plot(degree.distribution(jg, mode="in"), log="xy")

## pause()

## ### Out-degree distribution
## plot(degree.distribution(jg, mode="out"), log="xy")

## pause()

## ### Largest in- and out-degree, total degree
## c(max(degree(jg, mode="in")),
##   max(degree(jg, mode="out")),
##   max(degree(jg, mode="all")))

## pause()

## ### Density
## graph.density(jg)

## pause()

## ### Transitivity
## transitivity(jg)

## pause()

## ### Transitivity of a random graph of the same size
## g <- erdos.renyi.game(vcount(jg), ecount(jg), type="gnm")
## transitivity(g)

## pause()

## ### Transitivity of a random graph with the same degree distribution
## g <- degree.sequence.game(degree(jg, mode="out"), degree(jg, mode="in"),
##                           method="simple")
## transitivity(g)

## pause()

## ### Authority and Hub scores
## AS <- authority.score(jg)$vector
## HS <- hub.score(jg)$vector

## pause()

## ### Time evolution of authority scores
## AS <- authority.score(jg)$vector
## center <- which.max(AS)
## startyear <- V(jg)[center]$year

## pause()

## ### Function to go back in time
## auth.year <- function(y) {
##   print(y)
##   keep <- which(V(jg)$year <= y)
##   g2 <- subgraph(jg, keep)
##   as <- abs(authority.score(g2, scale=FALSE)$vector)
##   w <- match(V(jg)[center]$usid, V(g2)$usid)
##   as[w]
## }

## pause()

## ### Go back in time for the top authority, do a plot
## AS2 <- sapply(startyear:2005, auth.year)
## plot(startyear:2005, AS2, type="b", xlab="year", ylab="authority score")

## pause()

## ### Check another case
## center <- "22US1"
## startyear <- V(jg)[center]$year

## pause()

## ### Calculate past authority scores & plot them
## AS3 <- sapply(startyear:2005, auth.year)
## plot(startyear:2005, AS3, type="b", xlab="year", ylab="authority score")