Skip to contents

To specify a stochastic blockmodel, you must specify the number of nodes (via n), the mixing matrix (via k or B), and the relative block probabilites (optional, via pi). We provide defaults for most of these options to enable rapid exploration, or you can invest the effort for more control over the model parameters. We strongly recommend setting the expected_degree or expected_density argument to avoid large memory allocations associated with sampling large, dense graphs.

Usage

sbm(
  n,
  k = NULL,
  B = NULL,
  ...,
  pi = rep(1/k, k),
  sort_nodes = TRUE,
  poisson_edges = TRUE,
  allow_self_loops = TRUE
)

Arguments

n

The number of nodes in the network. Must be a positive integer. This argument is required.

k

(mixing matrix) The number of blocks in the blockmodel. Use when you don't want to specify the mixing-matrix by hand. When k is specified, the elements of B are drawn randomly from a Uniform(0, 1) distribution. This is subject to change, and may not be reproducible. k defaults to NULL. You must specify either k or B, but not both.

B

(mixing matrix) A k by k matrix of block connection probabilities. The probability that a node in block i connects to a node in community j is Poisson(B[i, j]). Must be a square matrix. matrix and Matrix objects are both acceptable. If B is not symmetric, it will be symmetrized via the update B := B + t(B). Defaults to NULL. You must specify either k or B, but not both.

...

Arguments passed on to undirected_factor_model

expected_degree

If specified, the desired expected degree of the graph. Specifying expected_degree simply rescales S to achieve this. Defaults to NULL. Do not specify both expected_degree and expected_density at the same time.

expected_density

If specified, the desired expected density of the graph. Specifying expected_density simply rescales S to achieve this. Defaults to NULL. Do not specify both expected_degree and expected_density at the same time.

pi

(relative block probabilities) Relative block probabilities. Must be positive, but do not need to sum to one, as they will be normalized internally. Must match the dimensions of B or k. Defaults to rep(1 / k, k), or a balanced blocks.

sort_nodes

Logical indicating whether or not to sort the nodes so that they are grouped by block and by theta. Useful for plotting. Defaults to TRUE.

poisson_edges

Logical indicating whether or not multiple edges are allowed to form between a pair of nodes. Defaults to TRUE. When FALSE, sampling proceeds as usual, and duplicate edges are removed afterwards. Further, when FALSE, we assume that S specifies a desired between-factor connection probability, and back-transform this S to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) for some additional details.

allow_self_loops

Logical indicating whether or not nodes should be allowed to form edges with themselves. Defaults to TRUE. When FALSE, sampling proceeds allowing self-loops, and these are then removed after the fact.

Value

An undirected_sbm S3 object, which is a subclass of the dcsbm() object.

Details

A stochastic block is equivalent to a degree-corrected stochastic blockmodel where the degree heterogeneity parameters have all been set equal to 1.

See also

Other stochastic block models: dcsbm(), directed_dcsbm(), mmsbm(), overlapping_sbm(), planted_partition()

Other undirected graphs: chung_lu(), dcsbm(), erdos_renyi(), mmsbm(), overlapping_sbm(), planted_partition()

Examples


set.seed(27)

lazy_sbm <- sbm(n = 1000, k = 5, expected_density = 0.01)
lazy_sbm
#> $fun
#> function (n, pref.matrix, block.sizes, directed = FALSE, loops = FALSE) 
#> {
#>     n <- as.integer(n)
#>     pref.matrix <- as.matrix(structure(as.double(pref.matrix), 
#>         dim = dim(pref.matrix)))
#>     block.sizes <- as.integer(block.sizes)
#>     directed <- as.logical(directed)
#>     loops <- as.logical(loops)
#>     on.exit(.Call(C_R_igraph_finalizer))
#>     res <- .Call(C_R_igraph_sbm_game, n, pref.matrix, block.sizes, 
#>         directed, loops)
#>     if (igraph_opt("add.params")) {
#>         res$name <- "Stochastic block model"
#>         res$loops <- loops
#>     }
#>     res
#> }
#> <bytecode: 0x563b362a4418>
#> <environment: namespace:igraph>
#> 
#> $args
#> $n
#> $expr
#> [1] 1000
#> 
#> $env
#> <environment: 0x563b34993298>
#> 
#> attr(,"class")
#> [1] "lazy"
#> 
#> $k
#> $expr
#> [1] 5
#> 
#> $env
#> <environment: 0x563b34993298>
#> 
#> attr(,"class")
#> [1] "lazy"
#> 
#> $expected_density
#> $expr
#> [1] 0.01
#> 
#> $env
#> <environment: 0x563b34993298>
#> 
#> attr(,"class")
#> [1] "lazy"
#> 
#> attr(,"class")
#> [1] "lazy_dots"
#> 
#> $lazy
#> [1] FALSE
#> 
#> attr(,"class")
#> [1] "igraph_constructor_spec"

# by default we get a multigraph (i.e. multiple edges are
# allowed between the same two nodes). using bernoulli edges
# will with an adjacency matrix with only zeroes and ones

bernoulli_sbm <- sbm(
  n = 5000,
  k = 300,
  poisson_edges = FALSE,
  expected_degree = 8
)

bernoulli_sbm
#> $fun
#> function (n, pref.matrix, block.sizes, directed = FALSE, loops = FALSE) 
#> {
#>     n <- as.integer(n)
#>     pref.matrix <- as.matrix(structure(as.double(pref.matrix), 
#>         dim = dim(pref.matrix)))
#>     block.sizes <- as.integer(block.sizes)
#>     directed <- as.logical(directed)
#>     loops <- as.logical(loops)
#>     on.exit(.Call(C_R_igraph_finalizer))
#>     res <- .Call(C_R_igraph_sbm_game, n, pref.matrix, block.sizes, 
#>         directed, loops)
#>     if (igraph_opt("add.params")) {
#>         res$name <- "Stochastic block model"
#>         res$loops <- loops
#>     }
#>     res
#> }
#> <bytecode: 0x563b362a4418>
#> <environment: namespace:igraph>
#> 
#> $args
#> $n
#> $expr
#> [1] 5000
#> 
#> $env
#> <environment: 0x563b34993298>
#> 
#> attr(,"class")
#> [1] "lazy"
#> 
#> $k
#> $expr
#> [1] 300
#> 
#> $env
#> <environment: 0x563b34993298>
#> 
#> attr(,"class")
#> [1] "lazy"
#> 
#> $poisson_edges
#> $expr
#> [1] FALSE
#> 
#> $env
#> <environment: 0x563b34993298>
#> 
#> attr(,"class")
#> [1] "lazy"
#> 
#> $expected_degree
#> $expr
#> [1] 8
#> 
#> $env
#> <environment: 0x563b34993298>
#> 
#> attr(,"class")
#> [1] "lazy"
#> 
#> attr(,"class")
#> [1] "lazy_dots"
#> 
#> $lazy
#> [1] FALSE
#> 
#> attr(,"class")
#> [1] "igraph_constructor_spec"

edgelist <- sample_edgelist(bernoulli_sbm)
#> Error in UseMethod("sample_edgelist"): no applicable method for 'sample_edgelist' applied to an object of class "igraph_constructor_spec"
edgelist
#> Error in eval(expr, envir, enclos): object 'edgelist' not found

A <- sample_sparse(bernoulli_sbm)
#> Error in UseMethod("sample_sparse"): no applicable method for 'sample_sparse' applied to an object of class "igraph_constructor_spec"

# only zeroes and ones!
sign(A)
#> Error in eval(expr, envir, enclos): object 'A' not found