From 09064a834c478a6a1e885c68cc62a413d546e46a Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 3 Aug 2024 20:26:10 +0200 Subject: [PATCH] fix cycling --- R/check_dag.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/check_dag.R b/R/check_dag.R index 945851bf2..207779407 100644 --- a/R/check_dag.R +++ b/R/check_dag.R @@ -157,6 +157,17 @@ check_dag.default <- function(..., # helper ---------------------------------------------------------------------- .finalize_dag <- function(dag, effect, outcome, exposure, adjusted) { + # check for cyclic DAG + cycles <- unlist(dagitty::findCycle(dag)) + + # stop if cyclic + if (!is.null(cycles)) { + insight::format_error(paste0( + "Model is cyclic. Causal effects can't be determined for cyclic models. Please remove cycles from the model. To do so, check following variables: ", # nolint + datawizard::text_concatenate(unique(cycles)) + )) + } + # data for checking effects checks <- lapply(c("direct", "total"), function(x) { adjustment_set <- unlist(dagitty::adjustmentSets(dag, effect = x), use.names = FALSE)