Skip to contents

Plot a causal graph from an adjacency matrix with DiagrammeR

Usage

plot_adjacency(
  B,
  labels = NULL,
  threshold = 0,
  rankdir = "TB",
  title = "Estimated Causal Structure",
  shape = "circle",
  fillcolor = "lightyellow",
  bordercolor = "black",
  fontsize_node = 14,
  fontsize_edge = 10,
  edge_color = "gray40",
  edge_label_color = "red",
  true_B = NULL,
  color_tp = "forestgreen",
  color_fp = "firebrick",
  color_fn = "darkorange",
  debug = FALSE
)

Arguments

B

Adjacency matrix (n_features x n_features). Convention: B[i, j] is the causal coefficient from variable j to variable i (j -> i). The adjacency_matrix from lingam_direct() can be passed directly.

labels

Vector of variable names (if NULL, x0, x1, ... are generated automatically)

threshold

Minimum absolute coefficient value to display (default: 0)

rankdir

Layout direction (default: "LR") "LR" = left -> right, "RL" = right -> left, "TB" = top -> bottom, "BT" = bottom -> top

title

Graph title (default: "Estimated Causal Structure")

shape

Node shape (default: "circle") "circle", "box", "ellipse", "diamond", "plaintext", "square", "triangle", "hexagon", "octagon", etc.

fillcolor

Node fill color (default: "lightyellow")

bordercolor

Border color

fontsize_node

Node font size (default: 14)

fontsize_edge

Edge label font size (default: 10)

edge_color

Edge color (default: "gray40"). Unused when true_B is specified.

edge_label_color

Edge label color (default: "red"). Unused when true_B is specified.

true_B

True adjacency matrix (may be NULL). When specified, edges are classified into three colors:

  • Correct edges (estimated and true): solid line in color_tp

  • False positives (estimated but not true): solid line in color_fp

  • Missed edges (not estimated but true): dashed line in color_fn (showing the true coefficient)

color_tp

Color for correct edges (default: "forestgreen")

color_fp

Color for false-positive edges (default: "firebrick")

color_fn

Color for missed edges (default: "darkorange")

debug

Enable debug mode (logical)

Value

A grViz object (when DiagrammeR is available)

Examples

if (requireNamespace("DiagrammeR", quietly = TRUE)) {
  LiNGAM_sample_1000 <- generate_lingam_sample_6()

  LiNGAM_sample_1000$true_adjacency |>
    plot_adjacency(title = "True Causal Structure")

  model <- LiNGAM_sample_1000$data |>
    lingam_direct(reg_method = "ols")

  model$adjacency_matrix |>
    plot_adjacency()

  # \donttest{
  # Compare with the true structure
  # (correct = green, false positive = red, missed = orange dashed)
  model$adjacency_matrix |>
    plot_adjacency(true_B = LiNGAM_sample_1000$true_adjacency)
  # }
}