Skip to main content

CausalStructures CausalStructures logoCausalStructures logo

CausalStructures is an R binding to the Julia package CausalStructures.jl, for representing, validating, and reasoning about causal graphs

Installation

Install CausalStructures from GitHub:

pak::pak("BjarkeHautop/CausalStructures")

CausalStructures needs Julia (>= 1.13); see https://julialang.org/install/ for the recommended way to install Julia. Once the package is installed, build the compiled CausalStructures.jl library it calls with

jlbind::install_library()

This takes a few minutes while Julia downloads and compiles the dependencies, and needs to be redone after installing or updating CausalStructures (or any other package built on jlbind, since they share one compiled library). Pass julia = "/path/to/julia" (or set the JULIA environment variable) to use a Julia other than the one on the PATH.

The compiled library uses the runtime of the Julia that built it, so run jlbind::install_library(force = TRUE) again if that Julia is removed (for example by a juliaup update).

Graph types

The following graph types are supported, shown here following the abstract type hierarchy from CausalStructures.jl:

CausalGraph
├─ DAG            Directed Acyclic Graph
├─ UG             Undirected Graph
├─ AbstractPDAG   All Partially Directed Acyclic Graphs
│  ├─ PDAG        Partially Directed Acyclic Graph
│  ├─ CPDAG       Completed Partially Directed Acyclic Graph
│  └─ MPDAG       Maximally Oriented Partially Directed Acyclic Graph
├─ ADMG           Acyclic Directed Mixed Graph
├─ AbstractAG     All Ancestral Graphs
│  ├─ AG          Ancestral Graph
│  └─ MAG         Maximal Ancestral Graph
├─ PAG            Partial Ancestral Graph
└─ UNKNOWN        No structural constraints

Each graph class encodes structural constraints, such as no cycles, closed under Meek’s rules, etc., and these constraints are verified on construction.

Example

First, we define a causal graph using the cgraph() function, giving its edges and its class:

library(CausalStructures)

dag <- cgraph("A --> X + Y, X --> M --> Y", class = "DAG")

# Alternatively, wrap the edges in %...%, and then not use a string:
cgraph(A %-->% X + Y, X %-->% M %-->% Y, class = "DAG")
#> DAG with 4 nodes and 4 edges:
#>   nodes: A, M, X, Y
#>   edges:
#>     A --> X, A --> Y, X --> M, M --> Y

You can then run a number of queries and transformations on the causal graph:

parents(dag, "Y")
#> [1] "A" "M"
adjustment_set(dag, "X", "Y")
#> [1] "A"

cpdag <- dag_to_cpdag(dag)
cpdag
#> CPDAG with 4 nodes and 4 edges:
#>   nodes: A, M, X, Y
#>   edges:
#>     A --- X, M --- X, M --> Y, A --> Y

and plot it, here highlighting the treatment, the outcome and the adjustment set:

cgraph_plot(dag) |>
  style_nodes(fill = "lightskyblue", nodes = c("X", "Y")) |>
  style_nodes(fill = "gold", nodes = adjustment_set(dag, "X", "Y"))

The DAG drawn left to right, with X and Y in blue and the adjustment set, A, in gold.

See the documentation site for the full documentation.

Multi-threading

Some algorithms (all_backdoor_sets(), all_adjustment_sets(), all_frontdoor_sets(), enumerate_dags(), count_dags(), enumerate_mags(), pagcauses()) can use multiple threads. To do so, set the JULIA_NUM_THREADS environment variable (a number, or "auto" for one per core) before loading the package, for example in your .Renviron file or with

Sys.setenv(JULIA_NUM_THREADS = "4")
library(CausalStructures)

License

Full license

MIT + file LICENSE

Citation

Citing CausalStructures

Developers

Bjarke Hautop Kristensen
Author, maintainer, copyright holder

More about authors…

Dev status

Codecov test coverage R-CMD-check