Skip to content

Commit

Permalink
Add basic examples vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
EeethB committed Aug 17, 2023
1 parent b99f46a commit 4be8567
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 19 deletions.
11 changes: 8 additions & 3 deletions R/plot.initial_graph.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' S3 plot method for the class `initial_graph`
#'
#' @param x An initial graph as returned by [graph_create()]
#' @param ... Other arguments passed on to `[igraph::plot.igraph]`
#' @param ... Other arguments passed on to [igraph::plot.igraph()]
#' @param layout An igraph layout specification (See `?igraph.plotting`), or
#' `"grid"`, which lays out hypotheses left-to-right, top-to-bottom with
#' `nrow` rows and `ncol` columns.
Expand All @@ -22,7 +22,10 @@
#' displayed as \eqn{\epsilon}, and edge weights between `1 - eps` and 1 will
#' be displayed as \eqn{1 - \epsilon}
#' @param background_color A character scalar specifying a background color for
#' the whole plotting area
#' the whole plotting area. Passed directly to [graphics::par()] (`bg`)
#' @param margins A length 4 numeric vector specifying the margins for the plot.
#' Defaults to all 0, since igraph plots tend to have large margins. It is
#' passed directly to [graphics::par()] (`mar`)
#'
#' @return NULL, after plotting the graph
#' @export
Expand All @@ -37,7 +40,8 @@ plot.initial_graph <- function(x,
edge_curves = NULL,
precision = 4,
eps = .001,
background_color = "white") {
background_color = "white",
margins = c(0, 0, 0, 0)) {
graph_size <- length(x$hypotheses)
graph_seq <- seq_along(x$hypotheses)

Expand Down Expand Up @@ -101,6 +105,7 @@ plot.initial_graph <- function(x,
}
}

graphics::par(mar = margins)
graphics::par(bg = background_color)

# draw! ----------------------------------------------------------------------
Expand Down
16 changes: 12 additions & 4 deletions man/plot.initial_graph.Rd

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

