From 00c48d30ee483405574699661a61007c57a9f210 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 12 Aug 2024 20:32:01 +0200 Subject: [PATCH] Add tests, allow to plot specific effect --- DESCRIPTION | 2 +- R/plot.check_dag.R | 10 +- .../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 +++++++++++++ tests/vdiffr.Rout.fail | 99 ++++++++++++++ 17 files changed, 1217 insertions(+), 9 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 create mode 100644 tests/vdiffr.Rout.fail 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/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")) + ) +}) diff --git a/tests/vdiffr.Rout.fail b/tests/vdiffr.Rout.fail new file mode 100644 index 000000000..e8498c907 --- /dev/null +++ b/tests/vdiffr.Rout.fail @@ -0,0 +1,99 @@ +Environment: +- vdiffr-svg-engine: 2.0 +- vdiffr: 1.0.7 + + +Failed doppelganger: plot-check-dag-all (C:\Users\DL\Documents\GitHub\easystats\see\tests\testthat\_snaps/plot.check_dag/plot-check-dag-all.svg) + +< before +> after +@@ 116,5 / 116,5 @@ + adjusted + unadjusted +< Required model +> Required model (total effect +: ) + plot.check_dag all + + + +Failed doppelganger: plot-check-dag-required (C:\Users\DL\Documents\GitHub\easystats\see\tests\testthat\_snaps/plot.check_dag/plot-check-dag-required.svg) + +< before +> after +@@ 57,5 / 57,5 @@ + adjusted + unadjusted +< Required model +> Required model (total effect)< +: /text> + + + + +Failed doppelganger: plot-check-dag-all-adjusted (C:\Users\DL\Documents\GitHub\easystats\see\tests\testthat\_snaps/plot.check_dag/plot-check-dag-all-adjusted.svg) + +< before +> after +@@ 116,5 / 116,5 @@ + adjusted + unadjusted +< Required model +> Required model (total effect +: ) + plot.check_dag all-adjusted + + + +Failed doppelganger: plot-check-dag-required-adjusted (C:\Users\DL\Documents\GitHub\easystats\see\tests\testthat\_snaps/plot.check_dag/plot-check-dag-required-adjusted.svg) + +< before +> after +@@ 57,5 / 57,5 @@ + adjusted + unadjusted +< Required model +> Required model (total effect)< +: /text> + + + + +Failed doppelganger: plot-check-dag-multiple-adjustments (C:\Users\DL\Documents\GitHub\easystats\see\tests\testthat\_snaps/plot.check_dag/plot-check-dag-multiple-adjustments.svg) + +< before +> after +@@ 333,5 / 333,5 @@ + adjusted + unadjusted +< Required model +> Required model (total effect +: ) + plot.check_dag multiple adjus + tments + +