Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Aug 3, 2024
1 parent 2ad1d61 commit a5fc906
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
28 changes: 23 additions & 5 deletions R/plot.check_dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#' hex-format) for different types of variables.
#' @param which Character string indicating which plot to show. Can be either
#' `"all"`, `"current"` or `"required"`.
#' @param check_colliders Logical indicating whether to highlight colliders.
#' Set to `FALSE` if the algorithm to detect colliders is very slow.
#' @param ... Not used.
#'
#' @return A ggplot2-object.
Expand All @@ -27,23 +29,39 @@
#' # plot only model with required adjustments
#' plot(dag, which = "required")
#' @export
plot.see_check_dag <- function(x, size_point = 15, colors = NULL, which = "all", ...) {
plot.see_check_dag <- function(x,
size_point = 15,
colors = NULL,
which = "all",
check_colliders = TRUE,
...) {
.data <- NULL
insight::check_if_installed(c("ggdag", "ggplot2"))
which <- match.arg(which, choices = c("all", "current", "required"))

p1 <- suppressWarnings(ggdag::ggdag_adjust(x, stylized = TRUE))
p2 <- suppressWarnings(ggdag::ggdag_adjustment_set(x, shadow = TRUE, stylized = TRUE))
# get plot data
p1 <- p2 <- suppressWarnings(ggdag::dag_adjustment_sets(x))
adjusted_for <- attributes(x)$adjusted

# for current plot, we need to update the "adjusted" column
p1$data$adjusted <- "unadjusted"
if (!is.null(adjusted_for)) {
p1$data$adjusted[p1$data$name %in% adjusted_for] <- "adjusted"
}

# tweak data
p1$data$type <- as.character(p1$data$adjusted)
p1$data$type[vapply(p1$data$name, ggdag::is_collider, logical(1), .dag = x)] <- "collider"
if (check_colliders) {
p1$data$type[vapply(p1$data$name, ggdag::is_collider, logical(1), .dag = x)] <- "collider"
}
p1$data$type[p1$data$name == attributes(x)$outcome] <- "outcome"
p1$data$type[p1$data$name %in% attributes(x)$exposure] <- "exposure"
p1$data$type <- factor(p1$data$type, levels = c("outcome", "exposure", "adjusted", "unadjusted", "collider"))

p2$data$type <- as.character(p2$data$adjusted)
p2$data$type[vapply(p2$data$name, ggdag::is_collider, logical(1), .dag = x)] <- "collider"
if (check_colliders) {
p2$data$type[vapply(p2$data$name, ggdag::is_collider, logical(1), .dag = x)] <- "collider"
}
p2$data$type[p2$data$name == attributes(x)$outcome] <- "outcome"
p2$data$type[p2$data$name %in% attributes(x)$exposure] <- "exposure"
p2$data$type <- factor(p2$data$type, levels = c("outcome", "exposure", "adjusted", "unadjusted", "collider"))
Expand Down
12 changes: 11 additions & 1 deletion man/plot.see_check_dag.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a5fc906

Please sign in to comment.