In R
, I am currently working with the package igraph
. I am wondering if there are any ways to simulate graphs with a homophilic or assortativity structure to it -- or if other R
packages allow for this. Thanks!
Asked
Active
Viewed 838 times
2

user321627
- 2,350
- 4
- 20
- 43
-
Have you tried `sample_pa` and/or `sample_smallworld`? Or in general the graph generators, which are functions ending in `game`? – Apr 30 '18 at 09:50
-
Are there any stochastic blockmodel formulations you are aware of? – user321627 May 05 '18 at 03:50
-
Sorry, I don't even know what those are! – May 05 '18 at 17:00
1 Answers
3
Have you looked at the ergm package? Using an exponential random graph model you can simulate an assortative network with a nodematch
term. See ?"ergm-terms"
for a description of the term.
library(ergm)
test.net = as.network(matrix(0,10,10), directed = F) #10-node network
test.net%v%"class" = sample(c('1','2'), 10, replace = T) #nodal attribute
simulate
(or simulate.formula
) a network with a term that controls density (edges
) and one that controls homophily (nodematch
) on the nodal attribute:
test.sim = simulate(test.net ~ edges + nodematch("class"), coef = c(-1, 4))
plot(test.sim, vertex.col = as.numeric(test.net%v%"class"), vertex.cex = 2)
You can move the network back into igraph with asIgraph
from the intergraph package.

jac
- 620
- 4
- 11
-
Thanks! What exactly is the nodal attribute `test.net%v%"class"`? Is it the covariates to be used (like 1 = male, 2 = female) or the class memberships? I am also wondering how you decided to use `coef = c(-1, 4)` and what it means here? thanks! – user321627 Jun 07 '18 at 05:12
-
the nodal attribute in this case is a random categorical variable assigned to the nodes. (If assigned based on data) it could represent something like 1 = male, 2 = female. Coef(-1, 4) means in the [ergm formula](https://en.wikipedia.org/wiki/Exponential_random_graph_models#Definition) a coefficient of -1 on the edges which keeps the graph density down, and a coefficient of 4 on homophily for the "class" variable which means most edges will occur between the 1's or between the 2's. You see that in the plot. – jac Jun 12 '18 at 16:34