Skip to content

Instantly share code, notes, and snippets.

@mingjiphd
Last active January 27, 2026 18:41
Show Gist options
  • Select an option

  • Save mingjiphd/ec4fc2f73ea0a5c64ea3273d1a2e41ae to your computer and use it in GitHub Desktop.

Select an option

Save mingjiphd/ec4fc2f73ea0a5c64ea3273d1a2e41ae to your computer and use it in GitHub Desktop.
Causal Analysis in R: Implementing DAGs with a Simple Example
This R script provides a step-by-step tutorial on implementing DAGs in R for causal inference. We use the dagitty and ggdag packages with a simple simulated dataset containing three variables: Y (outcome), A (exposure), and L (confounder).
A step by step video demonstration can be found at: https://youtu.be/12gb1GQCd4I
### How to Implement Directed Acyclic Graphs for Causal Inference in R: The Simple Case
##A Directed Acyclic Graph (DAG) is a finite graph composed of nodes connected
## by directed edges with no cycles, meaning it's impossible to start at any node
## and follow a sequence of edge directions that returns to the same node.
##in DAGs, nodes to be arranged in a linear order that respects the direction of
## edges and represents dependencies or causal relationships.
##Directed Acyclic Graphs (DAGs) are used in causal inference to visually represent
## assumptions about causal relationships, identify confounders, guide variable
## selection for adjustment, and clarify the pathways affecting causal effect estimation
##Generate a sample data set
set.seed(09072025)
n <- 500
L <- rnorm(n) # Confounder
A <- 0.8 * L + rnorm(n) # Exposure: influenced by L
Y <- 1.2 * A + 0.7 * L + rnorm(n) # Outcome: influenced by A and L
dat <- data.frame(L, A, Y)
head(dat)
##Specify and plot the DAG
# Install if not already installed
#install.packages(c("dagitty", "ggdag"))
library(dagitty)
library(ggdag)
# Define the DAG: L -> A, L -> Y, A -> Y
dag <- dagitty("dag {
L -> A
L -> Y
A -> Y
}")
# Plot the DAG
ggdag(dag) + theme_dag()
##Analyze DAG: Find adjustment sets
# Find minimal adjustment sets for estimating effect of A on Y
adjustmentSets(dag, exposure = "A", outcome = "Y")
##Regression for Causal Effect
# Estimate causal effect, adjusting for confounder L
model <- lm(Y ~ A + L, data = dat)
summary(model)
A step by step video demonstration can be found at: https://youtu.be/12gb1GQCd4I
@mingjiphd
Copy link
Author

A step by step video demonstration can be found at: https://youtu.be/12gb1GQCd4I

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment