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