vec.to.mat: Creating Objects for Network Models

View source: R/auxfunc.R

norm.networkR Documentation

Creating Objects for Network Models

Description

The vec.to.mat function creates a list of square matrices from a given vector. Elements of the generated matrices are taken from the vector and placed column-wise or row-wise, progressing from the first matrix in the list to the last. The diagonals of the generated matrices are set to zeros.
The mat.to.vec function creates a vector from a given list of square matrices. Elements of the generated vector are taken column-wise or row-wise, starting from the first matrix in the list to the last, excluding diagonal entries.
The norm.network function row-normalizes matrices in a given list.

Usage

norm.network(W)

vec.to.mat(u, N, normalise = FALSE, byrow = FALSE)

mat.to.vec(W, ceiled = FALSE, byrow = FALSE)

Arguments

W

A matrix or list of matrices to convert.

u

A numeric vector to convert.

N

A vector of sub-network sizes such that length(u) == sum(N * (N - 1)).

normalise

A boolean indicating whether the returned matrices should be row-normalized (TRUE) or not (FALSE).

byrow

A boolean indicating whether entries in the matrices should be taken by row (TRUE) or by column (FALSE).

ceiled

A boolean indicating whether the given matrices should be ceiled before conversion (TRUE) or not (FALSE).

Value

A vector of size sum(N * (N - 1)) or a list of length(N) square matrices, with matrix sizes determined by ⁠N[1], N[2], ...⁠.

See Also

simnetwork, peer.avg.

Examples

# Generate a list of adjacency matrices
## Sub-network sizes
N <- c(250, 370, 120)  
## Rate of friendship
p <- c(0.2, 0.15, 0.18)   
## Network data
u <- unlist(lapply(1:3, function(x) rbinom(N[x] * (N[x] - 1), 1, p[x])))
W <- vec.to.mat(u, N)

# Convert W into a list of row-normalized matrices
G <- norm.network(W)

# Recover u
v <- mat.to.vec(G, ceiled = TRUE)
all.equal(u, v)

CDatanet documentation built on April 3, 2025, 11:07 p.m.