#---------------------------------------------------------------------------------------
# EXAMPLE 1A: Define some bernoulli nodes (W1,W2,W3), treatment A, outcome Y and put
# together in a DAG object
#---------------------------------------------------------------------------------------
W1 <- node(name="W1", distr="rbern", prob=plogis(-0.5), order=1)
W2 <- node(name="W2", distr="rbern", prob=plogis(-0.5 + 0.5*W1), order=2)
A <- node(name="A", distr="rbern", prob=plogis(-0.5 - 0.3*W1 - 0.3*W2), order=3)
Y <- node(name="Y", distr="rbern", prob=plogis(-0.1 + 1.2*A + 0.3*W1 + 0.3*W2), order=4, EFU=TRUE)
D1A <- set.DAG(c(W1,W2,A,Y))
#---------------------------------------------------------------------------------------
# EXAMPLE 1B: Same as 1A; using alternative "+" syntax and omitting "order" argument
#---------------------------------------------------------------------------------------
D1B <- DAG.empty()
D1B <- D1B + node(name="W1", distr="rbern", prob=plogis(-0.5))
D1B <- D1B + node(name="W2", distr="rbern", prob=plogis(-0.5 + 0.5*W1))
D1B <- D1B + node(name="A", distr="rbern", prob=plogis(-0.5 - 0.3*W1 - 0.3*W2))
D1B <- D1B + node(name="Y", distr="rbern", prob=plogis(-0.1 + 1.2*A + 0.3*W1 + 0.3*W2), EFU=TRUE)
D1B <- set.DAG(D1B)
#---------------------------------------------------------------------------------------
# EXAMPLE 1C: Add a uniformly distributed node and redefine outcome Y as categorical
#---------------------------------------------------------------------------------------
D_unif <- DAG.empty()
D_unif <- D_unif + node("W1", distr="rbern", prob=plogis(-0.5))
D_unif <- D_unif + node("W2", distr="rbern", prob=plogis(-0.5 + 0.5*W1))
D_unif <- D_unif + node("W3", distr="runif", min=plogis(-0.5 + 0.7*W1 + 0.3*W2), max=10)
D_unif <- D_unif + node("Anode", distr="rbern", prob=plogis(-0.5 - 0.3*W1 - 0.3*W2 - 0.2*sin(W3)))
# Categorical syntax 1 (probabilities as values)
D_cat_1 <- D_unif + node("Y", distr="rcategor", probs=c(0.3,0.4))
D_cat_1 <- set.DAG(D_cat_1)
# Categorical syntax 2 (probabilities as formulas)
D_cat_2 <- D_unif + node("Y", distr="rcategor", probs=c(plogis(1.2*Anode + 0.5*cos(W3)),
plogis(-0.5 + 0.7*W1)))
D_cat_2 <- set.DAG(D_cat_2)
#---------------------------------------------------------------------------------------
# EXAMPLE 2A: Define Bernoulli nodes using R rbinom() function, defining prob argument
# for L2 as a function of node L1
#---------------------------------------------------------------------------------------
D <- DAG.empty()
D <- D + node("L1", t=0, distr="rbinom", prob=0.05, size=1)
D <- D + node("L2", t=0, distr="rbinom", prob=ifelse(L1[0]==1,0.5,0.1), size=1)
D <- set.DAG(D)
#---------------------------------------------------------------------------------------
# EXAMPLE 2B: Equivalent to 2A, passing argument size to rbinom inside a named list
# params
#---------------------------------------------------------------------------------------
D <- DAG.empty()
D <- D + node("L1", t=0, distr="rbinom", prob=0.05, params=list(size=1))
D <- D + node("L2", t=0, distr="rbinom", prob=ifelse(L1[0]==1,0.5,0.1), params=list(size=1))
D <- set.DAG(D)
#---------------------------------------------------------------------------------------
# EXAMPLE 2C: Equivalent to 2A and 2B, define Bernoulli nodes using a wrapper "rbern"
#---------------------------------------------------------------------------------------
D <- DAG.empty()
D <- D + node("L1", t=0, distr="rbern", prob=0.05)
D <- D + node("L2", t=0, distr="rbern", prob=ifelse(L1[0]==1,0.5,0.1))
D <- set.DAG(D)
#---------------------------------------------------------------------------------------
# EXAMPLE 3: Define node with normal distribution using rnorm() R function
#---------------------------------------------------------------------------------------
D <- DAG.empty()
D <- D + node("L2", t=0, distr="rnorm", mean=10, sd=5)
D <- set.DAG(D)
#---------------------------------------------------------------------------------------
# EXAMPLE 4: Define 34 Bernoulli nodes, or 2 Bernoulli nodes over 17 time points,
# prob argument contains .() expression that is immediately evaluated in the calling
# environment (.(t_end) will evaluate to 16)
#---------------------------------------------------------------------------------------
t_end <- 16
D <- DAG.empty()
D <- D + node("L2", t=0:t_end, distr="rbinom", prob=ifelse(t==.(t_end),0.5,0.1), size=1)
D <- D + node("L1", t=0:t_end, distr="rbinom", prob=ifelse(L2[0]==1,0.5,0.1), size=1)
D <- set.DAG(D)
Run the code above in your browser using DataLab