Symmetrize an adjacency matrix

to_symmetric_matrix(
  g,
  rule = c("weak", "mutual", "out", "in", "average", "max", "min"),
  na.rm = TRUE
)

Arguments

g

input matrix

rule

character, which rule to follow when deciding how to symmetrize

na.rm

logical, should missing values for the cells be ignored? This is used for the rules "average", "max", or "min". For rules "out" and "in", the NA is copied over if it is in a cell that is copied by the rule. Under "weak", a NA is copied if it is in either i->j or j->i and the other edges is not 1. Under "mutual", a NA is copied if it is in either i->j or j->i and the other edges is 1.

Value

symmatrized matrix

Details

Turn a (adjacency) matrix into a symmetric one, according to several potential rules.

The diagonal of the matrix remains unaffected.

Rule out: g is made symmetric by copying the upper triangle onto the lower triangle.

Rule in: g is made symmetric by copying the lower triangle onto the upper triangle.

Rule weak: every edge is assumed to be reciprocated. Hence, an edge from $i$ to $j$ also becomes an edge from $j$ to $i$, Mathematically: i<->j iff i->j or i<-j (OR rule)

Rule mutual: every edge is only maintained if it is already reciprocated in g. Hence, if an edge from $i$ to $j$ exist and also from $j$ to $i$, these edges are maintained in the resulting matrix. Otherwise (for unreciprocated edges or null dyads), the resulting matrix will neither contain an edge from $i$ to $j$ nor from $j$ to $i$. Mathematically: i<->j iff i->j and i<-j (AND rule)

Rule average: the values for the edges ($i$, $j$) and ($j$, $i$) are averaged and assigned to both edges in the resulting matrix. This will typically result in a non-binary matrix.

Rule max: edges ($i$, $j$) and ($j$, $i$) are both assigned the highest value of the two edges. If input matrix g is binary, this will yield the same result as rule weak.

Rule min: edges ($i$, $j$) and ($j$, $i$) are both assigned the lowest value of the two edges. If input matrix g is binary, this will yield the same result as rule mutual.

Examples

m <- matrix(c(0,1,1,0,1,0,0,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,0,1,0), byrow = TRUE, ncol = 5)#' 
m
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    1    0    1
#> [2,]    0    0    1    0    0
#> [3,]    0    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    0    1    0
to_symmetric_matrix(m, "out")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    1    0    1
#> [2,]    1    0    1    0    0
#> [3,]    1    1    0    1    1
#> [4,]    0    0    1    0    1
#> [5,]    1    0    1    1    0
to_symmetric_matrix(m, "in")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    0    0    1    1
#> [2,]    0    0    1    0    0
#> [3,]    0    1    0    1    0
#> [4,]    1    0    1    0    1
#> [5,]    1    0    0    1    0
to_symmetric_matrix(m, "weak")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    1    1    1
#> [2,]    1    0    1    0    0
#> [3,]    1    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    1    1    0
to_symmetric_matrix(m, "mutual")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    0    0    0    1
#> [2,]    0    0    1    0    0
#> [3,]    0    1    0    1    0
#> [4,]    0    0    1    0    1
#> [5,]    1    0    0    1    0
to_symmetric_matrix(m, "average")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]  0.0  0.5  0.5  0.5  1.0
#> [2,]  0.5  0.0  1.0  0.0  0.0
#> [3,]  0.5  1.0  0.0  1.0  0.5
#> [4,]  0.5  0.0  1.0  0.0  1.0
#> [5,]  1.0  0.0  0.5  1.0  0.0
to_symmetric_matrix(m, "max")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    1    1    1
#> [2,]    1    0    1    0    0
#> [3,]    1    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    1    1    0
to_symmetric_matrix(m, "min")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    1    0    1
#> [2,]    0    0    1    0    0
#> [3,]    0    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    0    1    0
m[1, 2] <- m[3, 1] <- NA
m
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0   NA    1    0    1
#> [2,]    0    0    1    0    0
#> [3,]   NA    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    0    1    0
to_symmetric_matrix(m, "out")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0   NA    1    0    1
#> [2,]   NA    0    1    0    0
#> [3,]    1    1    0    1    1
#> [4,]    0    0    1    0    1
#> [5,]    1    0    1    1    0
to_symmetric_matrix(m, "in")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    0   NA    1    1
#> [2,]    0    0    1    0    0
#> [3,]   NA    1    0    1    0
#> [4,]    1    0    1    0    1
#> [5,]    1    0    0    1    0
to_symmetric_matrix(m, "weak")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0   NA    1    1    1
#> [2,]   NA    0    1    0    0
#> [3,]    1    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    1    1    0
to_symmetric_matrix(m, "mutual")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    0   NA    0    1
#> [2,]    0    0    1    0    0
#> [3,]   NA    1    0    1    0
#> [4,]    0    0    1    0    1
#> [5,]    1    0    0    1    0
to_symmetric_matrix(m, "average")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]  0.0    0  1.0  0.5  1.0
#> [2,]  0.0    0  1.0  0.0  0.0
#> [3,]  1.0    1  0.0  1.0  0.5
#> [4,]  0.5    0  1.0  0.0  1.0
#> [5,]  1.0    0  0.5  1.0  0.0
to_symmetric_matrix(m, "max")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    0    1    1    1
#> [2,]    0    0    1    0    0
#> [3,]    1    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    1    1    0
to_symmetric_matrix(m, "min")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0   NA    1    0    1
#> [2,]    0    0    1    0    0
#> [3,]   NA    1    0    1    1
#> [4,]    1    0    1    0    1
#> [5,]    1    0    0    1    0