在 R 中找到完整的邻接矩阵
Finding the complete adjacency matrix in R
我有一个来自包 'bnlearn' 的邻接矩阵,使用函数 amat(矩阵是非循环的)。例如:
+---+-------------------------------+
| | 1 2 3 4 5 |
+---+-------------------------------+
| 1 | 0 1 0 0 0 |
| 2 | 0 0 1 0 0 |
| 3 | 0 0 0 1 0 |
| 4 | 0 0 0 0 1 |
| 5 | 0 0 0 0 0 |
+---+-------------------------------+
我需要从中找到完整的依赖矩阵。
对于一个滞后依赖矩阵,我可以使用:
New_matrix<- if(old_matrix+old_matrix*old_matrix)>0 then 1 else 0
对于两个滞后依赖矩阵,我可以使用:
New_matrix_2<- if(new_matrix+new_matrix*old_matrix)>0 then 1 else 0
问题是我不知道邻接在哪里完成,也就是说,我要运行迭代多少次才能得到包含所有相互依赖性的最终矩阵?
+---+-------------------------------+
| | 1 2 3 4 5 |
+---+-------------------------------+
| 1 | 0 1 1 1 1 |
| 2 | 0 0 1 1 1 |
| 3 | 0 0 0 1 1 |
| 4 | 0 0 0 0 1 |
| 5 | 0 0 0 0 0 |
+---+-------------------------------+
对此,答案是3次迭代。但是我需要解决这个问题的矩阵是 500x500。
有没有直接的方法可以得到完整的邻接矩阵?
要查找所有节点的路径,使用 igraph
包可能更容易。
使用你的例子,
library(bnlearn)
library(igraph)
# Create BN in your example
g <- empty.graph(LETTERS[1:5])
amat(g) <- rbind(cbind(0, diag(4)),0)
amat(g)
# A B C D E
# A 0 1 0 0 0
# B 0 0 1 0 0
# C 0 0 0 1 0
# D 0 0 0 0 1
# E 0 0 0 0 0
# Convert to igraph object using BN adj. matrix as input
g1 <- graph_from_adjacency_matrix(amat(g))
# You can find all ancestors for each node by using
# the mode="in" argument, and order to specify the depth of the search
neighborhood(g1, order=nrow(amat(g)), mode="in")
# Similarly, you can get the full connected graph
# using the same options
ances <- connect(g1, order=nrow(amat(g)), mode="in" )
get.adjacency(ances, sparse=FALSE)
# A B C D E
# A 0 1 1 1 1
# B 0 0 1 1 1
# C 0 0 0 1 1
# D 0 0 0 0 1
# E 0 0 0 0 0
或者,您可以使用指数矩阵
m <- amat(g)
1* as.matrix((Matrix::expm(m) - diag(ncol(m))) > 0)
我有一个来自包 'bnlearn' 的邻接矩阵,使用函数 amat(矩阵是非循环的)。例如:
+---+-------------------------------+
| | 1 2 3 4 5 |
+---+-------------------------------+
| 1 | 0 1 0 0 0 |
| 2 | 0 0 1 0 0 |
| 3 | 0 0 0 1 0 |
| 4 | 0 0 0 0 1 |
| 5 | 0 0 0 0 0 |
+---+-------------------------------+
我需要从中找到完整的依赖矩阵。 对于一个滞后依赖矩阵,我可以使用:
New_matrix<- if(old_matrix+old_matrix*old_matrix)>0 then 1 else 0
对于两个滞后依赖矩阵,我可以使用:
New_matrix_2<- if(new_matrix+new_matrix*old_matrix)>0 then 1 else 0
问题是我不知道邻接在哪里完成,也就是说,我要运行迭代多少次才能得到包含所有相互依赖性的最终矩阵?
+---+-------------------------------+
| | 1 2 3 4 5 |
+---+-------------------------------+
| 1 | 0 1 1 1 1 |
| 2 | 0 0 1 1 1 |
| 3 | 0 0 0 1 1 |
| 4 | 0 0 0 0 1 |
| 5 | 0 0 0 0 0 |
+---+-------------------------------+
对此,答案是3次迭代。但是我需要解决这个问题的矩阵是 500x500。 有没有直接的方法可以得到完整的邻接矩阵?
要查找所有节点的路径,使用 igraph
包可能更容易。
使用你的例子,
library(bnlearn)
library(igraph)
# Create BN in your example
g <- empty.graph(LETTERS[1:5])
amat(g) <- rbind(cbind(0, diag(4)),0)
amat(g)
# A B C D E
# A 0 1 0 0 0
# B 0 0 1 0 0
# C 0 0 0 1 0
# D 0 0 0 0 1
# E 0 0 0 0 0
# Convert to igraph object using BN adj. matrix as input
g1 <- graph_from_adjacency_matrix(amat(g))
# You can find all ancestors for each node by using
# the mode="in" argument, and order to specify the depth of the search
neighborhood(g1, order=nrow(amat(g)), mode="in")
# Similarly, you can get the full connected graph
# using the same options
ances <- connect(g1, order=nrow(amat(g)), mode="in" )
get.adjacency(ances, sparse=FALSE)
# A B C D E
# A 0 1 1 1 1
# B 0 0 1 1 1
# C 0 0 0 1 1
# D 0 0 0 0 1
# E 0 0 0 0 0
或者,您可以使用指数矩阵
m <- amat(g)
1* as.matrix((Matrix::expm(m) - diag(ncol(m))) > 0)