Getting started

1. Main analysis

Run this code to load the causalDisco package, load in the example dataset and our jointly made expert graph:

# load the causalDisco package
library("causalDisco")

# load the data
load(url("https://biostatistics.dk/teaching/advtopics/data/nlsdata.rda"))

# load the expert graph
load(url("https://biostatistics.dk/teaching/advtopics/data/expertm.rda"))

You will now have two objects available in your global environment: nlsdata (the dataset) and expertm (an adjacency matrix representation of the “expert graph” we constructed together).

The example code provided below uses the example data nlsdata, but you can use whichever dataset you prefer, if you would rather try the functions on your own data. Note that exercise 2.5 shows you how you may type in your own expert graph and use it in the package.

1.1 Inspect data and expert graph

  • Have a quick look at the dataset, e.g. by using the summary() command on it, to make sure it has been loaded correctly. You can find more information about the dataset in the previous exercise (“Human generated DAG”).

  • Have a look at the expert graph and check that it corresponds to the graph we have drawn together. The adjacency matrix can be plotted if you convert it to a graph structure first. This is done using exp_graph <- caugi::as_caugi(expertm, class = "DAG"). You can plot it using plot(exp_graph).

1.2 Run PC

Start by setting up the algorithm we want to use. We will use the tpc() algorithm as implemented in the causalDisco package. We also need to set the test we will use for conditional independence testing and the significance level.

Typically the pc() function is used to set up the PC algorithm, but here we will use the tpc() function, which is a temporal version of the PC algorithm.

pcsetup <- tpc(                       # Use the tpc implementation from the causalDisco package
   engine = "causalDisco",                   
   test = "reg",                      # Select test statistic for conditional independence testing
   alpha = 0.05                       # Use a significance level of 0.05
)

Now we have set up the algorithm we wish to use. Then we can call the disco() function.

pcres <- disco(nlsdata,    # Provide the data frame for the analysis as first argument
                pcsetup)    # Use this algorithm for the causal discovery analysis

Plot your result using plot() (see code example below), and have a look at the resulting graph.

  • Do you find any meaningful proposed direct causal relationships? What about indirect (mediates) causal relationships?
  • Do you find any proposed causal relationships you do not think is correct?

Next, compare the CPDAG from the PC algorithm with our jointly made expert graph:

  • Where do the two graphs agree on adjacencies (i.e., presence of an edge, disregarding its orientation)?
  • Where do the two graphs disagree on adjacencies?
  • Among the adjacencies included in both graphs, where do the two agree concerning the orientation of the edge? Where do they disagree?
  • Which graphs do you trust more to be a good representation of the data generating mechanism?


We can set a particular layout of the nodes to ensure that the position is kept from one plot to the next. This can be done by setting the position of the nodes manually. In our case, we have three time points: r1, r6 and r12.

my_layout <- data.frame(
    name = c("r1_mcollege", "r1_urban", "r1_hhchildren", 
             "r6_depressed", "r6_docvisits", "r6_exercise",
             "r12_depressed", "r12_docvisits", "r12_health"),
    x = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
    y = c(1, 2, 3, 1, 2, 3, 1, 2, 3)
)

plot(pcres, layout = my_layout)

2. Additional analyses

The following exercises 2.1-2.4 can be done in whichever order you prefer. There are way too many for you to do all of them – choose the ones you find interesting!

The exercises cover these topics:

2.1 Comparing graphs

Here we will consider different ways to compare two graphs. First, we consider the confusion() function for comparing edges, which computes a confusion matrix. The function takes two arguments: the first is the estimated graph, and the second is the “true” graph, and both should be caugi objects. Luckily, the output from disco() already contains a caugi object.

# first argument is the estimated graph, second is the "true" graph
confusion(pcres$caugi, exp_graph)

Note that for this to work, you need to have the expert graph in the form of a caugi object as shown above.

This outputs 4 numbers:

  1. The number of true positives (tp)

  2. The number of true negatives (tn)

  3. The number of false positives (fp)

  4. The number of false negatives (fn)

What do you think these numbers mean? Try having a look at the documentation for the confusion function by typing ?confusion if you get stuck.

Now try comparing edge directions:

