Closed mizano924 closed 8 years ago
This is more of an issue of R
function ifelse
which determines its output length by the length of its conditional statement (expression t==1
has length 1, while the expression C[t-1]==1
has length n
). This is also an issue of inconsistent interface, since C
is internally stored as a vector of length n
, while t
is stored internally as an integer of length 1. However, replacing such problematic ifelse(...)
calls with statements {if(condition) {do1} else {do2}}
is a possible work-around and will make the above code run as intended:
K <- 10
act_dynAD0 <-c(
node("E", t = 1:K, distr = "rconst", const = {if(t==1) {abar} else {ifelse(C[t-1]==1, abar, 0)}} ),
node("D", t = 1:K, distr = "rconst", const = 0))
DynDact <- RCDAG1 + action("DynE0D0", nodes = act_dynAD0, abar = 0) + action("DynE1D0", nodes = act_dynAD0, abar = 1)
dt <- sim(DynDact,actions = c("DynE0D0","DynE1D0"), n = 1000, rndseed = 4)
The line
node("E", t = 1:K, distr = "rconst", const = ifelse(t==1,abar,ifelse(C[t-1]==1, abar, 0))),
involves a call to a vectorized function ifelse(condition, do1, do2)
, where the conditional statement is t==1
, which is a scalar of length 1. This ifesle
expression will thus evaluate only for the first observation and it will be a constant result for all N
observation. The next condition C[t-1]==1
will be ignored for all observations, but the very first one.
Currently there is no easy way to modify the package to make this interface work. One possible fix is to explicitly catch ifelse
calls which have t
as part of the conditional statement. Such an expression parser could then replace each instance of scalar t
with a vector rep.int(t,n)
. Changing the internal representation of t
to a vector of length n
is not a viable option, since it would break all of the interface for formulas Var[t]
.
I am trying to specify an action that assigns a value of "abar" to E[t] while a time-varying covariate C[t-1] is 1, and 0 otherwise. The DAG is defined as follows:
I tried to generate an observed dataset as follows:
And obtained the following warning:
Many of the E[t] columns contain NAs. Would appreciate your help in resolving this.