From b294a189afb63d463aa76eedd184c367a5da7948 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 12 Aug 2024 22:11:40 +0100 Subject: [PATCH] Add tests, allow to plot specific effect (#355) * Add tests, allow to plot specific effect * docs * wordlist --- DESCRIPTION | 2 +- R/plot.check_dag.R | 10 +- inst/WORDLIST | 2 + man/plot.see_check_dag.Rd | 6 +- .../plot-check-dag-all-adjusted.svg | 2 +- .../plot.check_dag/plot-check-dag-all.svg | 2 +- .../plot.check_dag/plot-check-dag-direct1.svg | 125 +++++++++++++++++ .../plot.check_dag/plot-check-dag-direct2.svg | 127 ++++++++++++++++++ .../plot.check_dag/plot-check-dag-direct3.svg | 127 ++++++++++++++++++ .../plot.check_dag/plot-check-dag-direct4.svg | 125 +++++++++++++++++ .../plot-check-dag-multiple-adjustments.svg | 2 +- .../plot-check-dag-required-adjusted.svg | 2 +- .../plot-check-dag-required.svg | 2 +- .../plot.check_dag/plot-check-dag-total1.svg | 127 ++++++++++++++++++ .../plot.check_dag/plot-check-dag-total2.svg | 127 ++++++++++++++++++ .../plot.check_dag/plot-check-dag-total3.svg | 127 ++++++++++++++++++ .../plot.check_dag/plot-check-dag-total4.svg | 125 +++++++++++++++++ tests/testthat/test-plot.check_dag.R | 95 +++++++++++++ 18 files changed, 1125 insertions(+), 10 deletions(-) create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg create mode 100644 tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg diff --git a/DESCRIPTION b/DESCRIPTION index f7ee79635..63fcbc04c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: see Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2' -Version: 0.8.5.5 +Version: 0.8.5.6 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/R/plot.check_dag.R b/R/plot.check_dag.R index 747cf6fd1..b55e5899f 100644 --- a/R/plot.check_dag.R +++ b/R/plot.check_dag.R @@ -12,7 +12,9 @@ #' `"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. +#' @param effect Character string indicating which effect for the required model +#' is to be estimated. Can be either `"total"` or `"direct"`. +#' @param ... Currently not used. #' #' @return A ggplot2-object. #' @@ -55,14 +57,16 @@ plot.see_check_dag <- function(x, size_text = 4.5, colors = NULL, which = "all", + effect = "total", check_colliders = TRUE, ...) { .data <- NULL insight::check_if_installed(c("ggdag", "ggplot2")) which <- match.arg(which, choices = c("all", "current", "required")) + effect <- match.arg(effect, choices = c("total", "direct")) # get plot data - p1 <- p2 <- suppressWarnings(ggdag::dag_adjustment_sets(x)) + p1 <- p2 <- suppressWarnings(ggdag::dag_adjustment_sets(x, effect = effect)) adjusted_for <- attributes(x)$adjusted # if we have multiple sets, we only need one for the current model @@ -148,7 +152,7 @@ plot.see_check_dag <- function(x, # plot2 - required model plot2 <- ggplot2::ggplot(p2$data, ggplot2::aes(x = .data$x, y = .data$y)) + common_layers + - ggplot2::ggtitle("Required model") + ggplot2::ggtitle(sprintf("Required model (%s effect)", effect)) # if we have multiple sets, we want to facet the second plot by sets if (!is.null(p2$data$set) && insight::n_unique(p2$data$set) > 1) { diff --git a/inst/WORDLIST b/inst/WORDLIST index b5a4460af..067baec4b 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -1,3 +1,4 @@ +Acyclic BFs BayesFactor Biserial @@ -6,6 +7,7 @@ Bluebrown BossekJakob CMD Cai +DAGs DOI Dasgupta Dom diff --git a/man/plot.see_check_dag.Rd b/man/plot.see_check_dag.Rd index 89d33d030..10895d43c 100644 --- a/man/plot.see_check_dag.Rd +++ b/man/plot.see_check_dag.Rd @@ -10,6 +10,7 @@ size_text = 4.5, colors = NULL, which = "all", + effect = "total", check_colliders = TRUE, ... ) @@ -28,10 +29,13 @@ order: \code{outcome}, \code{exposure}, \code{adjusted}, \code{unadjusted}, and \item{which}{Character string indicating which plot to show. Can be either \code{"all"}, \code{"current"} or \code{"required"}.} +\item{effect}{Character string indicating which effect for the required model +is to be estimated. Can be either \code{"total"} or \code{"direct"}.} + \item{check_colliders}{Logical indicating whether to highlight colliders. Set to \code{FALSE} if the algorithm to detect colliders is very slow.} -\item{...}{Not used.} +\item{...}{Currently not used.} } \value{ A ggplot2-object. diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg index 6e37f1608..95822a2db 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg @@ -115,7 +115,7 @@ exposure adjusted unadjusted -Required model +Required model (total effect) plot.check_dag all-adjusted diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg index dbf6253e3..8b86196b3 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-all.svg @@ -115,7 +115,7 @@ exposure adjusted unadjusted -Required model +Required model (total effect) plot.check_dag all diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg new file mode 100644 index 000000000..e76ef0ca3 --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct1.svg @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + +outcome +exposure +adjusted +Required model (direct effect) +plot.check_dag direct1 + + diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg new file mode 100644 index 000000000..da4d27376 --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct2.svg @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + + +outcome +exposure +adjusted +unadjusted +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Required model (direct effect) +plot.check_dag direct2 + + diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg new file mode 100644 index 000000000..f4abadf97 --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct3.svg @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + + +outcome +exposure +adjusted +unadjusted +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Required model (direct effect) +plot.check_dag direct3 + + diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg new file mode 100644 index 000000000..72f80e6af --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-direct4.svg @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + +outcome +exposure +adjusted +Required model (direct effect) +plot.check_dag direct4 + + diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg index 0aa4206e4..90c2c7d2e 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg @@ -332,7 +332,7 @@ exposure adjusted unadjusted -Required model +Required model (total effect) plot.check_dag multiple adjustments diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required-adjusted.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required-adjusted.svg index b013feb55..76644e322 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required-adjusted.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required-adjusted.svg @@ -56,6 +56,6 @@ exposure adjusted unadjusted -Required model +Required model (total effect) diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required.svg index b013feb55..76644e322 100644 --- a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required.svg +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-required.svg @@ -56,6 +56,6 @@ exposure adjusted unadjusted -Required model +Required model (total effect) diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg new file mode 100644 index 000000000..428c5a28f --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total1.svg @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + + +outcome +exposure +adjusted +unadjusted +Required model (total effect) +plot.check_dag total1 + + diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg new file mode 100644 index 000000000..58447c6e8 --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total2.svg @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + + +outcome +exposure +adjusted +unadjusted +Required model (total effect) +plot.check_dag total2 + + diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg new file mode 100644 index 000000000..ce20d9a44 --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total3.svg @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + + +outcome +exposure +adjusted +unadjusted +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Required model (total effect) +plot.check_dag total3 + + diff --git a/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg new file mode 100644 index 000000000..e5ddeaada --- /dev/null +++ b/tests/testthat/_snaps/plot.check_dag/plot-check-dag-total4.svg @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + +Current model + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +exposure + +outcome + +x1 + +x2 + + + + + +outcome +exposure +unadjusted +Required model (total effect) +plot.check_dag total4 + + diff --git a/tests/testthat/test-plot.check_dag.R b/tests/testthat/test-plot.check_dag.R index 917c1079d..2c1a79f41 100644 --- a/tests/testthat/test-plot.check_dag.R +++ b/tests/testthat/test-plot.check_dag.R @@ -70,3 +70,98 @@ test_that("`plot()` for check_dag, multiple adjustment sets", { fig = plot(dag) ) }) + + +test_that("`plot()` for check_dag, different total and direct adjustments", { + set.seed(1234) + dag <- performance::check_dag( + outcome ~ exposure + x1 + x2, + x2 ~ exposure, + exposure ~ x1, + outcome = "outcome", + exposure = "exposure", + coords = data.frame( + name = c("outcome", "exposure", "x1", "x2"), + x = c(0, 1, 0, 0.5), + y = c(0, 0.5, 1, 1), + stringsAsFactors = FALSE + ) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag total1", + fig = plot(dag) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag direct1", + fig = plot(plot(dag, effect = "direct")) + ) + + dag <- performance::check_dag( + outcome ~ exposure + x1 + x2, + x2 ~ exposure, + exposure ~ x1, + adjusted = "x1", + outcome = "outcome", + exposure = "exposure", + coords = data.frame( + name = c("outcome", "exposure", "x1", "x2"), + x = c(0, 1, 0, 0.5), + y = c(0, 0.5, 1, 1), + stringsAsFactors = FALSE + ) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag total2", + fig = plot(dag) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag direct2", + fig = plot(plot(dag, effect = "direct")) + ) + + dag <- performance::check_dag( + outcome ~ exposure + x1 + x2, + x2 ~ exposure, + exposure ~ x1, + adjusted = "x2", + outcome = "outcome", + exposure = "exposure", + coords = data.frame( + name = c("outcome", "exposure", "x1", "x2"), + x = c(0, 1, 0, 0.5), + y = c(0, 0.5, 1, 1), + stringsAsFactors = FALSE + ) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag total3", + fig = plot(dag) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag direct3", + fig = plot(plot(dag, effect = "direct")) + ) + + dag <- performance::check_dag( + outcome ~ exposure + x1 + x2, + x2 ~ exposure, + exposure ~ x1, + adjusted = c("x1", "x2"), + outcome = "outcome", + exposure = "exposure", + coords = data.frame( + name = c("outcome", "exposure", "x1", "x2"), + x = c(0, 1, 0, 0.5), + y = c(0, 0.5, 1, 1), + stringsAsFactors = FALSE + ) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag total4", + fig = plot(dag) + ) + vdiffr::expect_doppelganger( + title = "plot.check_dag direct4", + fig = plot(plot(dag, effect = "direct")) + ) +})