osofr / simcausal

Simulating Longitudinal and Network Data with Causal Inference Applications
64 stars 11 forks source link

Performing intervention in ver. 0.5.0 #5

Open wilsoncai1992 opened 8 years ago

wilsoncai1992 commented 8 years ago

I am trying to perform a deterministic intervention on a DAG.

library(simcausal)
library(rje)
D <- DAG.empty()

D <- D +
  node("U1", distr="runif"
  )

D <- D +
  node("U2", distr="runif"
       )

D <- D +
  node("UA", distr="runif"
  )

D <- D +
  node("UY", distr="runif"
  )

D <- D +
  node("W1", distr="rconst",
       const = as.numeric(U1 < 0.5))

D <- D +
  node("W2", distr="rconst",
       const = as.numeric(U2 < 0.5))

D <- D +
  node("A", distr="rconst",
       const = as.numeric(UA < expit(-0.5 + W1 - 1.5 * W2)) )

D <- D +
  node("Y", distr="rconst",
       const = as.numeric(UY < expit(-0.75 + W1 - 2 * W2 + 2.5 * A + A*W1)),
       EFU = TRUE)

setDl2 <- set.DAG(D)
# ----------------------------------------------------------------------
plotDAG(setDl2, xjitter = 0.32, yjitter = 0.03,
        edge_attrs = list(width = 0.5, arrow.width = 0.4, arrow.size = 0.8),
        vertex_attrs = list(size = 19, label.cex = 1.5))
title(main = "SCM graph")
# ----------------------------------------------------------------------
# intervention
actN_A <- node("A",distr="rconst", const = theta)

D_act <- D + action("A_th0", nodes=actN_A, theta=0)
D_act <- D_act + action("A_th1", nodes=actN_A, theta=1)
D_act <- D_act + action("A_no_intervene", nodes=node("A", distr="rconst", const = as.numeric(UA < expit(-0.5 + W1 - 1.5 * W2)) ))

ate.data <- simfull(A(D_act), n=1e5 , rndseed = 252)

The D_act assignment fails to run under 0.5.0 while suceeds under 0.4.0. Could you please take a look when you have time?

osofr commented 8 years ago

Thanks for pointing this out, this is very interesting. This code should have given an error in the previous versions of the package as well. It was working, but it wasn't designed to work in this manner. Actions should only be added to DAGs returned from set.DAG() function, which checks the DAG object for consistency and errors. The above code is trying to add an action to a DAG object "D", rather than object "setDl2". Otherwise you are not using setDl2 anywhere in your code.

D_act <- setDl2 + action("A_th0", nodes=actN_A, theta=0)
D_act <- D_act + action("A_th1", nodes=actN_A, theta=1)
D_act <- D_act + action("A_no_intervene", nodes=node("A", distr="rconst", const = as.numeric(UA < expit(-0.5 + W1 - 1.5 * W2)) ))
ate.data <- simfull(A(D_act), n=1e5 , rndseed = 252)

The error message returned is non-informative and needs to be changed, so keeping this issue open.

wilsoncai1992 commented 8 years ago

Thank you for your timely reply! I overlooked the difference when I did it, since the class for both "D" and "setDl2" are both "DAG".