-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetStats.R
147 lines (118 loc) · 3.6 KB
/
netStats.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
library(igraph)
library(brainGraph)
rm(list=ls())
all.nets<-list.files(pattern = 'Rdata')
mynets<- lapply(all.nets, function(x) {
load(file = x)
get(ls()[ls()!= g])
})
all.nets<-gsub('.Rdata','',all.nets)
names(mynets) <- all.nets
#===== BASE CHECK ========#
#summary(mynets$Abidjan)
#plot(mynets$Abidjan, edge.arrow.size = 0.2)
#vertex_attr_names(mynets$Abidjan)
#vertex_attr(mynets$Abidjan, "name")
#edge_attr(mynets$Abidjan)
#unique(edge_attr(mynets$Abidjan, "route_type"))
##### componets
# CS = components(mynets$Abidjan, mode="strong")
# BigComp = which.max(CS$csize)
# Main_comp = induced_subgraph(mynets$Abidjan,
# which(CS$membership == BigComp))
#===================================================================
netStats<-lapply(mynets, function(g){
#-----------------------------------------------
## Topological properties of the networks ##
## size, density, diameter, and clustering coefficient.
## These provide a very elementary and high-level information
## about the network properties
# Number of links
NL<-gsize(g)
# Number of nodes
ND<-gorder(g)
# diameter
DIAM<-diameter(g, directed=T)
# Density
DEN<-graph.density(g)
# Average clustering coefficient/transitivity
ACF<-transitivity(g, type = "global")
# Global efficiency
GF<-efficiency(g, type = "global")
#-----------------------------------------------
## centrality measures.These measures help finding out hubs, if present,
## and better define the distribution of stations
## Average betweenness centrality
ABC<-mean(betweenness(g, directed=T,normalized = T))#weights = E(g)$weight)
#Average Closeness centrality
CS = components(g, mode="strong")
BigComp = which.max(CS$csize)
Main_comp = induced_subgraph(g,
which(CS$membership == BigComp))
ACC<-mean(closeness(Main_comp, mode ='all',normalized = T))
#vertex_connectivity/cohesion/Beta index/connectivity
#CON<-vertex_connectivity(Main_comp) #cohesion(g)
#SIZE OF THE BIGGEST COMPONENT
CBIG<-max(CS$csize)
#-----------------------------------------------
## Additiona Network measures
## helpful to understand the networks dynamic and topology
#mean distance /average path length
#APL<-mean_distance(g, directed=T)
APL<-average.path.length(g, directed=TRUE, unconnected=TRUE)
#-----------------------------------------------
#community structure
#simulated annealing (Guimerà and Amaral 2005).
spin=cluster_spinglass(Main_comp)
length(unique(spin$membership))
# The modularity of a graph with respect to some division (or vertex types) measures
# how good the division is, or how separated are the different vertex types from each other.
MOD<-spin$modularity
NC<-length(unique(spin$membership))
list(NL,#'Number of links'
ND,#'Number of nodes'
DIAM,#'Diameter'
DEN,#'Density'
ACF,#'Transitivity'
GF,#Global Efficiency
ABC,#'Avg Betweenness centrality',
ACC,#'Avg Closeness centrality'
CBIG,#'Max Component Size'
APL,#'Avg Path Length'
MOD,#'Modularity',
NC#'Number of Clusters'
)
})
names(netStats)<-all.nets
library(data.table)
netTab<-rbindlist(netStats,idcol = 'City')
names(netTab)<-c('City',
'NL',
'ND',
'DIAM',
'DEN',
'ACF',
'GF',
'ABC',
'ACC',
'CBIG',
'APL',
'MOD',
'NC')
names(netTab)<-c('City',
'Number of links',
'Number of nodes',
'Diameter',
'Density',
'Transitivity',
'Global Efficiency',
'Avg Betweenness Centrality',
'Avg Closeness Centrality',
'Max Component Size',
'Avg Path Length',
'Modularity',
'Number of Clusters'
)
netTab<-netTab[, lapply(.SD, round, 4), City]
library(xtable)
print(xtable(netTab, type = "latex"), file = "netTab.tex")