confusion(pcres$caugi, exp_graph, type = "dir")
  • Which results do you get now?

  • In what way do you think these results are conditional on the results above?

Again, you may have a look at the documentation for the confusion function if you get stuck.

2.2 Changing the significance level

In the main analysis we used a significance level (alpha) of 0.05 to estimate the graphs. Rerun the analysis above using different significance levels (try e.g. 0.1 and 0.001) and investigate how the results change:

  • For which significance levels do the graphs become denser/sparser?

  • For which significance levels is the result closer to the expert graph?

  • Do the estimated graphs become more/less plausible?

  • Do you run into trouble?

2.3 Changing the test

So far, we have used the the reg option to test for conditional independence. This test accommodates mixed numeric/binary data.

causalDisco and tetrad have many additional options for testing conditional independence, for example the conditional_gaussian test, which can also be used for mixed categorical and continuous data. Here is a description of it from the Tetrad manual:

“Conditional Gaussian Test is a likelihood ratio test based on the conditional Gaussian likelihood function. This is intended for use with datasets where there is a mixture of continuous and discrete variables. It is assumed that the continuous variables are Gaussian conditional on each combination of values for the discrete variables, though it will work fairly well even if that assumption does not hold strictly. This test may be used with any constraint-based algorithm (PC, FCI, etc.). See Andrews, B., Ramsey, J., & Cooper, G. F. (2018). Scoring Bayesian networks of mixed variables. International journal of data science and analytics, 6(1), 3-18.”

Run the PC algorithm again using the conditional_gaussian conditional independence test. This requires

pcres_cortest <- disco(nlsdata, 
                       tpc(engine= "causalDisco", 
                          test = "conditional_gaussian", 
                          alpha = 0.05))

Compare the results with what you found in exercise 1.2:

  • Which edges are shared and which edges differ?

  • Which estimated graphs do you find most plausible and why?

Again, compare the results with what you found in exercise 1.2:

  • Which edges are shared and which edges differ?

  • Which estimated graphs do you find most plausible and why?

2.4 More variables or more observations?

Load in a new version of the NLS97 dataset:

#load the large dataset
load(url("https://biostatistics.dk/teaching/advtopics/data/nlsdata2.rda"))

The dataset nlsdata2 contains more variables, but has missing values.

  1. Get an overview of the data, and focus especially on missing information (you can e.g. run summary(nlsdata2)).

Missing data is a big problem in causal discovery (just like other statistical analyses) and there are multiple ways to deal with it (see e.g. Witte et al., 2022), but for now we will do a complete case analysis:

  1. Remove observations with missing values using the function na.omit() and count how many observations remain (using e.g. nrow()).

  2. Estimate a CPDAG using the PC algorithm on the fully observed version of the nlsdata2 dataset and compare if with your CPDAG from Exercise 1.2. Reflect on how selection bias might be affecting your results.

  3. Try to construct other subsets of the data, remove missing information (see code hint below), and run PC on them. Think about the tradeoff between including more variables or having less missing information. Are there specific important variables you think need to be included for the analysis to be more valid?

# Code hint: How to remove all observations with missing values from a dataset:
nlsdata2_completecases <- na.omit(nlsdata2)

References

Ryan M. Andrews, Ronja Foraita, Vanessa Didelez, Janine Witte. “A practical guide to causal discovery with cohort data”. arXiv preprint arXiv:2108.13395, https://arxiv.org/abs/2108.13395, 2021.

Diego Colombo and Marloes H Maathuis. “Order-independent constraint-based causal structure learning”. Journal of Machine Learning Research, 15(1):3741–3782, 2014.

Ronja Foraita and Janine Witte. “micd: Multiple Imputation in Causal Graph Discovery”, R package version 1.1.1, 2023.

Christopher Meek. “Causal inference and causal explanation with background knowledge”. Proceedings of the Eleventh conference on Uncertainty in artificial intelligence, pages 403–410, 1995.

Janine Witte, Ronja Foraita, and Vanessa Didelez. “Multiple imputation and test‐wise deletion for causal discovery with incomplete cohort data.” Statistics in Medicine, 41.23 4716-4743, 2022


The causalDisco package mainly uses other software packages as a backend for doing computations. The disco() function hence allows you to access all these different tools without having to learn how each package works. The backend packages include: