How to get the adjacency matrix from the dag?

I know how to generate a DAG.

library(pcalg)
dag <- randDAG(10, 1, "er") 

But now I need to get the adjacency matrix of the above dag. I guess there exists such a function but I don't know what it is.

The chatgpt give me the false result:

# Get the adjacency matrix from the DAG
adjacency_matrix <- amat(dag)

# Print or inspect the adjacency matrix
print(adjacency_matrix)

But unfortunately, Error in amat(dag) : could not find function "amat"

library(pcalg)
dag <- randDAG(10, 1, "er")
as(dag,"matrix")
#>    1 2 3 4         5         6         7 8 9 10
#> 1  0 0 0 0 0.4478490 0.0000000 0.8779392 0 0  0
#> 2  0 0 0 0 0.0000000 0.0000000 0.0000000 0 0  0
#> 3  0 0 0 0 0.0000000 0.0000000 0.0000000 0 0  0
#> 4  0 0 0 0 0.0000000 0.0000000 0.5793444 0 0  0
#> 5  0 0 0 0 0.0000000 0.0000000 0.0000000 0 0  0
#> 6  0 0 0 0 0.9741464 0.0000000 0.0000000 0 0  0
#> 7  0 0 0 0 0.0000000 0.0000000 0.0000000 0 0  0
#> 8  0 0 0 0 0.0000000 0.0000000 0.0000000 0 0  0
#> 9  0 0 0 0 0.0000000 0.3137807 0.0000000 0 0  0
#> 10 0 0 0 0 0.0000000 0.0000000 0.0000000 0 0  0

Created on 2023-09-02 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.