https://github.com/robindenz1/simdag
An R-Package to Simulate Data from a (Time-Dependent) Causal DAG
https://github.com/robindenz1/simdag
causal-inference directed-acyclic-graph simulation
Last synced: 3 months ago
JSON representation
An R-Package to Simulate Data from a (Time-Dependent) Causal DAG
- Host: GitHub
- URL: https://github.com/robindenz1/simdag
- Owner: RobinDenz1
- License: gpl-3.0
- Created: 2022-05-02T12:46:07.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2026-03-30T11:42:10.000Z (3 months ago)
- Last Synced: 2026-03-30T13:27:11.620Z (3 months ago)
- Topics: causal-inference, directed-acyclic-graph, simulation
- Language: R
- Homepage: https://robindenz1.github.io/simDAG/
- Size: 18.4 MB
- Stars: 18
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE.md
- Codemeta: codemeta.json
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
[](https://www.repostatus.org/#active)
[](https://cran.r-project.org/package=simDAG)
[](https://cran.r-project.org/package=simDAG)
[](https://github.com/RobinDenz1/simDAG/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/RobinDenz1/simDAG)
[](https://doi.org/10.48550/arXiv.2506.01498)
# simDAG 
Author: Robin Denz
## Description
`simDAG` is an R-Package which can be used to generate data from a known directed acyclic graph (DAG) with associated information on distributions and causal coefficients. The root nodes are sampled first and each subsequent child node is generated according to a regression model (linear, logistic, multinomial, cox, ...) or other function. The result is a dataset that has the same causal structure as the specified DAG and by expectation the same distributions and coefficients as initially specified. It also implements a comprehensive framework for conducting discrete-time and discrete-event simulations in a similar fashion and directly supports network dependencies among individuals.
## Installation
A stable version of this package can be installed from CRAN:
```R
install.packages("simDAG")
```
and the developmental version may be installed from github using the `remotes` R-Package:
```R
library(remotes)
remotes::install_github("RobinDenz1/simDAG")
```
## Bug Reports and Feature Requests
If you encounter any bugs or have any specific feature requests, please file an [Issue](https://github.com/RobinDenz1/simDAG/issues).
## Examples
Suppose we want to generate data with the following causal structure:
where `age` is normally distributed with a mean of 50 and a standard deviation of 4 and `sex` is bernoulli distributed with `p = 0.5` (equal number of men and women). Both of these "root nodes" (meaning they have no parents - no arrows pointing into them) have a direct causal effect on the `bmi`. The causal coefficients are 1.1 and 0.4 respectively, with an intercept of 12 and a sigma standard deviation of 2. `death` is modeled as a bernoulli variable, which is caused by both `age` and `bmi` with causal coefficients of 0.1 and 0.3 respectively. As intercept we use -15.
The following code can be used to generate 10000 samples from these specifications:
```{r}
library(simDAG)
dag <- empty_dag() +
node("age", type="rnorm", mean=50, sd=4) +
node("sex", type="rbernoulli", p=0.5) +
node("bmi", type="gaussian", formula= ~ 12 + age*1.1 + sex*0.4, error=2) +
node("death", type="binomial", formula= ~ -15 + age*0.1 + bmi*0.3)
set.seed(42)
sim_dat <- sim_from_dag(dag, n_sim=100000)
```
By fitting appropriate regression models, we can check if the data really does approximately conform to our specifications. First, lets look at the `bmi`:
```{r}
mod_bmi <- glm(bmi ~ age + sex, data=sim_dat, family="gaussian")
summary(mod_bmi)
```
This seems about right. Now we look at `death`:
```{r}
mod_death <- glm(death ~ age + bmi, data=sim_dat, family="binomial")
summary(mod_death)
```
The estimated coefficients are also very close to the ones we specified. More examples can be found in the documentation and the multiple vignettes.
## Citation
If you use this package, please cite the associated article:
Denz, Robin and Nina Timmesfeld (2025). Simulating Complex Crossectional and Longitudinal Data using the simDAG R Package. arXiv preprint, doi: 10.48550/arXiv.2506.01498.
## License
© 2024 Robin Denz
The contents of this repository are distributed under the GNU General Public License. You can find the full text of this License in this github repository. Alternatively, see .