Importing a model from dagitty

213 views
Skip to first unread message

Pat Malone

unread,
Mar 3, 2022, 10:12:40 AM3/3/22
to lav...@googlegroups.com
Hello, folks.

I've constructed a directed acyclic graph in dagitty (the R package).

I've worked through using paste() and fiddling around with loops to create lavaan model syntax to test the implied conditional independencies.

Has anyone tried to/managed to import an entire model, with all the ~ and ~~ relations? The dagitty package can import lavaan syntax, but not export.

I know I could still do it fiddling with paste(), but I'm hoping this is a solved problem.

(I am aware that DAG arrows are not in principle constrained to linear or other specific functional forms. One problem at a time.)

Thanks,
Pat

--
Patrick S. Malone, PhD
Sr Research Statistician, FAR HARBΦR
This message may contain confidential information; if you are not the intended recipient please notify the sender and delete the message.

Pat Malone

unread,
Mar 3, 2022, 10:18:13 AM3/3/22
to lav...@googlegroups.com
Ah, I just found the edges() function in dagitty which will make the paste job fairly trivial. Still, if there's a tailor-made function out there...

Thanks.

Pat Malone

unread,
Mar 4, 2022, 10:51:04 AM3/4/22
to lav...@googlegroups.com
Here's what worked for me, in case it's helpful to anyone else.

"dag" is the name of the dagitty object generated by the daggity function in the daggity package; "df " is the name of the dataframe where variables that are used in the DAG are either real or (if endogenous) ordered. Unused variables in the dataframe are ok.

The loop adds all the directed paths and covariances to a lavaan syntax string, except for covariances that involve an exogenous variable (discerned from the DAG) and/or an ordered variable (discerned from the data).

lavsyntax <- ''
for (i in (1:nrow(edges(dag)))) {
  lavline <- ifelse(edges(dag)[i,"e"] == "->",
                    paste(edges(dag)[i,"w"], "~", edges(dag)[i,"v"]),
                    ifelse(edges(dag)[i,"v"] %in% exogenousVariables(dag)|edges(dag)[i,"w"] %in% exogenousVariables(dag)|is.ordered(df[edges(dag)[i,"w"]])|is.ordered(df[edges(dag)[i,"w"]]),
                           "",
                           paste(edges(dag)[i,"w"], "~~", edges(dag)[i,"v"])))
  lavsyntax <- paste(lavsyntax, lavline, sep = " \n ")
}

Pat

Christian Arnold

unread,
Mar 6, 2022, 7:01:06 AM3/6/22
to lav...@googlegroups.com
Hi Pat

Thanks for the syntax. I don't know if I can contribute anything useful. I will give it a try. Would this simplify the code a bit?

lavsyntax <- function(obj) {
  res <- apply(edges(obj), 1, function(x) {
    op <- switch(x["e"], "->" = "~", "<->" = "~~")
    c(x["w"], op, x["v"], "\n")
  })
  paste(res, collapse = "")
}
lavsyntax(dag)

I didn't understand exactly what is being done with the DF. But it seems like if a certain condition is present, no lavaan syntax line should be created? Would that be an alternative solution then?

lavsyntax <- function(obj) {
  res <- apply(edges(obj), 1, function(x) {
    op <- switch(x["e"], "->" = "~", "<->" = "~~")
    cond <- if(edges(dag)[i,"v"] %in% exogenousVariables(dag)|edges(dag)[i,"w"] %in% exogenousVariables(dag)|is.ordered(df[edges(dag)[i,"w"]])|is.ordered(df[edges(dag)[i,"w"]]) TRUE else FALSE
    if(cond) c(x["w"], op, x["v"], "\n")
  })
  paste(res, collapse = "")
}

Best

Christian


Von: 'Pat Malone' via lavaan <lav...@googlegroups.com>
Gesendet: Freitag, 4. März 2022 16:50
An: lav...@googlegroups.com <lav...@googlegroups.com>
Betreff: Re: Importing a model from dagitty
 
--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/CAJhmz4f2%2BSjXcUdpXmLHw0OrpEFhyjZ4eaAY3%3D6nhi-nxgEb2w%40mail.gmail.com.

Pat Malone

unread,
Mar 6, 2022, 8:08:59 AM3/6/22
to lav...@googlegroups.com
hi, Christian.

Yes, I'm sure a function could do it more cleanly! I'd need to test yours on some different cases.

The role of df is to go check the data to make sure an endogenous variable isn't discrete before specifying a disturbance covariance with it.

It should be the case that the only time mine returns completely empty is if there are no directional paths, which is not a scenario I'm worried about.

Pat

Reply all
Reply to author
Forward
0 new messages