邻接矩阵
在本练习中,您将提取并计算网络的一阶和二阶邻接矩阵。
您已经在幻灯片中看到如何使用 as_adjaceny_matrix() 函数提取一阶邻接矩阵。
对于二阶邻接矩阵,您需要将一阶矩阵与其自身相乘,并将所有正值替换为 1,因为我们只处理无权网络。还需要确保主对角线元素为 0,因为不允许自环边。
本练习是课程的一部分
使用 R 进行网络数据的预测分析
练习说明
- 使用
as_adjacency_matrix()函数提取网络的邻接矩阵。将该矩阵命名为AdjacencyMatrix。 - 通过将
AdjacencyMatrix与其自身相乘来计算二阶邻接矩阵,并将其命名为SecondOrderMatrix_adj。 - 基于
SecondOrderMatrix_adj构造一个新矩阵SecondOrderMatrix,将所有正值置为 1。主对角线元素应为 0。 - 查看
SecondOrderMatrix的前 10 行和前 10 列。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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[___:___, ___:___]