20 changes: 8 additions & 12 deletions tests/testthat/test-graph_test_closure.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ test_that("Simes & parametric adjusted p-values are less than Bonferroni", {
rando,
rep(.01, 4),
test_types = "p",
corr = diag(4)
corr = list(diag(4))
)$outputs$adjusted_p
)
)
Expand Down Expand Up @@ -177,9 +177,8 @@ test_that("check assertions in testing vignette", {
ignore_attr = TRUE
)

corr1 <- matrix(nrow = 4, ncol = 4)
corr1[3:4, 3:4] <- .5
diag(corr1) <- 1
corr1 <- list(NA, matrix(1, nrow = 2, ncol = 2))
corr1[[2]][1, 2] <- corr1[[2]][2, 1] <- .5

expect_equal(
graph_test_closure(
Expand All @@ -198,17 +197,14 @@ test_that("check assertions in testing vignette", {
ignore_attr = TRUE
)

corr3 <- matrix(nrow = 4, ncol = 4)
diag(corr3) <- 1

expect_equal(
graph_test_closure(
par_gate,
pvals,
.05,
list(1, 2, 3, 4),
rep("p", 4),
corr3
rep(list(matrix(1)), 4)
)$outputs,
graph_test_closure(par_gate, pvals, .05)$outputs
)
Expand All @@ -219,7 +215,7 @@ test_that("compare adjusted p-values to gMCP - Bonferroni & parametric", {
p <- pnorm(rnorm(6, 2.5), lower.tail = FALSE)

if (requireNamespace("gMCP", quietly = TRUE)) {
gmcp_g <- as_gmcp_graph(g)
gmcp_g <- as_graphMCP(g)

expect_equal(
graph_test_shortcut(g, p)$outputs$adjusted_p,
Expand All @@ -236,7 +232,7 @@ test_that("compare adjusted p-values to gMCP - Bonferroni & parametric", {
g,
p,
test_types = "p",
corr = diag(6)
corr = list(diag(6))
)$outputs$adjusted_p,
gMCP::gMCP(gmcp_g, p, "parametric", correlation = diag(6))@adjPValues
)
Expand Down Expand Up @@ -308,7 +304,7 @@ test_that("closure internal consistency", {
.025,
list(1:2, 3:4, 5:6),
c("b", "p", "s"),
diag(6),
list(NA, diag(2), NA),
TRUE,
TRUE
)
Expand Down Expand Up @@ -364,7 +360,7 @@ test_that("parametric floating point errors", {
bh <- bonferroni_holm(3)
p <- rep(.025, 3)

t_corr <- matrix(1, 3, 3)
t_corr <- list(matrix(1, 3, 3))

res_para <- graph_test_closure(
bh,
Expand Down
168 changes: 168 additions & 0 deletions vignettes/graph-examples.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
title: "Commonly-used graph examples"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Commonly-used graph examples}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
fig.align = "center",
collapse = TRUE,
comment = "#>"
)
```

```{r setup}
library(graphicalMCP)
```

# Introduction

Any valid graph can be made with `graph_create()`, but some patterns have been established over time. We demonstrate how to run some of these patterns with graphicalMCP.

# Bonferroni-Holm

```{r bonferroni-holm, fig.dim=c(6, 6)}
transitions <- matrix(1 / 4, 5, 5)
diag(transitions) <- 0
bonferroni_holm_graph <- graph_create(rep(1 / 5, 5), transitions)
plot(bonferroni_holm_graph)
```

# Fixed sequence

```{r fixed-sequence, fig.dim=c(6, 2)}
fixed_sequence_graph <- graph_create(
c(1, 0, 0, 0),
rbind(
c(0, 1, 0, 0),
c(0, 0, 1, 0),
c(0, 0, 0, 1),
c(0, 0, 0, 0)
)
)
plot(fixed_sequence_graph, layout = "grid", nrow = 1, asp = .1)
```

# Simplel successive

```{r simple-successive, fig.dim=c(5, 5)}
simple_successive_graph <- graph_create(
c(.5, .5, 0, 0),
rbind(
c(0, 0, 1, 0),
c(0, 0, 0, 1),
c(0, 1, 0, 0),
c(1, 0, 0, 0)
)
)
plot(simple_successive_graph, layout = "grid", nrow = 2)
```

General successive graphs are a good example where multiple variations may be useful with slight differences in starting edge weights. Variable edge weights are not currently supported, but they can be done for a particular graph with a light wrapper.

```{r simple-successive-var-fun}
simple_successive_var <- function(gamma) {
graph_create(
c(.5, .5, 0, 0),
rbind(
c(0, gamma, 1 - gamma, 0),
c(gamma, 0, 0, 1 - gamma),
c(0, 1, 0, 0),
c(1, 0, 0, 0)
)
)
}
```

Then multiple variations can be created and compared easily.

```{r simple-successive-var, fig.show="hold", fig.align='default', out.width="47%"}
plot(
simple_successive_var(.75),
layout = "grid",
nrow = 2,
vertex.label.cex = .7,
edge.label.cex = .7
)
plot(
simple_successive_var(.9),
layout = "grid",
nrow = 2,
vertex.label.cex = .7,
edge.label.cex = .7
)
```

# Huque-Alosh-Bhore (2011)

```{r huque-alosh-bhore, fig.dim=c(5, 5)}
hab_2011_graph <- graph_create(
c(1, 0, 0, 0),
rbind(
c(0, .5, .5, 0),
c(0, 0, 0, 1),
c(0, .5, 0, .5),
c(0, 1, 0, 0)
)
)
plot(hab_2011_graph, layout = "grid")
```

# Wiens-Dmitrienko (2005)

```{r wiens-dmitrienko, fig.dim=c(6, 2)}
wd_2005_graph <- graph_create(
c(1 / 3, 1 / 3, 1 / 3),
rbind(
c(0, 1, 0),
c(0, 0, 1),
c(.5, .5, 0)
)
)
plot(wd_2005_graph, layout = "grid", nrow = 1, asp = .1, edge_curves = c(pairs = -6, "H3|H1" = -6))
```

# Parallel gate-keeping

```{r parallel-gatekeeping, fig.dim=c(5, 5)}
par_gate_graph <- graph_create(
c(.5, .5, 0, 0),
rbind(
c(0, 0, .5, .5),
c(0, 0, .5, .5),
c(0, 0, 0, 1),
c(0, 0, 1, 0)
)
)
plot(par_gate_graph, layout = "grid", nrow = 2)
```

# Improved parallel gate-keeping

```{r improved-parallel-gatekeeping, fig.dim=c(5, 5)}
imp_par_gate_graph <- graph_create(
c(.5, .5, 0, 0),
rbind(
c(0, 0, .5, .5),
c(0, 0, .5, .5),
c(.0001, 0, 0, .9999),
c(0, .0001, .9999, 0)
)
)
plot(imp_par_gate_graph, layout = "grid", nrow = 2)
```

0 comments on commit 4be8567

Please sign in to comment.