[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[igraph] R : Loop contract.vertices to calculate network measures for gr
From: |
Dimitri Marschall |
Subject: |
[igraph] R : Loop contract.vertices to calculate network measures for groups in a social network in Igraph |
Date: |
Tue, 16 Dec 2014 19:45:28 +0100 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 |
Hi,
I am trying to calculate different networks measures such as
`betweenness()` and `constraint()`in my network using Igraph in R. My
problem is that I am not looking at individuals but on groups of
individuals in my network. Therefore I have to contract the vertices
before I calculate the different network measures. Thus far I have been
able to create a basic code to calculate the measures. But I have a
total of ca. 900 groups (with up to 7 members per group) in a network of
ca. 70.000 nodes and 250.000 edges. So I am trying to create a loop to
automate the approach and make life a little bit easier.
Now I want to present my approach to calculate the `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)
I would like to loop the upcoming code , to calculate not each
`constraint()` separately. But for all the three groups given in the
reproducible example at once.
#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)
So now I have the constraint measure for the first group. For the second
and third I would have to repeat my steps again. This would be feasible,
but as I stated I have 900 groups. Is there any possibility to loop this?
Kind regards,
D.A.M.
- [igraph] R : Loop contract.vertices to calculate network measures for groups in a social network in Igraph,
Dimitri Marschall <=