dag2matrix {simDAG} | R Documentation |
Obtain a Adjacency Matrix from a DAG
object
Description
The sim_from_dag
function requires the user to specify the causal relationships inside a DAG
object containing node information. This function takes this object as input and outputs the underlying adjacency matrix. This can be useful to plot the theoretical DAG or to check if the nodes have been specified correctly.
Usage
dag2matrix(dag, include_root_nodes=TRUE, include_td_nodes=FALSE)
Arguments
dag |
A |
include_root_nodes |
Whether to include root nodes in the output matrix. Should usually be kept at |
include_td_nodes |
Whether to include time-dependent nodes added to the |
Details
An adjacency matrix is simply a square matrix in which each node has one column and one row associated with it. For example, if the node A has a causal effect on node B, the matrix will contain 1
in the spot matrix["A", "B"]
.
If a time-varying node is also defined as a time-fixed node, the parents of both parts will be pooled when creating the output matrix.
Value
Returns a numeric square matrix with one row and one column per used node in dag
.
Author(s)
Robin Denz
See Also
Examples
library(simDAG)
# some example DAG
dag <- empty_dag() +
node("death", type="binomial", parents=c("age", "sex"), betas=c(1, 2),
intercept=-10) +
node("age", type="rnorm", mean=10, sd=2) +
node("sex", parents="", type="rbernoulli", p=0.5) +
node("smoking", parents=c("sex", "age"), type="binomial",
betas=c(0.6, 0.2), intercept=-2)
# get adjacency matrix
dag2matrix(dag)
# get adjacency matrix using only the child nodes
dag2matrix(dag, include_root_nodes=FALSE)
## adding time-varying nodes
dag <- dag +
node_td("disease", type="time_to_event", parents=c("age", "smoking"),
prob_fun=0.01) +
node_td("death", type="time_to_event", parents=c("age", "sex", "smoking",
"disease"),
prob_fun=0.001, event_duration=Inf)
# get adjacency matrix including all nodes
dag2matrix(dag, include_td_nodes=TRUE)
# get adjacency matrix including only time-constant nodes
dag2matrix(dag, include_td_nodes=FALSE)
# get adjacency matrix using only the child nodes
dag2matrix(dag, include_root_nodes=FALSE)