annennenne / causalDisco

Tools for causal discovery in R

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

causalDisco

causalDisco is in an R package with tools for causal discovery on observational data.

Installation

To install the development version of causalDisco run the following commands from within R (requires that the remotes package is already installed)

remotes::install_github("annennenne/causalDisco")

Note that the package requires installation of the pcalg package as well, which depends on packages available only Bioconductor. There is an installation guide for pcalg here.

Temporal causal discovery

causalDisco includes an implementation of temporal PC, a temporal version of the PC algorithm. The following examples shows how this function may be used to produce a temporal partially directed acyclic graph (TPDAG) for an observed data set with temporal information.

library(causalDisco)

#Simulate data
set.seed(123)
n <- 500

child_x <- rnorm(n)^2
child_y <- 0.5*child_x + rnorm(n)
child_z <- sample(c(0,1), n, replace = TRUE, 
                  prob = c(0.3, 0.7))
adult_x <- child_x + rnorm(n)
adult_z <- as.numeric(child_z + rnorm(n) > 0)
adult_w <- 2*adult_z + rnorm(n)
adult_y <- 2*sqrt(child_x) + adult_w^2 + rnorm(n)

simdata <- data.frame(child_x, child_y, child_z,
                      adult_x, adult_z, adult_w,
                      adult_y)


#Define order
simorder <- c("child", "adult")

#Perform TPC with sparsity psi = 0.01
results <- tpc(simdata, order = simorder, sparsity = 10^(-2))

#Plot results
plot(results)


#Plot results with custom labels for variables and periods
varlabs <- list(`child_x` = "x", `child_y` = "y",
                `child_z` = "z", `adult_x` = "x",
                `adult_z` = "z", `adult_w` = "w",
                `adult_y` = "y")
perlabs <- c("Childhood", "Adulthood")
plot(results, varLabels = varlabs, periodLabels = perlabs)

Here is an example for using the package for plotting a user supplied adjacency matrix with order information:

library(causalDisco)

#Adjacency matrix for the data generating mechanism for simdata
vnames <- c("child_x", "child_y", "child_z", "adult_x", 
            "adult_z", "adult_w", "adult_y")
thisamat <- matrix(c(0, 0, 0, 0, 0, 0, 0,
                     1, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 
                     1, 0, 0, 0, 0, 0, 0,
                     0, 0, 1, 0, 0, 0, 0, 
                     0, 0, 0, 0, 1, 0, 0,
                     1, 0, 0, 0, 0, 1, 0),
                    7, 7, 
                    byrow = TRUE,
                    dimnames = list(vnames, vnames))
thisorder <- c("child", "adult")

#Make temporal adjacency matrix
thistamat <- tamat(thisamat, thisorder)

#Plot
plot(thistamat)

Webtool

The causalDisco webtool provides an overview of R procedures for working with causal discovery in R, including example code. The webtool can be accessed here. Source code for the webtool is available in this repository.

Bugs & requests

If you find bugs or have a request for a new feature, please open an issue.

About

Tools for causal discovery in R


Languages

Language:HTML 99.0%Language:R 1.0%