library(simDAG)
set.seed(1234)
## different linear regression models per level of a different covariate
# here, A is the group that is used for the conditioning, B is a predictor
# and Y is the mixture distributed outcome
dag <- empty_dag() +
node("A", type="rbernoulli") +
node("B", type="rnorm") +
node("Y", type="mixture", parents="A",
distr=list(
"A==0", node(".", type="gaussian", formula= ~ -2 + B*2, error=1),
"A==1", node(".", type="gaussian", formula= ~ 3 + B*5, error=1)
))
data <- sim_from_dag(dag, n_sim=100)
head(data)
# also works with multiple conditions
dag <- empty_dag() +
node(c("A", "C"), type="rbernoulli") +
node("B", type="rnorm") +
node("Y", type="mixture", parents=c("A", "C"),
distr=list(
"A==0 & C==1", node(".", type="gaussian", formula= ~ -2 + B*2, error=1),
"A==1", node(".", type="gaussian", formula= ~ 3 + B*5, error=1)
))
data <- sim_from_dag(dag, n_sim=100)
head(data)
# using the mixture node itself in the condition
# see cookbook vignette, section on outliers for more info
dag <- empty_dag() +
node(c("A", "B", "C"), type="rnorm") +
node("Y", type="mixture", parents=c("A", "B", "C"),
distr=list(
"TRUE", node(".", type="gaussian", formula= ~ -2 + A*0.1 + B*1 + C*-2,
error=1),
"Y > 2", node(".", type="rnorm", mean=10000, sd=500)
))
data <- sim_from_dag(dag, n_sim=100)
Run the code above in your browser using DataLab