Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix bugs in instrumental functions #99

Merged
merged 4 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions R/adjustment_sets.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ is_confounder <- function(.tdy_dag, z, x, y, direct = FALSE) {
#' @param collider_lines logical. Should the plot show paths activated by
#' adjusting for a collider?
#' @param as_factor logical. Should the `adjusted` column be a factor?
#' @param activate_colliders logical. Include colliders activated by adjustment?
#'
#' @return a `tidy_dagitty` with a `adjusted` column for adjusted
#' variables, as well as any biasing paths that arise, or a `ggplot`
Expand All @@ -202,10 +203,10 @@ is_confounder <- function(.tdy_dag, z, x, y, direct = FALSE) {
#'
#' @rdname control_for
#' @name Adjust for variables
control_for <- function(.tdy_dag, var, as_factor = TRUE, ...) {
control_for <- function(.tdy_dag, var, as_factor = TRUE, activate_colliders = TRUE, ...) {
.tdy_dag <- if_not_tidy_daggity(.tdy_dag, ...)
dagitty::adjustedNodes(.tdy_dag$dag) <- var
.tdy_dag <- activate_collider_paths(.tdy_dag, var)
if (isTRUE(activate_colliders)) .tdy_dag <- activate_collider_paths(.tdy_dag, var)
.tdy_dag$data <- dplyr::mutate(.tdy_dag$data, adjusted = ifelse(name %in% var, "adjusted", "unadjusted"))
if (as_factor) .tdy_dag$data <- dplyr::mutate(.tdy_dag$data, adjusted = factor(adjusted, exclude = NA))
.tdy_dag
Expand Down
37 changes: 28 additions & 9 deletions R/instrumental.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ node_instrumental <- function(.dag, exposure = NULL, outcome = NULL, ...) {
conditional_vars <- ifelse(is.null(.z), "", paste("|", paste(.z, collapse = ", ")))
.dag$data$instrumental_name <- paste(.i, conditional_vars) %>% stringr::str_trim()
if (!is.null(.z)) {
.dag <- .dag %>% control_for(.z)
.dag <- .dag %>% control_for(.z, activate_colliders = FALSE)
} else {
.dag$data$adjusted <- factor("unadjusted", levels = c("unadjusted", "adjusted"), exclude = NA)
}
Expand All @@ -75,34 +75,53 @@ ggdag_instrumental <- function(.tdy_dag, exposure = NULL, outcome = NULL, ...,
node = TRUE, stylized = FALSE, text = TRUE, use_labels = NULL) {
.tdy_dag <- if_not_tidy_daggity(.tdy_dag) %>%
node_instrumental(exposure = exposure, outcome = outcome, ...)

p <- .tdy_dag %>%
ggplot2::ggplot(ggplot2::aes(x = x, y = y, xend = xend, yend = yend, color = instrumental, shape = adjusted)) +
ggplot2::ggplot(ggplot2::aes(x = x, y = y, xend = xend, yend = yend, shape = adjusted)) +
geom_dag_edges() +
scale_adjusted() +
breaks("instrumental")

if (node) {
if (all(is.na(.tdy_dag$data$instrumental))) {
node_aes <- NULL
} else {
node_aes <- aes(color = instrumental)
}

if (stylized) {
p <- p + geom_dag_node(size = node_size)
p <- p + geom_dag_node(node_aes, size = node_size)
} else {
p <- p + geom_dag_point(size = node_size)
p <- p + geom_dag_point(node_aes, size = node_size)
}
}

if (text) p <- p + geom_dag_text(col = text_col, size = text_size)

if (!is.null(use_labels)) {
if (all(is.na(.tdy_dag$data$instrumental))) {
label_aes <- ggplot2::aes(
label = !!rlang::sym(use_labels)
)
} else {
label_aes <- ggplot2::aes(
label = !!rlang::sym(use_labels),
fill = instrumental
)
}

p <- p +
geom_dag_label_repel(
ggplot2::aes(
label = !!rlang::sym(use_labels),
fill = instrumental
),
label_aes,
size = text_size,
col = label_col,
show.legend = FALSE
)
}
if (dplyr::n_distinct(.tdy_dag$data$instrumental_name) > 1) p <- p + ggplot2::facet_wrap(~instrumental_name)
if (all(is.na(.tdy_dag$data$instrumental))) {
p <- p + ggplot2::facet_wrap(~"{No instrumental variables present}")
} else {
p <- p + ggplot2::facet_wrap(~instrumental_name)
}
p
}
6 changes: 4 additions & 2 deletions man/control_for.Rd

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

2 changes: 1 addition & 1 deletion man/geom_dag_text.Rd

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading