library(sbm)This vignette presents the multipartite stochastic block model and illustrates, on simulated data, the use of the functions sampleMultipartiteSBM and estimateMultipartiteSBM and of the methods accompanying the R6 class MultipartiteSBM_fit. An application to a real ecological network is given in the vignette Multipartite Stochastic Block Models (vignette("Multipartite_EcologicalNetwork", package = "sbm")).
The only package required for the analysis is sbm:
library(sbm)Multipartite networks consist in the joint observation of several networks implying some common individuals. The individuals (or entities represented by nodes) at stake are partitioned into groups defined by their nature, for instance plants, pollinators and birds in an ecological network. In what follows, these groups are referred to as functional groups (they are the dimLabels of the networks in sbm).
A multipartite network is a collection of networks, each of them involving one or two functional groups:
The networks may be binary or weighted, and different networks may have different types of weights.
Assume that functional groups are at stake, the functional group containing nodes. We denote by the set of pairs of functional groups for which a network is observed: is the interaction between node of functional group and node of functional group . If , is an incidence matrix (bipartite network); is an adjacency matrix, symmetric if the relation inside the functional group is not oriented.
The multipartite SBM
The nodes of functional group are divided into blocks with proportions , this clustering being encoded into the latent variables such that if node of functional group belongs to block . The clusterings of the functional groups are independent, and the interactions are driven by the blocks of the nodes: where is Bernoulli, Poisson, Gaussian or Zero Inflated Gaussian, and may differ from one network to another.
The blocks of a functional group are thus defined by the patterns of connection of its nodes in all the networks it is involved in. This model is introduced in Bar-Hen et al. (2022).
The parameters are estimated with a variational version of the Expectation-Maximization algorithm, which also supplies the clustering of the nodes. The numbers of blocks are chosen with a penalized likelihood criterion (ICL), by a forward-backward exploration: starting from an initial clustering, blocks are split or merged, one functional group at a time, as long as the ICL increases.
By default, the initial clusterings are obtained by fitting a simple or bipartite SBM on each network separately (with the package blockmodels); all the combinations of these clusterings are used as starting points (option initBM = TRUE). An initial clustering can also be given by the user (option classifInit, see Section 7).
The networks may contain missing values (NA): the corresponding dyads are assumed to be missing at random and are ignored in the inference.
The inference is performed by the package GREMLINS.
We simulate a multipartite network involving functional groups, A and B, of respective sizes and , divided into and blocks.
nbNodes <- c(60, 50)
blockProp <- list(c(0.16, 0.40, 0.44), c(0.3, 0.7)) # proportions of the blocks in each functional groupWe observe networks:
A-B: continuous weighted interactions (Gaussian),B-B: binary directed interactions (Bernoulli),A-A: counts, undirected (Poisson).The architecture of the multipartite network is given by a matrix with one row per network, giving the indices of the functional groups in row and in column.
archiMultipartite <- rbind(c(1, 2), c(2, 2), c(1, 1))
model <- c("gaussian", "bernoulli", "poisson")
directed <- c(NA, TRUE, FALSE) # NA for a bipartite network
C1 <- list(mean = matrix(c(6.1, 8.9, 6.6, 9.8, 2.6, 1.0), 3, 2),
var = matrix(c(1.6, 1.6, 1.8, 1.7, 2.3, 1.5), 3, 2))
C2 <- list(mean = matrix(c(0.7, 1.0, 0.4, 0.6), 2, 2))
m3 <- matrix(c(2.5, 2.6, 2.2, 2.2, 2.7, 3.0, 3.6, 3.5, 3.3), 3, 3)
C3 <- list(mean = 0.5 * (m3 + t(m3))) # symmetric, the network being undirected
connectParam <- list(C1, C2, C3)mySampleMSBM <- sampleMultipartiteSBM(nbNodes, blockProp, archiMultipartite, connectParam,
model, directed, dimLabels = c("A", "B"), seed = 2)
listSBM <- mySampleMSBM$listSBM
trueMemberships <- mySampleMSBM$membershipslistSBM is a list of networks, as defined by the function defineSBM. The data can be plotted with:
plotMyMultipartiteMatrix(listSBM)The numbers of blocks are chosen between the bounds given in nbBlocksRange, a list named with the functional groups (default: from to blocks in each functional group).
myMSBM <- estimateMultipartiteSBM(listSBM, estimOptions = list(verbosity = 0, nbCores = 1))The selected numbers of blocks and the agreement of the clusterings with the simulated ones (adjusted Rand index):
myMSBM$nbBlocks
#> A B
#> 3 2
sapply(c("A", "B"), function(s) aricode::ARI(myMSBM$memberships[[s]], trueMemberships[[s]]))
#> A B
#> 1 1The estimated parameters:
myMSBM$blockProp
#> $A
#> [1] 0.1333333 0.3500000 0.5166667
#>
#> $B
#> [1] 0.4 0.6
myMSBM$connectParam[[2]]
#> $mean
#> [,1] [,2]
#> [1,] 0.6625 0.3950000
#> [2,] 1.0000 0.6088889All the models visited during the exploration are stored; $setModel() makes one of them the current model.
myMSBM$storedModels
#> indexModel nbParams nbBlocks A nbBlocks B nbBlocks ICL loglik
#> 1 1 25 3 2 5 -5727.889 -5635.616
#> 2 2 17 2 2 4 -6247.506 -6184.561
#> 3 3 10 1 2 3 -7611.141 -7573.768plot(myMSBM, type = "expected")If the lower and upper bounds of nbBlocksRange are equal for all the functional groups, the parameters are estimated for these numbers of blocks, without model selection. The bounds may also be equal for some functional groups only.
myMSBM_fixed <- estimateMultipartiteSBM(listSBM,
estimOptions = list(verbosity = 0, nbCores = 1, nbBlocksRange = list(A = c(3, 3), B = c(2, 2))))myMSBM_fixed$nbBlocks
#> A B
#> 3 2An initial clustering can be given with the option classifInit: a list with one vector of block labels per functional group, named with the functional groups. The search of the numbers of blocks then starts from this clustering (initBM is ignored). Here, we start from a clustering with a single block in A and blocks in B.
set.seed(1)
myClassifInit <- list(A = rep(1, 60), B = sample(1:3, 50, replace = TRUE))
myMSBM_init <- estimateMultipartiteSBM(listSBM,
estimOptions = list(verbosity = 0, nbCores = 1, classifInit = myClassifInit))myMSBM_init$nbBlocks
#> A B
#> 3 2
myMSBM_init$ICL
#> [1] -5727.889With fixed numbers of blocks, classifInit must have these numbers of blocks: it is then the starting point of the variational EM algorithm.
We remove of the dyads of each network, at random (symmetrically for the undirected network).
addNA <- function(net, prop) {
Y <- net$networkData
isNA <- matrix(runif(length(Y)) < prop, nrow(Y), ncol(Y))
simple <- inherits(net, "SimpleSBM_fit")
if (simple && !net$directed) isNA <- isNA | t(isNA)
Y[isNA] <- NA
defineSBM(Y, net$modelName, type = ifelse(simple, "simple", "bipartite"),
directed = net$directed, dimLabels = net$dimLabels)
}
set.seed(3)
listSBM_NA <- lapply(listSBM, addNA, prop = 0.1)myMSBM_NA <- estimateMultipartiteSBM(listSBM_NA, estimOptions = list(verbosity = 0, nbCores = 1))myMSBM_NA$nbBlocks
#> A B
#> 3 2
sapply(c("A", "B"), function(s) aricode::ARI(myMSBM_NA$memberships[[s]], trueMemberships[[s]]))
#> A B
#> 1 1The method predict gives the expected value of each dyad, which can be used to predict the missing ones. For the binary network B-B, the predicted probabilities of the missing dyads are higher when there is actually an edge:
pred <- myMSBM_NA$predict()
missingBB <- is.na(listSBM_NA[[2]]$networkData)
tapply(pred[[2]][missingBB], listSBM[[2]]$networkData[missingBB], mean)
#> 0 1
#> 0.5315599 0.6906959The variational EM runs are parallelized with the package future. If no plan has been chosen, a multisession plan with nbCores workers (option of estimOptions, by default) is used during the estimation. Another plan can be set before the call:
future::plan(future::multisession, workers = 4)
myMSBM <- estimateMultipartiteSBM(listSBM)
future::plan(future::sequential)