如何在R中用igraph或其他包模拟同配性/同质性图?
Great question! When working in R to simulate graphs with homophily (or assortativity), you've got solid options both within igraph and from specialized packages. Let's walk through the most practical approaches:
Using igraph for Homophily Simulation
igraph has built-in tools to create graphs with assortative structure, with stochastic block models (SBMs) being the most straightforward for modeling homophily.
Stochastic Block Models (SBMs)
SBMs group nodes into blocks where connections within blocks are far more frequent than between blocks—this directly mirrors the idea of homophily (nodes connecting to similar others). Use sample_sbm() to generate these graphs:
library(igraph) # Define 2 groups of 50 nodes each block_sizes <- c(50, 50) # Set connection probabilities: high within groups, low between block_probs <- matrix(c(0.8, 0.1, 0.1, 0.8), nrow = 2) # Generate the SBM graph sbm_graph <- sample_sbm(n = sum(block_sizes), pref.matrix = block_probs, block.sizes = block_sizes) # Verify assortativity by group membership assortativity_nominal(sbm_graph, membership(sbm_graph))
The pref.matrix controls connection likelihood between blocks—we set diagonal values (same-group connections) higher here to enforce strong homophily.
Rewiring Existing Graphs for Assortativity
If you want to adjust an existing graph to add homophily, use rewire() with a custom probability function that favors same-attribute connections:
# Start with a random base graph base_graph <- sample_gnm(n = 100, m = 200) # Add a binary group attribute to nodes V(base_graph)$group <- sample(c(0, 1), 100, replace = TRUE) # Rewire edges to prioritize same-group connections homophilous_graph <- rewire( base_graph, with = keeping_degseq( niter = 1000, prob = function(from, to) { # 80% chance to retain same-group edges, 20% for cross-group if (V(base_graph)$group[from] == V(base_graph)$group[to]) 0.8 else 0.2 } ) )
Specialized R Packages for Advanced Homophily Simulation
For more complex scenarios (like layered networks, weighted edges, or custom attribute-based biases), these packages offer tailored tools:
blockmodels
Built explicitly for block models, this package supports layered, directed, and weighted SBMs—ideal for fine-tuning homophily structures:
library(blockmodels) # Simulate a binary undirected SBM with 2 blocks bm <- BM_bernoulli("SBM", n = 100, k = 2) bm$sampling() # Convert the result to an igraph object bm_graph <- graph_from_adjacency_matrix(bm$model_parameters$adjacency, mode = "undirected")
statnet (via ergm)
The ergm package from the statnet suite lets you simulate networks using exponential random graph models, where you can explicitly include homophily terms (e.g., nodematch() to reward same-attribute connections):
library(ergm) # Initialize a network with a binary group attribute n <- 100 net <- network.initialize(n, directed = FALSE) set.vertex.attribute(net, "group", sample(c(0, 1), n, replace = TRUE)) # Define an ERGM with edges + homophily term homophily_model <- ergm(net ~ edges + nodematch("group")) # Simulate a graph from the model simulated_net <- simulate(homophily_model)
netdiffuseR
While focused on diffusion processes, this package includes simulate_net() to generate networks with configurable homophily bias:
library(netdiffuseR) # Create node data with a binary group attribute nodes <- data.frame(group = sample(c(0, 1), 100, replace = TRUE)) # Simulate network with 70% homophily bias (prioritize same-group connections) hom_net <- simulate_net(nodes, homophily = 0.7, randomness = 0.3) # Convert to an igraph object hom_igraph <- graph_from_adjacency_matrix(hom_net, mode = "undirected")
内容的提问来源于stack exchange,提问作者user321627

