Create a matrix based on a vertex attribute

make_matrix_from_vertex_attribute(
  x,
  name,
  measure = c("absdiff", "diff", "sum", "max", "min", "mean", "sender", "receiver",
    "equal"),
  diag = FALSE
)

Arguments

x

input, a vector or an object of class igraph or network

name

the name of the attribute to be extracted. This will only be used if x is of class igraph or network

measure

character, currently a choice between "absdiff", "diff", "sum", "max", "min". The default value is "absdiff"

diag

if FALSE (the default), the diagonal will be zero. If anything else, the diagonal is filled with that (if it fits)

Value

matrix

Details

This function creates a matrix on the basis of a vertex attribute in an object of class igraph or network. In this case, the vertex attribute provided in the name argument will be extracted.

Alternatively, x can be a numeric vector provided by the user.

This vector/attribute is then used to construct a valued matrix, which can be fed into igraph or network for further use.

The function hosts several options to create this valued matrix with:

  • absdiff the absolute difference between values for two vertices

  • diff the difference is returned, so the value of the vertex with the lower vertex index minus the value of the vertex with the higher vertex index. This value can be negative.

  • sum the sum of the values for both vertices

  • max the highest value between the two vertices

  • min the highest value between the two vertices

  • mean the mean between the two vertices

  • sender the value of the sender's attribute in the entire row

  • receiver the value of the receiver's attribute in the entire column

  • equal 1 if both vertices have the same on the attribute, 0 otherwise

Most of the options speak for themselves. The sender option is used to model a sender effect. For example, if it is to be tested whether the sender's age drives edges, the sender's age is the edge attribute for every edge this sender is involved in. Mathematically, this means that the sender's age should be in all of the cells of that sender's row.

Similarly, the receiver option places the receiver's attribute in each column. For the attribute 'age', this makes the edge attribute equal to the receiver's age, for every edge the vertex is the receiver.

If diag == FALSE (the default), the diagonal of the returned matrix will contain zeroes. If diag is any other value, then that will be inserted into the diagonal. Using recycling rules, any object of fitting length is accepted. If diag == TRUE, the measure will also be applied to the diagonal elements.

Examples

make_matrix_from_vertex_attribute(1:5)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    2    3    4
#> [2,]    1    0    1    2    3
#> [3,]    2    1    0    1    2
#> [4,]    3    2    1    0    1
#> [5,]    4    3    2    1    0
make_matrix_from_vertex_attribute(1:5, measure = "sum", diag = 99)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   99    3    4    5    6
#> [2,]    3   99    5    6    7
#> [3,]    4    5   99    7    8
#> [4,]    5    6    7   99    9
#> [5,]    6    7    8    9   99
make_matrix_from_vertex_attribute(1:5, measure = "max", diag = NA)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   NA    2    3    4    5
#> [2,]    2   NA    3    4    5
#> [3,]    3    3   NA    4    5
#> [4,]    4    4    4   NA    5
#> [5,]    5    5    5    5   NA
make_matrix_from_vertex_attribute(1:5, measure = "sum", diag = TRUE)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    3    4    5    6
#> [2,]    3    4    5    6    7
#> [3,]    4    5    6    7    8
#> [4,]    5    6    7    8    9
#> [5,]    6    7    8    9   10
make_matrix_from_vertex_attribute(1:5, measure = "min", diag = 11:15)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   11    1    1    1    1
#> [2,]    1   12    2    2    2
#> [3,]    1    2   13    3    3
#> [4,]    1    2    3   14    4
#> [5,]    1    2    3    4   15
make_matrix_from_vertex_attribute(1:5, measure = "sender")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    1    1    1
#> [2,]    2    0    2    2    2
#> [3,]    3    3    0    3    3
#> [4,]    4    4    4    0    4
#> [5,]    5    5    5    5    0
make_matrix_from_vertex_attribute(1:5, measure = "receiver")
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    2    3    4    5
#> [2,]    1    0    3    4    5
#> [3,]    1    2    0    4    5
#> [4,]    1    2    3    0    5
#> [5,]    1    2    3    4    0
if (FALSE) { # \dontrun{
data(florentine, package = "SNA4DSData")
# absdiff, with zeroes on the diagonal
make_matrix_from_vertex_attribute(florentine$floattrs$Wealth)
} # }