dag2matrix | R Documentation |
DAG
object
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.
dag2matrix(dag, include_root_nodes=TRUE, include_td_nodes=FALSE)
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 |
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.
Returns a numeric square matrix with one row and one column per used node in dag
.
Robin Denz
empty_dag
, node
, node_td
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("cve", 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.