R/struct_equiv.R
transformGraphBy.Rd
When there are structural zeros given by groups, this function applies
a particular transformation function of a graph by groups returning a
square matrix of the same size of the original one with structural zeros
and the function applied by INDICES
.
transformGraphBy(graph, INDICES, fun = function(g, ...) g, ...)
# S3 method for class 'diffnet'
transformGraphBy(graph, INDICES, fun = function(g, ...) g, ...)
# S3 method for class 'dgCMatrix'
transformGraphBy(graph, INDICES, fun = function(g, ...) g, ...)
A graph
A vector of length \(n\).
A function. This function must return a matrix of class
dgCMatrix
with the same dimension as
dim(g)
.
Further arguments passed to fun
A transformed version of the network, with the desired function applied by blocks.
The transformation function fun
must return a square matrix
of size \(m\times m\), where \(m\) is the size of the subgroup
given by INDICES
. See examples below
# Rewiring a graph by community --------------------------------------------
# Two Random graphs of different size
set.seed(123)
g0 <- rgraph_ba(m=2, self=FALSE)
g1 <- rgraph_ba(m=3, t=19, self=FALSE)
# Need a place to store both networks together!
G <- methods::new(
Class = "dgCMatrix",
Dim = c(1L,1L)*(nnodes(g0) + nnodes(g1)),
p = rep(0L, (nnodes(g0) + nnodes(g1)) + 1L)
)
# Filling the matrix
G[1:nnodes(g0),1:nnodes(g0)] <- g0
G[(nnodes(g0) + 1):nnodes(G), (nnodes(g0) + 1):nnodes(G)] <- g1
# Creating an index (community)
indx <- c(rep(1, nnodes(g0)), rep(2, nnodes(g1)))
# Apply the rewiring algorithm per group
ans <- transformGraphBy(G, indx, function(g, ...) {
rewire_graph(g, 100, "swap")
})
#> Warning: The option -copy.first- is set to TRUE. In this case, the first graph will be treated as a baseline, and thus, networks after T=1 will be replaced with T-1.
#> Warning: The option -copy.first- is set to TRUE. In this case, the first graph will be treated as a baseline, and thus, networks after T=1 will be replaced with T-1.
ans
#> 31 x 31 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 31 column names ‘1’, ‘2’, ‘3’ ... ]]
#>
#> 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 2 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 3 . . . 2 . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 4 . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 5 . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 6 1 . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 7 . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 8 . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 9 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 10 . . 1 . . 1 . . . . . . . . . . . . . . . . . . . . . . . . .
#> 11 . . 1 . . . . 1 . . . . . . . . . . . . . . . . . . . . . . .
#> 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> 13 . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . .
#> 14 . . . . . . . . . . . . 2 . . . . . . . . . . . . . . . . . .
#> 15 . . . . . . . . . . . . . . . . . . . 1 . . 1 . . . . . . . .
#> 16 . . . . . . . . . . . . . 3 . . . . . . . . . . . . . . . . .
#> 17 . . . . . . . . . . . . . . . . . . . 2 . . . . . . . . . . .
#> 18 . . . . . . . . . . . 2 1 . . . . . . . . . . . . . . . . . .
#> 19 . . . . . . . . . . . . 1 1 1 . . . . . . . . . . . . . . . .
#> 20 . . . . . . . . . . . . 2 1 . . . . . . . . . . . . . . . . .
#> 21 . . . . . . . . . . . . . 1 . . . . 2 . . . . . . . . . . . .
#> 22 . . . . . . . . . . . . 1 . 2 . . . . . . . . . . . . . . . .
#> 23 . . . . . . . . . . . . 1 2 . . . . . . . . . . . . . . . . .
#> 24 . . . . . . . . . . . . . 1 . 1 . . . . . . 1 . . . . . . . .
#> 25 . . . . . . . . . . . . . 1 . . . . . 1 . . 1 . . . . . . . .
#> 26 . . . . . . . . . . . 2 . 1 . . . . . . . . . . . . . . . . .
#> 27 . . . . . . . . . . . . 1 1 . . . . . 1 . . . . . . . . . . .
#> 28 . . . . . . . . . . . . 1 . . 2 . . . . . . . . . . . . . . .
#> 29 . . . . . . . . . . . . 1 . . 2 . . . . . . . . . . . . . . .
#> 30 . . . . . . . . . . . . 1 2 . . . . . . . . . . . . . . . . .
#> 31 . . . . . . . . . . . . 2 . . . . . . . . . 1 . . . . . . . .