Learn R Programming

⚠️There's a newer version (1.1.3) of this package.Take me there.

Overview

BCDAG (Bayesian Causal DAG) is a package for Bayesian DAG structure learning and causal effect estimation from observational Gaussian data.

The methodology implemented has been presented in Castelletti, F. & Mascaro, A. (2021). Structural learning and estimation of joint causal effects among network-dependent variables, Statistical Methods & Applications, 1-26

Installation

The package can be installed executing the following code:

# install.packages("devtools")
devtools::install_github("alesmascaro/BCDAG")

How to use

The workflow of the package consists of two sequential steps: causal structure learning, performed through the function learn_DAG() and causal effect estimation, performed through the function get_causaleffect(). For a more detailed description of the inner mechanisms of these two functions, we refer the reader to the vignettes.

Example

Before using the two main functions of the package, we generate some data:

library(BCDAG)
# Randomly generate a DAG and the DAG-parameters
q = 8
w = 0.2
set.seed(123)
DAG = rDAG(q = q, w = w)
outDL = rDAGWishart(n = 1, DAG = DAG, a = q, U = diag(1, q))
L = outDL$L; D = outDL$D
Sigma = solve(t(L))%*%D%*%solve(L)
n = 200
# Generate observations from a Gaussian DAG-model
X = mvtnorm::rmvnorm(n = n, sigma = Sigma)

We can use the function learn_DAG() to perform causal structure learning from the generated observational dataset:

# Run the MCMC
out = learn_DAG(S = 5000, burn = 1000, a = q, U = diag(1,q)/n, data = X, w = w)

Next, we can compute the BMA estimate of the causal effect on a response variable consequent to a hard intervention on a set of nodes by using get_causaleffect():

# the causal effect on node 1 of an intervention on {3,4}
out |>
  get_causaleffect(targets = c(3,4), response = 1)

The three steps here implemented are detailed in the vignettes 1 - Random data generation from Gaussian DAG-models, 2 - MCMC scheme for posterior inference of Gaussian DAG models: the learn_DAG() function and 3 - Elaborate on the output of learn_DAG() using get_ functions

Authors

Copy Link

Version

Install

install.packages('BCDAG')

Monthly Downloads

381

Version

1.1.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Alessandro Mascaro

Last Published

January 30th, 2025

Functions in BCDAG (1.1.2)

get_MAPdag

Compute the maximum a posteriori DAG model from the MCMC output
get_MPMdag

Compute the median probability DAG model from the MCMC output
get_neighboringDAGs

Enumerate all neighbors of a DAG
get_causaleffect

Estimate total causal effects from the MCMC output
get_edgeprobs

Compute posterior probabilities of edge inclusion from the MCMC output
as_graphNEL

Transform adjacency matrix into graphNEL object
causaleffect

Compute causal effects between variables
confint.bcdagCE

Credible Intervals for bcdagCE Object
get_diagnostics

MCMC diagnostics
learn_DAG

MCMC scheme for Gaussian DAG posterior inference
print.bcdag

bcdag object print
summary.bcdag

bcdag object summaries
plot.bcdag

bcdag object plot
print.bcdagCE

bcdagCE object print
rDAG

Generate a Directed Acyclic Graph (DAG) randomly
plot.bcdagCE

bcdagCE object plot
leukemia

Protein levels for 68 diagnosed AML patients of subtype M2
rDAGWishart

Random samples from a compatible DAG-Wishart distribution
summary.bcdagCE

bcdagCE object summary