Jenny Jiang
2014-Aug-06 00:50 UTC
[R] weighted network centrality measures by network size
Dear R-help, My name is Jenny Jiang and I am a Finance Honours research student from the University of New South Wales Australia. Currently my research project involves the calculating of some network centrality measures in R, which are degree, closeness, betweenness and eigenvector. However I am having some issue regarding to the calculation of the weighted centrality measures by network size. For example, currently my code allows me to calculate centrality measures for each firm year, and now I would like to calculate centrality measures weighted by the firm network size for each firm year. My current code is like the following: install.packages("statnet") library(statnet) #read csv data <- read.csv("D:\\Users\\z3377013\\Desktop\\networknew1.csv",header=TRUE) #companies <- unique(data$CompanyID_) #years <- unique(data$Year) pairs <- unique(data[,c(1,3)]) #directors <- unique(c(data$DirectorID_,data$DirectorID_Connected)) #director_map <- 1:length(directors) #names(director_map) <- c(as.character(directors)) #for (i in 1:nrow(data)) { # data[i,2] = director_map[as.character(data[i,2])] # data[i,4] = director_map[as.character(data[i,4])] #} sink("D:\\Users\\z3377013\\Desktop\\measure1.csv") for (i in 1:nrow(pairs)) { d <- subset(data, CompanyID_==pairs[i,1]&Year==pairs[i,2]) directors <- unique(c(d$DirectorID_,d$DirectorID_Connected)) director_map <- 1:length(directors) names(director_map) <- c(as.character(directors)) for (j in 1:nrow(d)) { d[j,2] = director_map[as.character(d[j,2])] d[j,4] = director_map[as.character(d[j,4])] } net<-network(d[,c(2,4)],directed=F,loops=F,matrix.type="edgelist") degree <- degree(net, cmode="freeman", gmode="graph") closeness <- closeness(net,gmode="graph",cmode="undirected") betweenness <- betweenness(net,gmode="graph",cmode="undirected") evcent <- evcent(net,gmode="graph",use.eigen=TRUE) write.csv(cbind(pairs[i,], directors, degree, closeness, betweenness, evcent), row.names=FALSE) } sink() And an example of my data structure is like the following: CompanyID_ DirectorID_ Year DirectorID_Connected 900 3700068021 2003 3699838021 900 3700418032 2003 3699838021 900 3700598032 2003 3699838021 900 3700898032 2003 3699838021 900 3703478063 2003 3699838021 900 3703628063 2003 3699838021 900 3703838063 2003 3699838021 900 3703998063 2003 3699838021 900 3699838021 2003 3700068021 900 3700418032 2003 3700068021 900 3700598032 2003 3700068021 900 3700898032 2003 3700068021 900 3703478063 2003 3700068021 900 3703628063 2003 3700068021 900 3703838063 2003 3700068021 900 3703998063 2003 3700068021 900 3699838021 2003 3700418032 900 3700068021 2003 3700418032 900 3700598032 2003 3700418032 900 3700068021 2004 3699838021 900 3700418032 2004 3699838021 900 3700598032 2004 3699838021 900 3700898032 2004 3699838021 900 3703478063 2004 3699838021 1290 1604538114 2003 427207466 1290 3556906472 2003 427207466 1290 3701108032 2003 427207466 1290 3708458104 2003 427207466 1290 3708478104 2003 427207466 1290 3711248135 2003 427207466 1290 10167110612 2003 427207466 1290 10271811383 2003 427207466 where for each firm-year I have a list of directors and their corresponding connected directors within that firm-year. If you could provide me the R code regarding to how to calculate the weighted measures by network size that that would be really helpful. I cannot be more than appreciated. Best regards Jenny [[alternative HTML version deleted]]
Suzen, Mehmet
2014-Aug-07 10:32 UTC
[R] weighted network centrality measures by network size
Hi Jenny, Have you tried igraph before? See, http://igraph.org/r/doc/ There are couple of centrality measures there. Best, -m On 6 August 2014 02:50, Jenny Jiang <jiangyunbei at y7mail.com> wrote:> Dear R-help, > > My name is Jenny Jiang and I am a Finance Honours research > student from the University of New South Wales Australia. Currently my > research project involves the calculating of some network centrality > measures in R, which are degree, closeness, betweenness and eigenvector. However I am having some issue regarding to the calculation of > the weighted centrality measures by network size. For example, currently > my code allows me to calculate centrality measures for each firm year, > and now I would like to calculate centrality measures weighted by the > firm network size for each firm year. > > My current code is like the following: > > install.packages("statnet") > > library(statnet) > > #read csv > data <- read.csv("D:\\Users\\z3377013\\Desktop\\networknew1.csv",header=TRUE) > #companies <- unique(data$CompanyID_) > #years <- unique(data$Year) > pairs <- unique(data[,c(1,3)]) > #directors <- unique(c(data$DirectorID_,data$DirectorID_Connected)) > #director_map <- 1:length(directors) > #names(director_map) <- c(as.character(directors)) > > #for (i in 1:nrow(data)) { > # data[i,2] = director_map[as.character(data[i,2])] > # data[i,4] = director_map[as.character(data[i,4])] > #} > > sink("D:\\Users\\z3377013\\Desktop\\measure1.csv") > for (i in 1:nrow(pairs)) { > d <- subset(data, CompanyID_==pairs[i,1]&Year==pairs[i,2]) > directors <- unique(c(d$DirectorID_,d$DirectorID_Connected)) > director_map <- 1:length(directors) > names(director_map) <- c(as.character(directors)) > for (j in 1:nrow(d)) { > d[j,2] = director_map[as.character(d[j,2])] > d[j,4] = director_map[as.character(d[j,4])] > } > > net<-network(d[,c(2,4)],directed=F,loops=F,matrix.type="edgelist") > > degree <- degree(net, cmode="freeman", gmode="graph") > closeness <- closeness(net,gmode="graph",cmode="undirected") > betweenness <- betweenness(net,gmode="graph",cmode="undirected") > evcent <- evcent(net,gmode="graph",use.eigen=TRUE) > > write.csv(cbind(pairs[i,], directors, degree, closeness, betweenness, evcent), row.names=FALSE) > } > sink() > > And an example of my data structure is like the following: > > CompanyID_ DirectorID_ Year DirectorID_Connected > 900 3700068021 2003 3699838021 > 900 3700418032 2003 3699838021 > 900 3700598032 2003 3699838021 > 900 3700898032 2003 3699838021 > 900 3703478063 2003 3699838021 > 900 3703628063 2003 3699838021 > 900 3703838063 2003 3699838021 > 900 3703998063 2003 3699838021 > 900 3699838021 2003 3700068021 > 900 3700418032 2003 3700068021 > 900 3700598032 2003 3700068021 > 900 3700898032 2003 3700068021 > 900 3703478063 2003 3700068021 > 900 3703628063 2003 3700068021 > 900 3703838063 2003 3700068021 > 900 3703998063 2003 3700068021 > 900 3699838021 2003 3700418032 > 900 3700068021 2003 3700418032 > 900 3700598032 2003 3700418032 > 900 3700068021 2004 3699838021 > 900 3700418032 2004 3699838021 > 900 3700598032 2004 3699838021 > 900 3700898032 2004 3699838021 > 900 3703478063 2004 3699838021 > 1290 1604538114 2003 427207466 > 1290 3556906472 2003 427207466 > 1290 3701108032 2003 427207466 > 1290 3708458104 2003 427207466 > 1290 3708478104 2003 427207466 > 1290 3711248135 2003 427207466 > 1290 10167110612 2003 427207466 > 1290 10271811383 2003 427207466 > > where for each firm-year I have a list of directors and their corresponding connected directors within that firm-year. > > If you could > provide me the R code regarding to how to calculate the weighted measures by network size that that would be really > helpful. > > I cannot be more than appreciated. > > Best regards > > Jenny > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >