-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cohesion centrality.R
135 lines (78 loc) · 3.67 KB
/
Cohesion centrality.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
#========================Cohesion centrality scores for bipartite networks
##======================intramodal cohesion is the influence of an actor in the actor x actor projection or an event in the event x event projection
##======================Crossmodal cohesion is the influence of an actor in hte event x event projection, or vice versa
##======================Total cohesion is the average of intramodal and cross modal cohesion
#===================================================================================================
# WARNING
#===================================================================================================
## Function is not optimized for efficiency--computation can take a long time in large networks
#===================================================================================================
##affiliation.mat is an affiliation matrix with the first mode in the rows and the second mode in the columns
cohesion.centrality.bipartite<-function(affiliation.mat){
require(statnet)
require(igraph)
require(tnet)
if(nrow(affiliation.mat)>=150 |
ncol(affiliation.mat)>=150)(warning('Large network; computation may take a few minutes.'))
davis<-affiliation.mat
projnet<-as.edgelist.sna(davis)
projfirstmode<-projecting_tm(projnet, method="Newman") #projects first mode with distances proposed by Newman
distfirstproj<-distance_w(projfirstmode, directed=NULL, gconly=FALSE) #weights distances with shortest path algorithm
nact<-nrow(davis)+ncol(davis)
daviscoh<-vector(length=nact)
###calculate intramodal cohesion
for (i in 1:nrow(davis)){
distnet<-(sum(1/distfirstproj[i,], na.rm=TRUE)) #sum 1/path lengths for i
distnet<-distnet/(nrow(davis)) #divide by # of actors in projection
daviscoh[i]<-distnet}
projnet<-t(davis)
projsecmode<-projecting_tm(projnet, method="Newman")
distsecproj<-distance_w(projsecmode, directed=NULL, gconly=FALSE)
for (i in nrow(davis):nact){
if(i==nrow(davis)){i<-i+1}
distnet2<-(sum(1/distsecproj[i-nrow(davis),], na.rm=TRUE))
distnet2<-distnet2/(ncol(davis))
daviscoh[i]<-distnet2
}
daviscohesioncentrality<-daviscoh
###cross modal cohesion
crossdav<-vector(length=nact)
davisgraph<-graph_from_incidence_matrix(davis)
a<-matrix(0, nr=1, nc=nrow(davis))
for(i in 1:nrow(davis)){
b<-neighbors(davisgraph, i)##index neighbors of i in two-mode network
b<-as.vector(b)
b<-as.matrix(b)
dimen<-nrow(b)
c<-matrix(0, nrow=1, ncol=dimen)
for(j in 1:dimen){
c[1,j]<-daviscoh[b[j,]] ##retrieve neighbors' intramodal cohesion scores
}
sumscore<-(sum(c, na.rm=TRUE))/ncol(davis) ###sum neighbors'cohesion, divide by number of actors in the alternate mode
crossdav[i]<-sumscore
}
for(i in nrow(davis):nact){
if(i==nrow(davis)){i<-i+1}
b<-neighbors(davisgraph, i)
b<-as.vector(b)
b<-as.matrix(b)
dimen<-nrow(b)
c<-matrix(0, nrow=1, ncol=dimen)
for(j in 1:dimen){
c[1,j]<-daviscoh[b[j,]]
}
sumscore<-(sum(c, na.rm=TRUE))/nrow(davis)
crossdav[i]<-sumscore
}
crosscohesiondavis<-crossdav
totdav<-(crossdav+daviscoh)/2 ###total cohesion
totalcohesiondavis<-totdav
cohesion.centrality.output<-list(daviscohesioncentrality,crosscohesiondavis,totalcohesiondavis)
names(cohesion.centrality.output)<-c("Intramodal cohesion centrality","Crossmodal cohesion centrality","Total cohesion centrality")
cohesion.centrality.output
}
#####Example--not run
library(igraph)
fakenet<-sample_bipartite(50, 15, type="gnp", runif(1, min=0.1, max=0.4), directed=FALSE)
my.affiliation.matrix<-as.matrix(as_incidence_matrix(fakenet))
cohesion.centrality.bipartite(my.affiliation.matrix)