Editing

CausalStructures.add_edges — Function
add_edges(cg::CausalGraph, es::CausalEdge...) -> CausalGraph
add_edges(cg::CausalGraph, es::AbstractVector{CausalEdge}) -> CausalGraph

Return a new graph of the same class with edges es added. Nodes referenced by any edge are added automatically if not already present.

  • cg::CausalGraph: the graph to add edges to.
  • es: the edges to add, as separate arguments or as one vector. They are applied together, so only the result must be a valid graph of the class of cg.

A new CausalGraph of the same class as cg.

julia> dag = DAG("A --> B");

julia> add_edges(dag, directed(:B, :C))
DAG with 3 nodes and 2 edges:
  nodes: A, B, C
  edges:
    A --> B, B --> C

julia> add_edges(dag, directed(:B, :C), directed(:C, :D))
DAG with 4 nodes and 3 edges:
  nodes: A, B, C, D
  edges:
    A --> B, B --> C, C --> D
source
CausalStructures.remove_edges — Function
remove_edges(cg::CausalGraph, es::CausalEdge...) -> CausalGraph
remove_edges(cg::CausalGraph, es::AbstractVector{CausalEdge}) -> CausalGraph

Return a new graph of the same class with edges es removed. Nodes that become isolated are retained. Throws ArgumentError if any edge in es is not present, or if the remaining graph is not a valid graph of that class.

  • cg::CausalGraph: the graph to remove edges from.
  • es: the edges to remove, as separate arguments or as one vector. They are applied together, so only the result must be a valid graph of the class of cg.

A new CausalGraph of the same class as cg.

  • ArgumentError: if any edge in es is not present in cg.
  • ArgumentError: if the remaining graph violates the invariant of its class, which can happen for CPDAG, MPDAG, MAG and PAG. To remove such edges, first reclass cg to a class without that invariant: PDAG for a CPDAG or MPDAG, AG for a MAG, UNKNOWN for a PAG.
julia> dag = DAG("A --> B --> C");

julia> remove_edges(dag, directed(:A, :B), directed(:B, :C))
DAG with 3 nodes and 0 edges:
  nodes: A, B, C
  edges:
    (none)
source
CausalStructures.add_nodes — Function
add_nodes(cg::CausalGraph, ns::Symbol...) -> CausalGraph
add_nodes(cg::CausalGraph, ns::AbstractVector{Symbol}) -> CausalGraph

Return a new graph of the same class with isolated nodes ns added. Nodes already present are ignored.

  • cg::CausalGraph: the graph to add nodes to.
  • ns: the nodes to add, as separate arguments or as one vector.

A new CausalGraph of the same class as cg, or cg itself if every node in ns is already present.

julia> g = DAG("A --> B");

julia> add_nodes(g, :C, :D)
DAG with 4 nodes and 1 edge:
  nodes: A, B, C, D
  edges:
    A --> B

julia> add_nodes(g, :A) === g
true
source
CausalStructures.remove_nodes — Function
remove_nodes(cg::CausalGraph, ns::Symbol...) -> CausalGraph
remove_nodes(cg::CausalGraph, ns::AbstractVector{Symbol}) -> CausalGraph

Return a new graph of the same class with nodes ns and all their incident edges removed. Throws ArgumentError if any node in ns is not present, or if the remaining graph is not a valid graph of that class.

  • cg::CausalGraph: the graph to remove nodes from.
  • ns: the nodes to remove, as separate arguments or as one vector. They are applied together, so only the result must be a valid graph of the class of cg.

A new CausalGraph of the same class as cg.

  • ArgumentError: if any node in ns is not present in cg.
  • ArgumentError: if the remaining graph violates the invariant of its class, which can happen for CPDAG and PAG. Use subgraph instead, which returns the result in a weaker class.
julia> g = DAG("A --> B --> C");

julia> remove_nodes(g, :B)
DAG with 2 nodes and 0 edges:
  nodes: A, C
  edges:
    (none)

julia> remove_nodes(g, :A, :B)
DAG with 1 node and 0 edges:
  nodes: C
  edges:
    (none)
source
CausalStructures.reclass — Function
reclass(cg::CausalGraph, T::Type{<:CausalGraph}) -> T

Reinterpret cg's nodes and edges as a graph of class T. Throws an error if the edges violate the structural constraints of T.

  • cg::CausalGraph: the graph to reclass.

A new graph of class T with the same nodes and edges as cg.

julia> dag = DAG("A --> B --> C");

julia> pdag = reclass(dag, PDAG)
PDAG with 3 nodes and 2 edges:
  nodes: A, B, C
  edges:
    A --> B, B --> C
source