R:Loop contract.vertices计算Igraph中社交网络中组的网络度量

时间:2014-12-16 16:30:28

标签: r loops social-networking igraph

我正在尝试使用R中的Igraph在我的网络中计算betweenness()constraint()等不同的网络度量。我的问题是我不是在关注个人,而是关注网络中的个人群体。因此,在计算不同的网络度量之前,我必须收缩顶点。到目前为止,我已经能够创建一个基本代码来计算度量。但我总共有一个。在一个网络中有900个组(每组最多7个成员)。 70.000个节点和250.000个边缘。所以我试图创建一个循环来自动化方法,让生活更轻松一点。

现在我想介绍一下计算constrain()的方法。

# load package
library(igraph)

# load data and create a weighted edgelist  
df <- data.frame(from=c(6, 9, 10, 1, 7, 8, 8, 4, 5, 2, 5, 10), to=c(3, 4, 2, 5, 10, 1, 9, 10, 6, 9, 3, 6), weight=c(4, 2, 1, 2, 3, 3, 1, 1, 4, 5, 2, 2))
g <- graph.data.frame(df, directed =FALSE)

#import groups
groups <- "
1 5 8
2 
10 7  "

subv <- read.table(text = groups, fill = TRUE, header = FALSE)

我想循环即将推出的代码,分别计算每个constraint()。但对于同时在可重复的例子中给出的所有三个组。

#create a subvector of the first group and delete all the NA entries
subv1 <- c(as.numeric(as.vector(subv[1,])))
subv1 <- subv1[!is.na(subv1)]

#save subvector as charcter    
subv1 <- as.character(subv1)

#creat subgraph with the nodes of group 1 from graph and add their 1st neighbors 
g2 <- induced.subgraph(graph=g ,vids=unlist(neighborhood(graph=g ,order=1, nodes = subv1)))

#identify the igraph IDs of the nodes in the first group
match("1", V(g2)$name) 
match("5", V(g2)$name) 
match("8", V(g2)$name) 

#create a contract vector and contract the vertices from largest to smallest using the output from match
convec1 <- c(1:(5-1), 3, 5:(vcount(g2)-1))
g3 <- contract.vertices(g2, convec1, vertex.attr.comb=toString)
convec2 <- c(1:(4-1), 3, 4:(vcount(g3)-1))
g4 <- contract.vertices(g3, convec2, vertex.attr.comb=toString)

#remove the selfloops and sum the weight attributes for the created graph
g5  <- simplify(g4, remove.loops = TRUE, edge.attr.comb=list(weight="sum"))

# calculate the constraint measure for the vertex 1, 5, 8
constraint(g5, nodes=3, weights=NULL)

所以现在我有第一组的约束措施。对于第二和第三,我将不得不重复我的步骤。这是可行的,但正如我所说,我有900个小组。有没有可能循环这个?

如果我不熟悉R和Stackoverflow,请告诉我这个例子。

0 个答案:

没有答案