library(simDAG)
set.seed(42)
#### with one parent node ####
# define conditional distributions
distr <- list(male=list("rnorm", mean=100, sd=5),
female=list("rcategorical", probs=c(0.1, 0.2, 0.7)))
# define DAG
dag <- empty_dag() +
node("sex", type="rcategorical", labels=c("male", "female"),
output="factor", probs=c(0.4, 0.6)) +
node("chemo", type="rbernoulli", p=0.5) +
node("A", type="conditional_distr", parents="sex", distr=distr)
# generate data
data <- sim_from_dag(dag=dag, n_sim=1000)
#### with two parent nodes ####
# define conditional distributions with interaction between parents
distr <- list(male.FALSE=list("rnorm", mean=100, sd=5),
male.TRUE=list("rnorm", mean=100, sd=20),
female.FALSE=list("rbernoulli", p=0.5),
female.TRUE=list("rcategorical", probs=c(0.1, 0.2, 0.7)))
# define DAG
dag <- empty_dag() +
node("sex", type="rcategorical", labels=c("male", "female"),
output="factor", probs=c(0.4, 0.6)) +
node("chemo", type="rbernoulli", p=0.5) +
node("A", type="conditional_distr", parents=c("sex", "chemo"), distr=distr)
# generate data
data <- sim_from_dag(dag=dag, n_sim=1000)
Run the code above in your browser using DataLab