Adjacency matrices
In this exercise, you will extract and compute the first and second order adjacency matrices of the network.
You've already seen how to extract the first order adjacency matrix using the as_adjaceny_matrix()
function in the slides.
For the second-order adjacency matrix, you need to multiply the first order matrix with itself and replace all the positive values with 1 since we are working with unweighted networks only. You also need to make sure the elements on the diagonal are 0 since we do not allow self-edges.
Este exercício faz parte do curso
Predictive Analytics using Networked Data in R
Instruções do exercício
- Extract the network's adjacency matrix using the
as_adjacency_matrix()
function. Name the matrixAdjacencyMatrix
. - Compute the second order adjacency matrix by multiplying
AdjacencyMatrix
with itself and call itSecondOrderMatrix_adj
. - Create a new matrix,
SecondOrderMatrix
, by conditioning onSecondOrderMatrix_adj
to make all positive values equal to 1. The elements on the diagonal should be 0. - Inspect the first 10 rows and first 10 columns of
SecondOrderMatrix
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Extract the adjacency matrix
AdjacencyMatrix <- as_adjacency_matrix(___)
# Compute the second order matrix
SecondOrderMatrix_adj <- ___ %*% ___
# Adjust the second order matrix
SecondOrderMatrix <- ((___) > 0) + 0
diag(SecondOrderMatrix) <- 0
# Inspect the second order matrix
SecondOrderMatrix[___:___, ___:___]